numpy/scipy equivalent of R ecdf(x)(x) function?

前端 未结 4 1530
情话喂你
情话喂你 2020-12-29 07:40

What is the equivalent of R\'s ecdf(x)(x) function in Python, in either numpy or scipy? Is ecdf(x)(x) basically the same as:

import         


        
4条回答
  •  醉话见心
    2020-12-29 08:28

    The ecdf function in R returns the empirical cumulative distribution function, so the have exact equivalent would be rather:

    def ecdf(x):
        x = np.sort(x)
        n = len(x)
        def _ecdf(v):
            # side='right' because we want Pr(x <= v)
            return (np.searchsorted(x, v, side='right') + 1) / n
        return _ecdf
    
    np.random.seed(42)
    X = np.random.normal(size=10_000)
    Fn = ecdf(X)
    Fn([3, 2, 1]) - Fn([-3, -2, -1])
    ## array([0.9972, 0.9533, 0.682 ])
    

    As shown, it gives the correct 68–95–99.7% probabilities for normal distribution.

提交回复
热议问题