htrdr_accum.h (2397B)
1 /* Copyright (C) 2018-2019, 2022-2025 Centre National de la Recherche Scientifique 2 * Copyright (C) 2020-2022 Institut Mines Télécom Albi-Carmaux 3 * Copyright (C) 2022-2025 Institut Pierre-Simon Laplace 4 * Copyright (C) 2022-2025 Institut de Physique du Globe de Paris 5 * Copyright (C) 2018-2025 |Méso|Star> (contact@meso-star.com) 6 * Copyright (C) 2022-2025 Observatoire de Paris 7 * Copyright (C) 2022-2025 Université de Reims Champagne-Ardenne 8 * Copyright (C) 2022-2025 Université de Versaille Saint-Quentin 9 * Copyright (C) 2018-2019, 2022-2025 Université Paul Sabatier 10 * 11 * This program is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License as published by 13 * the Free Software Foundation, either version 3 of the License, or 14 * (at your option) any later version. 15 * 16 * This program is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU General Public License for more details. 20 * 21 * You should have received a copy of the GNU General Public License 22 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 23 24 #ifndef HTRDR_ACCUM_H 25 #define HTRDR_ACCUM_H 26 27 #include <rsys/rsys.h> 28 #include <rsys/math.h> 29 30 /* Monte carlo accumulator */ 31 struct htrdr_accum { 32 double sum_weights; /* Sum of Monte-Carlo weights */ 33 double sum_weights_sqr; /* Sum of Monte-Carlo square weights */ 34 size_t nweights; /* #accumlated weights */ 35 size_t nfailures; /* #failures */ 36 }; 37 #define HTRDR_ACCUM_NULL__ {0, 0, 0, 0} 38 static const struct htrdr_accum HTRDR_ACCUM_NULL = HTRDR_ACCUM_NULL__; 39 40 /* Monte carlo estimate */ 41 struct htrdr_estimate { 42 double E; /* Expected value */ 43 double SE; /* Standard error */ 44 }; 45 #define HTRDR_ESTIMATE_NULL__ {0, 0} 46 static const struct htrdr_estimate HTRDR_ESTIMATE_NULL = HTRDR_ESTIMATE_NULL__; 47 48 static FINLINE void 49 htrdr_accum_get_estimation 50 (const struct htrdr_accum* acc, 51 struct htrdr_estimate* estimate) 52 { 53 ASSERT(acc && estimate); 54 55 if(!acc->nweights) { 56 estimate->E = 0; 57 estimate->SE = 0; 58 } else { 59 const double N = (double)acc->nweights; 60 double E, V, SE; 61 E = acc->sum_weights / N; 62 V = MMAX(acc->sum_weights_sqr / N - E*E, 0); 63 SE = sqrt(V/N); 64 65 estimate->E = E; 66 estimate->SE = SE; 67 } 68 } 69 70 #endif /* HTRDR_SOLVE_H */