tharvey
tharvey

Reputation: 57

scipy kstest, used on scipy lognormal distrubtion

I am trying to use scipy's kstest on some data, and a few different distributions. I got to testing my data against a log-normal distribution, and got confused so I made a test.

I am parameterizing the log normal by its own mean and standard deviation (instead of scipys version where loc is exponential of the corresponding normal mean and s is the standard deviation of the corresponding normal.) Explained here: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.lognorm.html

I have written a function that takes in the my parameters, converts them to scipys parameters, and then samples. Here it is:

def lognormal_samples(M_y, Sig_y):

    m_x = (2*math.log(M_y)) - (.5)*(math.log(math.pow(Sig_y,2) + math.pow(M_y,2)))
    scale = math.exp(m_x)

    sigma2 = -2 * math.log(M_y) + math.log(math.pow(Sig_y,2) + math.pow(M_y,2))
    s = math.sqrt(sigma2)

    result = stats.lognorm(s, scale=scale).rvs(size=10000)

    return result, s, scale

To test I want to see if the ks statistic is near 0 if I fit these samples to a scipy.stats.lognormal. Here I try to do that:

def lognormal_test_of_ks_test():

samples, my_s, my_scale = lognormal_samples(1, .25)
ks = stats.kstest(samples, 'lognorm', args=[my_s, my_scale])[0]
print('ks: ', ks)

When I run this I get ks: 0.958038612187 which is ridiculously high. My problem I believe is that when I pass [my_s,my_scale] to args these are not actually getting passed to s and scale in lognorm in kstest. How do I pass my two paremeters into kstest so that they actually parameterize lognorm? I would image it would be something like:

my_s = 's=' + str(my_s)
my_scale = 'scale=' + str(my_scale)
my_args = [my_s, my_scale]
ks = stats.kstest(samples, 'lognorm', args=my_args)[0]

But this doesn't work either.

Upvotes: 1

Views: 1981

Answers (1)

Ghasem Naddaf
Ghasem Naddaf

Reputation: 862

kstest ends up calling lognorm.cdf which takes the following arguments according to doc:

cdf(x, s, loc=0, scale=1)

So you need to pass:

my_args = [my_s, 0, my_scale]
ks = stats.kstest(samples, 'lognorm', args=my_args)[0]

which outputs:

ks:  0.007790356168134116

Upvotes: 1

Related Questions