Skip to content

Off by one error in cumulative_distribution_function #111

Description

@EliasL

Hi,

I think there is a small but important error in cumulative_distribution_function: the empirical CDF is missing a +1. Here is a minimal example:

import numpy as np
from powerlaw import cumulative_distribution_function

data = np.array([1, 2, 3, 4, 5])
x, F = cumulative_distribution_function(data)
print(F)

Current output:
[0. 0.2 0.4 0.6 0.8]

Expected output (standard ECDF):
[0.2 0.4 0.6 0.8 1.0]

In cumulative_distribution_function, the line

        CDF = arange(n)/n

should be

    CDF = arange(1, n+1)/n

Here is a reputable library implementing the same function (more accurately called Empirical CDF). They use

x = np.sort(np.asarray(x))
nobs = len(x)
y = np.linspace(1./nobs, 1, nobs)

which is equivalent to arange(1, n+1)/n, and you can also see this tutorial do the same.

To fix this issue on the duplicates path (not all data entries are unique) as well, we need to use side='right' instead of 'left'. That is equivalent to adding the +1 when all data entries are unique.

I made a pull request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions