Jpp test-rotations-old-533-g2bdbdb559
the software that should make you happy
Loading...
Searching...
No Matches
JAspera.hh
Go to the documentation of this file.
1#ifndef __JASTRONOMY__JASPERA__
2#define __JASTRONOMY__JASPERA__
3
4#include <vector>
5#include <cmath>
6
7
8/**
9 * \file
10 *
11 * Per aspera ad astra.
12 * \author mdejong
13 */
14namespace JASTRONOMY {}
15namespace JPP { using namespace JASTRONOMY; }
16
17namespace JASTRONOMY {
18
19 /**
20 * Auxiliary data structure to fit signal strength using likelihood ratio.
21 */
22 struct JAspera :
23 public std::vector<double> // N/S values
24 {
25
26 static constexpr double EPSILON = 1.0e-3; //!< precision determination of signal strength
27
28
29 /**
30 * Result of fit.
31 */
32 struct fit_type {
33 double likelihood = 0.0; //!< likelihood
34 double signal = 0.0; //!< signal strength
35 };
36
37
38 /**
39 * Put signal and background to list of pre-computed N/S values.
40 *
41 * \param s signal
42 * \param b background
43 */
44 void put(const double s,
45 const double b)
46 {
47 push_back(b/s);
48
49 ws += s;
50 }
51
52
53 /**
54 * Put signal and background to list of pre-computed N/S values.
55 *
56 * \param n data
57 * \param s signal
58 * \param b background
59 */
60 void put(const size_t n,
61 const double s,
62 const double b)
63 {
64 for (size_t i = 0; i != n; ++i) {
65 push_back(b/s);
66 }
67
68 ws += s;
69 }
70
71
72 /**
73 * Get likelihood for given signal strength.
74 *
75 * \param p signal strength
76 * \return likelihood
77 */
78 double getLikelihood(const double p) const
79 {
80 double y = -p * ws;
81
82 for (const auto& i : static_cast<const std::vector<double>&>(*this)) {
83 y += log1p(p/i);
84 }
85
86 if (y > 0.0)
87 return y;
88 else
89 return 0.0;
90 }
91
92
93 /**
94 * Get derivative of likelihood for given signal strength.
95 *
96 * \param p signal strength
97 * \return derivative of likelihood
98 */
99 double getDerivative(const double p) const
100 {
101 double y = -ws;
102
103 for (const auto& i : static_cast<const std::vector<double>&>(*this)) {
104 y += 1.0 / (p + i);
105 }
106
107 return y;
108 }
109
110
111 /**
112 * Fit signal strength.
113 *
114 * \return result
115 */
117 {
118 using namespace std;
119
120 double x1 = 0.0; // lower limit corresponds to no signal; less to under-fluctuation
121 double x2 = (double) this->size() / ws; // upper limit corresponds to no background (i.e. all N/S = 0)
122 // bisection
123 /*
124 if (getDerivative(x1) > 0.0) {
125
126 for ( ; ; ) {
127
128 const double x = 0.5 * (x1 + x2);
129
130 if (x2 - x1 <= EPSILON) {
131 return { getLikelihood(x), x };
132 }
133
134 if (getDerivative(x) < 0.0)
135 x2 = x;
136 else
137 x1 = x;
138 }
139 }
140 */
141 // Ridder's method
142 double f1 = getDerivative(x1);
143
144 if (f1 > 0.0) {
145
146 double f2 = getDerivative(x2);
147
148 while (x2 - x1 > EPSILON) {
149
150 const double xm = 0.5 * (x1 + x2);
151 const double fm = getDerivative(xm);
152
153 const double s = sqrt(fm*fm - f1*f2);
154
155 if (s == 0.0) {
156 break;
157 }
158
159 const double xn = xm + (xm - x1) * fm/s;
160 const double fn = getDerivative(xn);
161
162 if (signbit(fn) != signbit(fm)) {
163
164 x1 = xm;
165 f1 = fm;
166 x2 = xn;
167 f2 = fn;
168
169 } else {
170
171 if (signbit(fn)) {
172
173 x2 = xn;
174 f2 = fn;
175
176 } else {
177
178 x1 = xn;
179 f1 = fn;
180 }
181 }
182 }
183
184 const double x = 0.5 * (x1 + x2);
185
186 return { getLikelihood(x), x };
187 }
188 //
189 return { 0.0, 0.0 };
190 }
191
192
193 /**
194 * Get total signal strength.
195 *
196 * \return signal strength
197 */
198 double getSignal() const
199 {
200 return ws;
201 }
202
203
204 /**
205 * Set signal strength.
206 *
207 * \param wS signal strength
208 */
209 void setSignal(const double wS)
210 {
211 ws = wS;
212 }
213
214
215 /**
216 * Add signal strength.
217 *
218 * \param wS signal strength
219 */
220 void addSignal(const double wS)
221 {
222 ws += wS;
223 }
224
225 protected:
226
227 double ws = 0.0; //!< total signal strength
228 };
229}
230
231#endif
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
double signal
signal strength
Definition JAspera.hh:34
double likelihood
likelihood
Definition JAspera.hh:33
Auxiliary data structure to fit signal strength using likelihood ratio.
Definition JAspera.hh:24
void addSignal(const double wS)
Add signal strength.
Definition JAspera.hh:220
double getSignal() const
Get total signal strength.
Definition JAspera.hh:198
double getLikelihood(const double p) const
Get likelihood for given signal strength.
Definition JAspera.hh:78
void setSignal(const double wS)
Set signal strength.
Definition JAspera.hh:209
void put(const double s, const double b)
Put signal and background to list of pre-computed N/S values.
Definition JAspera.hh:44
double ws
total signal strength
Definition JAspera.hh:227
static constexpr double EPSILON
precision determination of signal strength
Definition JAspera.hh:26
double getDerivative(const double p) const
Get derivative of likelihood for given signal strength.
Definition JAspera.hh:99
fit_type operator()() const
Fit signal strength.
Definition JAspera.hh:116
void put(const size_t n, const double s, const double b)
Put signal and background to list of pre-computed N/S values.
Definition JAspera.hh:60