From 99abb36a8865aa02b20a8a506ef476ba880e6034 Mon Sep 17 00:00:00 2001 From: diluculo Date: Wed, 2 Jan 2019 20:13:32 +0900 Subject: [PATCH] Add Correlation matrix to ObjectiveModel. --- src/Numerics/Optimization/IObjectiveModel.cs | 15 +++++++++++++++ .../ObjectiveModels/FittingObjectiveModel.cs | 16 +++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Numerics/Optimization/IObjectiveModel.cs b/src/Numerics/Optimization/IObjectiveModel.cs index 2409dffe..ff117091 100644 --- a/src/Numerics/Optimization/IObjectiveModel.cs +++ b/src/Numerics/Optimization/IObjectiveModel.cs @@ -6,6 +6,16 @@ namespace MathNet.Numerics.Optimization { IObjectiveModel CreateNew(); + /// + /// Get the y-values of the observations. + /// + Vector ObservedY { get; } + + /// + /// Get the values of the weights for the observations. + /// + Matrix Weights { get; } + /// /// Get the y-values of the fitted model that correspond to the independent values. /// @@ -38,6 +48,11 @@ namespace MathNet.Numerics.Optimization /// Matrix Covariance { get; } + /// + /// Get the correlation matrix. + /// + Matrix Correlation { get; } + /// /// Get the number of calls to function. /// diff --git a/src/Numerics/Optimization/ObjectiveModels/FittingObjectiveModel.cs b/src/Numerics/Optimization/ObjectiveModels/FittingObjectiveModel.cs index c5558f99..f1204189 100644 --- a/src/Numerics/Optimization/ObjectiveModels/FittingObjectiveModel.cs +++ b/src/Numerics/Optimization/ObjectiveModels/FittingObjectiveModel.cs @@ -123,6 +123,11 @@ namespace MathNet.Numerics.Optimization.ObjectiveModels /// public Matrix Covariance { get; private set; } + /// + /// Get the correlation matrix. + /// + public Matrix Correlation { get; private set; } + /// /// Get the number of calls to function. /// @@ -484,18 +489,23 @@ namespace MathNet.Numerics.Optimization.ObjectiveModels EvaluateFunction(Pext); EvaluateJacobian(Pext); + // restore isBounded + this.IsBounded = (LowerBound != null || UpperBound != null); + if (Hessian == null || Residuals == null || DegreeOfFreedom < 1) { Covariance = null; + Correlation = null; return; } var covariance = Hessian.PseudoInverse() * Residuals.DotProduct(Residuals) / DegreeOfFreedom; - Covariance = covariance; - // restore isBounded - this.IsBounded = (LowerBound != null || UpperBound != null); + var correlation = covariance.Clone(); + var d = correlation.Diagonal().PointwiseSqrt(); + var dd = d.OuterProduct(d); + Correlation = correlation.PointwiseDivide(dd); return; }