The code you posted implements the empirical cumulative distribution function. We want to estimate unknown cumulative distribution function, $P(X \le x)$, from the data. The function applies the most basic definition of probability, based on counts
$$
P(\text{something}) = \frac{\text{how often something happen}}{\text{sample size}}
$$
In this case, we are are calculating cumulative probabilities, so it is
$$
P(X \le x) = \frac{\#(X \le x)}{N}
$$
where $\#$ is the count operator, that counts the values in $X$ meeting the condition.
The function however is incorrect, as it should be rather
yvals = np.arange(1, len(sorted_data)+1)/float(len(sorted_data))
because for the first element of sorted_data
we don't want it to return probability equal to zero (obviously we observed this value, so it cannot happen with zero probability!).
But let's discuss it in more details. We are dealing with sorted_data
, the data that is already sorted for us in ascending order. For each element in sorted_data
, we assign some value from yvals
. To calculate the values, we use the np.arange(1, len(sorted_data)+1)
function, that returns series $1,2,\dots,N$. This series corresponds to empirical cumulative counts, since first value in sorted_data
appeared only once, there was two values smaller or equal to the second value from sorted_data
, all the values were smaller or equal then the last value from sorted_data
etc. Next, the values are divided by float(len(sorted_data))
, so by the sample size $N$ (the float
function is needed only for python 2, for it to calculate the floating-point division rather then integer). So we end up with fractions $\tfrac{1}{N},\tfrac{2}{N},\dots,\tfrac{N}{N}$, that correspond to probabilities.
Notice that this assumes that we are dealing with continuous variable in here, as the function ignores the fact that there could be duplicated elements in sorted_data
. If you are dealing with discrete variable, you should rather calculate this on counts of sorted values.
If you wonder if we could use similar function to calculate the non-cumulative probabilities, $P(X=x)$, then the answer is: yes, but only for discrete variable (and large sample size to get reasonable approximation). For continuous variable it gets more complicated, because there is infinitely many possible values of $X$, so $P(X=x)=0$, and for this reason we have probability densities, i.e. "probabilities per foot". What follows, for continuous variables we have histograms, kernel density estimators and a few other, less popular, estimators.