Re: Series of Percentage Changes

• To: mathgroup at smc.vnet.net
• Subject: [mg96881] Re: [mg96789] Series of Percentage Changes
• From: DrMajorBob <btreat1 at austin.rr.com>
• Date: Thu, 26 Feb 2009 08:00:21 -0500 (EST)
• References: <200902250901.EAA15578@smc.vnet.net>

```Like this?

data = {1.1, NA, 1.2, 1.3, "Missing", 1.4, 1.5, 0, 1.6};

pctChange[{_, _}] := "not numeric"
pctChange[{0 | 0., _}] := "Zero Denominator"
pctChange[{previous_?NumericQ, current_?NumericQ}] :=
current/previous - 1

pctChange /@ Partition[data, 2, 1]

{"not numeric", "not numeric", 0.0833333, "not numeric", "not \
numeric", 0.0714286, -1, "Zero Denominator"}

Bobby

On Wed, 25 Feb 2009 03:01:55 -0600, Gregory Lypny
<gregory.lypny 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
>
>

--
DrMajorBob at longhorns.com

```

• Prev by Date: Re: Counting Categories
• Next by Date: Re: Map and functional constructs to replace iterative
• Previous by thread: Series of Percentage Changes
• Next by thread: Re: Series of Percentage Changes