ReFRACtor
|
#include <connor_solver.h>
Public Member Functions | |
ConnorSolver (const boost::shared_ptr< CostFunction > &Cf, const boost::shared_ptr< ConvergenceCheck > &Conv, double Gamma_initial=0.0, const std::string &Save_test_data="") | |
Constructor. More... | |
virtual | ~ConnorSolver () |
virtual void | add_observer (Observer< ConnorSolver > &Obs) |
Add an observer. More... | |
void | add_observer_and_keep_reference (boost::shared_ptr< Observer< ConnorSolver > > &Obs) |
Add an observer and keep a reference to it. More... | |
virtual blitz::Array< double, 2 > | aposteriori_covariance () const |
Return a posteriori covariance matrix for last problem solved. More... | |
virtual blitz::Array< double, 2 > | aposteriori_covariance_scaled () const |
Return a scaled a posteriori covariance matrix for last problem solved. More... | |
blitz::Array< double, 2 > | apriori_covariance () const |
Apriori covariance matrix. More... | |
virtual blitz::Array< double, 2 > | apriori_covariance_inv_norm () const |
Return normalized apriori covariance inverse matrix. More... | |
virtual blitz::Array< double, 2 > | averaging_kernel () const |
Return averaging kernel for last problem solved. More... | |
boost::shared_ptr< ConvergenceCheck > | convergence_check () const |
The convergence check object. More... | |
boost::shared_ptr< CostFunction > | cost_function () const |
Cost function. More... | |
virtual FitStatistic | fit_statistic () const |
Return fit results for solution to last problem solved. More... | |
void | from_stream (std::istream &is) |
Restore state of object previous saved with "to_stream". More... | |
double | gamma_last_step () const |
Levenberg-Marquardt parameter for last step we processed. More... | |
virtual blitz::Array< double, 2 > | jacobian () const |
Return the last jacobian calculated. More... | |
int | number_divergent () const |
Number of divergent steps for the last problem solved. More... | |
int | number_iteration () const |
Number of iterations for the last problem solved. More... | |
int | outcome_flag () const |
Outcome flag. This is an integer version of FitStatistic::OUTCOME. More... | |
void | print (std::ostream &Os) const |
std::string | print_to_string () const |
Print to string. More... | |
virtual void | remove_observer (Observer< ConnorSolver > &Obs) |
Remove an observer. More... | |
virtual blitz::Array< double, 1 > | residual () const |
Return residual for solution to last problem solved. More... | |
virtual blitz::Array< double, 1 > | residual_covariance_diagonal () const |
Return diagonal of covariance matrix of residual for solution to last problem solved. More... | |
void | save_test_data (const std::string &Fname) |
Set save_test_data argument, see explanation before class for this. More... | |
virtual bool | solve (const blitz::Array< double, 1 > &Initial_guess, const blitz::Array< double, 1 > &Apriori, const blitz::Array< double, 2 > &Apriori_cov) |
This solves the least squares problem starting at the initial guess. More... | |
boost::shared_ptr< ConnorSolverState > | state () const |
Save state to a ConnorSolverState object. More... | |
void | state (const ConnorSolverState &S) |
Set the state of the ConnorSolver from a previously created ConnorSolverState. More... | |
void | test_do_inversion (const std::string &Fname, blitz::Array< double, 1 > &Dx, blitz::Array< double, 2 > &Kt_se_m1_k) |
Restore state previously saved, and run do_inversion. More... | |
void | to_stream (std::ostream &os) const |
Save state of object. More... | |
blitz::Array< double, 1 > | x_apriori () const |
Return the a priori of the last problem solved. More... | |
blitz::Array< double, 1 > | x_apriori_uncertainty () const |
Return the uncertainty of the apriori of the last problem solved. More... | |
virtual blitz::Array< double, 1 > | x_solution () const |
int | x_solution_size () const |
blitz::Array< double, 1 > | x_solution_uncertainty () const |
Return the uncertainty of x_solution, this is just the sqrt of the diagonal of the full aposteriori_covariance. More... | |
blitz::Array< double, 1 > | x_solution_zero_unused () const |
Return the solution to the last problem solved. More... | |
blitz::Array< double, 1 > | x_update () const |
Protected Member Functions | |
void | add_observer_do (Observer< ConnorSolver > &Obs, ConnorSolver &t) |
Add an observer. More... | |
void | add_observer_do (Observer< ConnorSolver > &Obs) |
void | clean_dead_ptr () |
Remove any dead pointers. More... | |
void | do_inversion () |
This does an inversion step. More... | |
void | notify_update_do (const ConnorSolver &Self) |
Function to call to notify Observers of a state change. More... | |
void | remove_observer_do (Observer< ConnorSolver > &Obs, ConnorSolver &t) |
Remove an observer. More... | |
void | remove_observer_do (Observer< ConnorSolver > &Obs) |
blitz::Array< double, 1 > | zero_unused_parm () const |
Array that is 0 where we are not using a parameter, and 1 elsewhere. More... | |
Protected Attributes | |
blitz::Array< double, 2 > | apriori_cov_scaled |
This is the apriori covariance matrix scaled by sigma_ap. More... | |
boost::shared_ptr< ConvergenceCheck > | convergence_check_ |
The convergence check object. More... | |
boost::shared_ptr< CostFunction > | cost_function_ |
The cost function. More... | |
blitz::Array< double, 1 > | dx |
This is the update to , . More... | |
FitStatistic | fstat |
Results from last fit step. More... | |
double | gamma |
Levenberg-Marquardt parameter. More... | |
double | gamma_initial |
Initial value of gamma. More... | |
double | gamma_last_step_ |
Stash gamma from last step we processed. More... | |
blitz::Array< double, 2 > | k |
This is the Jacobian, called in the ATB. More... | |
blitz::Array< double, 2 > | kt_se_m1_k |
This is . More... | |
std::list< boost::weak_ptr< Observer< ConnorSolver > > > | olist |
std::vector< boost::shared_ptr< Observer< ConnorSolver > > > | ref_list |
blitz::Array< double, 1 > | residual_ |
This is the residual from the model, . More... | |
blitz::Array< double, 2 > | sa_m1_scaled |
This is the inverse of apriori_cov_scaled, but only for the rows and columns of the jacobian that are nonzero. More... | |
std::string | save_test_data_ |
If this isn't an empty string, save to this file in the first iteration. More... | |
blitz::Array< double, 1 > | se |
This is the covariance matrix of the residual, called in the ATB. More... | |
blitz::Array< double, 1 > | sigma_ap |
This is the sqrt of S_a diagonal, or the sigma of the apriori. More... | |
blitz::Array< double, 1 > | x_a |
This is the apriori X value, called in the ATB. More... | |
blitz::Array< double, 1 > | x_i |
This is the current value of the state vector X value, called in the ATB. More... | |
Static Protected Attributes | |
static double | rcond = 1e-12 |
Factor to determine if we treat a singular factor as 0. More... | |
Definition at line 57 of file connor_solver.h.
|
inline |
Constructor.
This takes a CostFunction that we will minimize, a ConvergenceCheck to check for convergence, and optionally the initial value for gamma.
See the comments above this class for the "save_test_data" argument.
Definition at line 68 of file connor_solver.h.
|
inlinevirtual |
Definition at line 75 of file connor_solver.h.
|
inlinevirtual |
Add an observer.
Implements FullPhysics::Observable< ConnorSolver >.
Definition at line 77 of file connor_solver.h.
|
inlineinherited |
Add an observer and keep a reference to it.
See the discussion in the Observer class description for details.
Definition at line 107 of file observer.h.
|
inlineprotectedinherited |
Add an observer.
Definition at line 148 of file observer.h.
|
inlineprotectedinherited |
Definition at line 159 of file observer.h.
|
virtual |
Return a posteriori covariance matrix for last problem solved.
Definition at line 403 of file connor_solver.cc.
|
virtual |
Return a scaled a posteriori covariance matrix for last problem solved.
This is scaled by sigma_ap.
Definition at line 386 of file connor_solver.cc.
Array< double, 2 > ConnorSolver::apriori_covariance | ( | ) | const |
Apriori covariance matrix.
Definition at line 475 of file connor_solver.cc.
|
inlinevirtual |
Return normalized apriori covariance inverse matrix.
Definition at line 199 of file connor_solver.h.
|
virtual |
Return averaging kernel for last problem solved.
Definition at line 428 of file connor_solver.cc.
|
inlineprotectedinherited |
Remove any dead pointers.
Definition at line 196 of file observer.h.
|
inline |
The convergence check object.
Definition at line 122 of file connor_solver.h.
|
inline |
Cost function.
Definition at line 115 of file connor_solver.h.
|
protected |
This does an inversion step.
For this we solve the system
Note, to improve numerical stability we scale the system by
and solve the system
To help with testing for convergence, we also calculate:
This calculates the values kt_se_m1_k, dx, and fstat.
Definition at line 294 of file connor_solver.cc.
|
inlinevirtual |
Return fit results for solution to last problem solved.
Definition at line 179 of file connor_solver.h.
void ConnorSolver::from_stream | ( | std::istream & | is | ) |
Restore state of object previous saved with "to_stream".
Definition at line 118 of file connor_solver.cc.
|
inline |
Levenberg-Marquardt parameter for last step we processed.
Definition at line 109 of file connor_solver.h.
|
inlinevirtual |
Return the last jacobian calculated.
Note that this is not the jacobian calculated at the final solution x_sol, but rather is the for the iteration right before x_sol. Never the less this can be used as an approximation to the final Jacobian.
We currently don't calculate the final Jacobian because each iteration of the CostFunction can be rather expensive to calculate. We can revisit this if necessary in the future.
Definition at line 173 of file connor_solver.h.
|
inlineprotectedinherited |
Function to call to notify Observers of a state change.
The object should pass itself to this function, so it can be passed to the Observers.
Definition at line 121 of file observer.h.
|
inline |
Number of divergent steps for the last problem solved.
Definition at line 135 of file connor_solver.h.
|
inline |
Number of iterations for the last problem solved.
Definition at line 129 of file connor_solver.h.
|
inline |
Outcome flag. This is an integer version of FitStatistic::OUTCOME.
Definition at line 141 of file connor_solver.h.
|
inline |
Definition at line 202 of file connor_solver.h.
|
inlineinherited |
Print to string.
This is primarily useful for SWIG wrappers to this class, e.g. a to_s method in ruby.
Definition at line 31 of file printable.h.
|
inlinevirtual |
Remove an observer.
Implements FullPhysics::Observable< ConnorSolver >.
Definition at line 79 of file connor_solver.h.
|
inlineprotectedinherited |
Remove an observer.
Definition at line 173 of file observer.h.
|
inlineprotectedinherited |
Definition at line 181 of file observer.h.
|
inlinevirtual |
Return residual for solution to last problem solved.
Definition at line 185 of file connor_solver.h.
|
inlinevirtual |
Return diagonal of covariance matrix of residual for solution to last problem solved.
Definition at line 192 of file connor_solver.h.
|
inline |
Set save_test_data argument, see explanation before class for this.
Definition at line 85 of file connor_solver.h.
|
virtual |
This solves the least squares problem starting at the initial guess.
We don't directly return the solution and related matrices, you can query this object for them after the solver has completed.
Initial_guess | The initial value of x_i |
Apriori | The apriori value of x_i. Often but not always the same as the initial guess. |
Apriori_cov | The covariance matrix of the apriori value. |
Definition at line 150 of file connor_solver.cc.
boost::shared_ptr< ConnorSolverState > ConnorSolver::state | ( | ) | const |
Save state to a ConnorSolverState object.
This is similar to to_stream, but we put the state into another object. This fits better with Python, where we can pickle the ConnorSolverState (right now we can't pickle an actual ConnorSolver because we don't have support for pickling a forward model.
Definition at line 42 of file connor_solver.cc.
void ConnorSolver::state | ( | const ConnorSolverState & | S | ) |
Set the state of the ConnorSolver from a previously created ConnorSolverState.
This is like from_stream, but works better with Python.
Definition at line 56 of file connor_solver.cc.
void ConnorSolver::test_do_inversion | ( | const std::string & | Fname, |
blitz::Array< double, 1 > & | Dx, | ||
blitz::Array< double, 2 > & | Kt_se_m1_k | ||
) |
Restore state previously saved, and run do_inversion.
This is a backdoor to help do unit testing on do_inversion.
Definition at line 244 of file connor_solver.cc.
void ConnorSolver::to_stream | ( | std::ostream & | Os | ) | const |
Save state of object.
This is meant as an aid to testing, you can run through a long calculation, then save this as test data. You can then use "from_stream" to pick up where this calculation is in a test environment.
Definition at line 81 of file connor_solver.cc.
|
inline |
Return the a priori of the last problem solved.
Definition at line 147 of file connor_solver.h.
|
inline |
Return the uncertainty of the apriori of the last problem solved.
This is the sqrt of the diagonal of the a priori covariance matrix.
Definition at line 154 of file connor_solver.h.
|
inlinevirtual |
Definition at line 156 of file connor_solver.h.
|
inline |
Definition at line 157 of file connor_solver.h.
blitz::Array< double, 1 > ConnorSolver::x_solution_uncertainty | ( | ) | const |
Return the uncertainty of x_solution, this is just the sqrt of the diagonal of the full aposteriori_covariance.
Definition at line 444 of file connor_solver.cc.
blitz::Array< double, 1 > ConnorSolver::x_solution_zero_unused | ( | ) | const |
Return the solution to the last problem solved.
We set unused parameters to 0 (this makes sense for our current forward model, we may want to do something more sophisticated in the future).
Definition at line 418 of file connor_solver.cc.
|
inline |
Definition at line 160 of file connor_solver.h.
|
protected |
Array that is 0 where we are not using a parameter, and 1 elsewhere.
Definition at line 463 of file connor_solver.cc.
|
protected |
This is the apriori covariance matrix scaled by sigma_ap.
Definition at line 268 of file connor_solver.h.
|
protected |
The convergence check object.
Definition at line 327 of file connor_solver.h.
|
protected |
The cost function.
Definition at line 321 of file connor_solver.h.
|
protected |
This is the update to , .
Definition at line 315 of file connor_solver.h.
|
protected |
Results from last fit step.
Definition at line 262 of file connor_solver.h.
|
protected |
Levenberg-Marquardt parameter.
This gets updated in each iteration in "solve"
Definition at line 250 of file connor_solver.h.
|
protected |
Initial value of gamma.
Definition at line 332 of file connor_solver.h.
|
protected |
Stash gamma from last step we processed.
Definition at line 256 of file connor_solver.h.
|
protected |
This is the Jacobian, called in the ATB.
This gets updated each iteration of the inversion in "solve"
Definition at line 232 of file connor_solver.h.
|
protected |
This is .
We keep this term because it is needed to calculate the aposteriori_covariance and averaging_kernel.
This gets updated in each iteration, in "do_inversion"
Definition at line 242 of file connor_solver.h.
|
protectedinherited |
Definition at line 200 of file observer.h.
|
staticprotected |
Factor to determine if we treat a singular factor as 0.
This is Rcond in solve_least_squares routine.
Definition at line 334 of file connor_solver.h.
|
protectedinherited |
Definition at line 201 of file observer.h.
|
protected |
This is the residual from the model, .
Definition at line 306 of file connor_solver.h.
|
protected |
This is the inverse of apriori_cov_scaled, but only for the rows and columns of the jacobian that are nonzero.
This is calculated in do_inversion
Definition at line 276 of file connor_solver.h.
|
protected |
If this isn't an empty string, save to this file in the first iteration.
Definition at line 215 of file connor_solver.h.
|
protected |
This is the covariance matrix of the residual, called in the ATB.
Because of the size, we assume this is a diagonal matrix and store just the diagonal components.
This gets updated each iteration of the inversion in "solve"
Definition at line 225 of file connor_solver.h.
|
protected |
This is the sqrt of S_a diagonal, or the sigma of the apriori.
Definition at line 282 of file connor_solver.h.
|
protected |
This is the apriori X value, called in the ATB.
This gets set by the constructor, and then held constant
Definition at line 289 of file connor_solver.h.
|
protected |
This is the current value of the state vector X value, called in the ATB.
This gets updated in each iteration in "solve".
Definition at line 297 of file connor_solver.h.