Skip to content

Commit

Permalink
Methods to calculate MSE, NMSE, and R2
Browse files Browse the repository at this point in the history
  • Loading branch information
gkronber committed May 7, 2024
1 parent ec00dbe commit da3c05a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion HEAL.NonlinearRegression.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private static void Evaluate(EvalOptions options) {
if (options.Likelihood == LikelihoodEnum.Gaussian) {
var yPred = nlr.Predict(x);
var ssr = Util.SSR(y, yPred);
var nmse = ssr / y.Length / Util.Variance(y);
var nmse = Util.NMSE(y, yPred);
var mae = Util.MAE(y, yPred);
System.Console.WriteLine($"SSR: {ssr:g6} MSE: {ssr / y.Length:g6} RMSE: {Math.Sqrt(ssr / y.Length):g6} NMSE: {nmse:g6} R2: {1 - nmse:g4} MAE: {mae:g4} LogLik: {logLik:g6} AIC: {nlr.AIC:f2} AICc: {aicc:f2} BIC: {bic:f2} DL: {dl:f2} DL_lattice: {dlLattice:f2} neg. Evidence: {evidence:f2} DoF: {p.Length} m: {m}");
}
Expand Down
6 changes: 6 additions & 0 deletions HEAL.NonlinearRegression/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public static double SSR(double[] y, double[] yPred) {
return ssr;
}

public static double MSE(double[] y, double[] yPred) => SSR(y, yPred) / y.Length;
public static double NMSE(double[] y, double[] yPred) => MSE(y, yPred) / Util.Variance(y);
public static double R2(double[] y, double[] yPred) => 1.0 - NMSE(y, yPred);


public static double MAE(double[] y, double[] yPred) {
if (y.Length != yPred.Length) throw new ArgumentException("arrays must have the same length");
var sae = 0.0;
Expand Down Expand Up @@ -45,5 +50,6 @@ public static double[][] ToColumns(double[,] x) {
}
return xc;
}

}
}

0 comments on commit da3c05a

Please sign in to comment.