#region License Information /* HeuristicLab * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using HeuristicLab.Common; using HeuristicLab.Data; using System; using System.Linq; namespace HeuristicLab.Problems.CharacteristicAnalysis { public static class DoubleMatrixCharacteristicCalculator { public static double CoeffVariation(DoubleMatrix m) { var avg = m.Average(); var stdDev = m.StandardDeviation(); return stdDev / avg; } public static double Sparsity(DoubleMatrix m) { return m.Count(x => x == 0) / (double)(m.Rows * m.Columns); } public static double Disparity(DoubleMatrix m) { if (m.Rows != m.Columns) throw new ArgumentException("not a quadratic matrix", "m"); var n = m.Rows; var avg = m.Sum() / n; var disparity = 0.0; for (var i = 0; i < n; i++) { var tmp = 0.0; for (var j = 0; j < n; j++) { tmp += m[i, j]; } disparity += (tmp - avg) * (tmp - avg); } return Math.Sqrt(disparity / n) / avg; } public static double Skewness(DoubleMatrix m) { double mean = 0, variance = 0, skewness = 0, kurtosis = 0; alglib.basestat.samplemoments(m.ToArray(), m.Rows * m.Columns, ref mean, ref variance, ref skewness, ref kurtosis); return skewness; } public static double Asymmetry(DoubleMatrix m) { if (m.Rows != m.Columns) throw new ArgumentException("not a quadratic matrix", "m"); var n = m.Rows; var count = 0; for (var i = 0; i < n; i++) { for (var j = 0; j < i; j++) { var v = Math.Abs(m[i, j] - m[j, i]); if (v > 0) { count++; } } } return count / (n * (n - 1) / 2.0); } } }