Here is how I do it -
VO = FinancialData["VO", {2004,6,26}];
But you can also use the "Value" option of the FinancialData function to drop the dates.
If you only want the daily returns, you can ask for the "FractionalChange" property - the default is the AdjustedClose property which is what was retrieved above. But let's get the daily returns ourselves so we know where they came from.
VOret = Drop[VO[[All,2]],1]/Drop[VO[[All,2]],-1];
[[All,2]] just says give me part 2 of each item in the VO expression.
The Drop commands just leave out the first or last item of the list, using the 1 to drop the first, and -1 to drop from the end.
The division of 2 lists of the same length divides them term-by-term, because it is Listable. The offset means that we are getting the second term divided by the first, then the third divided by the second, etc. That gives us the "geometric first difference" 1.02x, 0.99x, etc. Subtracting 1 from each term gives us just the fractional change.
Let's visualize it to see that we got it right -
Histogram[VOret, {.005}, "Probability"]
Note that the second argument tells the histogram to use bin-widths of half of one percent for these daily changes.
The "Probability" third argument tells the histogram to report a portion of the data in each bin, rather than an absolute count.
When you plot it, notice that it is sharply peaked and not really a bell. We can verify that by asking for
Kurtosis[VOret]
It comes back as 11.34, where a Gaussian bell has a Kurtosis of 3. So there is much less "weight" in the midrange flanks and much more right around the zero midpoint, and out on the long tails, than in a Gaussian distribution. A lognormal wouldn't actually capture those features (it would force a Kurtosis of 3), and that is a reason to use a bootstrap approach instead of a fitted LogNormalDistribution.
Let's now directly make a synthetic distribution fitted to our data -
retD = SmoothKernelDistribution[VOret];
Simple as that, retD is now a distribution object that we can use to simulate random walks with the daily return matching VO.
Let's make some samples, each 252 steps long because that is the number of trading days in a year, and let's make 1000 of them.
sample = RandomVariate[retD, {1000,252}];
Those are full paths and we could look at them all. But what we really care about is the yearly return, which results from all those daily returns - we have 1000 of those.
yearreturns = (Fold[#1 * #2&, 1, #]-1)&/@sample;
The function Fold "eats" one element of its third-argument list at each step, and applies its first-argument function to its previous result - #1 - with the new element from the list fed in as its second argument - #2. So we read that as, at each step take your own number and multiply it by the new return. The second argument to Fold says start this process at the value 1. The rest of the line is just asking for this to be done to every line of our sample - all 1000 of them. (We subtract off the starting 1 to get the return as a change in value for the year).
By the way, if you want to keep the whole history instead of just the endpoint of each year, then you would just want FoldList instead of Fold. FoldList leaves behind a history of each step in applied, instead of just the last result.
Let's visualize those annual returns from our simulated VO -
Histogram[yearreturns, {.05}, "Probability"]
We can also ask for things like the Mean and StandardDeviation of those annual returns -
Mean[yearreturns]
12.34% is what we get for my sample - a different group of 1000 runs might be somewhat different.
StandardDeviation[yearreturns]
Technically we probably want to get the standard deviation slightly differently, since we expect these to be approximately lognormal not normally distributed -
{E^-StandardDeviation[Log[1+yearreturns]], E^StandardDeviation[Log[1+yearreturns]]}
That helps make clear that we would expect anything between -20% and +26% in any given year around the expected return.
Let's also just calculate what that means a -1 SD year return looks like, a mean = 0 SD year, and a +1 SD year -
Mean[yearreturns+1]* {E^-StandardDeviation[Log[1+yearreturns]], 1, E^StandardDeviation[Log[1+yearreturns]]}
That tells us that a "typical" 1SD down year will see an 11% decline, a normal expected year +12,3%, and a good +1SD year a 41.5% gain.
We can keep going, of course...
Sincerely,
Jason Cawley
Phoenix, AZ