Plotting Q-Q graph in Python like in R

Viewed 164

Ive´a trouble when I try to plot the following data store in variable i.

   Dates
2015-08-21    0.032369
2015-08-24    0.040211
2015-09-01    0.030023 

Python

import statsmodels.api as sm
from matplotlib import pyplot as plt
fig = sm.qqplot(i, line='45')
plt.show()

R

r <- 0.01
spcs <- as.numeric(diff(time(i)))
qq_plot(spcs, FUN = function(p) qexp(p, rate = r)) # r = exceedance probability

in R

I get the following plot:

QQ_PLOT in R

I share the link to the notebook in colab may be help to understand the problem

[Colab Link][2]

1 Answers

You should plot the index of the pandas series instead (take the diff of the DatetimeIndex, convert it to timedelta and extract days), like the following, in order to get the correct qq-plot (with the appropriate distribution):

import statsmodels.api as sm
import scipy.stats as stats
from matplotlib import pyplot as plt

pd.to_timedelta(np.diff(i.index), unit='ns').days.values
# array([3, 8])

r = 0.01
fig = sm.qqplot(pd.to_timedelta(np.diff(i.index), unit='ns').days.values, \
                stats.expon, loc=0, scale=r)
plt.show()

enter image description here

Related