MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Series of Percentage Changes

  • To: mathgroup at smc.vnet.net
  • Subject: [mg96834] Re: Series of Percentage Changes
  • From: dh <dh at metrohm.com>
  • Date: Thu, 26 Feb 2009 07:51:44 -0500 (EST)
  • References: <go31da$f5u$1@smc.vnet.net>


Hi Gregory,

you may use MapThread together with a function thta checks its 

arguments. E.g.:

fun = If[NumericQ[#1] && NumericQ[#2], (#2 - #1)/#1, NA] &;

MapThread[fun, {Most[theList], Rest[theList]}]

hope this helps, Daniel





Gregory Lypny 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

> 

> 




  • Prev by Date: Re: question related to (-1)^(1/3)
  • Next by Date: Re: Map and functional constructs to replace iterative
  • Previous by thread: Re: Series of Percentage Changes
  • Next by thread: Re: Series of Percentage Changes