# Thread: Calculating CPI using VB.net ... Correctly?

1. ## Calculating CPI using VB.net ... Correctly?

Code:
```Public Class Form1
Dim initialWage As Decimal = 55.0D
Dim inflationRates As Dictionary(Of DateTime, Decimal) = New Dictionary(Of DateTime, Decimal) From {
{New DateTime(2014, 1, 1), 242.434}, ' 2014 inflation rate
{New DateTime(2015, 1, 1), 244.632}, ' 2015 inflation rate
{New DateTime(2016, 1, 1), 249.246}, ' 2016 inflation rate
{New DateTime(2017, 1, 1), 256.21}, ' 2017 inflation rate
{New DateTime(2018, 1, 1), 265.962}, ' 2018 inflation rate
{New DateTime(2019, 1, 1), 274.114}, ' 2019 inflation rate
{New DateTime(2020, 1, 1), 278.567}, ' 2020 inflation rate
{New DateTime(2021, 1, 1), 289.244}, ' 2021 inflation rate
{New DateTime(2022, 1, 1), 310.782}, ' 2022 inflation rate
{New DateTime(2023, 1, 1), 321.583}, ' 2023 inflation rate
{New DateTime(2024, 1, 1), 332.357}  ' 2024 inflation rate
}
Dim startDate As DateTime = New DateTime(2016, 1, 1)
Dim endDate As DateTime = New DateTime(2024, 1, 1)

Dim realWage As Decimal = CalculateRealWage(initialWage, inflationRates, startDate, endDate)
Console.WriteLine("Real Wage: " & realWage.ToString("C"))
End Sub

Function CalculateRealWage(ByVal initialWage As Decimal, ByVal inflationRates As Dictionary(Of DateTime, Decimal), ByVal startDate As DateTime, ByVal endDate As DateTime) As Decimal
Dim startCPI As Decimal = inflationRates(startDate)
Dim endCPI As Decimal = inflationRates(endDate)
Dim realWage As Decimal = initialWage * (1 / (startCPI / endCPI))
Return realWage
End Function

End Class```
Greetings everyone! I hope this question is simple enough for an easy answer. I wrote this code as a proof of concept which I plan to apply to other locations once I write the code to automate the fetching of the values.

I tried to use the BLS.gov's calculator (cpicalc.pl) to compare my value to theirs and of course if they matched then I know my logic and formula is correct but their public real wage calculator I think is using the Annual US rate and not at the locality level (the site does not say what the index values are sadly), so I don't have an exact comparison.

What I ended up doing is taking the "Annual" column rates and implemented them into the dictionary (code above), then to calculate forward the difference of the dollar amount in 2014 to today's money, I use " initialWage * (1 / (startCPI / endCPI))". My understanding is if I want to go backwards, to see what the input dollar amount is equivalent to a dollar amount in 2014 I can then run the formula initialWage * (startCPI / endCPI).

Is this logic appear correct and that I am on the right track? I've never calculated CPI before so I don't have much of a baseline as to determining if my value is current.

Current my code appears to be pretty close, just not sure if its spot on. When I enter \$55, Its returning Real Wage: \$73.34.
If I reverse the formula as described above I get \$41.25

Thank you for any pointers, once I confirm my function is correct, Ill then next working on importing the data from the rest of bls.gov

SRC's:
https://data.bls.gov/timeseries/CUURS49ASA0
https://www.dir.ca.gov/oprl/CPI/CPIC...alculator.aspx (this source agrees with the above links chart data)
https://data.bls.gov/cgi-bin/cpicalc...1&year2=202401 (Wage CPI calc, but is not of a local area, but the US as a whole I think)

2. ## Re: Calculating CPI using VB.net ... Correctly?

Your code is comparing 2016 wages to 2024 wages, but your post says you want to use 2014 wages.

The result of your code is right against 2016 wages. You can also simplify your calculation/Return as:

Code:
`Return (initialWage * (endCPI / startCPI))`

3. ## Re: Calculating CPI using VB.net ... Correctly?

Originally Posted by OptionBase1
Your code is comparing 2016 wages to 2024 wages, but your post says you want to use 2014 wages.

The result of your code is right against 2016 wages. You can also simplify your calculation/Return as:

Code:
`Return (initialWage * (endCPI / startCPI))`
Ah, great.
I need to brush back up on my math formulas but I do recall that rule. Very helpful.
Thank you for the input!

4. ## Re: Calculating CPI using VB.net ... Correctly?

By the way, you're not really using the date values, only the years.

Checkout this modified code. It is similar, just built a bit differently:
Code:
```Private ReadOnly _inflationRateValues = {
242.434,
244.632,
249.246,
256.21,
265.962,
274.114,
278.567,
289.244,
310.782,
321.583,
332.357
}

Public Sub Main()
Dim initialWage = 55.0D
Dim startingYear = 2014
Dim endingYear = 2024

Dim realWage = CalculateRealWage(initialWage, startingYear, endingYear)
Console.WriteLine("Real Wage: {0:c}", realWage)
End Sub

Private Function CalculateRealWage(initialWage As Decimal, startingYear As Integer, endingYear As Integer) As Decimal
Dim startIndex = startingYear - _inflationRateStartingYear
Dim endIndex = (endingYear - _inflationRateStartingYear)
Dim startCPI = _inflationRateValues(startIndex)
Dim endCPI = _inflationRateValues(endIndex)
Dim realWage = initialWage * (endCPI / startCPI)
Return realWage
End Function```
Fiddle: https://dotnetfiddle.net/UenLCu

5. ## Re: Calculating CPI using VB.net ... Correctly?

Thanks, I think I want to use the dictionary approach because as I import the data it will fill in the values for the months to select from.
The example table is here: https://data.bls.gov/timeseries/CUURS49ASA0
I appreciate the suggestion as well!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•