GRINS-0.8.0
List of all members | Protected Member Functions
GRINSTesting::RegressionHelper Class Reference

#include <regression_helper.h>

Inheritance diagram for GRINSTesting::RegressionHelper:
Inheritance graph
[legend]

Protected Member Functions

libMesh::Real linear_slope (std::vector< libMesh::Real > x, std::vector< libMesh::Real > y, unsigned int start_index, unsigned int end_index)
 Slope $ m $ for linear regression. More...
 
libMesh::Real linear_intercept (std::vector< libMesh::Real > x, std::vector< libMesh::Real > y, unsigned int start_index, unsigned int end_index)
 y-intercept $ b $ for linear regression More...
 
void check_convergence_rate (std::vector< libMesh::Real > x, std::vector< libMesh::Real > y, unsigned int start_index, unsigned int end_index, libMesh::Real convergence_rate, libMesh::Real tol)
 Check the convergence rate of the supplied data to within the given absolute tolerance. More...
 

Detailed Description

Provides helper functions for fitting data to a curve.

Currently supports linear regression $ Y = m X + b $

Definition at line 46 of file regression_helper.h.

Member Function Documentation

void GRINSTesting::RegressionHelper::check_convergence_rate ( std::vector< libMesh::Real >  x,
std::vector< libMesh::Real >  y,
unsigned int  start_index,
unsigned int  end_index,
libMesh::Real  convergence_rate,
libMesh::Real  tol 
)
inlineprotected

Check the convergence rate of the supplied data to within the given absolute tolerance.

Definition at line 110 of file regression_helper.h.

References linear_slope().

Referenced by GRINSTesting::IntegratedFunctionTest::test_convergence().

111  {
112  libmesh_assert(tol > 0.0);
113  libmesh_assert(x.size() == y.size());
114  libmesh_assert(start_index < end_index);
115  libmesh_assert(end_index < x.size());
116 
117  libMesh::Real calculated_rate = linear_slope(x,y,start_index,end_index);
118 
119 #ifdef GRINS_HAVE_CPPUNIT
120 
121  CPPUNIT_ASSERT_DOUBLES_EQUAL(convergence_rate,calculated_rate,tol);
122 #else
123  if (std::abs(convergence_rate - calculated_rate) > tol)
124  {
125  std::stringstream ss;
126  ss <<"ERROR" <<std::endl
127  <<"calculated convergence rate: " <<calculated_rate <<std::endl
128  <<"desired convergence rate: " <<convergence_rate <<std::endl
129  <<"tolerance: " <<tol <<std::endl;
130 
131  libmesh_error_msg(ss.str());
132  }
133 #endif // GRINS_HAVE_CPPUNIT
134  }
libMesh::Real linear_slope(std::vector< libMesh::Real > x, std::vector< libMesh::Real > y, unsigned int start_index, unsigned int end_index)
Slope for linear regression.
libMesh::Real GRINSTesting::RegressionHelper::linear_intercept ( std::vector< libMesh::Real >  x,
std::vector< libMesh::Real >  y,
unsigned int  start_index,
unsigned int  end_index 
)
inlineprotected

y-intercept $ b $ for linear regression

Definition at line 81 of file regression_helper.h.

82  {
83  libmesh_assert(x.size() == y.size());
84  libmesh_assert(start_index < end_index);
85 
86  unsigned int n = x.size();
87  libmesh_assert(end_index < n);
88 
89  libMesh::Real sum_y = 0.0,
90  sum_x2 = 0.0,
91  sum_x = 0.0,
92  sum_xy = 0.0;
93 
94  for (unsigned int i=start_index; i<=end_index; i++)
95  {
96  libMesh::Real xi = x[i];
97  libMesh::Real yi = y[i];
98 
99  sum_y += yi;
100  sum_x2 += xi*xi;
101  sum_x += xi;
102  sum_xy += xi*yi;
103  }
104 
105  libMesh::Real intercept = ( sum_y*sum_x2 - sum_x*sum_xy )/( n*sum_x2 - (sum_x*sum_x) );
106  return intercept;
107  }
libMesh::Real GRINSTesting::RegressionHelper::linear_slope ( std::vector< libMesh::Real >  x,
std::vector< libMesh::Real >  y,
unsigned int  start_index,
unsigned int  end_index 
)
inlineprotected

Slope $ m $ for linear regression.

Definition at line 51 of file regression_helper.h.

Referenced by check_convergence_rate().

52  {
53  libmesh_assert(x.size() == y.size());
54  libmesh_assert(start_index < end_index);
55 
56  unsigned int n = x.size();
57  libmesh_assert(end_index < n);
58 
59  libMesh::Real sum_y = 0.0,
60  sum_x2 = 0.0,
61  sum_x = 0.0,
62  sum_xy = 0.0;
63 
64  for (unsigned int i=start_index; i<=end_index; i++)
65  {
66  libMesh::Real xi = x[i];
67  libMesh::Real yi = y[i];
68 
69  sum_y += yi;
70  sum_x2 += xi*xi;
71  sum_x += xi;
72  sum_xy += xi*yi;
73  }
74 
75 
76  libMesh::Real slope = ( n*sum_xy - sum_x*sum_y )/( n*sum_x2 - (sum_x*sum_x) );
77  return slope;
78  }

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

Generated on Tue Dec 19 2017 12:47:32 for GRINS-0.8.0 by  doxygen 1.8.9.1