Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on Any idea for predicting the peak points in the graph by perl

Replies are listed 'Best First'.
Re: Any idea for predicting the peak points in the graph by perl
by inman (Curate) on Apr 18, 2005 at 11:35 UTC
    I haven't done any of this since university but I remember that a spline can be used to calculate the points on a line given a number of existing ordinates. I forget how it works (I learned about it for an exam and then forgot it pronto) but a quick CPAN search yielded Math::Spline which appears to do the trick.

    according to the POD, you can create a Math::Spline object with a number of points (as two array refs). You can then evaluate the result for a particular point.

    The advantage of this method is that you can locate a point that is on a continuous line but which isn't a point in your data.

Re: Any idea for predicting the peak points in the graph by perl
by tlm (Prior) on Apr 18, 2005 at 11:31 UTC

    This is what I would do:

    my $max = [ $x[ 0 ], $y[ 0 ] ]; for my $i ( 1..$#x ) { last if $y[ $i ] <= $max[ 1 ]; $max = [ $x[ $i ], $y[ $i ] ]; } print "max at ($max->[ 0 ], $max->[ 1 ])\n";

    This code makes two important assumptions: that your data is ordered in the x-axis and that it is really as smooth as you describe it. If neither of these two assumptions hold, the following would be better:

    my $max = [ $x[ 0 ], $y[ 0 ] ]; for my $i ( 1..$#x ) { $max = [ $x[ $i ], $y[ $i ] ] if $y[ $i ] > $max->[ 1 ]; } print "max at ($max->[ 0 ], $max->[ 1 ])\n";

    the lowliest monk

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Any idea for predicting the peak points in the graph by perl
by zentara (Cardinal) on Apr 18, 2005 at 11:57 UTC
    I think what inman is talking about, is creating a line in the cartesian coordinate system, by using 2 adjacent data points. If the "slope" of the line is positive, then shift data points by one, so the "previously second" x value, is now the first, and compute the next slope. Where the slope turns from a positive to negative value, is a "local" peak.

    The slope of a line is y = Mx + b, where M is the slope. M is usually (y2 -y1)/(x2 - x1).

    I think Pustular Postulant's method is probably faster.


    I'm not really a human, but I play one on earth. flash japh
Re: Any idea for predicting the peak points in the graph by perl
by salva (Canon) on Apr 18, 2005 at 12:07 UTC
    what do you mean exactly by "gradual"?

    If there is some noise in your input data that causes small peaks to appear, then you need to apply a filter first.

    For example, substitute every element by the mean of its predecessor, the element itself and its successor. If this is not enough, try extending the mean to cover more elements around. You can also ponderate the mean calculation so nearer elements have more weight, i.e.:

    $fd[$n] = ( 0.25*$d[$n-2] + 0.5*$d[$n-1] + 1.0*$d[$n] + 0.5*$d[$n+1] + 0.25*$d[$n+2] ) / (0.25+0.5+1.0+0.5+0.25);
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Any idea for predicting the peak points in the graph by perl
by pvaldes (Chaplain) on Jul 15, 2012 at 10:22 UTC

    Not, this is not the right way

    What you want probably is to find (to predict) the critic points of the function defined by your sample points. Obtaining the maximum point of the sample is not of much value because what is really interesting in the graph is the curve of the function that fits better your points. Its maximum and minimun points can be (and probably will be) between two given points

    You probably need instead

    use Math:Derivative;

    increase gradually and then decrease gradually

    Clearly your teacher was trying to tell you something with this words. A requisite to be able to find what you want is that your function is continuous

    EDIT: Well I see now that a lot of the last messages point to this yet. Your problem is a math problem, not a "sort this array" problem

A reply falls below the community's threshold of quality. You may see it by logging in.