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: [mg96873] Re: [mg96783] Map and functional constructs to replace iterative
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Thu, 26 Feb 2009 07:58:52 -0500 (EST)
  • References: <200902250900.EAA15513@smc.vnet.net>
  • Reply-to: drmajorbob at bigfoot.com

timeSeries = Table[k + RandomReal[{0, 1}], {k, 0, 20}]

{0.120891, 1.16064, 2.50042, 3.18862, 4.02033, 5.13744, 6.37348, \
7.86513, 8.88356, 9.46121, 10.8472, 11.8567, 12.9746, 13.3022, \
14.606, 15.8236, 16.8817, 17.2755, 18.8514, 19.2049, 20.4023}

percentChange[previous_, current_] := 100*(current - previous)/previous
percentChange @@@ Partition[timeSeries, 2, 1]

{860.067, 115.435, 27.5234, 26.0837, 27.7867, 24.0594, 23.404, \
12.9487, 6.5024, 14.6494, 9.30684, 9.42813, 2.52475, 9.80145, \
8.33611, 6.68689, 2.33276, 9.12233, 1.87529, 6.23463}

or

Clear[percentChange]
percentChange[current_, previous_] := 100*(current - previous)/previous
percentChange @@@ Partition[Reverse@timeSeries, 2, 1]

{6.23463, 1.87529, 9.12233, 2.33276, 6.68689, 8.33611, 9.80145, \
2.52475, 9.42813, 9.30684, 14.6494, 6.5024, 12.9487, 23.404, 24.0594, \
27.7867, 26.0837, 27.5234, 115.435, 860.067}

Under Help for Apply, you'll find:

"f@@@expr is equivalent to Apply[f,expr,{1}]."

or, if you prefer Map:

Clear[percentChange]
percentChange[{current_, previous_}] :=
  100*(current - previous)/previous
percentChange /@ Partition[Reverse@timeSeries, 2, 1]

{6.23463, 1.87529, 9.12233, 2.33276, 6.68689, 8.33611, 9.80145, \
2.52475, 9.42813, 9.30684, 14.6494, 6.5024, 12.9487, 23.404, 24.0594, \
27.7867, 26.0837, 27.5234, 115.435, 860.067}

You can even use both approaches with the same function:

Clear[percentChange]
percentChange[current_, previous_] := percentChange@{current, previous}
percentChange[{current_, previous_}] :=
  100*(current - previous)/previous
percentChange /@ Partition[Reverse@timeSeries, 2, 1]

{6.23463, 1.87529, 9.12233, 2.33276, 6.68689, 8.33611, 9.80145, \
2.52475, 9.42813, 9.30684, 14.6494, 6.5024, 12.9487, 23.404, 24.0594, \
27.7867, 26.0837, 27.5234, 115.435, 860.067}

percentChange @@@ Partition[Reverse@timeSeries, 2, 1]

{6.23463, 1.87529, 9.12233, 2.33276, 6.68689, 8.33611, 9.80145, \
2.52475, 9.42813, 9.30684, 14.6494, 6.5024, 12.9487, 23.404, 24.0594, \
27.7867, 26.0837, 27.5234, 115.435, 860.067}

Or (faster than the others, and no need for a function):

100 (Rest@timeSeries - Most@timeSeries)/Most@timeSeries // Reverse

{6.23463, 1.87529, 9.12233, 2.33276, 6.68689, 8.33611, 9.80145, \
2.52475, 9.42813, 9.30684, 14.6494, 6.5024, 12.9487, 23.404, 24.0594, \
27.7867, 26.0837, 27.5234, 115.435, 860.067}

Bobby

On Wed, 25 Feb 2009 03:00:50 -0600, 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.
>



-- 
DrMajorBob at longhorns.com


  • Prev by Date: Re: Manipulating list of functions
  • Next by Date: Re: Graphics export for high quality documents
  • Previous by thread: Re: Map and functional constructs to replace iterative
  • Next by thread: Re: Map and functional constructs to replace iterative statements