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.
- 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?
- 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 mep-value = 0.007781
so I'm definitely missing something.
qqnorm
instead. If you likeplot
, at least look into usingppoints
andqnorm
to generate the reference values. Also: your use ofks.test
is invalid (read the man page carefully: you can't base the comparison on a mean and SD computed from the data). $\endgroup$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 givesp-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? $\endgroup$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"? $\endgroup$