Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Re: Map and functional constructs to replace iterative

  • To: mathgroup at smc.vnet.net
  • Subject: [mg96848] Re: [mg96783] Map and functional constructs to replace iterative
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Thu, 26 Feb 2009 07:54:18 -0500 (EST)
  • Reply-to: hanlonr at cox.net

percentChange[currentValue_, previousValue_] :=
 
 100*(currentValue - previousValue)/previousValue

data = {v1, v2, v3, v4};

percentChange @@@ Reverse /@ Partition[data, 2, 1]

{(100*(v2 - v1))/v1, (100*(v3 - v2))/v2, (100*(v4 - v3))/v3}

100*ListCorrelate[{-1, 1}, data]/Most[data]

{(100*(v2 - v1))/v1, (100*(v3 - v2))/v2, (100*(v4 - v3))/v3}

% == %%

True


Bob Hanlon

---- Andreas <aagas at ix.netcom.com> wrote: 

=============
I have begun to replace Table, Do, While, or For, but in my thinking with Map, Inner, Outer and other functional programming constructs as I learn more about Mathematica.

But, I'm stuck.  I need to figure out how to use Map (or some related approach) when a function needs both a current value and a previous value in a time series, such as a percentage change function (or perhaps more generally stated, when I need to refer to previous values in a series or even previous calculations).  But first a simple case:

percentChange[currentValue_, previousValue_] := 100 * ( currentValue - previousValue) / previousValue

I know how to apply this iteratively, but how can I do it with Map or some other functional construct?

Say I have a list: timeSeries 

I thought to try something like this:

Map[percentChange[?,?], timeSeries] 

but I don't know how what to put in for "?".

Any help much appreciated.

Thx.




  • Prev by Date: Re: Series of Percentage Changes
  • Next by Date: Re: N00b help needed: Graphics
  • Previous by thread: Re: Galois resolvent
  • Next by thread: Experimental Mathematica debugger