Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for equality of doubles in a list with a threshold

Tags:

c#

math

I am working on a method wich should decide whether or not a curve has a nearly constant slope or not.

There are of course x,y points involved. What I did so far is dividing y of each data point by its x to get the slope of that data point. I store this slopes in a List<double>

I think so far I am on the right track (tell me please, if I am not!). Now it's time to decide about being dealing with a constant curve or not, so I ended up with the method below:

private bool IsConstantSlope(List<double> slopes)
{            
    var max = slopes.Max();
    var min = slopes.Min();
    var diff = max - min;

    return (diff > 0.01) ? false : true;        
}

So what I do here checking for maximum and minimum values of slopes and compare it to a custom threshold which I beleive is not good at all.

This method works good for perfectly constant sloped lines, but I want to give it some felexibility, I don't think comparing the difference of max and min values to a constant number is a good practice.

I will appriciate more ideas!

like image 655
Saeid Yazdani Avatar asked Jan 17 '23 04:01

Saeid Yazdani


2 Answers

There are ofcource x,y points involved. what I did so far is dividing y of each data point by its x to get the slope of that data point. I store this slopes in a List

Strictly speaking a point does not have a slope, what you are measuring here is the slope of the line that connects your point (x,y) and the point (0,0). So if you are doing this for an ordered set of points, then the notion of having a single line is not quite correct. You dont even have the set of slopes of lines that connect adjacent points. Also in your function

 return (max > 0.01) || (min < -0.01);

is better if your threshold is 0.01.

If what you really want is a line that fits or approximates the set of points then you first need to perform some kind of straight line regression to your data and test the gradient of this approximating line to see if it is within your threshold limits.

This might be a useful read http://en.wikipedia.org/wiki/Simple_linear_regression

Alternatively, you can order your points by their x value, then work out the slope between each consecutive pair (effectively generating a polyline) and store these in your list and then use your slope camparison function.

like image 63
mathematician1975 Avatar answered Feb 07 '23 12:02

mathematician1975


I would design a recursive algorithm, working on the whole set of slopes. Considering only the min/max slopes doesn't tell anything about the whole curve. First of all, I would establish which is the requirement that two slopes A and B must fulfill in order to determine a "constant slope". Then, I would consider the first (A) and last (B) values in your list: do the two values statisfy the requirement? No: no constant slope; Yes: subdivide the range (A,B) into two subranges: (A,M), (M,B) where M is the value equidistant, in the list, from A and B. Then you apply the same algorithm to the two subranges. The number of subranges depends on the accuracy you want to achieve.

like image 20
Zaphod Beeblebrox Avatar answered Feb 07 '23 12:02

Zaphod Beeblebrox