Series of Percentage Changes
- To: mathgroup at smc.vnet.net
- Subject: [mg96789] Series of Percentage Changes
- From: Gregory Lypny <gregory.lypny at videotron.ca>
- Date: Wed, 25 Feb 2009 04:01:55 -0500 (EST)
Hello everyone, We can transform a series to percentage changes by using (Differences@theList)/(Most@theList) or (Rest@theList/Most@theList) - 1 However, Mathematica will smartly difference symbols as well as numeric values, so the difference of adjacent null elements will be zero (Null - Null = 0), resulting in a percentage change of zero, and so will adjacent elements with a string, such as "NA" or "Missing", that denotes a missing value. The trouble is, you may want to have missing values flagged in the resulting percentage change series just as they were in the original series. I wrote the following little function, which makes use of a Do loop, to take care of missing values and instances of division by zero, but I was wondering whether there is a more elegant way to incorporate the conditions with functions such as Rest and Most and perhaps avoid the use of the loop. percentageChange[theList_] := Module[{tmpList, tmpElements}, (tmpList = Array[tmpElements, (Length@theList) - 1]; Do[tmpList[[t - 1]] = If[theList[[t - 1]] != 0 && NumberQ[theList[[t - 1]]] && NumberQ[theList[[t]]], theList[[t]]/theList[[t - 1]] - 1, "NA"], {t, 2, Length@theList}]; tmpList)]; Regards, Gregory
- Follow-Ups:
- Re: Series of Percentage Changes
- From: DrMajorBob <btreat1@austin.rr.com>
- Re: Series of Percentage Changes