+3 votes
in Programming Languages by (9.6k points)

I am using statsmodels package to estimate probability density. My training data is a list of 100 continuous values. The pdf() function of the module is retuning an error instead of probability density. 

Here is my code. What is wrong with the code?

>>> import statsmodels as sm

>>> import numpy as np

>>> v=np.random.random(100)

>>> dens = sm.nonparametric.kernel_density.KDEMultivariate(v,var_type='c', bw=0.01)

>>> dens

KDE instance

Number of variables: k_vars = 1

Number of samples:   nobs = 100

Variable types:      c

BW selection method: user-specified

>>> dens.pdf()

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "/usr/local/lib/python3.8/dist-packages/statsmodels/nonparametric/kernel_density.py", line 191, in pdf

    pdf_est.append(gpke(self.bw, data=self.data,

  File "/usr/local/lib/python3.8/dist-packages/statsmodels/nonparametric/_kernel_base.py", line 511, in gpke

    Kval[:, ii] = func(bw[ii], data[:, ii], data_predict[ii])

IndexError: too many indices for array: array is 0-dimensional, but 1 were indexed

1 Answer

+3 votes
by (42.1k points)

If you look at the code before the "IndexError:" line, bandwidth should be an array, not a scalar value.

Kval[:, ii] = func(bw[ii], data[:, ii], data_predict[ii])

The documentation page of "statsmodels" also says that the bandwidth should be array_like or str.

So, you need to make a small change in your code to fix the error. You need to pass bandwidth as an array.

From

dens = sm.nonparametric.kernel_density.KDEMultivariate(v,var_type='c', bw=0.01)

to

dens = sm.nonparametric.kernel_density.KDEMultivariate(v,var_type='c', bw=[0.01])

 Now, when you use the pdf() function, you should get probability density.

>>> dens.pdf()
array([0.93576608, 1.33566324, 1.20704937, 1.80351188, 1.81875581,
       1.51516665, 0.89062936, 1.58329715, 1.22700952, 1.06059397,
       0.71512836, 1.54702014, 0.79761149, 1.23734685, 1.4262315 ,
       0.80663581, 2.16846024, 0.49280956, 1.18420447, 2.20298496,
       2.02025112, 1.58441982, 1.90166434, 2.36251347, 1.53255557,
       1.31409131, 1.5425467 , 0.98515632, 0.87081109, 1.55647159,
       1.67918891, 0.95854484, 1.28548948, 1.06750887, 0.88545365,
       1.50659786, 1.48258047, 0.89849563, 1.08101309, 1.24352269,
       1.32795349, 1.03990374, 0.55019702, 2.13360702, 1.11418973,
       1.3953946 , 1.22836918, 0.42480256, 1.31166456, 1.81168169,
       1.83213356, 1.61794576, 1.03155669, 1.1937047 , 0.51740995,
       1.27018985, 1.35019434, 1.55660672, 1.83607643, 1.67935869,
       0.69868855, 1.49157491, 2.23200936, 1.37639306, 1.00678124,
       1.67379048, 1.37299867, 1.24108142, 2.18630987, 1.26790982,
       1.26945955, 0.78485074, 1.62732738, 0.75846431, 0.91707162,
       2.34255543, 0.98250775, 1.23858443, 1.79723892, 0.61162554,
       1.16223329, 1.36762502, 2.31184928, 0.79395791, 1.4167074 ,
       0.68784724, 1.31792448, 1.44168927, 1.76746678, 2.05878253,
       1.89587752, 1.45545394, 1.24398978, 2.20341703, 0.91273624,
       1.52657731, 0.88443466, 0.9142357 , 1.89401561, 1.27891166])
 

...