EstimateStats.h
1//-*-C++-*-
2/***************************************************************************
3 *
4 * Copyright (C) 2006 by Willem van Straten
5 * Licensed under the Academic Free License version 2.1
6 *
7 ***************************************************************************/
8
9// psrchive/Util/units/EstimateStats.h
10
11#ifndef __EstimateStats_h
12#define __EstimateStats_h
13
14#include "Estimate.h"
15#include "Error.h"
16#include <vector>
17#include <algorithm>
18
19template <typename T, typename U>
20double chisq (const Estimate<T,U>& a, const Estimate<T,U>& b)
21{
22 T diff = a.get_value() - b.get_value();
23 T var = a.get_variance() + b.get_variance();
24
25 if (var == 0.0)
26 return 0;
27 else
28 return diff * diff / var;
29}
30
31template <typename T, typename U>
32double chisq (const MeanEstimate<T,U>& a, const MeanEstimate<T,U>& b)
33{
34 return chisq (a.get_Estimate(), b.get_Estimate());
35}
36
37template <typename T, typename U>
38double chisq (const MeanRadian<T,U>& a, const MeanRadian<T,U>& b)
39{
40 return 0.5 * ( chisq(a.get_cos(), b.get_cos()) +
41 chisq(a.get_sin(), b.get_sin()) );
42}
43
44template <typename T, typename U>
45Estimate<T,U> weighted_mean (const std::vector< Estimate<T,U> >& vals)
46{
48 for (auto element: vals)
49 mean += element;
50 return mean.get_Estimate();
51}
52
53template <typename T, typename U>
54void weighted_quartiles (const std::vector< Estimate<T,U> >& vals,
55 Estimate<T,U>& Q1,
56 Estimate<T,U>& Q2,
57 Estimate<T,U>& Q3)
58{
59 unsigned nval = vals.size();
60
61 if (vals.size () < 3)
62 throw Error (InvalidState, "weighted_quartiles", "ndat=%u < 3", nval);
63
64 // remove all esimates with zero variance (flagged as bad)
65 std::vector< Estimate<T,U> > cleaned ( nval );
66 unsigned iclean = 0;
67 for (unsigned ival=0; ival < nval; ival++)
68 if (vals[ival].var > 0)
69 {
70 cleaned[iclean] = vals[ival];
71 iclean ++;
72 }
73
74#if _DEBUG
75 std::cerr << "weighted_quartiles: nval=" << nval << " nclean=" << iclean << std::endl;
76#endif
77
78 nval = iclean;
79
80 cleaned.resize (nval);
81
82 U total_weight = 0.0;
83 for (auto element: cleaned)
84 total_weight += 1.0 / element.var;
85
86 std::sort (cleaned.begin(), cleaned.end());
87
88 U quartile_weight[3];
89 for (unsigned i=0; i<3; i++)
90 quartile_weight[i] = 0.25 * (i+1) * total_weight;
91
92 unsigned quartile_index[3] = { 0, 0, 0 };
93
94 U weight = 0.0;
95
96 for (unsigned ival=0; ival < nval; ival++)
97 {
98 weight += 1.0 / cleaned[ival].var;
99
100 for (unsigned i=0; i<3; i++)
101 if (weight < quartile_weight[i])
102 quartile_index[i] = ival;
103 }
104
105#if _DEBUG
106 std::cerr << "weighted_quartile indeces: ";
107 for (unsigned i=0; i<3; i++)
108 std::cerr << " Q" << i+1 << "=" << quartile_index[i];
109 std::cerr << std::endl;
110#endif
111
112 Q1 = Estimate<T,U> (cleaned[quartile_index[0]].val, 1.0/total_weight);
113 Q2 = Estimate<T,U> (cleaned[quartile_index[1]].val, 1.0/total_weight);
114 Q3 = Estimate<T,U> (cleaned[quartile_index[2]].val, 1.0/total_weight);
115}
116
117template <typename T, typename U>
118Estimate<T,U> weighted_median (const std::vector< Estimate<T,U> >& vals)
119{
120 Estimate<T,U> Q1, Q2, Q3;
121 weighted_quartiles (vals, Q1, Q2, Q3);
122 return Q2;
123}
124
125#endif
A convenient exception handling class.
Definition Error.h:54
T get_value() const
U get_variance() const

Generated using doxygen 1.14.0