| 1 |
#ifndef _SlopeSelector_h_ |
| 2 |
#define _SlopeSelector_h_ |
| 3 |
/* SlopeSelector.h |
| 4 |
* |
| 5 |
* Copyright (C) 2025-2026 David Weenink |
| 6 |
* |
| 7 |
* This code is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or (at |
| 10 |
* your option) any later version. |
| 11 |
* |
| 12 |
* This code is distributed in the hope that it will be useful, but |
| 13 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
/* |
| 22 |
The algorithm to find the kth (or median) slope in O (n log(n)) time is described in |
| 23 |
Matousek (1991): Randomized optimal algorithm for slope selection. |
| 24 |
Information processing letters 39: 183-187. |
| 25 |
Given points p[i] = (ax[i], ay[i]), i = 1..N, with ax[i] < ax[i+1] for i = 1..N-1. |
| 26 |
Find the line with the k-th largest slope of all the N(N-1)/2 lines connecting |
| 27 |
p[i] = (ax[i],ay[i]) with p[j] = (ax[j],ay[j]) where i = 1..N,and j = i+1..N. |
| 28 |
We consider this problem in the dual space where we have N lines line[l] where |
| 29 |
line[l] is y = ax[N-l+1] * x - ay[N-l+1]. |
| 30 |
The ordering of the lines is such that when x = -inf, line [1] has the lowest y-value and |
| 31 |
line [N] the highest. Therefore, the correspondence between line number 'l' and point number 'i' is |
| 32 |
l = N-i+1, for i = 1..N. |
| 33 |
The x coordinate of the intersection of line[i] and line[j] is at x = (ay[N-i+1]-ay[N-j+1])/(ax[N-i+1]-ax[N-j+1]), |
| 34 |
which equals the slope of the line connecting p[i] and p[j]. |
| 35 |
In the dual space (x,y) we then search for the intersection with the k-th largest x-coordinate. |
| 36 |
For x = -inf the y-values of line[1]..line[N] are y[1] > y[2] > .. y[N], and |
| 37 |
for x = +inf the y-values of line[1]..line[N] are in reverse order y[N] > y[N-1] > .. y[1]. |
| 38 |
If we sort the N crossings of the lines at x = a according to line number this will be a permutation |
| 39 |
of the numbers 1..N. The number of inversions in this permutation has to be equal to the number of intersections |
| 40 |
to the left of x=a (an inversion occurs when i < j && perm[i] > perm[j]). |
| 41 |
The number of inversions in a permutation of size N can be counted in O(N log(N)). |
| 42 |
*/ |
| 43 |
|
| 44 |
#include "PermutationInversionCounter.h" |
| 45 |
#include "melder.h" |
| 46 |
#include "NUM2.h" |
| 47 |
#include "SlopeSelector_enums.h" |
| 48 |
#include "ExtendedReal.h" |
| 49 |
|
| 50 |
//#include "SlopeSelector_def.h" |
| 51 |
|
| 52 |
struct structExtendedCrossing { |
| 53 |
double real; |
| 54 |
integer low, high; |
| 55 |
|
| 56 |
friend bool operator< (const structExtendedCrossing& lhs, const structExtendedCrossing& rhs) { |
| 57 |
auto approximatelyEqual = [&] (double numericalEqualityPrecision) { |
| 58 |
if (std::fabs (lhs.real) < numericalEqualityPrecision || std::fabs (rhs.real) < numericalEqualityPrecision) { |
| 59 |
return std::fabs (lhs.real - rhs.real) < numericalEqualityPrecision; |
| 60 |
} |
| 61 |
// Use relative difference otherwise |
| 62 |
return std::fabs (lhs.real - rhs.real) <= numericalEqualityPrecision * std::fmax (std::fabs (lhs.real), std::fabs (rhs.real)); |
| 63 |
}; |
| 64 |
const bool r = ( !approximatelyEqual (1e-12) ? (lhs.real < rhs.real) : |
| 65 |
(lhs.high != rhs.high ? (lhs.high > rhs.high) : (lhs.low < rhs.low)) ); |
| 66 |
return r; |
| 67 |
} |
| 68 |
/* |
| 69 |
All derived from above comparison |
| 70 |
*/ |
| 71 |
friend inline bool operator> (const structExtendedCrossing& lhs, const structExtendedCrossing& rhs) { |
| 72 |
return rhs < lhs; |
| 73 |
} |
| 74 |
friend inline bool operator<= (const structExtendedCrossing& lhs, const structExtendedCrossing& rhs) { |
| 75 |
return ! (lhs > rhs); |
| 76 |
} |
| 77 |
friend inline bool operator>= (const structExtendedCrossing& lhs, const structExtendedCrossing& rhs) { |
| 78 |
return ! (lhs < rhs); |
| 79 |
} |
| 80 |
friend inline bool operator== (const structExtendedCrossing& lhs, const structExtendedCrossing& rhs) { |
| 81 |
return std::tie (lhs.real, lhs.low, lhs.high) == std::tie (rhs.real, rhs.low, rhs.high); |
| 82 |
} |
| 83 |
friend inline bool operator!= (const structExtendedCrossing& lhs, const structExtendedCrossing& rhs) { |
| 84 |
return ! (lhs == rhs); |
| 85 |
} |
| 86 |
}; |
| 87 |
|
| 88 |
typedef struct structExtendedCrossing *ExtendedCrossing; |
| 89 |
typedef const struct structExtendedCrossing *constExtendedCrossing; |
| 90 |
|
| 91 |
Thing_define (SlopeSelector, Thing) { |
| 92 |
integer numberOfPoints; |
| 93 |
integer sampleSize; |
| 94 |
integer numberOfTries; |
| 95 |
integer maximumNumberOfTries; |
| 96 |
integer maximumContractionSize; // > sampleSize + numberOfPoints |
| 97 |
autoPermutation lineRankingAtLowX; |
| 98 |
autoPermutation lineRankingAtLowXPrevious; |
| 99 |
autoPermutation inverseOfLineRankingAtLowX; |
| 100 |
autoPermutation lineRankingAtHighX; |
| 101 |
autoPermutation lineRankingAtHighXPrevious; |
| 102 |
autoINTVEC sortedRandomCrossingCodes; |
| 103 |
integer inversionsSize; |
| 104 |
autoINTVEC currentInversions; |
| 105 |
autovector<structExtendedCrossing> xslopes; |
| 106 |
autoVEC buffer; // for buffering and final quantile calculations |
| 107 |
autovector<structExtendedCrossing> xcrossings; |
| 108 |
autoPermutationInversionCounter inversionCounter; |
| 109 |
|
| 110 |
constVEC xp; // links to the outside world data points (by newDataPoints(x,y)) |
| 111 |
constVEC yp; // |
| 112 |
|
| 113 |
void newDataPoints (constVEC const& x, constVEC const& y); |
| 114 |
|
| 115 |
double getSlope_Siegel (); |
| 116 |
|
| 117 |
double getSlope_TheilSen (); |
| 118 |
|
| 119 |
double getIntercept (double slope); |
| 120 |
|
| 121 |
void getSlopeAndIntercept_leastSquares (double &slope, double& intercept); |
| 122 |
|
| 123 |
void getKth_TheilSen (integer k, double& kth, double& kp1th); |
| 124 |
|
| 125 |
double slopeQuantile_TheilSen (double factor); |
| 126 |
|
| 127 |
double slopeQuantile_orderNSquared (double factor); |
| 128 |
|
| 129 |
double slopeQuantile_orderNSquaredWithBuffer (double factor, VEC const& buffer); |
| 130 |
|
| 131 |
double slopeQuantile (double factor) { |
| 132 |
return slopeQuantile_TheilSen (factor); |
| 133 |
} |
| 134 |
}; |
| 135 |
|
| 136 |
/* |
| 137 |
Preconditions: |
| 138 |
x.size == y.size && x.size > 0 |
| 139 |
i < j && x[i] < x[j] |
| 140 |
*/ |
| 141 |
|
| 142 |
autoSlopeSelector SlopeSelector_create (constVEC const& x, constVEC const& y); |
| 143 |
|
| 144 |
autoSlopeSelector SlopeSelector_create (integer numberOfPoints); |
| 145 |
|
| 146 |
void SlopeSelector_getSlopeAndIntercept (SlopeSelector me, double &slope, double &intercept, kSlopeSelector_method method); |
| 147 |
|
| 148 |
/****************/ |
| 149 |
|
| 150 |
void timeSlopeSelection (); |
| 151 |
|
| 152 |
#endif /* _SlopeSelector_h_ */ |