# 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 Sep 19 '12 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 Sep 19 '12 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? – andreister Sep 20 '12 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 Sep 20 '12 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"? – andreister Sep 20 '12 at 14:00

• 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. – andreister Sep 19 '12 at 19:22