Skip to content

Instantly share code, notes, and snippets.

@jamespantalones
Created December 16, 2016 17:15
Show Gist options
  • Save jamespantalones/1fefe3aee2999baecfb356cc0eb17f86 to your computer and use it in GitHub Desktop.
Save jamespantalones/1fefe3aee2999baecfb356cc0eb17f86 to your computer and use it in GitHub Desktop.
Calculate Annual Inflation
//--------------------------------------------
// Inflation
//
export const calculateInflation = ( priceArray ) => {
/*
--let's assume the price rises from 100 to 150 over a span of 42 days.
--that's obviously a 50% increase
--then look at days in full year: 365/42 = 8.690476
--for the annualization calculation, the 50% is expressed mathematically as 1.50, like this:
1.50^8.690476 (that is, taking that number and raising it to 8.69th power)
--That equation spits out figure of 33.909147
--to get the annualized inflation rate, subtract 1 from that number and treat the
first number left of the decimal point as representing hundreds and the second
number representing thousands, meaning the annnualized rate in this case
is 3,291% (once rounded up); it sounds a lot more complicated when explained
like this than it really is.
*/
//--------------------------------------------
// Get first price and last price
//
const firstPrice = priceArray[0].price
const lastPrice = priceArray[priceArray.length - 1].price
const firstDate = moment(new Date(priceArray[0].date))
const lastDate = moment(new Date(priceArray[priceArray.length - 1].date))
const days = lastDate.diff(firstDate, 'days')
const percentOfYear = 365 / days
const priceDiff = lastPrice - firstPrice
const percentageIncrease = (priceDiff / firstPrice) + 1
const annualization = Math.pow(percentageIncrease, percentOfYear)
const inflationRate = Math.round((annualization - 1) * 100)
return inflationRate
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment