| 1 |
/* SlopeSelector.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 2025 David Weenink |
| 4 |
* |
| 5 |
* This code is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <limits> |
| 20 |
#include "NUMselect.h" |
| 21 |
#include "PermutationInversionCounter.h" |
| 22 |
#include "SlopeSelector.h" |
| 23 |
|
| 24 |
#if 0 |
| 25 |
#include "oo_DESTROY.h" |
| 26 |
#include "SlopeSelector_def.h" |
| 27 |
#include "oo_COPY.h" |
| 28 |
#include "SlopeSelector_def.h" |
| 29 |
#include "oo_EQUAL.h" |
| 30 |
#include "SlopeSelector_def.h" |
| 31 |
#include "oo_CAN_WRITE_AS_ENCODING.h" |
| 32 |
#include "SlopeSelector_def.h" |
| 33 |
#include "oo_WRITE_TEXT.h" |
| 34 |
#include "SlopeSelector_def.h" |
| 35 |
#include "oo_READ_TEXT.h" |
| 36 |
#include "SlopeSelector_def.h" |
| 37 |
#include "oo_WRITE_BINARY.h" |
| 38 |
#include "SlopeSelector_def.h" |
| 39 |
#include "oo_READ_BINARY.h" |
| 40 |
#include "SlopeSelector_def.h" |
| 41 |
#include "oo_DESCRIPTION.h" |
| 42 |
#include "SlopeSelector_def.h" |
| 43 |
#endif |
| 44 |
|
| 45 |
#include "enums_getText.h" |
| 46 |
#include "SlopeSelector_enums.h" |
| 47 |
#include "enums_getValue.h" |
| 48 |
#include "SlopeSelector_enums.h" |
| 49 |
|
| 50 |
static void randomInteger_INTVEC_inout (INTVEC iv, integer ilow, integer ihigh) { |
| 51 |
for (integer i = 1; i <= iv.size; i ++) |
| 52 |
iv [i] = NUMrandomInteger (ilow, ihigh); |
| 53 |
} |
| 54 |
|
| 55 |
Thing_implement (SlopeSelector, Thing, 0); |
| 56 |
|
| 57 |
void structSlopeSelector :: newDataPoints (constVEC const& x, constVEC const& y) { |
| 58 |
Melder_assert (x.size == y.size); |
| 59 |
Melder_assert (x.size == numberOfPoints); |
| 60 |
xp = x; |
| 61 |
yp = y; |
| 62 |
} |
| 63 |
|
| 64 |
void structSlopeSelector :: getSlopeAndIntercept_leastSquares (double &slope, double& intercept) { |
| 65 |
const double xsum = NUMsum (xp); |
| 66 |
const double xmean = xsum / numberOfPoints; |
| 67 |
longdouble variance = 0.0; |
| 68 |
slope = 0; |
| 69 |
for (integer i = 1; i <= numberOfPoints; i ++) { |
| 70 |
const double ti = xp [i] - xmean; |
| 71 |
variance += ti * ti; |
| 72 |
slope += ti * yp [i]; |
| 73 |
} |
| 74 |
slope /= variance; |
| 75 |
const double ysum = NUMsum (yp); |
| 76 |
intercept = (ysum - xsum * slope) / numberOfPoints; |
| 77 |
} |
| 78 |
|
| 79 |
double structSlopeSelector :: getIntercept (double slope) { |
| 80 |
buffer.resize (numberOfPoints); |
| 81 |
for (integer i = 1; i <= numberOfPoints; i ++) { |
| 82 |
buffer [i] = yp [i] - slope * xp [i]; |
| 83 |
} |
| 84 |
return num::NUMquantile_e (buffer.get(), 0.5); |
| 85 |
} |
| 86 |
|
| 87 |
double structSlopeSelector :: getSlope_Siegel () { |
| 88 |
integer numberOfMedians = 0; |
| 89 |
/* reuse buffer*/ |
| 90 |
buffer.resize (buffer._capacity); |
| 91 |
const integer siegelSize = numberOfPoints - 1; |
| 92 |
VEC siegelSlopes = buffer.part (1, siegelSize); |
| 93 |
VEC siegelMedians = buffer.part (siegelSize + 1, 2 * siegelSize); |
| 94 |
for (integer i = 1; i <= numberOfPoints; i ++) { |
| 95 |
integer iline = 0; |
| 96 |
for (integer j = 1; j <= numberOfPoints; j ++) { |
| 97 |
if (i != j) |
| 98 |
siegelSlopes [++ iline] = (yp [i] - yp [j]) / (xp [i] - xp [j]); |
| 99 |
} |
| 100 |
Melder_assert (iline == siegelSize); |
| 101 |
siegelMedians [++ numberOfMedians] = num::NUMquantile_e (siegelSlopes, 0.5); |
| 102 |
} |
| 103 |
Melder_assert (numberOfMedians == numberOfPoints); |
| 104 |
return num::NUMquantile_e (siegelMedians, 0.5); |
| 105 |
} |
| 106 |
|
| 107 |
/* |
| 108 |
Given point (X[i],Y[I]), where I=1..n. |
| 109 |
In the dual space the n lines are given by y = X[I]x - Y[I]. Our X[I] are ordered, i.e., |
| 110 |
if I < J then X[I] < X[J]. This means that at -infinity the line with X[1] has the largest |
| 111 |
y-values while the line with X[n] has the lowest y-values. We will number the dual line |
| 112 |
with X[n] as line number 1 and the line with X[1] as line number n. |
| 113 |
Therefore the indices i of the lines in dual space are reverse to the indices I of the |
| 114 |
points: i = n - I + 1 |
| 115 |
Equation of line i: y(i) = X [I].x - Y [I] = X [n-i+1].x - Y [n-i+1]. |
| 116 |
Line i and line j cross each other at the x where y(i) = y(j) |
| 117 |
This gives x = (Y[I]-Y[J])/(X[I]-X[J]) = slope[i,j]! |
| 118 |
|
| 119 |
By using infinitesimals on the Y[I] we can guarantee that all x's are different |
| 120 |
and also that all values at x of y(x) = X [I].x - Y [I] are different too! |
| 121 |
|
| 122 |
*/ |
| 123 |
void structSlopeSelector :: getKth_TheilSen (integer k, double& kth, double& kp1th) { |
| 124 |
try { |
| 125 |
const integer maxNumberOfIntervalCrossings = numberOfPoints * (numberOfPoints - 1) / 2; |
| 126 |
const integer krt = Melder_iroundDown (/* 1.5 */ sqrt (sampleSize)); // smaller than in the paper |
| 127 |
const double sampleSize_real = sampleSize; |
| 128 |
integer currentNumberOfIntervalCrossings = maxNumberOfIntervalCrossings; |
| 129 |
integer numberOfCrossingsAtLowX = 0, numberOfCrossingsAtLowXPrevious = 0; |
| 130 |
integer numberOfCrossingsAtHighX = maxNumberOfIntervalCrossings, numberOfCrossingsAtHighXPrevious = maxNumberOfIntervalCrossings; |
| 131 |
integer kappa = k, ilow, ihigh; |
| 132 |
structExtendedCrossing lowXPrevious, lowX = {- std::numeric_limits<double>::infinity(), 0, 0}; |
| 133 |
|
| 134 |
auto approximatelyEqual = [&] (double lhs, double rhs, double numericalEqualityPrecision) { |
| 135 |
if (std::fabs (lhs) < numericalEqualityPrecision || std::fabs (rhs) < numericalEqualityPrecision) { |
| 136 |
return std::fabs (lhs - rhs) < numericalEqualityPrecision; |
| 137 |
} |
| 138 |
// Use relative difference otherwise |
| 139 |
return std::fabs (lhs - rhs) <= numericalEqualityPrecision * std::fmax (std::fabs (lhs), std::fabs (rhs)); |
| 140 |
}; |
| 141 |
|
| 142 |
auto getSlopes = [&] (integer numberOfSlopes) { |
| 143 |
Melder_assert (numberOfSlopes <= maximumContractionSize); |
| 144 |
xslopes.resize (numberOfSlopes); |
| 145 |
for (integer j = 1, i = 1; i <= numberOfSlopes; i ++, j += 2) { |
| 146 |
ilow = currentInversions [j]; |
| 147 |
ihigh = currentInversions [j + 1]; |
| 148 |
const integer ipoint = numberOfPoints + 1 - ilow; // ilow |
| 149 |
const integer jpoint = numberOfPoints + 1 - ihigh; // ihigh; |
| 150 |
xslopes [i].real = (yp [ipoint] - yp [jpoint]) / (xp [ipoint] - xp [jpoint]); // the slope |
| 151 |
xslopes [i].low = ilow; |
| 152 |
xslopes [i].high = ihigh; |
| 153 |
} |
| 154 |
}; |
| 155 |
|
| 156 |
auto getPermutationAtX = [&](structExtendedCrossing& x, mutablePermutation p) { |
| 157 |
for (integer iline = 1; iline <= numberOfPoints; iline ++) { |
| 158 |
const integer ipoint = numberOfPoints + 1 - iline; |
| 159 |
xcrossings [iline].real = x.real * xp [ipoint] - yp [ipoint]; // the dual line's y-value |
| 160 |
xcrossings [iline].low = iline; // k or l |
| 161 |
xcrossings [iline].high = x.high; // j |
| 162 |
p -> p [iline] = iline; |
| 163 |
} |
| 164 |
std::sort (p -> p.begin(), p -> p.end(), |
| 165 |
[&] (integer& leftLineNumber, integer& rightLineNumber) { |
| 166 |
const structExtendedCrossing& lhs = xcrossings [leftLineNumber]; |
| 167 |
const structExtendedCrossing& rhs = xcrossings [rightLineNumber]; |
| 168 |
// Y[k]<Y[l]= if y[k]!=y[l] then (y[k]<y[l]) else (if k<l then (j>l) else (j<k)) |
| 169 |
const bool r = ( !approximatelyEqual (lhs.real, rhs.real, 1e-12) ? (lhs.real < rhs.real) : |
| 170 |
(lhs.low < rhs.low ? (lhs.high > rhs.low) : (lhs.high < lhs.low)) ); |
| 171 |
return r; |
| 172 |
} |
| 173 |
); |
| 174 |
return inversionCounter -> getNumberOfInversions (p); |
| 175 |
}; |
| 176 |
|
| 177 |
|
| 178 |
/* |
| 179 |
In Matoušek's paper the following loop has to be run until the currentNumberOfIntervalCrossings <= samplingSize. |
| 180 |
We changed this to a number 10 times larger to reduce the number of iterations of this loop because |
| 181 |
the selection of a random sample of the inversions in the interval [xlow,xHigh] with the function |
| 182 |
'getSelectedInversionsNotInOther' is by far the most computational expensive part in the loop. |
| 183 |
Reducing the number of iterations of this loop therefore saves more time than calculating somewhat more slopes. |
| 184 |
*/ |
| 185 |
numberOfTries = 0; |
| 186 |
currentInversions.resize (2 * sampleSize); |
| 187 |
sortedRandomCrossingCodes.resize (sampleSize); // during the loop |
| 188 |
while (currentNumberOfIntervalCrossings > std::min (maximumContractionSize, maxNumberOfIntervalCrossings)) { // sampleSize*10 |
| 189 |
|
| 190 |
/* |
| 191 |
Pick a random sample of size sampleSize from the intersections in the interval (lowX, highX) |
| 192 |
and store these in the vector 'currentInversions'. |
| 193 |
The first time we have the maximum number of slopes in our interval (-inf, +inf) and |
| 194 |
therefore the current inversions can be sampled directly. |
| 195 |
In the other cases we have n' < n(n-1)/2 slopes and pick sampleSize random numbers from the |
| 196 |
interval [1,n']. We then search the corresponding slopes in O(n log(n)) time. |
| 197 |
*/ |
| 198 |
if (numberOfTries == 0) { |
| 199 |
for (integer i = 1; i <= sampleSize; i ++) { |
| 200 |
const integer irandom = NUMrandomInteger (1_integer, currentNumberOfIntervalCrossings); |
| 201 |
getInversionFromCode (irandom, ilow, ihigh); |
| 202 |
currentInversions [2 * i - 1] = ilow; |
| 203 |
currentInversions [2 * i ] = ihigh; |
| 204 |
} |
| 205 |
} else { |
| 206 |
randomInteger_INTVEC_inout (sortedRandomCrossingCodes.get(), 1_integer, currentNumberOfIntervalCrossings); |
| 207 |
sort_INTVEC_inout (sortedRandomCrossingCodes.get()); |
| 208 |
inversionCounter -> getSelectedInversionsNotInOther ( |
| 209 |
lineRankingAtHighX.get(), inverseOfLineRankingAtLowX.get(), sortedRandomCrossingCodes.get(), currentInversions.get() |
| 210 |
); |
| 211 |
} |
| 212 |
|
| 213 |
getSlopes (sampleSize); |
| 214 |
|
| 215 |
const double kappar = sampleSize_real / currentNumberOfIntervalCrossings * (k - numberOfCrossingsAtLowX); |
| 216 |
kappa = std::max (1_integer, Melder_iroundDown (kappar)); |
| 217 |
const integer kb = std::max (1_integer, kappa - krt); |
| 218 |
const integer ke = std::min (sampleSize, kappa + krt); |
| 219 |
lowXPrevious = lowX; |
| 220 |
lowX = num::NUMget_kth (xslopes.get(), kb); |
| 221 |
structExtendedCrossing highX = num::NUMget_kth (xslopes.get(), ke); |
| 222 |
trace (U"lowX:", lowX.real, U" highX:", highX.real); |
| 223 |
/* |
| 224 |
We have one of the following five situations for k, when lpX & hpX are the previous interval borders, |
| 225 |
lowX & highX the current interval borders and nlow and nhigh the number of inversions at the |
| 226 |
current borders. |
| 227 |
| | |
| 228 |
lowXPrevious highXPrevious |
| 229 |
| | |
| 230 |
lowX (?==?) highX |
| 231 |
nlow nhigh |
| 232 |
k k k k k |
| 233 |
(1) (2) (3) (4) (5) |
| 234 |
(1) k < nlow, we make lowX the new upper border |
| 235 |
(2) k == nlow, we know that kth == lowX |
| 236 |
(3) nlow < k < nhigh , next |
| 237 |
(4) k == nhigh |
| 238 |
(5) k > nhigh, make highX the new lowX |
| 239 |
*/ |
| 240 |
if (numberOfTries > 0) { // for the first try the history is already there |
| 241 |
numberOfCrossingsAtLowXPrevious = numberOfCrossingsAtLowX; |
| 242 |
lineRankingAtLowXPrevious -> p.get() <<= lineRankingAtLowX -> p.get(); |
| 243 |
} |
| 244 |
numberOfCrossingsAtLowX = getPermutationAtX (lowX, lineRankingAtLowX.get()); |
| 245 |
trace (U"k:", k, U" kb:", kb, U" ke:", ke, U" ss:", sampleSize, U" ", numberOfCrossingsAtLowX, U" tries:", numberOfTries); |
| 246 |
if (k < numberOfCrossingsAtLowX) { // (1) interval lp, lc |
| 247 |
Permutations_swap (lineRankingAtHighX.get(), lineRankingAtLowX.get()); // first set lineRankingAtHighX |
| 248 |
numberOfCrossingsAtHighX = numberOfCrossingsAtLowX; |
| 249 |
trace (U"k<l ", lowX.real, U" ", lowX.low, U" ", highX.real, U" ", highX.low); |
| 250 |
Permutations_swap (lineRankingAtLowX.get(), lineRankingAtLowXPrevious.get()); // then lineRankingAtLowX |
| 251 |
numberOfCrossingsAtLowX = numberOfCrossingsAtLowXPrevious; |
| 252 |
highX = lowX; |
| 253 |
lowX = lowXPrevious; |
| 254 |
// inverseOfLineRankingAtLowX is still valid!! |
| 255 |
} else if (k == numberOfCrossingsAtLowX) { |
| 256 |
kth = lowX.real; |
| 257 |
const integer kbp1 = kb + 1; |
| 258 |
highX = num::NUMget_kth (xslopes.get(), kbp1); |
| 259 |
numberOfCrossingsAtHighX = getPermutationAtX (highX, lineRankingAtHighX.get()); |
| 260 |
trace (U"k=l ", lowX.real, U" ", highX.real, U" ", numberOfCrossingsAtHighX); |
| 261 |
} else { // (3,4,5}) k > numberOfCrossingsAtLowX) |
| 262 |
if (numberOfTries > 0) { |
| 263 |
numberOfCrossingsAtHighXPrevious = numberOfCrossingsAtHighX; |
| 264 |
lineRankingAtHighXPrevious -> p.get() <<= lineRankingAtHighX -> p.get(); |
| 265 |
} |
| 266 |
numberOfCrossingsAtHighX = getPermutationAtX (highX, lineRankingAtHighX.get()); |
| 267 |
if (k < numberOfCrossingsAtHighX) { // (3) |
| 268 |
trace (U"k<h "); |
| 269 |
if (approximatelyEqual (lowX.real, highX.real, 1e-14)) { |
| 270 |
kth = kp1th = lowX.real; |
| 271 |
return; |
| 272 |
} |
| 273 |
// ok new interval |
| 274 |
} else if (k == numberOfCrossingsAtHighX) { // (4) |
| 275 |
kth = highX.real; |
| 276 |
const integer kep1 = ke + 1; |
| 277 |
if (kep1 <= sampleSize) { |
| 278 |
highX = num::NUMget_kth (xslopes.get(), kep1); |
| 279 |
if (kth == highX.real) { |
| 280 |
kp1th = kth; |
| 281 |
return; |
| 282 |
} |
| 283 |
trace (U"highX2:", highX.real); |
| 284 |
} else { |
| 285 |
Permutations_swap (lineRankingAtLowX.get(), lineRankingAtHighX.get()); |
| 286 |
numberOfCrossingsAtLowX = numberOfCrossingsAtHighX; |
| 287 |
} |
| 288 |
numberOfCrossingsAtHighX = getPermutationAtX (highX, lineRankingAtHighX.get()); |
| 289 |
trace (U"k=h ", numberOfCrossingsAtHighX); |
| 290 |
} else { // (5) k > numberOfCrossingsAtHighX |
| 291 |
Permutations_swap (lineRankingAtLowX.get(), lineRankingAtHighX.get()); |
| 292 |
numberOfCrossingsAtLowX = numberOfCrossingsAtHighX; |
| 293 |
Permutations_swap (lineRankingAtHighX.get(), lineRankingAtHighXPrevious.get()); |
| 294 |
numberOfCrossingsAtHighX = numberOfCrossingsAtHighXPrevious; |
| 295 |
trace (U"k>h"); |
| 296 |
} |
| 297 |
Permutation_invert_into (lineRankingAtLowX.get(), inverseOfLineRankingAtLowX.get()); |
| 298 |
} |
| 299 |
currentNumberOfIntervalCrossings = numberOfCrossingsAtHighX - numberOfCrossingsAtLowX; |
| 300 |
++ numberOfTries; |
| 301 |
if (numberOfTries > maximumNumberOfTries) { // escape |
| 302 |
getSlope_Siegel (); |
| 303 |
return; |
| 304 |
} |
| 305 |
} |
| 306 |
/* |
| 307 |
Finish with the remaining inversions. (We don't need the xcrossings anymore.) |
| 308 |
*/ |
| 309 |
sortedRandomCrossingCodes.resize (currentNumberOfIntervalCrossings); |
| 310 |
currentInversions.resize (2 * currentNumberOfIntervalCrossings); |
| 311 |
for (integer i = 1; i <= currentNumberOfIntervalCrossings; i ++) |
| 312 |
sortedRandomCrossingCodes [i] = i; |
| 313 |
(void) inversionCounter -> getSelectedInversionsNotInOther (lineRankingAtHighX.get(), |
| 314 |
inverseOfLineRankingAtLowX.get(), sortedRandomCrossingCodes.get(), currentInversions.get()); |
| 315 |
Melder_assert (currentNumberOfIntervalCrossings == inversionCounter -> numberOfInversionsRegistered); |
| 316 |
getSlopes (currentNumberOfIntervalCrossings); |
| 317 |
kappa = k - numberOfCrossingsAtLowX; |
| 318 |
if (kappa > 0) { // or should we avoid this situation |
| 319 |
num::NUMselect (xslopes.get(), kappa); |
| 320 |
kth = xslopes [kappa].real; |
| 321 |
} |
| 322 |
trace (kappa, U" ", numberOfCrossingsAtLowX, U" ", currentNumberOfIntervalCrossings); |
| 323 |
kp1th = xslopes [kappa + 1].real; |
| 324 |
for (integer i = kappa + 2; i <= currentNumberOfIntervalCrossings; i ++) |
| 325 |
if (xslopes [i].real < kp1th) |
| 326 |
kp1th = xslopes [i].real; |
| 327 |
} catch (MelderError) { |
| 328 |
Melder_throw (U"kth slope could not be selected."); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
double structSlopeSelector :: slopeQuantile_TheilSen (double factor) { |
| 333 |
const integer numberOfLines = xp.size * (xp.size - 1) / 2; |
| 334 |
const double place = factor * numberOfLines + 0.5; |
| 335 |
const integer left = Melder_clipped (1_integer, Melder_ifloor (place), numberOfLines); |
| 336 |
Melder_assert (numberOfLines - 1 >= 1); |
| 337 |
double kleft, kright; |
| 338 |
getKth_TheilSen (left, kleft, kright); |
| 339 |
const double dif = kright - kleft; |
| 340 |
if (dif == 0.0) |
| 341 |
return kleft; |
| 342 |
return kleft + (place - left) * dif; |
| 343 |
} |
| 344 |
|
| 345 |
double structSlopeSelector :: getSlope_TheilSen () { |
| 346 |
return slopeQuantile_TheilSen (0.5); |
| 347 |
} |
| 348 |
|
| 349 |
// buffer.size == xp.size * (xp.size - 1) / 2 |
| 350 |
double structSlopeSelector :: slopeQuantile_orderNSquaredWithBuffer (double factor, VEC const& buffer) { |
| 351 |
const integer numberOfLines = xp.size * (xp.size - 1) / 2; |
| 352 |
Melder_assert (numberOfLines == buffer.size); |
| 353 |
integer index = 0; |
| 354 |
for (integer i = 1; i < xp.size; i ++) |
| 355 |
for (integer j = i + 1; j <= xp.size; j ++) |
| 356 |
buffer [++ index] = (yp [j] - yp [i]) / (xp [j] - xp [i]); |
| 357 |
Melder_assert (index == numberOfLines); |
| 358 |
const double medianSlope = num::NUMquantile_e (buffer, factor); |
| 359 |
return medianSlope; |
| 360 |
} |
| 361 |
|
| 362 |
static void SlopeSelector_init (SlopeSelector me, integer numberOfPoints) { |
| 363 |
my numberOfPoints = numberOfPoints; |
| 364 |
my sampleSize = my numberOfPoints; |
| 365 |
my maximumContractionSize = 5 * my numberOfPoints; |
| 366 |
my maximumNumberOfTries = 20; |
| 367 |
|
| 368 |
my sortedRandomCrossingCodes = raw_INTVEC (my maximumContractionSize); |
| 369 |
my inversionsSize = 2 * my maximumContractionSize; |
| 370 |
my currentInversions = raw_INTVEC (my inversionsSize); |
| 371 |
my xslopes = newvectorraw <structExtendedCrossing> (my maximumContractionSize); |
| 372 |
my buffer = raw_VEC (my maximumContractionSize); |
| 373 |
my xcrossings = newvectorraw <structExtendedCrossing> (my numberOfPoints); |
| 374 |
my lineRankingAtLowX = Permutation_create (my numberOfPoints, true); // 1..n |
| 375 |
my lineRankingAtHighX = Permutation_reverse (my lineRankingAtLowX.get(), 0, 0); // n..1 |
| 376 |
my lineRankingAtLowXPrevious = Permutation_create (my numberOfPoints, true); // 1..n |
| 377 |
my lineRankingAtHighXPrevious = Permutation_reverse (my lineRankingAtLowX.get(), 0, 0); // n..1 |
| 378 |
my inverseOfLineRankingAtLowX = Permutation_create (my numberOfPoints, true); // 1..n |
| 379 |
my inversionCounter = PermutationInversionCounter_create (my numberOfPoints); |
| 380 |
} |
| 381 |
|
| 382 |
autoSlopeSelector SlopeSelector_create (integer numberOfPoints) { |
| 383 |
try { |
| 384 |
autoSlopeSelector me = Thing_new (SlopeSelector); |
| 385 |
SlopeSelector_init (me.get(), numberOfPoints); |
| 386 |
return me; |
| 387 |
} catch (MelderError) { |
| 388 |
Melder_throw (U"Cannot create SlopeSelector."); |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
autoSlopeSelector SlopeSelector_create (constVEC const& x, constVEC const& y) { |
| 393 |
try { |
| 394 |
Melder_assert (x.size == y.size); |
| 395 |
Melder_assert (x.size > 1); |
| 396 |
autoSlopeSelector me = Thing_new (SlopeSelector); |
| 397 |
SlopeSelector_init (me.get(), x.size); |
| 398 |
my newDataPoints (x, y); |
| 399 |
return me; |
| 400 |
} catch (MelderError) { |
| 401 |
Melder_throw (U"Cannot create SlopeSelector from data points."); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
static void SlopeSelectorTheilSen_getMedianSlope (SlopeSelector me, double& slope, double& intercept) { |
| 406 |
slope = my slopeQuantile (0.5); |
| 407 |
intercept = my getIntercept (slope); |
| 408 |
} |
| 409 |
|
| 410 |
void SlopeSelector_getSlopeAndIntercept (SlopeSelector me, double &slope, double &intercept, kSlopeSelector_method method) { |
| 411 |
if (method == kSlopeSelector_method :: THEILSEN) { |
| 412 |
slope = my getSlope_TheilSen (); |
| 413 |
intercept = my getIntercept (slope); |
| 414 |
} else if (method == kSlopeSelector_method :: SIEGEL) { |
| 415 |
slope = my getSlope_Siegel (); |
| 416 |
intercept = my getIntercept (slope); |
| 417 |
} else if (method == kSlopeSelector_method :: LEAST_SQUARES) { |
| 418 |
my getSlopeAndIntercept_leastSquares (slope, intercept); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
static void oneSpecial () { // gave a wrong slope (e-316) |
| 423 |
const integer n = 50; |
| 424 |
autoVEC y {0, 0.30673083561016756, 0.61346167122033513, 0.92019250683050269, 1.2269233424406703, 1.5336541780508377, 1.8403850136610054, 2.1471158492711728, 2.4538466848813405, 2.7605775204915082, 3.0673083561016754, 3.3740391917118435, 3.6807700273220108, 3.987500862932178, 4.2942316985423457, 4.6009625341525133, 4.907693369762681, 5.2144242053728478, 5.5211550409830163, -1.4631136353303322, 12.947760971671986, 6.4413475478135185, 6.748078383423687, 7.0548092190338538, 7.3615400546440215, 7.6682708902541883, 7.975001725864356, 8.2817325614745236, 8.5884633970846913, 8.895194232694859, 9.2019250683050267, 18.280348785400811, 9.815386739525362, 10.12211757513553, 10.428848410745696, 10.735579246355865, 11.042310081966033, 11.349040917576199, 11.655771753186368, 11.962502588796536, 12.269233424406702, 12.575964260016869, 12.882695095627037, 13.189425931237205, 13.496156766847374, 13.802887602457542, 14.109618438067708, 14.416349273677875, 33.000553040186531, 15.029810944898209}; |
| 425 |
autoVEC x = from_to_count_VEC (0.0, 10.0, n); |
| 426 |
autoSlopeSelector sls = SlopeSelector_create (x.get(), y.get()); |
| 427 |
const double slope = 1.502981094489821, factor = 0.5; |
| 428 |
const double slope4 = sls -> slopeQuantile_TheilSen (factor); |
| 429 |
} |
| 430 |
|
| 431 |
static void special50 () { // gets stucck because needs > maximumNumberOfTries |
| 432 |
//const integer n = 50; |
| 433 |
autoVEC x {0, 0.20408163265306123, 0.40816326530612246, 0.61224489795918369, 0.81632653061224492, 1.0204081632653061, 1.2244897959183674, 1.4285714285714286, 1.6326530612244898, 1.8367346938775511, 2.0408163265306123, 2.2448979591836737, 2.4489795918367347, 2.6530612244897958, 2.8571428571428572, 3.0612244897959187, 3.2653061224489797, 3.4693877551020407, 3.6734693877551021, 3.8775510204081636, 4.0816326530612246, 4.2857142857142856, 4.4897959183673475, 4.6938775510204085, 4.8979591836734695, 5.1020408163265305, 5.3061224489795915, 5.5102040816326534, 5.7142857142857144, 5.9183673469387754, 6.1224489795918373, 6.3265306122448983, 6.5306122448979593, 6.7346938775510203, 6.9387755102040813, 7.1428571428571432, 7.3469387755102042, 7.5510204081632653, 7.7551020408163271, 7.9591836734693882, 8.1632653061224492, 8.3673469387755102, 8.5714285714285712, 8.7755102040816322, 8.979591836734695, 9.183673469387756, 9.387755102040817, 9.591836734693878, 9.795918367346939, 10}; |
| 434 |
autoVEC y {0, 0.92803998653746245, 1.8560799730749249, 2.7841199596123873, 3.7121599461498498, 4.6401999326873122, 5.5682399192247747, 6.4962799057622371, 7.4243198922996996, 8.3523598788371629, 9.2803998653746245, 10.208439851912088, 11.136479838449549, 12.064519824987011, 12.992559811524474, 13.920599798061938, 14.848639784599399, 15.776679771136861, 16.704719757674326, 17.632759744211789, 18.560799730749249, 11.892870479075036, 20.416879703824176, 21.344919690361639, 22.272959676899099, 23.200999663436559, 24.129039649974022, 25.057079636511489, 25.985119623048949, 26.913159609586408, 27.841199596123875, 28.769239582661339, 29.697279569198798, 30.625319555736258, 31.553359542273721, 32.481399528811188, 33.409439515348652, 34.337479501886108, 35.265519488423578, 36.193559474961035, 52.020555528667835, 38.049639448035961, 38.977679434573417, 39.905719421110881, 40.833759407648351, 48.501531333854921, 42.689839380723278, 43.617879367260734, 44.545919353798197, 35.700411620170151}; |
| 435 |
autoSlopeSelector sls = SlopeSelector_create (x.get(), y.get()); |
| 436 |
//double slope = sls -> getSlope_TheilSen (); |
| 437 |
} |
| 438 |
/* |
| 439 |
22/2/2025 |
| 440 |
Old: n² slopes, sort, NUMquantile(0.5) |
| 441 |
New: Matoušek (1991) O(n log(n)) ? |
| 442 |
n ntries tTS tSiegel tOld tOld/tTS tOld/tSiegel SlopeOld/Siegel |
| 443 |
10 0 8.3446e-06 1.3589e-05 9.2983e-06 *0.6140* 0.8974 1.0057 |
| 444 |
50 2 6.9379e-05 0.0002 0.0001 *0.3115* 0.3493 1.0007 |
| 445 |
100 4 0.0002 0.0009 0.0008 *0.2950* 0.3647 1.0021 |
| 446 |
500 3 0.0056 0.0021 0.0091 *2.6722* 0.6178 1.0015 |
| 447 |
1000 4 0.0075 0.0065 0.0205 *1.1525* 0.3706 1.0007 |
| 448 |
5000 3 0.2045 0.0465 0.4115 *4.3929* 0.4970 1.0003 |
| 449 |
10000 4 0.7233 0.2095 1.7015 *3.4526* 0.4251 0.9996 |
| 450 |
*/ |
| 451 |
void timeSlopeSelection () { |
| 452 |
try { |
| 453 |
Melder_clearInfo (); |
| 454 |
autoINTVEC sizes { 10_integer, 50_integer, 100_integer, 500_integer, 1000_integer, 5000_integer, 10000_integer }; |
| 455 |
MelderInfo_write (U"Old: n² slopes, sort, NUMquantile(0.5)\n" |
| 456 |
"New: Matoušek (1991) O(n log(n))\n" |
| 457 |
); |
| 458 |
special50(); |
| 459 |
oneSpecial(); |
| 460 |
|
| 461 |
MelderInfo_writeLine (U"n ntries tTS tSiegel tOld tOld/tTS tOld/tSiegel SlopeOld/Siegel"); |
| 462 |
for (integer isize = 1; isize <= sizes.size; isize ++) { |
| 463 |
double slope = 1.0, b = 4.0, stddev = 0.1, factor = 0.5; |
| 464 |
const integer n = sizes [isize]; |
| 465 |
const integer maxNumberOfLines = n * (n - 1) / 2; |
| 466 |
autoVEC x = from_to_count_VEC (0.0, 10.0, n); |
| 467 |
autoVEC y = randomGauss_VEC (n, b, stddev); |
| 468 |
for (integer i = 1; i <= n; i ++) |
| 469 |
y[i] += slope * x [i]; |
| 470 |
autoSlopeSelector sls = SlopeSelector_create (x.get(), y.get()); |
| 471 |
autoVEC buffer = raw_VEC (maxNumberOfLines); |
| 472 |
Melder_stopwatch (); |
| 473 |
const double slope1 = sls -> slopeQuantile_orderNSquaredWithBuffer (factor, buffer.get()); |
| 474 |
const double t1 = Melder_stopwatch (); |
| 475 |
const double slope2 = sls -> slopeQuantile_TheilSen (factor); |
| 476 |
const double t2 = Melder_stopwatch (); |
| 477 |
const integer ntries = sls -> numberOfTries; |
| 478 |
Melder_assert (slope1 == slope2); |
| 479 |
Melder_stopwatch (); |
| 480 |
const double slope3 = sls -> getSlope_Siegel (); |
| 481 |
const double t3 = Melder_stopwatch (); |
| 482 |
MelderInfo_writeLine (n, U" ", ntries, U" ", t1, U" ", t2, U" ", t3, U" *", t1 / t2, U"*", U" ", t1 / t3, U" ", slope1/slope3); |
| 483 |
/* corner cases with many equal slopes */ |
| 484 |
|
| 485 |
MelderInfo_writeLine (U"\n\n\n"); |
| 486 |
for (integer i = 1; i <= 5; i ++) { |
| 487 |
const double slope = NUMrandomUniform (0.1, 10.0); |
| 488 |
for (integer i = 1; i <= n; i ++) |
| 489 |
y[i] = slope * x [i]; |
| 490 |
for (integer j = 1; j <= n / 10; j ++) { |
| 491 |
const integer index = NUMrandomInteger (1, n); |
| 492 |
y [index] += NUMrandomUniform (-20.0, 20.0); |
| 493 |
} |
| 494 |
sls -> newDataPoints (x.get(), y.get()); |
| 495 |
const double slope4 = sls -> slopeQuantile_TheilSen (factor); |
| 496 |
MelderInfo_writeLine (U"slope:", slope, U" TS:", slope4, U" ", fabs ((slope4 - slope) / slope)); |
| 497 |
//Melder_assert (fabs ((slope4 - slope) / slope) < 1e-12); |
| 498 |
} |
| 499 |
} |
| 500 |
MelderInfo_close (); |
| 501 |
} catch (MelderError) { |
| 502 |
Melder_throw (U""); |
| 503 |
} |
| 504 |
} |
| 505 |
/* End of file SlopeSelector.cpp */ |