Find Crossover Points of Two Spline Functions

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Find Crossover Points of Two Spline Functions

Dario Strbenac-2
Good day,

I have two probability densities, each with a function determined by splinefun(densityResult[['x']], densityResult[['y']], "natural"), where densityResult is the output of the density function in stats. How can I determine all of the x values at which the densities cross ?

--------------------------------------
Dario Strbenac
PhD Student
University of Sydney
Camperdown NSW 2050
Australia

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Find Crossover Points of Two Spline Functions

bbolker
Dario Strbenac <dstr7320 <at> uni.sydney.edu.au> writes:

>
> Good day,
>
> I have two probability densities, each with a function determined
> by splinefun(densityResult[['x']],
> densityResult[['y']], "natural"), where densityResult is the
> output of the density function in stats.
> How can I determine all of the x values at which the densities cross ?
>

  My initial thought was this is non-trivial, because the two densities could
cross (or nearly-but-not-quite cross) at an unlimited number of points.
I thought it would essentially boils down to "how do I find all
the roots of an arbitrary (continuous, smooth) function?

  However, after thinking about it for a few more seconds I realize
that at least the functions are piecewise cubic.  I still don't see
a *convenient* way to do it ... if the knots were all coincident between
the two densities (maybe you could constrain them to be so?) then you
just have a difference of cubics within each segment, and you can use
polyroot() to find the roots (and throw out any that are complex or
don't fall within the segment).
  If the knots are not coincident it's more of a pain but you should
still be able to do it by considering overlapping segments ...

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Find Crossover Points of Two Spline Functions

Bert Gunter-2
Use ?uniroot to do it numerically instead of polyroot()?

Cheers,
Bert
Bert Gunter

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
   -- Clifford Stoll


On Mon, Sep 28, 2015 at 9:17 AM, Ben Bolker <[hidden email]> wrote:

> Dario Strbenac <dstr7320 <at> uni.sydney.edu.au> writes:
>
>>
>> Good day,
>>
>> I have two probability densities, each with a function determined
>> by splinefun(densityResult[['x']],
>> densityResult[['y']], "natural"), where densityResult is the
>> output of the density function in stats.
>> How can I determine all of the x values at which the densities cross ?
>>
>
>   My initial thought was this is non-trivial, because the two densities could
> cross (or nearly-but-not-quite cross) at an unlimited number of points.
> I thought it would essentially boils down to "how do I find all
> the roots of an arbitrary (continuous, smooth) function?
>
>   However, after thinking about it for a few more seconds I realize
> that at least the functions are piecewise cubic.  I still don't see
> a *convenient* way to do it ... if the knots were all coincident between
> the two densities (maybe you could constrain them to be so?) then you
> just have a difference of cubics within each segment, and you can use
> polyroot() to find the roots (and throw out any that are complex or
> don't fall within the segment).
>   If the knots are not coincident it's more of a pain but you should
> still be able to do it by considering overlapping segments ...
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Find Crossover Points of Two Spline Functions

Bert Gunter-2
... (should have added)

However one might ask: Isn't this just a bit silly? The density()
function gives kernel density estimates (perhaps interpolated by
?approx -- see ?density) on as fine a grid as one likes, so why use
splines thereafter? And since these are density estimates -- i.e.
fitted approximations -- anyway, why search for "exact" solutions? Of
course I don't know the detailed context, but this whole question
sounds somewhat bogus.

Cheers,
Bert


Bert Gunter

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
   -- Clifford Stoll


On Mon, Sep 28, 2015 at 9:36 AM, Bert Gunter <[hidden email]> wrote:

> Use ?uniroot to do it numerically instead of polyroot()?
>
> Cheers,
> Bert
> Bert Gunter
>
> "Data is not information. Information is not knowledge. And knowledge
> is certainly not wisdom."
>    -- Clifford Stoll
>
>
> On Mon, Sep 28, 2015 at 9:17 AM, Ben Bolker <[hidden email]> wrote:
>> Dario Strbenac <dstr7320 <at> uni.sydney.edu.au> writes:
>>
>>>
>>> Good day,
>>>
>>> I have two probability densities, each with a function determined
>>> by splinefun(densityResult[['x']],
>>> densityResult[['y']], "natural"), where densityResult is the
>>> output of the density function in stats.
>>> How can I determine all of the x values at which the densities cross ?
>>>
>>
>>   My initial thought was this is non-trivial, because the two densities could
>> cross (or nearly-but-not-quite cross) at an unlimited number of points.
>> I thought it would essentially boils down to "how do I find all
>> the roots of an arbitrary (continuous, smooth) function?
>>
>>   However, after thinking about it for a few more seconds I realize
>> that at least the functions are piecewise cubic.  I still don't see
>> a *convenient* way to do it ... if the knots were all coincident between
>> the two densities (maybe you could constrain them to be so?) then you
>> just have a difference of cubics within each segment, and you can use
>> polyroot() to find the roots (and throw out any that are complex or
>> don't fall within the segment).
>>   If the knots are not coincident it's more of a pain but you should
>> still be able to do it by considering overlapping segments ...
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Find Crossover Points of Two Spline Functions

bbolker
In reply to this post by Bert Gunter-2
Bert Gunter <bgunter.4567 <at> gmail.com> writes:

>
> Use ?uniroot to do it numerically instead of polyroot()?
>
> Cheers,
> Bert
> Bert Gunter

  The problem with uniroot() is that we don't know how many intersections/
roots we might be looking for. With polyroot(), we know that there can
be at most 3 roots that lie within a particular cubic segment.

  (I'm not arguing that doing this is necessarily a good idea in
the larger context.)

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.