MathGroup Archive 2008

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

Search the Archive

Re: Basic programming

  • To: mathgroup at
  • Subject: [mg93638] Re: [mg93576] Basic programming
  • From: Stern <nycstern at>
  • Date: Wed, 19 Nov 2008 05:40:15 -0500 (EST)
  • References: <>

You have gotten excellent responses to your question. I won't suggest any
changes to the Mathematica code, but I will make a couple of suggestions
specific to the financial domain.

First, securities data is always going to be associated with a particular
time, and in generating these rolling standard deviations, you probably want
to preserve that. I always work with such data in a the form of lists of
{date,data} objects (actually, with a header at the start explaining what
the data is, etc., but I am leaving that out here for simplicity).

Your starting point would look something like the following:

startinglist =
      DatePlus["1/1/2008", #], {"Month", "/", "Day", "/", "Year"}] &,
    Range[100]], RandomReal[1, {100}]}]

For clarity I am setting the length of the rolling period in a global
variable. In practice, you would probably combine everything that follows
into a single function to which this would be a local variable.

rollLength = 10;

finalDates =
  Drop[Drop[Transpose[startinglist][[1]], Floor[rollLength/2]],
   Floor[rollLength/2] - rollLength + 1];

finalDeevs =
  Map[StandardDeviation[#] &,
   Partition[Transpose[startinglist][[2]], rollLength, 1]];

yourAnswer=Transpose[{finalDates, finalDeevs}]

That gives you a much more useful answer that can be passed to DateListPlot
and be compared much more easily to other time-dependent data.

Finally, there is usually not much value in computing standard deviations of
securities returns. You should consider working with *excess returns*

Assuming you have a list of risk free rates in the same {date,data} format
as your securities returns, and you know the dates match up and are in the
same order (checking these things are all trivial in Mathematica), you can
generate your excess returns by subtracting the risk free rates from the
securities returns, which I would generally do with something like

  Transpose[pricechanges][[2]] - Transpose[riskfreerate][[2]]}]

Note that the risk free rate here has to be adjusted for the time period of
the price change -- don't charge a year's worth of interest against one
day's stock price change.

Hope that helps,

Michael Stern

On Sat, Nov 15, 2008 at 6:03 AM, BionikBlue <frankflip at> wrote:

> Hey I'm a beginner in mathematica and I'm trying to code a little something
> but can't seem to be able to make it work... I know its pretty basic and
> that's exactly my problem since the mathematica help is a bit overkill for
> what I need.
> I have daily stock prices for a stock on a 100 day period, I want to
> compute the standard deviation for a rolling 10 days period for the whole
> list of data.
> So basically, I would like to do something like this :
> stdev(1;;10)
> then stdev(2;;11)
> until stdev(91;;100)
> and get the results in a list
> Id also like to get it another way, by starting with only one observation
> and building my standard deviation calculation until I have my whole rolling
> period built up, for example :
> stdev(1)
> stdev(1,2)
> stdev(1,2,3)
> until stdev(1;;10)
> then same as before, roll the period until the end of dataset and produce a
> list of the results.
> Thanks for the help, if what I wrote is not clear enough let me know, Ill
> try to explain it in more details!

  • Prev by Date: Re: Creating a Banner for presentation
  • Next by Date: Cost of Composition vs Pure Function
  • Previous by thread: Re: Re: LatticeReduce
  • Next by thread: Re: Basic programming