Skip to content

Commit

Permalink
Implement the standard deviation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbaudin47 committed Aug 22, 2023
1 parent a1f37df commit da8772f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,17 @@ Scalar LinearModelAnalysis::getFisherPValue() const
return FisherSnedecor(dofModel, dof).computeComplementaryCDF(FStatistic);
}

/* Kolmogorov-Smirnov normality test */
/* Estimator of the standard deviation */
Scalar LinearModelAnalysis::getResidualsStandardError() const
{
const Scalar sigma2 = linearModelResult_.getSampleResiduals().computeRawMoment(2)[0];
const UnsignedInteger dof = linearModelResult_.getDegreesOfFreedom();
const UnsignedInteger n = linearModelResult_.getSampleResiduals().getSize();
const Scalar stdError = std::sqrt(sigma2 * n / dof);
return stdError;
}

/* Kolmogorov-Smirnov normality test of the residuals */
TestResult LinearModelAnalysis::getNormalityTestResultKolmogorovSmirnov() const
{
// We check that residuals are centered with variance = sigma2
Expand All @@ -327,13 +337,13 @@ TestResult LinearModelAnalysis::getNormalityTestResultKolmogorovSmirnov() const
return FittingTest::Kolmogorov(residuals, dist);
}

/* Anderson-Darling normality test */
/* Anderson-Darling normality test of the residuals */
TestResult LinearModelAnalysis::getNormalityTestResultAndersonDarling() const
{
return NormalityTest::AndersonDarlingNormal(linearModelResult_.getSampleResiduals());
}

/* Chi-Squared normality test */
/* Chi-Squared normality test of the residuals */
TestResult LinearModelAnalysis::getNormalityTestResultChiSquared() const
{
// Using OT::FittingTest::ChiSquared
Expand All @@ -342,7 +352,7 @@ TestResult LinearModelAnalysis::getNormalityTestResultChiSquared() const
return FittingTest::ChiSquared(residuals, normalDistribution);
}

/* Cramer Von Mises normality test */
/* Cramer Von Mises normality test of the residuals */
TestResult LinearModelAnalysis::getNormalityTestCramerVonMises() const
{
return NormalityTest::CramerVonMisesNormal(linearModelResult_.getSampleResiduals());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,19 @@ public:
Scalar getFisherScore() const;
Scalar getFisherPValue() const;

/** Kolmogorov-Smirnov normality test */
/** Estimator of the standard deviation */
Scalar getResidualsStandardError() const;

/** Kolmogorov-Smirnov normality test of the residuals */
TestResult getNormalityTestResultKolmogorovSmirnov() const;

/** Anderson-Darling normality test */
/** Anderson-Darling normality test of the residuals */
TestResult getNormalityTestResultAndersonDarling() const;

/** Chi-Squared normality test */
/** Chi-Squared normality test of the residuals */
TestResult getNormalityTestResultChiSquared() const;

/** Cramer Von mises normality test */
/** Cramer Von mises normality test of the residuals */
TestResult getNormalityTestCramerVonMises() const;

/* [0] Draw model versus fitted values */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
import openturns.viewer as viewer

# %%
# Generation of the data set
# ~~~~~~~~~~~~~~~~~~~~~~~~~~
# Simulate the data set
# ~~~~~~~~~~~~~~~~~~~~~
#
# We first generate the data and we add noise to the output observations:

Expand Down Expand Up @@ -125,12 +125,12 @@
# Most of the variance is explained by the linear model.
# - The F-test tests if all the coefficients are simultaneously zero.
# The `Fisher-Snedecor` p-value is lower than 1%.
# The linear model assumption is accepted.
# Hence, there is at least one nonzero coefficient.
# - The normality test checks that the residuals have a normal distribution.
# The normality assumption can be rejected if the p-value is close to zero
# (say, lower than 0.05).
# The normality assumption can be rejected if the p-value is close to one
# (say, larger than 0.05).
# Based on the different statistical tests, we see that all p-values
# are larger than 0.05: the normality assumption is accepted.
# are close to zero: the normality assumption of the residuals is accepted.
#

# %%
Expand Down Expand Up @@ -187,7 +187,7 @@

# %%
# In this case, there seem to be no obvious influential outlier characterized
# by large leverage and residual values, as is also shown in the figure below:
# by large leverage and residual values.
#
# Similarly, we can also plot Cook's distances as a function of the sample
# leverages:
Expand Down
3 changes: 1 addition & 2 deletions python/src/LinearModelAnalysis.i
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def __LinearModelAnalysis_repr_html(self):
tscores = self.getCoefficientsTScores()
pValues = self.getCoefficientsPValues()
names = lmResult.getCoefficientsNames()
sigma2 = lmResult.getSampleResiduals().computeRawMoment(2)[0]
dof = lmResult.getDegreesOfFreedom()
n = lmResult.getSampleResiduals().getSize()
maximumNameLength = 20
Expand Down Expand Up @@ -78,7 +77,7 @@ def __LinearModelAnalysis_repr_html(self):

# Print statistics
html += "<ul>\n"
stdError = math.sqrt(sigma2 * n / dof)
stdError = self.getResidualsStandardError()
html += f" <li>Residual standard error: {stdError:.4e} " \
f"on {dof} degrees of freedom </li>\n"

Expand Down
20 changes: 16 additions & 4 deletions python/src/LinearModelAnalysis_doc.i.in
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ confidenceInterval : :class:`~openturns.Interval`
// ---------------------------------------------------------------------

%feature("docstring") OT::LinearModelAnalysis::getFisherScore
"Accessor to the Fisher test.
"Accessor to the Fisher statistics.

Returns
-------
Expand All @@ -101,9 +101,21 @@ fisherPValue : float

// ---------------------------------------------------------------------

%feature("docstring") OT::LinearModelAnalysis::getResidualsStandardError
"Accessor to the standard error of the residuals.

Returns
-------
stdError : float
This is the unbiased estimator :math:`\hat{\sigma}` of the standard deviation
of the Gaussian observation error of the observed output.
"

// ---------------------------------------------------------------------

%feature("docstring") OT::LinearModelAnalysis::getNormalityTestResultChiSquared
"Performs Chi-Square test.
The statistical test checks the Gaussian assumption of the model (null hypothesis).
The statistical test checks the Gaussian assumption of the residuals.

Returns
-------
Expand All @@ -124,7 +136,7 @@ the :meth:`~openturns.FittingTest.ChiSquared` to check the normality.
%feature("docstring") OT::LinearModelAnalysis::getNormalityTestResultKolmogorovSmirnov
"Performs Kolmogorov test.

Performs Kolmogorov test to check Gaussian assumption of the model (null hypothesis).
Performs Kolmogorov test to check Gaussian assumption of the residuals.

Returns
-------
Expand All @@ -140,7 +152,7 @@ We check that the residual is Gaussian thanks to :class:`~openturns.FittingTest:

%feature("docstring") OT::LinearModelAnalysis::getNormalityTestResultAndersonDarling
"Performs Anderson-Darling test.
The statistical test checks the Gaussian assumption of the model (null hypothesis).
The statistical test checks the Gaussian assumption of the residuals.

Returns
-------
Expand Down

0 comments on commit da8772f

Please sign in to comment.