creativity  v1.3.0
Agent-based model of creativity and piracy
OLS.hpp
1 #pragma once
2 #include "creativity/data/Equation.hpp"
3 #include <Eigen/Core>
4 #include <ostream>
5 
6 namespace creativity { namespace data {
7 
17 class OLS {
18  public:
20  OLS() = delete;
21 
24  OLS(const Equation &model);
25 
28  OLS(Equation &&model);
29 
32  const Equation& model() const { return model_; }
33 
36  unsigned int n() const { return solved_ ? X_.rows() : model_.depVar()->size(); }
37 
40  unsigned int k() const { return solved_ ? X_.cols() : model_.numVars(); }
41 
45  int df() const { return n() - k(); }
46 
52  void gather();
53 
58  void solve();
59 
64  const Eigen::VectorXd& beta() const;
65 
70  const Eigen::MatrixXd& covariance() const;
71 
77  const Eigen::VectorXd& se() const;
78 
83  const Eigen::VectorXd& tRatios() const;
84 
89  const Eigen::VectorXd& pValues() const;
90 
95  const double& s2() const;
96 
101  const Eigen::VectorXd& residuals() const;
102 
107  const double& ssr() const;
108 
117  const double& Rsq() const;
118 
120  struct ftest {
121  double f;
122  unsigned df_numerator;
123  unsigned df_denominator;
124  double p;
125  };
126 
132  ftest fTest() const;
133 
139  const Eigen::VectorXd& y() const;
140 
146  const Eigen::MatrixXd& X() const;
147 
152  friend std::ostream& operator<<(std::ostream &os, const OLS &ols);
153  protected:
156 
158  bool gathered_ = false;
159 
161  Eigen::VectorXd y_;
162 
164  Eigen::MatrixXd X_;
165 
167  bool solved_ = false;
168 
170  Eigen::VectorXd beta_;
171 
173  Eigen::MatrixXd var_beta_;
174 
176  Eigen::VectorXd se_;
177 
179  Eigen::VectorXd t_ratios_;
180 
182  Eigen::VectorXd p_values_;
183 
185  Eigen::VectorXd residuals_;
186 
187  double ssr_ = 0,
188  s2_ = 0,
189  R2_ = 0;
190 
192  void requireGathered() const { if (!gathered_) throw std::logic_error("Cannot access model data before calling gather()"); }
193 
195  void requireSolved() const { if (!solved_) throw std::logic_error("Cannot obtain model estimates before calling solve()"); }
196 
197 };
198 
199 }}
Class for running a basic OLS regression.
Definition: OLS.hpp:17
Equation model_
The model, given during construction.
Definition: OLS.hpp:155
const Equation & model() const
Accesses the model used for this OLS object.
Definition: OLS.hpp:32
double p
The p-value of the test.
Definition: OLS.hpp:124
Eigen::VectorXd y_
The y vector generated from the model.
Definition: OLS.hpp:161
double f
The F value.
Definition: OLS.hpp:121
The return value of fStat()
Definition: OLS.hpp:120
const double & ssr() const
Returns the sum-of-squared residuals.
Eigen::VectorXd p_values_
Cached p-values.
Definition: OLS.hpp:182
Eigen::MatrixXd X_
The X matrix generated from the model.
Definition: OLS.hpp:164
friend std::ostream & operator<<(std::ostream &os, const OLS &ols)
Overloaded so that an OLS object can be sent to an output stream; the output consists of the model fo...
Primary namespace for all Creativity library code.
Definition: config.hpp:4
bool gathered_
Whether gather() has been called, to populate y_ and X_.
Definition: OLS.hpp:158
Eigen::VectorXd beta_
The beta vector.
Definition: OLS.hpp:170
double ssr_
SSR.
Definition: OLS.hpp:187
Eigen::VectorXd se_
Cached standard errors.
Definition: OLS.hpp:176
const Eigen::VectorXd & se() const
Returns the standard errors (the square roots of the diagonal of covariance()) of the beta() values...
Eigen::VectorXd residuals_
Residuals.
Definition: OLS.hpp:185
int df() const
Returns the degrees of freedom of the model; this is simply n() - k().
Definition: OLS.hpp:45
Class to store a equation.
Definition: Equation.hpp:33
const Eigen::VectorXd & y() const
Returns the y data used to solve the model.
double R2_
R^2 value.
Definition: OLS.hpp:189
bool solved_
Whether solve() has been called, to populate the below.
Definition: OLS.hpp:167
void gather()
Calculates and stores the final numerical values from the model.
std::shared_ptr< const Variable > depVar() const
Accesses the dependent variable.
const Eigen::VectorXd & tRatios() const
Returns the t-ratios for =0 tests, i.e.
const Eigen::MatrixXd & covariance() const
Returns the covariance estimate of the beta estimators.
const double & Rsq() const
Returns the value for the regression.
const double & s2() const
Returns , the square of the regression standard error.
OLS()=delete
No default constructor.
Eigen::MatrixXd var_beta_
The estimated covariance of the beta estimators.
Definition: OLS.hpp:173
void requireSolved() const
Throws a std::logic_error if the model hasn&#39;t been solved.
Definition: OLS.hpp:195
unsigned df_denominator
Denominator d.f.
Definition: OLS.hpp:123
ftest fTest() const
Calculates and returns the F-test of all non-constant coefficients in the model being equal to 0...
void requireGathered() const
Throws a std::logic_error if the model hasn&#39;t been gathered.
Definition: OLS.hpp:192
void solve()
Attempts to solve the model, if not already done.
Eigen::VectorXd t_ratios_
Cached t ratios.
Definition: OLS.hpp:179
double s2_
sigma^2 estimate
Definition: OLS.hpp:188
unsigned int n() const
Returns the number of observations for this OLS object.
Definition: OLS.hpp:36
const Eigen::MatrixXd & X() const
Returns the X data used to solve the model.
unsigned int numVars() const
Returns the number of independent variables.
const Eigen::VectorXd & residuals() const
Returns the residuals.
unsigned int k() const
Returns the number of variables for this OLS object.
Definition: OLS.hpp:40
unsigned df_numerator
Numerator d.f.
Definition: OLS.hpp:122
const Eigen::VectorXd & beta() const
Returns the vector of coefficients (i.e.
const Eigen::VectorXd & pValues() const
Returns the p-values of the t-ratios returned by tRatios()