001/******************************************************************************* 002 * This software is provided as a supplement to the authors' textbooks on digital 003 * image processing published by Springer-Verlag in various languages and editions. 004 * Permission to use and distribute this software is granted under the BSD 2-Clause 005 * "Simplified" License (see http://opensource.org/licenses/BSD-2-Clause). 006 * Copyright (c) 2006-2016 Wilhelm Burger, Mark J. Burge. All rights reserved. 007 * Visit http://imagingbook.com for additional details. 008 *******************************************************************************/ 009 010package imagingbook.lib.math; 011 012//import org.apache.commons.math3.linear.MatrixUtils; 013import org.apache.commons.math3.stat.correlation.Covariance; 014 015 016public abstract class Statistics { 017 018 public static int getMax(int[] A) { 019 int maxVal = A[0]; 020 for (int i = 1; i < A.length; i++) { 021 if (A[i] > maxVal) 022 maxVal = A[i]; 023 } 024 return maxVal; 025 } 026 027 public static int getMin(int[] A) { 028 int minVal = A[0]; 029 for (int i = 1; i < A.length; i++) { 030 if (A[i] < minVal) 031 minVal = A[i]; 032 } 033 return minVal; 034 } 035 036 public static float getMax(float[] A) { 037 float maxVal = A[0]; 038 for (int i = 1; i < A.length; i++) { 039 if (A[i] > maxVal) 040 maxVal = A[i]; 041 } 042 return maxVal; 043 } 044 045 public static float getMin(float[] A) { 046 float minVal = A[0]; 047 for (int i = 1; i < A.length; i++) { 048 if (A[i] < minVal) 049 minVal = A[i]; 050 } 051 return minVal; 052 } 053 054 public static double getMax(double[] A) { 055 double maxVal = A[0]; 056 for (int i = 1; i < A.length; i++) { 057 if (A[i] > maxVal) 058 maxVal = A[i]; 059 } 060 return maxVal; 061 } 062 063 public static double getMin(double[] A) { 064 double minVal = A[0]; 065 for (int i = 1; i < A.length; i++) { 066 if (A[i] < minVal) 067 minVal = A[i]; 068 } 069 return minVal; 070 } 071 072 // -------------------------------------------------------------------- 073 074 //TODO: mean, variance for int, float, double arrays 075 076 077 078 079 private static boolean UseBiasCorrection = false; // we use NO bias-correction here 080 081 /** 082 * Calculates the covariance matrix for a sequence of sample vectors. 083 * Takes a sequence of n data samples, each of dimension m. 084 * The data element samples[i][j] refers to the j-th component 085 * of sample i. 086 * 087 * @param samples Array of m-dimensional vectors (double[n][m]). 088 * @return The covariance matrix (of dimension double{m][m]). 089 */ 090 public static double[][] covarianceMatrix(double[][] samples) { 091 Covariance cov = new Covariance(samples, UseBiasCorrection); 092 return cov.getCovarianceMatrix().getData(); 093 } 094 095 096 /** 097 * example from UTICS-C Appendix: 098 * N = 4 samples 099 * K = 3 dimensions 100 * @param args 101 */ 102// public static void main(String[] args) { 103// 104// // example: n = 4 samples of dimension m = 3: 105// // samples[i][j], i = column (sample index), j = row (dimension index). 106// double[][] samples = { 107// {75, 37, 12}, // i = 0 108// {41, 27, 20}, // i = 1 109// {93, 81, 11}, // i = 2 110// {12, 48, 52} // i = 3 111// }; 112// 113// // covariance matrix Cov (3x3) 114// double[][] cov = covarianceMatrix(samples); 115// System.out.println("cov = " + Matrix.toString(cov)); 116// 117// System.out.println(); 118// 119// double[][] icov = Matrix.inverse(cov); 120// System.out.println("icov = " + Matrix.toString(icov)); 121// 122// double trace = MatrixUtils.createRealMatrix(cov).getTrace(); 123// System.out.println("trace(cov) = " + trace); 124// 125//// double trace2 = Matrix.trace(cov); 126//// System.out.println("trace2(cov) = " + trace2); 127// 128// double Fnorm = MatrixUtils.createRealMatrix(cov).getFrobeniusNorm(); 129// System.out.println("Fnorm(cov) = " + Fnorm); 130// 131//// double Fnorm2 = Matrix.froebeniusNorm(cov); 132//// System.out.println("Fnorm2(cov) = " + Fnorm2); 133// } 134 135/* Results (bias-corrected): 136cov = {{1296.250, 442.583, -627.250}, 137{442.583, 550.250, -70.917}, 138{-627.250, -70.917, 370.917}} 139 140icov = {{0.024, -0.014, 0.038}, 141{-0.014, 0.011, -0.022}, 142{0.038, -0.022, 0.063}} 143*/ 144 145/* verified with Mathematica 146X1 = {75, 37, 12}; X2 = {41, 27, 20}; X3 = {93, 81, 11}; X4 = {12, 48, 52}; 147samples = {X1, X2, X3, X4} 148N[Covariance[samples]] 149-> {{1296.25, 442.583, -627.25}, {442.583, 550.25, -70.9167}, {-627.25, -70.9167, 370.917}} 150*/ 151 152/* Results (NON bias-corrected): 153cov = {{972.188, 331.938, -470.438}, 154{331.938, 412.688, -53.188}, 155{-470.438, -53.188, 278.188}} 156 157icov = {{0.032, -0.019, 0.051}, 158{-0.019, 0.014, -0.030}, 159{0.051, -0.030, 0.083}} 160*/ 161 162}