0

I would like to plot an empirical distribution function using statmodels. I tried what is advised here How to plot empirical cdf (ecdf):

import numpy as np
import statsmodels.api as sm
import matplotlib.pyplot as plt

price_values = [[4.2],
    [4.1],
    [4],
    [3.8],
    [3.9],
    [4.2],
    [4.5],
    [4.8],
    [5.2],
    [5.2],
    [5.2],
    [5.6],
    [5.2],
    [5.1],
    [5.3],
    [6],
    [6.2],
    [6.3],
    [6.2],
    [6],
    [5.5] ,
    [5.2],
    [4.8],
    [4.6]]

sample = price_values
ecdf = sm.distributions.ECDF(sample)

x = np.linspace(min(sample), max(sample))
y = ecdf(x)
plt.step(x, y)
plt.show()

Unforunately, the line ecdf = sm.distributions.ECDF(sample) yields the error "ValueError: x and y do not have the same shape" that I don't understand. Any idea why this error occurs and how to tackle it?

Update: Here is the whole traceback of the error:

Traceback (most recent call last):
  File "C:\Users\wi9632\PycharmProjects\building-optimization\src\Plot_EDF.py", line 31, in <module>
    ecdf = sm.distributions.ECDF(sample)
  File "C:\Users\wi9632\Anaconda3\lib\site-packages\statsmodels\distributions\empirical_distribution.py", line 139, in __init__
    super(ECDF, self).__init__(x, y, side=side, sorted=True)
  File "C:\Users\wi9632\Anaconda3\lib\site-packages\statsmodels\distributions\empirical_distribution.py", line 89, in __init__
    raise ValueError(msg)
ValueError: x and y do not have the same shape

Process finished with exit code 1
PeterBe
  • 700
  • 1
  • 17
  • 37
  • always show the traceback, or at least the last part of it that shows where the exception was raise. Without it, guessing is difficult. – Josef Dec 09 '22 at 13:27
  • linspace is missing the length argument, len(y), I guess error is in matplotlib usage and not in statsmodels usage – Josef Dec 09 '22 at 13:29
  • @Josef: Thanks for your comment. I added the traceback in the question. I hope this helps. – PeterBe Dec 09 '22 at 15:38
  • 1
    converting your `sample` to numpy array, creates a 2-dimensional array. It needs to be 1-dim. Use `sample = np.asarray(price_values).squeeze()` – Josef Dec 09 '22 at 17:52

0 Answers0