| 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"cxx . . $ `<file>/dwsys/NUM2.h`/ |
| 25 | #include "melder.h"cxx . . $ `<file>/melder/melder.h`/ |
| 26 | |
| 27 | void NUMmadcxx . . $ NUMmad(459b5cf602a8465f). (constVECcxx . . $ constVEC# x, double *inout_location, bool wantlocation, double *out_mad, VECcxx . . $ VEC# const& workSpace) { |
| 28 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (inout_location); |
| 29 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (workSpace.size >= x.size); |
| 30 | if (x.sizecxx . . $ constvector#size. == 1) { |
| 31 | if (wantlocation) |
| 32 | *inout_location= x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).1]; |
| 33 | if (out_mad) |
| 34 | *out_mad = undefinedcxx . . $ undefined.; |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | VECcxx . . $ VEC# work = workSpace.partcxx . . $ vector#part(ae267db9e2c04be7). (1, x.sizecxx . . $ constvector#size.); |
| 39 | workcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd). <<=cxx . . $ `operator<<=`(adea042513329390). xcxx . . $ constvectorview#constvectorview(fc39adb4395e9baf).; |
| 40 | |
| 41 | if (wantlocation) { |
| 42 | sort_e_VEC_inoutcxx . . $ sort_e_VEC_inout(e75f7985a5e05502). (work); |
| 43 | *inout_location = NUMquantilecxx . . $ NUMquantile(885a97927582bd48). (workcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)., 0.5); |
| 44 | } |
| 45 | if (out_mad) { |
| 46 | for (integercxx . . $ integer# i = 1; i <= x.sizecxx . . $ constvector#size.; i ++) |
| 47 | work [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = fabscxx . . $ fabs(fee4f1d22bc8e617). (work [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] - *inout_location); |
| 48 | sort_e_VEC_inoutcxx . . $ sort_e_VEC_inout(e75f7985a5e05502). (work); |
| 49 | *out_mad = 1.4826 * NUMquantilecxx . . $ NUMquantile(885a97927582bd48). (workcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)., 0.5); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | static double NUMgausscxx . . $ NUMgauss(447c67c6676f75c3). (double x) { |
| 54 | return NUM1_sqrt2picxx . . $ NUM1_sqrt2pi. * expcxx . . $ exp(fee4f1d22bc8e617). (- 0.5 * x * x); |
| 55 | } |
| 56 | |
| 57 | void NUMstatistics_hubercxx . . $ NUMstatistics_huber(f9ec060e31928512). (constVECcxx . . $ constVEC# x, double *inout_location, bool wantlocation, double *inout_scale, |
| 58 | bool wantscale, double k_stdev, double tol, integercxx . . $ integer# maximumNumberOfiterations, VECcxx . . $ VEC# const& workSpace) |
| 59 | { |
| 60 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (inout_location && inout_scale); |
| 61 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (workSpace.size >= x.size); |
| 62 | |
| 63 | const double theta = 2.0 * NUMgaussPcxx . . $ NUMgaussP(447c67c6676f75c3). (k_stdev) - 1.0; |
| 64 | const double beta = theta + k_stdev * k_stdev * (1.0 - theta) - 2.0 * k_stdev * NUMgausscxx . . $ NUMgauss(447c67c6676f75c3). (k_stdev); |
| 65 | double scale = *inout_scale, location = *inout_location; |
| 66 | |
| 67 | NUMmadcxx . . $ NUMmad(459b5cf602a8465f). (x, & location, wantlocation, & scale, workSpace); |
| 68 | if (scale > 0.0) { |
| 69 | VECcxx . . $ VEC# work = workSpace.partcxx . . $ vector#part(ae267db9e2c04be7). (1, x.sizecxx . . $ constvector#size.); |
| 70 | const double degreesOfFreedom = ( wantlocation ? x.sizecxx . . $ constvector#size. - 1 : x.sizecxx . . $ constvector#size. ); |
| 71 | integercxx . . $ integer# iter = 0; |
| 72 | bool farFromLocation = false, farFromScale = false; |
| 73 | do { |
| 74 | const double previousLocation = location; |
| 75 | const double previousScale = scale; |
| 76 | |
| 77 | workcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd). <<=cxx . . $ `operator<<=`(adea042513329390). xcxx . . $ constvectorview#constvectorview(fc39adb4395e9baf).; |
| 78 | VECclip_inplacecxx . . $ VECclip_inplace(c22bf7a6e3d33143). (location - k_stdev * scale, workcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)., location + k_stdev * scale); // winsorize |
| 79 | |
| 80 | if (wantlocation) { |
| 81 | location = NUMmeancxx . . $ NUMmean(4bb9c3e3a8dd5442). (workcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd).); |
| 82 | farFromLocation = ( fabscxx . . $ fabs(fee4f1d22bc8e617). (location - previousLocation) > stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a). (tol * fabscxx . . $ fabs(fee4f1d22bc8e617). (location), NUMepscxx . . $ `dwsys/NUM2.h:36:9`!) ); |
| 83 | } |
| 84 | if (wantscale) { |
| 85 | workcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd). -=cxx . . $ `operator-=`(c555d170d72ff256). location; |
| 86 | const double sumsq = NUMsum2cxx . . $ NUMsum2(fa513f1eb826a064). (workcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd).); |
| 87 | scale = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (sumsq / (degreesOfFreedom * beta)); |
| 88 | farFromScale = ( fabscxx . . $ fabs(fee4f1d22bc8e617). (scale - previousScale) > stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a). (tol * scale, NUMepscxx . . $ `dwsys/NUM2.h:36:9`!) ); |
| 89 | } |
| 90 | } while (++ iter < maximumNumberOfiterations && (farFromScale || farFromLocation)); |
| 91 | } |
| 92 | if (wantlocation) |
| 93 | *inout_location = location; |
| 94 | if (wantscale) |
| 95 | *inout_scale = scale; |
| 96 | } |