JKQTPlotter trunk/v5.0.0
an extensive Qt5+Qt6 Plotter framework (including a feature-richt plotter widget, a speed-optimized, but limited variant and a LaTeX equation renderer!), written fully in C/C++ and without external dependencies
Loading...
Searching...
No Matches
Collaboration diagram for Regression Analysis:

Enumerations

enum class  JKQTPStatRegressionModelType {
  JKQTPStatRegressionModelType::Linear ,
  JKQTPStatRegressionModelType::PowerLaw ,
  JKQTPStatRegressionModelType::Exponential ,
  JKQTPStatRegressionModelType::Logarithm
}
 when performing linear regression, different target functions can be fitted, if the input data is transformed accordingly. This library provides the options in this enum by default. More...
 

Functions

template<class InputItX , class InputItY >
double jkqtpstatCoefficientOfDetermination (InputItX firstX, InputItX lastX, InputItY firstY, InputItY lastY, std::function< double(double)> f)
 calculates the coefficient of determination $ R^2 $ for a set of measurements $ (x_i,y_i) $ with a fit function $ f(x) $
 
jkqtmath_LIB_EXPORT std::pair< std::function< double(double)>, std::function< double(double)> > jkqtpStatGenerateParameterATransformation (JKQTPStatRegressionModelType type)
 Generates the transformation function for a-parameter (offset, result.first : transform, result.second : back-transform) for each regression model in JKQTPStatRegressionModelType in type.
 
jkqtmath_LIB_EXPORT std::pair< std::function< double(double)>, std::function< double(double)> > jkqtpStatGenerateParameterBTransformation (JKQTPStatRegressionModelType type)
 Generates the transformation function for b-parameter (slope, result.first : transform, result.second : back-transform) for each regression model in JKQTPStatRegressionModelType in type.
 
jkqtmath_LIB_EXPORT std::function< double(double, double, double)> jkqtpStatGenerateRegressionModel (JKQTPStatRegressionModelType type)
 Generates functors f(x,a,b) for the models from JKQTPStatRegressionModelType in type.
 
jkqtmath_LIB_EXPORT std::function< double(double)> jkqtpStatGenerateRegressionModel (JKQTPStatRegressionModelType type, double a, double b)
 Generates functors f(x) for the models from JKQTPStatRegressionModelType in type and binds the parameter values and b to the returned function.
 
jkqtmath_LIB_EXPORT std::pair< std::function< double(double)>, std::function< double(double)> > jkqtpStatGenerateTransformation (JKQTPStatRegressionModelType type)
 Generates the transformation function for x-data (result.first ) and y-data (result.second ) for each regression model in JKQTPStatRegressionModelType in type.
 
template<class InputItX , class InputItY >
void jkqtpstatLinearRegression (InputItX firstX, InputItX lastX, InputItY firstY, InputItY lastY, double &coeffA, double &coeffB, bool fixA=false, bool fixB=false)
 calculate the linear regression coefficients for a given data range firstX / firstY ... lastX / lastY where the model is $ f(x)=a+b\cdot x $ So this function solves the least-squares optimization problem:
 
template<class InputItX , class InputItY , class InputItW >
void jkqtpstatLinearWeightedRegression (InputItX firstX, InputItX lastX, InputItY firstY, InputItY lastY, InputItW firstW, InputItW lastW, double &coeffA, double &coeffB, bool fixA=false, bool fixB=false, std::function< double(double)> fWeightDataToWi=&jkqtp_identity< double >)
 calculate the weighted linear regression coefficients for a given for a given data range firstX / firstY / firstW ... lastX / lastY / lastW where the model is $ f(x)=a+b\cdot x $ So this function solves the least-squares optimization problem:
 
template<class InputItX , class InputItY >
void jkqtpstatRegression (JKQTPStatRegressionModelType type, InputItX firstX, InputItX lastX, InputItY firstY, InputItY lastY, double &coeffA, double &coeffB, bool fixA=false, bool fixB=false)
 calculate the linear regression coefficients for a given data range firstX / firstY ... lastX / lastY where the model is defined by type So this function solves the least-squares optimization problem:
 
jkqtmath_LIB_EXPORT QString jkqtpstatRegressionModel2Latex (JKQTPStatRegressionModelType type, double a, double b)
 Generates a LaTeX string for the models from JKQTPStatRegressionModelType in type.
 
template<class InputItX , class InputItY >
void jkqtpstatRobustIRLSLinearRegression (InputItX firstX, InputItX lastX, InputItY firstY, InputItY lastY, double &coeffA, double &coeffB, bool fixA=false, bool fixB=false, double p=1.1, int iterations=100)
 calculate the (robust) iteratively reweighted least-squares (IRLS) estimate for the parameters of the model $ f(x)=a+b\cdot x $ for a given data range firstX / firstY ... lastX / lastY So this function finds an outlier-robust solution to the optimization problem:
 
template<class InputItX , class InputItY >
void jkqtpstatRobustIRLSRegression (JKQTPStatRegressionModelType type, InputItX firstX, InputItX lastX, InputItY firstY, InputItY lastY, double &coeffA, double &coeffB, bool fixA=false, bool fixB=false, double p=1.1, int iterations=100)
 calculate the robust linear regression coefficients for a given data range firstX / firstY ... lastX / lastY where the model is defined by type So this function solves the Lp-norm optimization problem:
 
template<class InputItX , class InputItY >
double jkqtpstatSumOfDeviations (InputItX firstX, InputItX lastX, InputItY firstY, InputItY lastY, std::function< double(double)> f)
 calculates the sum of deviations $ \chi^2 $ for a set of measurements $ (x_i,y_i) $ with a fit function $ f(x) $
 
template<class InputItX , class InputItY , class InputItW >
double jkqtpstatWeightedCoefficientOfDetermination (InputItX firstX, InputItX lastX, InputItY firstY, InputItY lastY, InputItW firstW, InputItW lastW, std::function< double(double)> f, std::function< double(double)> fWeightDataToWi=&jkqtp_identity< double >)
 calculates the weightedcoefficient of determination $ R^2 $ for a set of measurements $ (x_i,y_i,w_i) $ with a fit function $ f(x) $
 
template<class InputItX , class InputItY , class InputItW >
void jkqtpstatWeightedRegression (JKQTPStatRegressionModelType type, InputItX firstX, InputItX lastX, InputItY firstY, InputItY lastY, InputItW firstW, InputItW lastW, double &coeffA, double &coeffB, bool fixA=false, bool fixB=false, std::function< double(double)> fWeightDataToWi=&jkqtp_identity< double >)
 calculate the robust linear regression coefficients for a given data range firstX / firstY ... lastX / lastY where the model is defined by type So this function solves the Lp-norm optimization problem:
 
template<class InputItX , class InputItY , class InputItW >
double jkqtpstatWeightedSumOfDeviations (InputItX firstX, InputItX lastX, InputItY firstY, InputItY lastY, InputItW firstW, InputItW lastW, std::function< double(double)> f, std::function< double(double)> fWeightDataToWi=&jkqtp_identity< double >)
 calculates the weighted sum of deviations $ \chi^2 $ for a set of measurements $ (x_i,y_i,w_i) $ with a fit function $ f(x) $
 

Detailed Description

Enumeration Type Documentation

◆ JKQTPStatRegressionModelType

enum class JKQTPStatRegressionModelType
strong

when performing linear regression, different target functions can be fitted, if the input data is transformed accordingly. This library provides the options in this enum by default.

Enumerator
Linear 

linear model $ f(x)=a+b\cdot x $

PowerLaw 

power law model $ f(x)=a\cdot x^b $

Exponential 

exponential model $ f(x)=a\cdot \exp(b\cdot x) $

Logarithm 

exponential model $ f(x)=a+b\cdot \ln(x) $

Function Documentation

◆ jkqtpstatCoefficientOfDetermination()

template<class InputItX , class InputItY >
double jkqtpstatCoefficientOfDetermination ( InputItX  firstX,
InputItX  lastX,
InputItY  firstY,
InputItY  lastY,
std::function< double(double)>  f 
)
inline

calculates the coefficient of determination $ R^2 $ for a set of measurements $ (x_i,y_i) $ with a fit function $ f(x) $

Template Parameters
InputItXstandard iterator type of firstX and lastX.
InputItYstandard iterator type of firstY and lastY.
Parameters
firstXiterator pointing to the first item in the x-dataset to use $ x_1 $
lastXiterator pointing behind the last item in the x-dataset to use $ x_N $
firstYiterator pointing to the first item in the y-dataset to use $ y_1 $
lastYiterator pointing behind the last item in the y-dataset to use $ y_N $
ffunction $ f(x) $, result of a fit to the data
Returns
coeffcicient of determination

\[ R^2=1-\frac{\sum_i\bigl[y_i-f(x_i)\bigr]^2}{\sum_i\bigl[y_i-\overline{y}\bigr]^2} \]

where

\[ \overline{y}=\frac{1}{N}\cdot\sum_iy_i \]

See also
https://en.wikipedia.org/wiki/Coefficient_of_determination

◆ jkqtpStatGenerateParameterATransformation()

jkqtmath_LIB_EXPORT std::pair< std::function< double(double)>, std::function< double(double)> > jkqtpStatGenerateParameterATransformation ( JKQTPStatRegressionModelType  type)

Generates the transformation function for a-parameter (offset, result.first : transform, result.second : back-transform) for each regression model in JKQTPStatRegressionModelType in type.

◆ jkqtpStatGenerateParameterBTransformation()

jkqtmath_LIB_EXPORT std::pair< std::function< double(double)>, std::function< double(double)> > jkqtpStatGenerateParameterBTransformation ( JKQTPStatRegressionModelType  type)

Generates the transformation function for b-parameter (slope, result.first : transform, result.second : back-transform) for each regression model in JKQTPStatRegressionModelType in type.

◆ jkqtpStatGenerateRegressionModel() [1/2]

jkqtmath_LIB_EXPORT std::function< double(double, double, double)> jkqtpStatGenerateRegressionModel ( JKQTPStatRegressionModelType  type)

Generates functors f(x,a,b) for the models from JKQTPStatRegressionModelType in type.

◆ jkqtpStatGenerateRegressionModel() [2/2]

jkqtmath_LIB_EXPORT std::function< double(double)> jkqtpStatGenerateRegressionModel ( JKQTPStatRegressionModelType  type,
double  a,
double  b 
)

Generates functors f(x) for the models from JKQTPStatRegressionModelType in type and binds the parameter values and b to the returned function.

◆ jkqtpStatGenerateTransformation()

jkqtmath_LIB_EXPORT std::pair< std::function< double(double)>, std::function< double(double)> > jkqtpStatGenerateTransformation ( JKQTPStatRegressionModelType  type)

Generates the transformation function for x-data (result.first ) and y-data (result.second ) for each regression model in JKQTPStatRegressionModelType in type.

◆ jkqtpstatLinearRegression()

template<class InputItX , class InputItY >
void jkqtpstatLinearRegression ( InputItX  firstX,
InputItX  lastX,
InputItY  firstY,
InputItY  lastY,
double &  coeffA,
double &  coeffB,
bool  fixA = false,
bool  fixB = false 
)
inline

calculate the linear regression coefficients for a given data range firstX / firstY ... lastX / lastY where the model is $ f(x)=a+b\cdot x $ So this function solves the least-squares optimization problem:

\[ (a^\ast, b^\ast)=\mathop{\mathrm{arg\;min}}\limits_{a,b}\sum\limits_i\left(y_i-(a+b\cdot x_i)\right)^2 \]

Template Parameters
InputItXstandard iterator type of firstX and lastX.
InputItYstandard iterator type of firstY and lastY.
Parameters
firstXiterator pointing to the first item in the x-dataset to use $ x_1 $
lastXiterator pointing behind the last item in the x-dataset to use $ x_N $
firstYiterator pointing to the first item in the y-dataset to use $ y_1 $
lastYiterator pointing behind the last item in the y-dataset to use $ y_N $
[in,out]coeffAreturns the offset of the linear model
[in,out]coeffBreturns the slope of the linear model
fixAif true, the offset coefficient $ a $ is not determined by the fit, but the value provided in coeffA is used
fixBif true, the slope coefficient $ b $ is not determined by the fit, but the value provided in coeffB is used

This function computes internally:

\[ a=\overline{y}-b\cdot\overline{x} \]

\[ b=\frac{\sum x_iy_i-N\cdot\overline{x}\cdot\overline{y}}{\sum x_i^2-N\cdot(\overline{x})^2} \]

◆ jkqtpstatLinearWeightedRegression()

template<class InputItX , class InputItY , class InputItW >
void jkqtpstatLinearWeightedRegression ( InputItX  firstX,
InputItX  lastX,
InputItY  firstY,
InputItY  lastY,
InputItW  firstW,
InputItW  lastW,
double &  coeffA,
double &  coeffB,
bool  fixA = false,
bool  fixB = false,
std::function< double(double)>  fWeightDataToWi = &jkqtp_identity<double> 
)
inline

calculate the weighted linear regression coefficients for a given for a given data range firstX / firstY / firstW ... lastX / lastY / lastW where the model is $ f(x)=a+b\cdot x $ So this function solves the least-squares optimization problem:

\[ (a^\ast, b^\ast)=\mathop{\mathrm{arg\;min}}\limits_{a,b}\sum\limits_iw_i^2\cdot\left(y_i-(a+b\cdot x_i)\right)^2 \]

Template Parameters
InputItXstandard iterator type of firstX and lastX.
InputItYstandard iterator type of firstY and lastY.
InputItWstandard iterator type of firstW and lastW.
Parameters
firstXiterator pointing to the first item in the x-dataset to use $ x_1 $
lastXiterator pointing behind the last item in the x-dataset to use $ x_N $
firstYiterator pointing to the first item in the y-dataset to use $ y_1 $
lastYiterator pointing behind the last item in the y-dataset to use $ y_N $
firstWiterator pointing to the first item in the weight-dataset to use $ w_1 $
lastWiterator pointing behind the last item in the weight-dataset to use $ w_N $
[in,out]coeffAreturns the offset of the linear model
[in,out]coeffBreturns the slope of the linear model
fixAif true, the offset coefficient $ a $ is not determined by the fit, but the value provided in coeffA is used
fixBif true, the slope coefficient $ b $ is not determined by the fit, but the value provided in coeffB is used
fWeightDataToWian optional function, which is applied to the data from firstW ... lastW to convert them to weight, i.e. wi=fWeightDataToWi(*itW) e.g. if you use data used to draw error bars, you can use jkqtp_inversePropSaveDefault(). The default is jkqtp_identity(), which just returns the values. In the case of jkqtp_inversePropSaveDefault(), a datapoint x,y, has a large weight, if it's error is small and in the case if jkqtp_identity() it's weight is directly proportional to the given value.

This function internally computes:

\[ a=\frac{\overline{y}-b\cdot\overline{x}}{\overline{w^2}} \]

\[ b=\frac{\overline{w^2}\cdot\overline{x\cdot y}-\overline{x}\cdot\overline{y}}{\overline{x^2}\cdot\overline{w^2}-\overline{x}^2} \]

Here the averages are defined in terms of a weight vector $ w_i$:

\[ \overline{x}=\sum\limits_iw_i^2\cdot x_i \]

\[ \overline{y}=\sum\limits_iw_i^2\cdot y_i \]

\[ \overline{x\cdot y}=\sum\limits_iw_i^2\cdot x_i\cdot y_i \]

\[ \overline{x^2}=\sum\limits_iw_i^2\cdot x_i^2 \]

\[ \overline{w^2}=\sum\limits_iw_i^2 \]

◆ jkqtpstatRegression()

template<class InputItX , class InputItY >
void jkqtpstatRegression ( JKQTPStatRegressionModelType  type,
InputItX  firstX,
InputItX  lastX,
InputItY  firstY,
InputItY  lastY,
double &  coeffA,
double &  coeffB,
bool  fixA = false,
bool  fixB = false 
)
inline

calculate the linear regression coefficients for a given data range firstX / firstY ... lastX / lastY where the model is defined by type So this function solves the least-squares optimization problem:

\[ (a^\ast, b^\ast)=\mathop{\mathrm{arg\;min}}\limits_{a,b}\sum\limits_i\left(y_i-f_{\text{type}}(x_i,a,b)\right)^2 \]

by reducing it to a linear fit by transforming x- and/or y-data

Template Parameters
InputItXstandard iterator type of firstX and lastX.
InputItYstandard iterator type of firstY and lastY.
Parameters
typemodel to be fitted
firstXiterator pointing to the first item in the x-dataset to use $ x_1 $
lastXiterator pointing behind the last item in the x-dataset to use $ x_N $
firstYiterator pointing to the first item in the y-dataset to use $ y_1 $
lastYiterator pointing behind the last item in the y-dataset to use $ y_N $
[in,out]coeffAreturns the offset of the linear model
[in,out]coeffBreturns the slope of the linear model
fixAif true, the offset coefficient $ a $ is not determined by the fit, but the value provided in coeffA is used
fixBif true, the slope coefficient $ b $ is not determined by the fit, but the value provided in coeffB is used

This function computes internally first transforms the data, as appropriate to fit the model defined by type and then calls jkqtpstatLinearRegression() to obtain the parameters. The output parameters are transformed, so they can be used with jkqtpStatGenerateRegressionModel() to generate a functor that evaluates the model

See also
JKQTPStatRegressionModelType, jkqtpStatGenerateRegressionModel(), jkqtpstatLinearRegression(), jkqtpStatGenerateTransformation()

◆ jkqtpstatRegressionModel2Latex()

jkqtmath_LIB_EXPORT QString jkqtpstatRegressionModel2Latex ( JKQTPStatRegressionModelType  type,
double  a,
double  b 
)

Generates a LaTeX string for the models from JKQTPStatRegressionModelType in type.

◆ jkqtpstatRobustIRLSLinearRegression()

template<class InputItX , class InputItY >
void jkqtpstatRobustIRLSLinearRegression ( InputItX  firstX,
InputItX  lastX,
InputItY  firstY,
InputItY  lastY,
double &  coeffA,
double &  coeffB,
bool  fixA = false,
bool  fixB = false,
double  p = 1.1,
int  iterations = 100 
)
inline

calculate the (robust) iteratively reweighted least-squares (IRLS) estimate for the parameters of the model $ f(x)=a+b\cdot x $ for a given data range firstX / firstY ... lastX / lastY So this function finds an outlier-robust solution to the optimization problem:

\[ (a^\ast,b^\ast)=\mathop{\mathrm{arg\;min}}\limits_{a,b}\sum\limits_i|a+b\cdot x_i-y_i|^p \]

Template Parameters
InputItXstandard iterator type of firstX and lastX.
InputItYstandard iterator type of firstY and lastY.
Parameters
firstXiterator pointing to the first item in the x-dataset to use $ x_1 $
lastXiterator pointing behind the last item in the x-dataset to use $ x_N $
firstYiterator pointing to the first item in the y-dataset to use $ y_1 $
lastYiterator pointing behind the last item in the y-dataset to use $ y_N $
[in,out]coeffAreturns the offset of the linear model
[in,out]coeffBreturns the slope of the linear model
fixAif true, the offset coefficient $ a $ is not determined by the fit, but the value provided in coeffA is used
fixBif true, the slope coefficient $ b $ is not determined by the fit, but the value provided in coeffB is used
pregularization parameter, the optimization problem is formulated in the $ L_p $ norm, using this p (see image below for an example)
iterationsthe number of iterations the IRLS algorithm performs

This is a simple form of the IRLS algorithm to estimate the parameters a and b in a linear model $ f(x)=a+b\cdot x $. This algorithm solves the optimization problem for a $ L_p$-norm:

\[ (a^\ast,b^\ast)=\mathop{\mathrm{arg\;min}}\limits_{a,b}\sum\limits_i|a+b\cdot x_i-y_i|^p \]

by iteratively optimization weights $ \vec{w} $ and solving a weighted least squares problem in each iteration:

\[ (a_n,b_n)=\mathop{\mathrm{arg\;min}}\limits_{a,b}\sum\limits_i|a+b\cdot x_i-y_i|^{(p-2)}\cdot|a+b\cdot x_i-y_i|^2 \]

The IRLS-algorithm works as follows:

  • calculate initial $ a_0$ and $ b_0$ with unweighted regression from x and y
  • perform a number of iterations (parameter iterations ). In each iteration $ n$:
    • calculate the error vector $\vec{e}$:

      \[ e_i = a+b\cdot x_i -y_i \]

    • estimate new weights $\vec{w}$:

      \[ w_i=|e_i|^{(p-2)/2} \]

    • calculate new estimates $ a_n$ and $ b_n$ with weighted regression from $ \vec{x}$ and $ \vec{y}$ and $ \vec{w}$
  • return the last estimates $ a_n$ and $ b_n$
See also
https://en.wikipedia.org/wiki/Iteratively_reweighted_least_squares, C. Sidney Burrus: "Iterative Reweighted Least Squares", http://cnx.org/content/m45285/latest/

◆ jkqtpstatRobustIRLSRegression()

template<class InputItX , class InputItY >
void jkqtpstatRobustIRLSRegression ( JKQTPStatRegressionModelType  type,
InputItX  firstX,
InputItX  lastX,
InputItY  firstY,
InputItY  lastY,
double &  coeffA,
double &  coeffB,
bool  fixA = false,
bool  fixB = false,
double  p = 1.1,
int  iterations = 100 
)
inline

calculate the robust linear regression coefficients for a given data range firstX / firstY ... lastX / lastY where the model is defined by type So this function solves the Lp-norm optimization problem:

\[ (a^\ast, b^\ast)=\mathop{\mathrm{arg\;min}}\limits_{a,b}\sum\limits_i|y_i-f_{\text{type}}(x_i,a,b)|^p \]

by reducing it to a linear fit by transforming x- and/or y-data

Template Parameters
InputItXstandard iterator type of firstX and lastX.
InputItYstandard iterator type of firstY and lastY.
Parameters
typemodel to be fitted
firstXiterator pointing to the first item in the x-dataset to use $ x_1 $
lastXiterator pointing behind the last item in the x-dataset to use $ x_N $
firstYiterator pointing to the first item in the y-dataset to use $ y_1 $
lastYiterator pointing behind the last item in the y-dataset to use $ y_N $
[in,out]coeffAreturns the offset of the linear model
[in,out]coeffBreturns the slope of the linear model
fixAif true, the offset coefficient $ a $ is not determined by the fit, but the value provided in coeffA is used
fixBif true, the slope coefficient $ b $ is not determined by the fit, but the value provided in coeffB is used
pregularization parameter, the optimization problem is formulated in the $ L_p $ norm, using this p (see image below for an example)
iterationsthe number of iterations the IRLS algorithm performs

This function computes internally first transforms the data, as appropriate to fit the model defined by type and then calls jkqtpstatRobustIRLSLinearRegression() to obtain the parameters. The output parameters are transformed, so they can be used with jkqtpStatGenerateRegressionModel() to generate a functor that evaluates the model

See also
JKQTPStatRegressionModelType, jkqtpStatGenerateRegressionModel(), jkqtpstatRobustIRLSLinearRegression(), jkqtpStatGenerateTransformation()

◆ jkqtpstatSumOfDeviations()

template<class InputItX , class InputItY >
double jkqtpstatSumOfDeviations ( InputItX  firstX,
InputItX  lastX,
InputItY  firstY,
InputItY  lastY,
std::function< double(double)>  f 
)
inline

calculates the sum of deviations $ \chi^2 $ for a set of measurements $ (x_i,y_i) $ with a fit function $ f(x) $

Template Parameters
InputItXstandard iterator type of firstX and lastX.
InputItYstandard iterator type of firstY and lastY.
Parameters
firstXiterator pointing to the first item in the x-dataset to use $ x_1 $
lastXiterator pointing behind the last item in the x-dataset to use $ x_N $
firstYiterator pointing to the first item in the y-dataset to use $ y_1 $
lastYiterator pointing behind the last item in the y-dataset to use $ y_N $
ffunction $ f(x) $, result of a fit to the data
Returns
sum of deviations

\[ \chi^2=\sum_i\bigl[y_i-f(x_i)\bigr]^2 \]

See also
https://en.wikipedia.org/wiki/Coefficient_of_determination

◆ jkqtpstatWeightedCoefficientOfDetermination()

template<class InputItX , class InputItY , class InputItW >
double jkqtpstatWeightedCoefficientOfDetermination ( InputItX  firstX,
InputItX  lastX,
InputItY  firstY,
InputItY  lastY,
InputItW  firstW,
InputItW  lastW,
std::function< double(double)>  f,
std::function< double(double)>  fWeightDataToWi = &jkqtp_identity<double> 
)
inline

calculates the weightedcoefficient of determination $ R^2 $ for a set of measurements $ (x_i,y_i,w_i) $ with a fit function $ f(x) $

Template Parameters
InputItXstandard iterator type of firstX and lastX.
InputItYstandard iterator type of firstY and lastY.
InputItWstandard iterator type of firstW and lastW.
Parameters
firstXiterator pointing to the first item in the x-dataset to use $ x_1 $
lastXiterator pointing behind the last item in the x-dataset to use $ x_N $
firstYiterator pointing to the first item in the y-dataset to use $ y_1 $
lastYiterator pointing behind the last item in the y-dataset to use $ y_N $
firstWiterator pointing to the first item in the weight-dataset to use $ w_1 $
lastWiterator pointing behind the last item in the weight-dataset to use $ w_N $
ffunction $ f(x) $, result of a fit to the data
fWeightDataToWian optional function, which is applied to the data from firstW ... lastW to convert them to weight, i.e. wi=fWeightDataToWi(*itW) e.g. if you use data used to draw error bars, you can use jkqtp_inversePropSaveDefault(). The default is jkqtp_identity(), which just returns the values. In the case of jkqtp_inversePropSaveDefault(), a datapoint x,y, has a large weight, if it's error is small and in the case if jkqtp_identity() it's weight is directly proportional to the given value.
Returns
weighted coeffcicient of determination

\[ R^2=1-\frac{\sum_iw_i^2\bigl[y_i-f(x_i)\bigr]^2}{\sum_iw_i^2\bigl[y_i-\overline{y}\bigr]^2} \]

where

\[ \overline{y}=\frac{1}{N}\cdot\sum_iw_iy_i \]

with

\[ \sum_iw_i=1 \]

See also
https://en.wikipedia.org/wiki/Coefficient_of_determination

◆ jkqtpstatWeightedRegression()

template<class InputItX , class InputItY , class InputItW >
void jkqtpstatWeightedRegression ( JKQTPStatRegressionModelType  type,
InputItX  firstX,
InputItX  lastX,
InputItY  firstY,
InputItY  lastY,
InputItW  firstW,
InputItW  lastW,
double &  coeffA,
double &  coeffB,
bool  fixA = false,
bool  fixB = false,
std::function< double(double)>  fWeightDataToWi = &jkqtp_identity<double> 
)
inline

calculate the robust linear regression coefficients for a given data range firstX / firstY ... lastX / lastY where the model is defined by type So this function solves the Lp-norm optimization problem:

\[ (a^\ast, b^\ast)=\mathop{\mathrm{arg\;min}}\limits_{a,b}\sum\limits_iw_i^2\left(y_i-f_{\text{type}}(x_i,a,b)\right)^2 \]

by reducing it to a linear fit by transforming x- and/or y-data

Template Parameters
InputItXstandard iterator type of firstX and lastX.
InputItYstandard iterator type of firstY and lastY.
InputItWstandard iterator type of firstW and lastW.
Parameters
typemodel to be fitted
firstXiterator pointing to the first item in the x-dataset to use $ x_1 $
lastXiterator pointing behind the last item in the x-dataset to use $ x_N $
firstYiterator pointing to the first item in the y-dataset to use $ y_1 $
lastYiterator pointing behind the last item in the y-dataset to use $ y_N $
firstWiterator pointing to the first item in the weight-dataset to use $ w_1 $
lastWiterator pointing behind the last item in the weight-dataset to use $ w_N $
[in,out]coeffAreturns the offset of the linear model
[in,out]coeffBreturns the slope of the linear model
fixAif true, the offset coefficient $ a $ is not determined by the fit, but the value provided in coeffA is used
fixBif true, the slope coefficient $ b $ is not determined by the fit, but the value provided in coeffB is used
fWeightDataToWian optional function, which is applied to the data from firstW ... lastW to convert them to weight, i.e. wi=fWeightDataToWi(*itW) e.g. if you use data used to draw error bars, you can use jkqtp_inversePropSaveDefault(). The default is jkqtp_identity(), which just returns the values. In the case of jkqtp_inversePropSaveDefault(), a datapoint x,y, has a large weight, if it's error is small and in the case if jkqtp_identity() it's weight is directly proportional to the given value.

This function computes internally first transforms the data, as appropriate to fit the model defined by type and then calls jkqtpstatLinearWeightedRegression() to obtain the parameters. The output parameters are transformed, so they can be used with jkqtpStatGenerateRegressionModel() to generate a functor that evaluates the model

See also
JKQTPStatRegressionModelType, jkqtpStatGenerateRegressionModel(), jkqtpstatLinearWeightedRegression(), jkqtpStatGenerateTransformation()

◆ jkqtpstatWeightedSumOfDeviations()

template<class InputItX , class InputItY , class InputItW >
double jkqtpstatWeightedSumOfDeviations ( InputItX  firstX,
InputItX  lastX,
InputItY  firstY,
InputItY  lastY,
InputItW  firstW,
InputItW  lastW,
std::function< double(double)>  f,
std::function< double(double)>  fWeightDataToWi = &jkqtp_identity<double> 
)
inline

calculates the weighted sum of deviations $ \chi^2 $ for a set of measurements $ (x_i,y_i,w_i) $ with a fit function $ f(x) $

Template Parameters
InputItXstandard iterator type of firstX and lastX.
InputItYstandard iterator type of firstY and lastY.
InputItWstandard iterator type of firstW and lastW.
Parameters
firstXiterator pointing to the first item in the x-dataset to use $ x_1 $
lastXiterator pointing behind the last item in the x-dataset to use $ x_N $
firstYiterator pointing to the first item in the y-dataset to use $ y_1 $
lastYiterator pointing behind the last item in the y-dataset to use $ y_N $
firstWiterator pointing to the first item in the weight-dataset to use $ w_1 $
lastWiterator pointing behind the last item in the weight-dataset to use $ w_N $
ffunction $ f(x) $, result of a fit to the data
fWeightDataToWian optional function, which is applied to the data from firstW ... lastW to convert them to weight, i.e. wi=fWeightDataToWi(*itW) e.g. if you use data used to draw error bars, you can use jkqtp_inversePropSaveDefault(). The default is jkqtp_identity(), which just returns the values. In the case of jkqtp_inversePropSaveDefault(), a datapoint x,y, has a large weight, if it's error is small and in the case if jkqtp_identity() it's weight is directly proportional to the given value.
Returns
weighted sum of deviations

\[ \chi^2=\sum_iw_i^2\cdot\bigl[y_i-f(x_i)\bigr]^2 \]

See also
https://en.wikipedia.org/wiki/Reduced_chi-squared_statistic