[SciPy-User] inverse function of a spline

josef.pktd at gmail.com josef.pktd at gmail.com
Fri May 7 16:36:15 EDT 2010


On Fri, May 7, 2010 at 4:24 PM, Anne Archibald
<peridot.faceted at gmail.com> wrote:
> On 7 May 2010 12:40,  <josef.pktd at gmail.com> wrote:
>> I have a function  y = f(x) which is monotonically increasing (a
>> cumulative distribution function)
>> f is defined by piecewise polynomial interpolation, an interpolating
>> spline on some points
>>
>> I would like to get the inverse function (ppf)  x = f^{-1} (y)
>> if the spline is of higher order than linear
>>
>> In the linear case it's trivial, because the inverse function is also
>> just a piecewise linear interpolation.
>>
>> If I have a cubic spline, or any other smooth interpolator in scipy,
>> is there a way to get the
>> inverse function directly?
>>
>> I don't know much about general properties of splines, and would
>> appreciate any hints,
>> so I can avoid numerical inversion (fsolve or similar)
>
> I should first say that even though your input points are monotonic,
> the spline is not guaranteed to be. (Though in practice if your
> sampled points have no sharp corners you're probably fine.) If this
> matters to you, there are algorithms for enforcing monotonicity of
> splines, some of which are simply procedures for jiggering the
> interpolation points just enough to avoid non-monotonicty and some of
> which are more clever. Sadly none are implemented in scipy.
>
> As Charles Harris pointed out, the inverse of a cubic is not a cubic,
> so the inverse function won't be a spline. But you can nevertheless
> efficiently evaluate it with scipy.interpolate.sproot, which a
> special-purpose numerical solver. I'm not sure whether it uses cubic
> root solvers or an optimized numerical solver with knowledge about the
> bounding properties of spline control points, but in any case it's
> quite efficient. It only finds zeros, but (check this) you should be
> able to shift a spline vertically by subtracting a constant from the
> coefficient array (c in t,c,k).
>
> Since you are constructing the spline in the first place, you should
> also think about whether you're evaluating f or f inverse more often
> and choose which one to be the spline appropriately.

Thanks, I will try to figure out the sproot version.

For now I'm stuck (and go somewhere else) because in the examples
that I tried out, I get small non-monotonicities most of the time.
The spline of the inverse function is backwards bending.

I will stick with linear interpolation and kernel density estimation for the
smooth case.

BTW: I'm writing some histogram distribution and variants of empirical
distribution classes that have the same methods as the ones in scipy.stats.

Josef
>
> Anne
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>



More information about the SciPy-User mailing list