[9102] | 1 | ///
|
---|
| 2 | /// This file is part of ILNumerics Community Edition.
|
---|
| 3 | ///
|
---|
| 4 | /// ILNumerics Community Edition - high performance computing for applications.
|
---|
| 5 | /// Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
|
---|
| 6 | ///
|
---|
| 7 | /// ILNumerics Community Edition is free software: you can redistribute it and/or modify
|
---|
| 8 | /// it under the terms of the GNU General Public License version 3 as published by
|
---|
| 9 | /// the Free Software Foundation.
|
---|
| 10 | ///
|
---|
| 11 | /// ILNumerics Community Edition is distributed in the hope that it will be useful,
|
---|
| 12 | /// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 13 | /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 14 | /// GNU General Public License for more details.
|
---|
| 15 | ///
|
---|
| 16 | /// You should have received a copy of the GNU General Public License
|
---|
| 17 | /// along with ILNumerics Community Edition. See the file License.txt in the root
|
---|
| 18 | /// of your distribution package. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | ///
|
---|
| 20 | /// In addition this software uses the following components and/or licenses:
|
---|
| 21 | ///
|
---|
| 22 | /// =================================================================================
|
---|
| 23 | /// The Open Toolkit Library License
|
---|
| 24 | ///
|
---|
| 25 | /// Copyright (c) 2006 - 2009 the Open Toolkit library.
|
---|
| 26 | ///
|
---|
| 27 | /// Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
| 28 | /// of this software and associated documentation files (the "Software"), to deal
|
---|
| 29 | /// in the Software without restriction, including without limitation the rights to
|
---|
| 30 | /// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
---|
| 31 | /// the Software, and to permit persons to whom the Software is furnished to do
|
---|
| 32 | /// so, subject to the following conditions:
|
---|
| 33 | ///
|
---|
| 34 | /// The above copyright notice and this permission notice shall be included in all
|
---|
| 35 | /// copies or substantial portions of the Software.
|
---|
| 36 | ///
|
---|
| 37 | /// =================================================================================
|
---|
| 38 | ///
|
---|
| 39 |
|
---|
| 40 | using System;
|
---|
| 41 | using System.Collections.Generic;
|
---|
| 42 | using System.Text;
|
---|
| 43 | using ILNumerics;
|
---|
| 44 | using ILNumerics.Exceptions;
|
---|
| 45 | using ILNumerics.Storage;
|
---|
| 46 | using ILNumerics.Misc;
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 |
|
---|
| 50 | namespace ILNumerics {
|
---|
| 51 |
|
---|
| 52 | public partial class ILMath {
|
---|
| 53 |
|
---|
| 54 | |
---|
| 55 |
|
---|
| 56 | /// <summary>
|
---|
| 57 | /// Covariance matrix of A
|
---|
| 58 | /// </summary>
|
---|
| 59 | /// <param name="A">Input vector or data matrix, samples in columns, variables in rows</param>
|
---|
| 60 | /// <param name="unbiased">[Optional] If true, calculate the best unbiased variance estimate if the observations are from a normal distribution. This normalizes by n-1 if n>1 (n = number of samples). If n == 1 normalization is always 1. If false always normalize by n.</param>
|
---|
| 61 | /// <returns>Variance of vector A/Covariance matrix of A</returns>
|
---|
| 62 | /// <remarks><para>If A is a vector <c>cov(A)</c> returns the variance of A</para>
|
---|
| 63 | /// <para>If A is a m x n matrix, where each of the n columns is an m-dimensional observation, <c>cov(A)</c> is the n x n covariance matrix.</para>
|
---|
| 64 | /// <para>The mean is removed from each column before calculating the result.</para>
|
---|
| 65 | /// </remarks>
|
---|
| 66 | public static ILRetArray<double> cov(ILInArray<double> A, bool unbiased = true) {
|
---|
| 67 | using (ILScope.Enter(A)) {
|
---|
| 68 | if (isnull(A)) {
|
---|
| 69 | throw new ILArgumentException("Parameter A must not be null");
|
---|
| 70 | }
|
---|
| 71 | if (!A.IsMatrix)
|
---|
| 72 | throw new ILArgumentException("Input array A must be a matrix (2d)");
|
---|
| 73 | if (A.IsEmpty) {
|
---|
| 74 | if (A.S[0] == 0 && A.S[1] == 0)
|
---|
| 75 | return array<double>(double.NaN, 1, 1);
|
---|
| 76 | return array<double>(double.NaN, A.S[0], A.S[0]);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | if (A.IsVector)
|
---|
| 80 | {
|
---|
| 81 | // A vector, return variance
|
---|
| 82 | int normFactor = unbiased ? (A.Size.NumberOfElements > 1 ? A.Size.NumberOfElements - 1 : 1) : A.Size.NumberOfElements;
|
---|
| 83 | ILArray<double> AnoMean = A - mean(A);
|
---|
| 84 | return sum(multiplyElem(AnoMean, AnoMean)) / (double )normFactor;
|
---|
| 85 | // return zeros<double>(A.D[0], A.D[0]);
|
---|
| 86 | }
|
---|
| 87 | else
|
---|
| 88 | {
|
---|
| 89 | int normFactor = unbiased ? (A.S[1] > 1 ? A.S[1] - 1 : 1) : A.S[1];
|
---|
| 90 | ILArray<double> AnoMean = A - mean(A, 1);
|
---|
| 91 | return multiply(AnoMean, AnoMean.T) / (double )normFactor;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | |
---|
| 96 | #region HYCALPER AUTO GENERATED CODE
|
---|
| 97 | |
---|
| 98 |
|
---|
| 99 | /// <summary>
|
---|
| 100 | /// Covariance matrix of A
|
---|
| 101 | /// </summary>
|
---|
| 102 | /// <param name="A">Input vector or data matrix, samples in columns, variables in rows</param>
|
---|
| 103 | /// <param name="unbiased">[Optional] If true, calculate the best unbiased variance estimate if the observations are from a normal distribution. This normalizes by n-1 if n>1 (n = number of samples). If n == 1 normalization is always 1. If false always normalize by n.</param>
|
---|
| 104 | /// <returns>Variance of vector A/Covariance matrix of A</returns>
|
---|
| 105 | /// <remarks><para>If A is a vector <c>cov(A)</c> returns the variance of A</para>
|
---|
| 106 | /// <para>If A is a m x n matrix, where each of the n columns is an m-dimensional observation, <c>cov(A)</c> is the n x n covariance matrix.</para>
|
---|
| 107 | /// <para>The mean is removed from each column before calculating the result.</para>
|
---|
| 108 | /// </remarks>
|
---|
| 109 | public static ILRetArray<float> cov(ILInArray<float> A, bool unbiased = true) {
|
---|
| 110 | using (ILScope.Enter(A)) {
|
---|
| 111 | if (isnull(A)) {
|
---|
| 112 | throw new ILArgumentException("Parameter A must not be null");
|
---|
| 113 | }
|
---|
| 114 | if (!A.IsMatrix)
|
---|
| 115 | throw new ILArgumentException("Input array A must be a matrix (2d)");
|
---|
| 116 | if (A.IsEmpty) {
|
---|
| 117 | if (A.S[0] == 0 && A.S[1] == 0)
|
---|
| 118 | return array<float>(float.NaN, 1, 1);
|
---|
| 119 | return array<float>(float.NaN, A.S[0], A.S[0]);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | if (A.IsVector)
|
---|
| 123 | {
|
---|
| 124 | // A vector, return variance
|
---|
| 125 | int normFactor = unbiased ? (A.Size.NumberOfElements > 1 ? A.Size.NumberOfElements - 1 : 1) : A.Size.NumberOfElements;
|
---|
| 126 | ILArray<float> AnoMean = A - mean(A);
|
---|
| 127 | return sum(multiplyElem(AnoMean, AnoMean)) / (float )normFactor;
|
---|
| 128 | // return zeros<float>(A.D[0], A.D[0]);
|
---|
| 129 | }
|
---|
| 130 | else
|
---|
| 131 | {
|
---|
| 132 | int normFactor = unbiased ? (A.S[1] > 1 ? A.S[1] - 1 : 1) : A.S[1];
|
---|
| 133 | ILArray<float> AnoMean = A - mean(A, 1);
|
---|
| 134 | return multiply(AnoMean, AnoMean.T) / (float )normFactor;
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | #endregion HYCALPER AUTO GENERATED CODE
|
---|
| 140 |
|
---|
| 141 | }
|
---|
| 142 | } |
---|