Calculate percentage change in a time series?

Hi, and thanks in advance for any consideration. Have tried documentation to no avail.
What I’m looking for is how to calculate percentage change in a time series. Like if you have results for days it would be;
((Today-Yesterday)/Yesterday) throughout the time series. (T=.5 - Y=1)/Y=-.5 or -50% change.
Seems simple but cannot find the documentation on how to achieve it.

Also, a broader question would be, is there a way to visualize the code behind the functions in Mathematica? Or from Alpha?
Functions like these have no documentation…

FinancialData FractionalChange (from Mathematica)
TimeSeriesOperator:ChangeRate: (from Alpha)

Thank you, and kind regards., Kelly.

Hi there,

suppose that you have the time series:

ts = RandomReal[1, {20}]
(*{0.818908, 0.355672, 0.696431, 0.391657, 0.0635299, 0.724865, \
0.555864, 0.185575, 0.589479, 0.478966, 0.379647, 0.0756852, 0.3155, \
0.89368, 0.725909, 0.134319, 0.211325, 0.699537, 0.740061, 0.879502}*)

Then this applies your function to ts:

Differences[#]/Drop[#, 1] &@ts
(*{-1.30242, 0.489293, -0.778163, -5.16493, 0.912356, -0.304033, \
-1.99536, 0.685188, -0.230731, -0.261609, -4.01614, 0.76011, \
0.646966, -0.23112, -4.40438, 0.364397, 0.697908, 0.0547568, 0.158546}*)

Check for the first value (second “day” actually)

(0.355672 - .818908)/0.355672= -1.30242

Also, a broader question would be, is there a way to visualize the
code behind the functions in Mathematica? Or from Alpha? Functions
like these have no documentation…

I do not understand what you mean.

Cheers,

Marco

Thank you so much Marco, this points me in the right direction. However if;
Today=0.818908 and Yesterday=0.355672 then it would be a +130.242% daily increase.
And if;
Today=0.355672 and Yesterday=0.818908 then it would be a -56.567% daily decrease.

The broader question was, like in your example you used the function “Differences” is there any way to see the math/code behind that function? Or is that proprietary code from Wolfram?

Again, thanks. K.

Differences[#]/Drop[#, -1] &@ts seems to sort it out,

Again thanks. Cheers,K.

Hi there,

not that the first value of the time series is 0.818908 and the second one is 0.355672. So that means that today is T=0.355672 and yesterday is Y=0.818908; assuming that yesterday is before today. That is why I said:

(0.355672 - .818908)/0.355672= -1.30242

If you want to get it the other way around you can simply use:

Differences[#]/Drop[#, -1] &@ts
(*{-0.565675, 0.958068, -0.437622, -0.837792, 10.4098, -0.233148, \
-0.66615, 2.1765, -0.187475, -0.207362, -0.800643, 3.16858, 1.83259, \
-0.187731, -0.814965, 0.57331, 2.31025, 0.0579287, 0.188419}*)

The first value of this gives the -56% change that you calculate.

Yes, Differences is a built-in function and it is usually a good idea to use them because they are more efficient. You can, however, program it yourself. There are many ways of achieving this:

Drop[#, 1] - Drop[#, -1] &@ts

or

Table[ts[[i + 1]] - ts[[i]], {i, 1, Length[ts] - 1}]

or

(ts - RotateRight[ts])[[2 ;;]]

or

Drop[ts - RotateRight[ts], 1]

or

Reap[Do[Sow[ts[[i + 1]] - ts[[i]]], {i, 1, Length[ts] - 1}]][[2, 1]]

or whatever you like.

Cheers,

M.

Hi Marco, All your time and help on this is very much appreciated. Thank you!

Interesting solutions, but there is a built-in solution, that is much easier (and faster!):

Ratios[ts] - 1

Hi Sander,

you are right. Same LeafCount, but 30% faster. I was also thinking in the wrong directions such as about extensions of the form 2(T-Y)/(T+Y).

Completely overlooked Ratios.

Cheers,

Marco

Hi Sander, yes Thank you, that works well.

Actually looked at Ratios and Differences in the documentation prior to posting. Being new at this and not speaking the language was too hung up on the semantics, not really seeking a ratio (how many times one number contains another) or difference (subtracting one number from another) and did not think or know to use the modifiers to the statement.

Really trying feverishly to learn the language, but If I may be so bold as to ask another question; how do I get past the date?
Import from excel looks like this:

1981-01-05 1596.6
1981-01-12 1600.1
1981-01-19 1606.8
1981-01-26 1609.4
1981-02-02 1608.6

Thank you for the help, kind regards, Kelly.

Depending if that is a string or an array of a strings or …

Try looking up the functions Partition (to split it up in chunks). DateList/DateObject to convert a string to a date…

Okay, think that gets my heading the right direction. Again, Thank You!

I forgot to say: StringSplit to split string. Good luck!