| 1 |
/* NUMhuber.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1994-2020 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 |
/* |
| 20 |
djmw 20030814 First version |
| 21 |
djmw 20080122 float -> double |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "NUM2.h" |
| 25 |
#include "melder.h" |
| 26 |
|
| 27 |
void NUMmad (constVEC x, double *inout_location, bool wantlocation, double *out_mad, VEC const& workSpace) { |
| 28 |
Melder_assert (inout_location); |
| 29 |
Melder_assert (workSpace.size >= x.size); |
| 30 |
if (x.size == 1) { |
| 31 |
if (wantlocation) |
| 32 |
*inout_location= x [1]; |
| 33 |
if (out_mad) |
| 34 |
*out_mad = undefined; |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
VEC work = workSpace.part (1, x.size); |
| 39 |
work <<= x; |
| 40 |
|
| 41 |
if (wantlocation) { |
| 42 |
sort_e_VEC_inout (work); |
| 43 |
*inout_location = NUMquantile (work, 0.5); |
| 44 |
} |
| 45 |
if (out_mad) { |
| 46 |
for (integer i = 1; i <= x.size; i ++) |
| 47 |
work [i] = fabs (work [i] - *inout_location); |
| 48 |
sort_e_VEC_inout (work); |
| 49 |
*out_mad = 1.4826 * NUMquantile (work, 0.5); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
static double NUMgauss (double x) { |
| 54 |
return NUM1_sqrt2pi * exp (- 0.5 * x * x); |
| 55 |
} |
| 56 |
|
| 57 |
void NUMstatistics_huber (constVEC x, double *inout_location, bool wantlocation, double *inout_scale, |
| 58 |
bool wantscale, double k_stdev, double tol, integer maximumNumberOfiterations, VEC const& workSpace) |
| 59 |
{ |
| 60 |
Melder_assert (inout_location && inout_scale); |
| 61 |
Melder_assert (workSpace.size >= x.size); |
| 62 |
|
| 63 |
const double theta = 2.0 * NUMgaussP (k_stdev) - 1.0; |
| 64 |
const double beta = theta + k_stdev * k_stdev * (1.0 - theta) - 2.0 * k_stdev * NUMgauss (k_stdev); |
| 65 |
double scale = *inout_scale, location = *inout_location; |
| 66 |
|
| 67 |
NUMmad (x, & location, wantlocation, & scale, workSpace); |
| 68 |
if (scale > 0.0) { |
| 69 |
VEC work = workSpace.part (1, x.size); |
| 70 |
const double degreesOfFreedom = ( wantlocation ? x.size - 1 : x.size ); |
| 71 |
integer iter = 0; |
| 72 |
bool farFromLocation = false, farFromScale = false; |
| 73 |
do { |
| 74 |
const double previousLocation = location; |
| 75 |
const double previousScale = scale; |
| 76 |
|
| 77 |
work <<= x; |
| 78 |
VECclip_inplace (location - k_stdev * scale, work, location + k_stdev * scale); // winsorize |
| 79 |
|
| 80 |
if (wantlocation) { |
| 81 |
location = NUMmean (work); |
| 82 |
farFromLocation = ( fabs (location - previousLocation) > std::max (tol * fabs (location), NUMeps) ); |
| 83 |
} |
| 84 |
if (wantscale) { |
| 85 |
work -= location; |
| 86 |
const double sumsq = NUMsum2 (work); |
| 87 |
scale = sqrt (sumsq / (degreesOfFreedom * beta)); |
| 88 |
farFromScale = ( fabs (scale - previousScale) > std::max (tol * scale, NUMeps) ); |
| 89 |
} |
| 90 |
} while (++ iter < maximumNumberOfiterations && (farFromScale || farFromLocation)); |
| 91 |
} |
| 92 |
if (wantlocation) |
| 93 |
*inout_location = location; |
| 94 |
if (wantscale) |
| 95 |
*inout_scale = scale; |
| 96 |
} |