Re: Series of Percentage Changes
- To: mathgroup at smc.vnet.net
- Subject: [mg96855] Re: Series of Percentage Changes
- From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
- Date: Thu, 26 Feb 2009 07:55:34 -0500 (EST)
- References: <go31da$f5u$1@smc.vnet.net>
Hi Gregory,
If[NumericQ[#1] && NumericQ[#2], 100 Subtract[##]/#1, NA] & @@@
Partition[theList, 2, 1]
should work for you. You might add a minus sign to the 100 depending
on which way you want the differences be calculated.
Cheers -- Sjoerd
On Feb 25, 11:01 am, Gregory Lypny <gregory.ly... at videotron.ca> wrote:
> 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