Jpp 20.0.0-rc.8
the software that should make you happy
Loading...
Searching...
No Matches
JASTRONOMY::JAspera Struct Reference

Auxiliary data structure to fit signal strength using likelihood ratio. More...

#include <JAspera.hh>

Inheritance diagram for JASTRONOMY::JAspera:
std::vector< double > JASTRONOMY::JRealExperiment

Classes

struct  fit_type
 Result of fit. More...
 

Public Member Functions

void put (const double s, const double b)
 Put signal and background to list of pre-computed N/S values.
 
void put (const size_t n, const double s, const double b)
 Put signal and background to list of pre-computed N/S values.
 
double getLikelihood (const double p) const
 Get likelihood for given signal strength.
 
double getDerivative (const double p) const
 Get derivative of likelihood for given signal strength.
 
fit_type operator() (const bool ns=false) const
 Fit signal strength.
 
double getTestStatisticForUpperLimit (const double ps) const
 Get test statistic for given signal strength.
 
double getSignal () const
 Get total signal strength.
 
void setSignal (const double wS)
 Set signal strength.
 
void addSignal (const double wS)
 Add signal strength.
 

Static Public Attributes

static constexpr double EPSILON = 1.0e-3
 precision determination of signal strength
 

Protected Attributes

double ws = 0.0
 total signal strength
 

Detailed Description

Auxiliary data structure to fit signal strength using likelihood ratio.

Definition at line 22 of file JAspera.hh.

Member Function Documentation

◆ put() [1/2]

void JASTRONOMY::JAspera::put ( const double s,
const double b )
inline

Put signal and background to list of pre-computed N/S values.

Parameters
ssignal
bbackground

Definition at line 44 of file JAspera.hh.

46 {
47 push_back(b/s);
48
49 ws += s;
50 }
double ws
total signal strength
Definition JAspera.hh:265

◆ put() [2/2]

void JASTRONOMY::JAspera::put ( const size_t n,
const double s,
const double b )
inline

Put signal and background to list of pre-computed N/S values.

Parameters
ndata
ssignal
bbackground

Definition at line 60 of file JAspera.hh.

63 {
64 for (size_t i = 0; i != n; ++i) {
65 push_back(b/s);
66 }
67
68 ws += s;
69 }
const int n
Definition JPolint.hh:791

◆ getLikelihood()

double JASTRONOMY::JAspera::getLikelihood ( const double p) const
inline

Get likelihood for given signal strength.

Parameters
psignal strength
Returns
likelihood

Definition at line 78 of file JAspera.hh.

79 {
80 double y = -p * ws;
81
82 for (const double i : static_cast<const std::vector<double>&>(*this)) {
83 y += log1p(p/i);
84 }
85
86 return y;
87 }

◆ getDerivative()

double JASTRONOMY::JAspera::getDerivative ( const double p) const
inline

Get derivative of likelihood for given signal strength.

Parameters
psignal strength
Returns
derivative of likelihood

Definition at line 96 of file JAspera.hh.

97 {
98 double y = -ws;
99
100 for (const double i : static_cast<const std::vector<double>&>(*this)) {
101 y += 1.0 / (p + i);
102 }
103
104 return y;
105 }

◆ operator()()

fit_type JASTRONOMY::JAspera::operator() ( const bool ns = false) const
inline

Fit signal strength.

Parameters
nsallow for negative signal
Returns
result

Definition at line 114 of file JAspera.hh.

115 {
116 using namespace std;
117
118 if (this->empty()) {
119
120 // nothing to be done
121
122 return { 0.0, 0.0 };
123 }
124
125 double x1 = 0.0;
126 double x2 = 0.0;
127
128 double f1 = getDerivative(0.0); // discriminator between positive and negative signal
129 double f2 = 0.0;
130
131 if (f1 == 0.0) {
132
133 return { 0.0, 0.0 };
134
135 } else if (f1 > 0.0) { // positive signal
136
137 x1 = 0.0; // lower limit corresponds to no signal
138 x2 = (double) this->size() / ws; // upper limit corresponds to no background (i.e. all N/S = 0)
139
140 f2 = getDerivative(x2);
141
142 } else if (ns) { // negative signal
143
144 x2 = 0.0; // upper limit corresponds to no signal
145 x1 = -(*this)[0]; // lower limit corresponds to largest negated N/S ratio
146
147 for (const double i : static_cast<const std::vector<double>&>(*this)) {
148 if (-i > x1) {
149 x1 = -i;
150 }
151 }
152
153 x1 += EPSILON; // offset
154
155 f2 = f1;
156 f1 = getDerivative(x1);
157
158 } else {
159
160 return { 0.0, 0.0 };
161 }
162
163 // Ridder's method
164
165 while (x2 - x1 > EPSILON) {
166
167 const double xm = 0.5 * (x1 + x2);
168 const double fm = getDerivative(xm);
169
170 const double s = sqrt(fm*fm - f1*f2);
171
172 if (s == 0.0) {
173 break;
174 }
175
176 const double xn = xm + (xm - x1) * fm/s;
177 const double fn = getDerivative(xn);
178
179 if (fn == 0.0) {
180 return { getLikelihood(xn), xn };
181 }
182
183 if (signbit(fn) != signbit(fm)) {
184
185 x1 = xm;
186 f1 = fm;
187 x2 = xn;
188 f2 = fn;
189
190 } else {
191
192 if (signbit(fn)) {
193
194 x2 = xn;
195 f2 = fn;
196
197 } else {
198
199 x1 = xn;
200 f1 = fn;
201 }
202 }
203 }
204
205 const double x = 0.5 * (x1 + x2);
206
207 return { getLikelihood(x), x };
208 }
double getLikelihood(const double p) const
Get likelihood for given signal strength.
Definition JAspera.hh:78
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:96

◆ getTestStatisticForUpperLimit()

double JASTRONOMY::JAspera::getTestStatisticForUpperLimit ( const double ps) const
inline

Get test statistic for given signal strength.

See formula 16 in this reference.

Parameters
pssignal strength
Returns
test statistic

Definition at line 218 of file JAspera.hh.

219 {
220 const fit_type result = (*this)(true);
221
222 if (result.signal <= 0.0)
223 return 0.0 - this->getLikelihood(ps);
224 else if (result.signal <= ps)
225 return result.likelihood - this->getLikelihood(ps);
226 else
227 return 0.0;
228 }

◆ getSignal()

double JASTRONOMY::JAspera::getSignal ( ) const
inline

Get total signal strength.

Returns
signal strength

Definition at line 236 of file JAspera.hh.

237 {
238 return ws;
239 }

◆ setSignal()

void JASTRONOMY::JAspera::setSignal ( const double wS)
inline

Set signal strength.

Parameters
wSsignal strength

Definition at line 247 of file JAspera.hh.

248 {
249 ws = wS;
250 }

◆ addSignal()

void JASTRONOMY::JAspera::addSignal ( const double wS)
inline

Add signal strength.

Parameters
wSsignal strength

Definition at line 258 of file JAspera.hh.

259 {
260 ws += wS;
261 }

Member Data Documentation

◆ EPSILON

double JASTRONOMY::JAspera::EPSILON = 1.0e-3
staticconstexpr

precision determination of signal strength

Definition at line 26 of file JAspera.hh.

◆ ws

double JASTRONOMY::JAspera::ws = 0.0
protected

total signal strength

Definition at line 265 of file JAspera.hh.


The documentation for this struct was generated from the following file: