ReFRACtor
convergence_check.h
Go to the documentation of this file.
1 #ifndef CONVERGENCE_CHECK_H
2 #define CONVERGENCE_CHECK_H
3 #include "printable.h"
4 #include <blitz/array.h>
5 
6 namespace FullPhysics {
7 /****************************************************************/
12 class FitStatistic : public Printable<FitStatistic> {
13 public:
17 
18 //-----------------------------------------------------------------------
20 //-----------------------------------------------------------------------
26  {}
27 
28  FitStatistic(bool Fit_succeeded, OUTCOME Outcome, int Number_iteration,
29  int Number_divergent, double D_sigma_sq,
30  double D_sigma_sq_scaled, double Chisq_apriori,
31  double Chisq_measured, double Chisq_apriori_fc,
32  double Chisq_measured_fc)
33  : fit_succeeded(Fit_succeeded), outcome(Outcome),
34  number_iteration(Number_iteration),
35  number_divergent(Number_divergent), d_sigma_sq(D_sigma_sq),
36  d_sigma_sq_scaled(D_sigma_sq_scaled), chisq_apriori(Chisq_apriori),
37  chisq_measured(Chisq_measured), chisq_apriori_fc(Chisq_apriori_fc),
38  chisq_measured_fc(Chisq_measured_fc)
39  { }
40 
41 //-----------------------------------------------------------------------
43 //-----------------------------------------------------------------------
44 
45  void to_stream(std::ostream& os) const
46  {
47  os.precision(12);
48  os << fit_succeeded << " " << outcome << " " << number_iteration << " "
49  << number_divergent << " " << d_sigma_sq << " "
50  << d_sigma_sq_scaled << " " << chisq_apriori << " "
51  << chisq_measured << " " << chisq_apriori_fc << " "
52  << chisq_measured_fc << "\n";
53  }
54 
55 //-----------------------------------------------------------------------
57 //-----------------------------------------------------------------------
58 
59  void from_stream(std::istream& is)
60  {
61  int outcome_i;
62  is >> fit_succeeded >> outcome_i >> number_iteration
67  outcome = (OUTCOME) outcome_i;
68  }
69 
70 //-----------------------------------------------------------------------
72 //-----------------------------------------------------------------------
73 
75 
76 //-----------------------------------------------------------------------
78 //-----------------------------------------------------------------------
79 
81 
82 //-----------------------------------------------------------------------
84 //-----------------------------------------------------------------------
85 
87 
88 //-----------------------------------------------------------------------
90 //-----------------------------------------------------------------------
91 
93 
94 //-----------------------------------------------------------------------
97 //-----------------------------------------------------------------------
98 
99  double d_sigma_sq;
100 
101 //-----------------------------------------------------------------------
103 //-----------------------------------------------------------------------
104 
106 
107 //-----------------------------------------------------------------------
109 //-----------------------------------------------------------------------
110 
111  double gamma2() const {return chisq_apriori + chisq_measured;}
112 
113 //-----------------------------------------------------------------------
115 //-----------------------------------------------------------------------
116 
117  double gamma2_fc() const {return chisq_apriori_fc + chisq_measured_fc;}
118 
119 //-----------------------------------------------------------------------
121 //-----------------------------------------------------------------------
122 
124 
125 //-----------------------------------------------------------------------
127 //-----------------------------------------------------------------------
128 
130 
131 //-----------------------------------------------------------------------
134 //-----------------------------------------------------------------------
135 
137 
138 //-----------------------------------------------------------------------
141 //-----------------------------------------------------------------------
142 
144  void print(std::ostream& Os) const;
145 
146 //-----------------------------------------------------------------------
148 //-----------------------------------------------------------------------
149 
150  double chisq_measure_norm(const blitz::Array<double, 1>& Residual,
151  const blitz::Array<double, 1>& Residual_cov_diag) const
152  { return sum(Residual * Residual / Residual_cov_diag) / Residual.rows(); }
153 
154 //-----------------------------------------------------------------------
156 //-----------------------------------------------------------------------
157 
158  double residual_abs_rms(const blitz::Array<double, 1>& Residual) const
159  { return sqrt(sum(Residual * Residual) / Residual.rows()); }
160 
161 //-----------------------------------------------------------------------
163 //-----------------------------------------------------------------------
164 
165  double residual_rel_rms(const blitz::Array<double, 1>& Residual,
166  const blitz::Array<double, 1>& Rad_measure) const
167  { return residual_abs_rms(Residual) /
168  (sum(Rad_measure) / Rad_measure.rows()); }
169 
170 };
171 
172 inline std::istream& operator>>(std::istream& Is, FitStatistic& Fstat)
173 {
174  Fstat.from_stream(Is);
175  return Is;
176 }
177 
178 /****************************************************************/
181 class ConvergenceCheck : public Printable<ConvergenceCheck> {
182 public:
183  virtual ~ConvergenceCheck() {}
184 
185 //-----------------------------------------------------------------------
209 //-----------------------------------------------------------------------
210 
211  virtual void convergence_check(const FitStatistic& fit_stat_last,
212  FitStatistic& fit_stat,
213  bool& has_converged,
214  bool& convergence_failed,
215  double& gamma,
216  bool& step_diverged) = 0;
217 
218 //-----------------------------------------------------------------------
226 //-----------------------------------------------------------------------
227 
228 
229  virtual void evaluate_quality(FitStatistic& fit_stat_last,
230  const blitz::Array<double, 1>& Residual,
231  const blitz::Array<double, 1>& Residual_cov_diag) = 0;
232 
233 
234 //-----------------------------------------------------------------------
238 //-----------------------------------------------------------------------
239 
240  virtual void initialize_check() {}
241 
242 //-----------------------------------------------------------------------
244 //-----------------------------------------------------------------------
245 
246  virtual void print(std::ostream& Os) const { Os << "ConvergenceCheck";}
247 };
248 }
249 #endif
void to_stream(std::ostream &os) const
Dump data to a stream.
void from_stream(std::istream &is)
Load data from a stream.
This class holds various parameters describing how good of a fit we have.
FitStatistic(bool Fit_succeeded, OUTCOME Outcome, int Number_iteration, int Number_divergent, double D_sigma_sq, double D_sigma_sq_scaled, double Chisq_apriori, double Chisq_measured, double Chisq_apriori_fc, double Chisq_measured_fc)
double d_sigma_sq
This is d_sigma_sq, which is the product of the correction to the state vector and the right hand siz...
void print(std::ostream &Os) const
Print to a stream.
std::istream & operator>>(std::istream &Is, ConnorSolver &Solve)
double chisq_measured_fc
Chisq of the residuals of the measurement vs.
FitStatistic()
Default constructor.
This is a Mixin for classes that can be printed.
Definition: printable.h:24
double gamma2_fc() const
Parameter "gamma2_fc", which is just chisq_apriori_fc + chisq_measured_fc.
double chisq_apriori_fc
Chisq of the X_i vs.
double chisq_measure_norm(const blitz::Array< double, 1 > &Residual, const blitz::Array< double, 1 > &Residual_cov_diag) const
Calculate chisq for given residual and covariance matrix.
int number_divergent
Number of divergent steps.
This class tests for convergence of a Levenberg-Marquardt solver.
bool fit_succeeded
Was the fit successful?
OUTCOME outcome
Flag indicating success of fit, or why fit wasn&#39;t succesful.
double d_sigma_sq_scaled
This is d_sigma_sq, scaled by the size of the state vector.
double residual_rel_rms(const blitz::Array< double, 1 > &Residual, const blitz::Array< double, 1 > &Rad_measure) const
Calculate relative root mean squared for given residual.
double residual_abs_rms(const blitz::Array< double, 1 > &Residual) const
Calculate absolute root mean squared for given residual.
virtual void print(std::ostream &Os) const
Print description of object.
Contains classes to abstract away details in various Spurr Radiative Transfer software.
Definition: doxygen_python.h:1
double chisq_measured
Chisq of the residuals of the measurement vs. model prediction.
virtual void initialize_check()
Called before the first iteration, in case there is any setup.
int number_iteration
Number of iterations.
double chisq_apriori
Chisq of the X_i vs. the apriori X value.
double gamma2() const
Parameter "gamma2", which is just chi2_apriori + chi2_measured.

Copyright © 2017, California Institute of Technology.
ALL RIGHTS RESERVED.
U.S. Government Sponsorship acknowledged.
Generated Fri Aug 24 2018 15:44:10