Files
jube-fork/Accord.Statistics/Testing/Contingency/AverageKappaTest.cs
richard-churchman cc0a2bee21 Extensive modifications to the serialisation of Neural Network models to support Newtonsoft Json.net rather than Binary Serializer.
Included open-source Accord.net library code natively to the solution given the deprecated state of that project.  Accord.net will be supported inside Jube for own needs (such as upgrading to .Net 7+).

Accord.net libraries modified to compile to .Net 6.

Modified Accord.net libraries to better support Newtonsoft Json.net serialisation.

Included annotations in the Accord.net libraries to house important dependency statistics. Serialised json can be used independently of Jube to recall a Neural Network.

Removal of Binary Serializer totally as a precursor to the projects support of .Net 7+.

Extensive bug fixing in crucial functions of Neural Network training and recall.  While this part of the system remains in Alpha, it is substantially more fit for purpose.
2023-12-24 10:22:22 +01:00

155 lines
4.8 KiB
C#

// Accord Statistics Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2017
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
namespace Accord.Statistics.Testing
{
using System;
using Accord.Statistics.Analysis;
using Accord.Compat;
/// <summary>
/// Kappa Test for multiple contingency tables.
/// </summary>
///
/// <remarks>
/// <para>
/// The multiple-matrix Kappa test tries to assert whether the Kappa measure
/// of many contingency tables, each of which created by a different rater
/// or classification model, differs significantly. The computations are
/// based on the pages 607, 608 of (Fleiss, 2003).</para>
///
/// <para>
/// This is a <see cref="ChiSquareTest">Chi-square kind of test</see>.</para>
///
/// <para>
/// References:
/// <list type="bullet">
/// <item><description>J. L. Fleiss. Statistical methods for rates and proportions.
/// Wiley-Interscience; 3rd edition (September 5, 2003) </description></item>
/// </list></para>
/// </remarks>
///
/// <seealso cref="ChiSquareTest"/>
///
[Serializable]
public class AverageKappaTest : ChiSquareTest
{
/// <summary>
/// Gets the overall Kappa value
/// for the many contingency tables.
/// </summary>
///
public double OverallKappa { get; private set; }
/// <summary>
/// Gets the overall Kappa variance
/// for the many contingency tables.
/// </summary>
///
public double OverallVariance { get; private set; }
/// <summary>
/// Gets the variance for each kappa value.
/// </summary>
///
public double[] Variances { get; private set; }
/// <summary>
/// Gets the kappa for each contingency table.
/// </summary>
///
public double[] EstimatedValues { get; private set; }
/// <summary>
/// Creates a new multiple table Kappa test.
/// </summary>
///
/// <param name="kappas">The kappa values.</param>
/// <param name="variances">The variance for each kappa value.</param>
///
public AverageKappaTest(double[] kappas, double[] variances)
{
if (kappas.Length != variances.Length)
throw new DimensionMismatchException("variances",
"The number of variance estimates and kappa estimates must match.");
this.Compute(kappas, variances);
}
/// <summary>
/// Creates a new multiple table Kappa test.
/// </summary>
///
/// <param name="matrices">The contingency tables.</param>
///
public AverageKappaTest(GeneralConfusionMatrix[] matrices)
{
double[] kappas = new double[matrices.Length];
double[] variances = new double[matrices.Length];
for (int i = 0; i < matrices.Length; i++)
{
kappas[i] = matrices[i].Kappa;
variances[i] = matrices[i].Variance;
}
this.Compute(kappas, variances);
}
/// <summary>
/// Computes the multiple matrix Kappa test.
/// </summary>
///
protected void Compute(double[] kappas, double[] variances)
{
this.EstimatedValues = kappas;
this.Variances = variances;
double num = 0;
for (int i = 0; i < kappas.Length; i++)
num += kappas[i] / variances[i];
double den = 0;
for (int i = 0; i < variances.Length; i++)
den += 1.0 / variances[i];
this.OverallKappa = num / den;
this.OverallVariance = den;
double chiSquare = 0;
for (int i = 0; i < kappas.Length; i++)
chiSquare += Math.Pow(kappas[i] - OverallKappa, 2) / variances[i];
int df = kappas.Length - 1;
base.Compute(chiSquare, df);
}
}
}