Re: Basic programming
- To: mathgroup at smc.vnet.net
- Subject: [mg93587] Re: [mg93576] Basic programming
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Sun, 16 Nov 2008 07:04:14 -0500 (EST)
- Reply-to: hanlonr at cox.net
Use Partition to form the rolling windows. For example
data1 = {a, b, c, d, e, f, g};
Partition[data1, 3, 1]
{{a, b, c}, {b, c, d}, {c, d, e}, {d, e, f}, {e, f, g}}
Then map StandardDeviation onto the resulting windows
data2 = 100 + RandomReal[{0, 10}, {100}];
stdDev2 = StandardDeviation /@
Partition[data2, 10, 1];
The argument to StandardDeviation must have at least two elements
data3 = {a, b, c, d}
{a,b,c,d}
FoldList[Append, Take[data3, 2], Drop[data3, 2]]
{{a,b},{a,b,c},{a,b,c,d}}
stdDev = Map[StandardDeviation,
FoldList[Append, Take[#, 2], Drop[#, 2]] & /@
Partition[data2, 10, 1], {2}];
stdDev[[All, 9]] == stdDev2
True
ListLinePlot[stdDev2]
ListLinePlot[Flatten[stdDev, 1],
Epilog -> {Red, AbsolutePointSize[4],
Point[Thread[{Range[9, 9*Length[stdDev2], 9], stdDev2}]]},
ImageSize -> 600]
Bob Hanlon
---- BionikBlue <frankflip at hotmail.com> 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!
--
Bob Hanlon