# Goodness of fit for stock prices

I'm in my first days of learning R and hit a roadblock with a small use case from finance.

(EDIT) Basically I want to know how to test sample fitness against any distribution. Say, find the degree of freedom of a t-distribution which would have the best fit with the given list of asset returns. I thought the way to go is to generate a random dataset with given properties, and qqplot it against the actual data, along with an abline to see if it's linear or not. In the question I'm using normal/lognormal just as an example which I thought would be easier to discuss.

So, there's a list of stock prices (presumably a lognormal distribution), from which I calculate a list of returns (presumably a normal distribution). Now I want to check my assumptions about the distributions, so I plot both:

par(mfcol=c(2,3))

#plot densities
plot(density(sbux.prices))
plot(density(sbux.returns))

#plot q-q probabilities for normal distribution
sbux.prices.norm = rnorm(n=1000, mean=mean(sbux.prices), sd=sd(sbux.prices))
qqplot(sbux.prices.norm, sbux.prices)
abline(0,1)
sbux.returns.norm = rnorm(n=1000, mean=mean(sbux.returns), sd=sd(sbux.returns))
qqplot(sbux.returns.norm, sbux.returns)
abline(0,1)

#plot q-q probabilities for lognormal distribution
sbux.prices.lnorm = rlnorm(n=1000, mean=mean(sbux.prices), sd=sd(sbux.prices))
qqplot(sbux.prices.lnorm, sbux.prices)
abline(0,1)
sbux.returns.lnorm = rlnorm(n=1000, mean=mean(sbux.returns), sd=sd(sbux.returns))
qqplot(sbux.returns.lnorm, sbux.returns)
abline(-1,1)


Two questions here.

1. By just looking at the plots above, I can tell that prices follow lognormal distribution - because the corresponding QQ plot has a much better fit with lognormal than with normal. But returns seem to fit well with both distributions - is that correct or I'm doing something wrong?
2. Obviously I'd better rely on a mathematical estimation of fitness rather than on a human checking the charts. I think, I can use Chi-squared or Kolmogorov-Smirnov test, but cannot understand how exactly to do that. E.g., ks.test(sbux.returns, sbux.returns.norm) gives me p-value = 0.007781 so I'm definitely missing something.
• Some computational error has messed up the QQ plot for the log prices--you should fix that first. But all this begs a more fundamental question: precisely why are you testing distributions? What actions will you take as a result? For some analyses, the distribution will matter little; for yet others--the commonest ones in finance--deciding on lognormality will be fine--but for yet others, especially if you're concerned about rare events, there are danger signs dangling off the lower ends of all the QQ plots!
– whuber
Commented Sep 19, 2012 at 19:30
• I just noticed that these are not QQ plots: they are scatterplots against randomly generated datasets. That's not a good idea: how are you going to distinguish real departures in your data from meaningless random departures, mere artifacts of your random number generation? Use qqnorm instead. If you like plot, at least look into using ppoints and qnorm to generate the reference values. Also: your use of ks.test is invalid (read the man page carefully: you can't base the comparison on a mean and SD computed from the data).
– whuber
Commented Sep 19, 2012 at 19:34
• Generally, it would be great to test fitness against any distribution - just clarified this in the question, sorry if it was confusing. Re my ks.test being wrong: aha, I missed that the second parameter must be CDF - in this case, ks.test(sbux.returns, pnorm, mean(sbux.returns), sd(sbux.returns)) should be correct, right? But it gives p-value = 0.01134 which also doesn't seem quite right, given the density and qq plots I've got.. All in all, if my question is more clear now, do you want to put your comment as a separate answer? Commented Sep 20, 2012 at 7:57
• Still not quite right: it is invalid to use the KS test to compare data against a normal distribution whose parameters were estimated from the same data.
– whuber
Commented Sep 20, 2012 at 13:30
• Hmm. Ok I wasn't paying attention! This one from en.wikipedia.org/wiki/…: "..If either the form or the parameters of F(x) are determined from the data Xi the critical values determined in this way are invalid.." - is this what you'd pointed out? Uh oh. Then what if I have x <- rnorm(n=300, sd=2), how to "find out" its SD and the best fit distribution? Should I use Monte-Carlo simulations and "other methods"? Commented Sep 20, 2012 at 14:00

As I see the density plot on top for prices the distribution doesn't appear to be normal nor lognormal. It seems to have 2 humps and a lognormal would not. But that could be an artifact of the kernel width (I am assuming a kernel density estimation was used). Is that what the density function in R does? I am not very familiar with R.

To my eye the density for returns fits well to a normal distribution but there is possibly a problem in the lower tail as seen from the qq plot. i don't think it fits well to a lognormal. The qq plot diverges from the line in both tails.

Why do you think the lognormal fit looks good for prices? The line is very steep indicating that the scales must be different. It should follow the 45 degree line if the quantiles on both axes used the same scale. Because the slope is so steep and the circles so fat I don't think it is really clear that the quantiles fall on a straight line.

• Yes, density does kernel density. Well, my assumptions come from financial theory: returns are considered to be distributed close to a normal and prices - close to a lognormal. Thanks for the catch about 45' - I guess I misused the line on the qq-plot, it should be always abline(0,1) which I don't do, so messed up. Hmm so what, looks like p-value on ks.test was correct!? :) I'd check t-distribution on returns tomorrow to see if the tails look better on a qq-plot. Commented Sep 19, 2012 at 19:22
• Andreister, That business about 45 degrees is irrelevant. (-1 to the answer for creating that confusion.) You did fine with the plots; you didn't mess up at all. Because the scales on the x and y axes (obviously!) differ, you cannot expect exactly 45 degrees. All that matters is whether the points are approximately collinear. But please see my comments to the question itself concerning other issues with these plots.
– whuber
Commented Sep 19, 2012 at 19:37
• @whuber Shame on you! I said that it should be on a 45 degree line if the scales are the same which is how a qq plot should look. The picture is poor and I could not tell for myself whether or not the scales were the same on the x and y axes. Suppose the scales were the same and the data followed a line with a slope different from 1. What would that tell you? Commented Sep 19, 2012 at 20:50
• Don't scold, Michael (as you know, personal attacks are inappropriate on this site, as they should be everywhere): you brought up the 45 degree issue and that clearly is misleading, despite the disclaimer at the end. I urge you to edit your reply to prevent the confusion.
– whuber
Commented Sep 19, 2012 at 20:54
• If I edit my answer will you remove the downvote. Maybe I can politely say that the downvote is inappropriate. Commented Sep 19, 2012 at 20:57