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 me
p-value = 0.007781so I'm definitely missing something.