trulyrural
trulyrural

Reputation: 33

Implementing KS test for lognormal fit

So I have some stock price data and I want to test whether the prices follow the lognormal distribution. My code is as follows:

import scipy.stats as stats
print(stats.kstest(df['DJIA'], "lognorm", stats.lognorm.fit(df['DJIA'])))

The results are as follows:

KstestResult(statistic=0.90996368658950855, pvalue=0.0)

p-value of zero means that the data do not fit a lognormal distribution, which is not possible, as stock prices are expected to be lognormally distributed. The histogram of the prices is as follows:

enter image description here

I'm trying to fit the histogram with the lognormal function, but unable to do this.

Upvotes: 1

Views: 2488

Answers (1)

gluejar
gluejar

Reputation: 99

You are trying to fit the price data when you should be fitting the price distribution. Try fitting the count data from a histogram. The plot you show doesn't look like a histogram of the stock prices, it looks like a bar graph of price data.

count, bins, ignored = plt.hist(df['DJIA'], 100, normed=True, align='mid')
params = stats.lognorm.fit(count)
stats.kstest(count, "lognorm", params)

Be careful with kstest and fit because log-normal is a continuous distribution, and your histogram is discrete.

Upvotes: 3

Related Questions