[SciPy-user] piecewise continuous distribution

Robert Kern robert.kern at gmail.com
Mon Jul 20 23:45:32 EDT 2009


On Mon, Jul 20, 2009 at 22:26, Brennan
Williams<brennan.williams at visualreservoir.com> wrote:
> I have a user-specified  piecewise continuous distribution defined by
> pairs of (value, cumulative probability).
>
> For example...
>
> Value ,    Cumulative Probability
> ---------------------------
> 1.0, 0.0
> 1.5, 0.2
> 2.3, 0.4
> 3.5, 0.6
> 5.2, 0.8
> 8.5, 1.0
>
> So no values below 1.0, none above 8.5.
> Between adjacent specified values the generated values will be uniformly
> distributed,
> i.e. the cumulative probability graph is made up of straight lines
> between adjacent points/values.
>
> The generated values are created by a piece of TCL code.
>
> What I need to do is assign a pdf probability value to each generated value.
>
> How would I do this?

In [25]: x0 = np.array([1.0, 1.5, 2.3, 3.5, 5.2, 8.5])

In [26]: cdf = np.linspace(0, 1, len(x0))

In [27]: def pdf(x):
    y = np.zeros_like(x)
    inside = (x >= x0[0]) & (x <= x0[-1])
    x = x[inside]
    i = x0.searchsorted(x).clip(0, len(x0)-2)
    block_pdf = np.diff(cdf) / np.diff(x0)
    y[inside] = block_pdf[i]
    return y
   ....:

In [35]: pdf(np.linspace(0.0, 10.0))
Out[35]:
array([ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.25      ,  0.25      ,  0.25      ,  0.16666667,  0.16666667,
        0.16666667,  0.16666667,  0.11764706,  0.11764706,  0.11764706,
        0.11764706,  0.11764706,  0.11764706,  0.06060606,  0.06060606,
        0.06060606,  0.06060606,  0.06060606,  0.06060606,  0.06060606,
        0.06060606,  0.06060606,  0.06060606,  0.06060606,  0.06060606,
        0.06060606,  0.06060606,  0.06060606,  0.06060606,  0.06060606,
        0.06060606,  0.06060606,  0.06060606,  0.06060606,  0.06060606,
        0.06060606,  0.06060606,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ])


> What is the best way to do it in scipy.stats?

Neither here nor there, really.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco



More information about the SciPy-User mailing list