Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Functions/builtin/var.cs @ 11194

Last change on this file since 11194 was 9102, checked in by gkronber, 12 years ago

#1967: ILNumerics source for experimentation

File size: 11.0 KB
RevLine 
[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
40using System;
41using System.Collections.Generic;
42using System.Text;
43using ILNumerics;
44using ILNumerics.Exceptions;
45using ILNumerics.Storage;
46using ILNumerics.Misc;
47
48
49
50namespace ILNumerics {
51
52    public partial class ILMath {
53
54
55        /// <summary>
56        /// Variance along dimension of A
57        /// </summary>
58        /// <param name="A">Input array A</param>
59        /// <param name="Weights">[Optional] Vector of scaling factors, same length as working dimension of A, default: no scaling</param>
60        /// <param name="biased">[Optional] true: apply biased normalization to result, default: false (non-biased)</param>
61        /// <param name="dim">[Optional] Index of the dimension to operate along. If omitted operates along the first non singleton dimension (i.e. != 1).</param>
62        /// <returns>Variances</returns>
63        /// <remarks><para>On scalar A a scalar 0 of the same shape as A is returned.</para>
64        /// <para>On empty A an empty array is returned, having the dimension to operate along reduced to length 1.</para>
65        /// <para>The parameters <paramref name="Weights"/>, <paramref name="biased"/> and <paramref name="dim"/> are optional.
66        /// Ommiting either one will choose its respective default value.</para>
67        /// <para> The result for <paramref name="biased"/> = true is computed by the following formula:
68        /// <code>r = (A - mean(A));
69        /// var = sum(r * r) / A.D[dim];</code>
70        /// If <paramref name="biased"/> is false (default) the normalization is done with the length of the working dimension of A as follows:
71        /// <code>r = (A - mean(A));
72        /// var = sum(r * r) / (A.D[dim] - 1); </code>
73        /// If <paramref name="Weights"/> is given, the parameter <paramref name="biased"/> is ignored.</para>
74        /// <para>If <paramref name="Weights"/> is given, the normalization is applied to r as follows:
75        /// <code>w = w / sum(w);
76        /// r = A - sum(w * A);
77        /// var = sum(w * (r * r));
78        /// </code></para></remarks>
79        public static ILRetArray<double> var(ILInArray<double> A,
80                                                            ILInArray<double> Weights = null,
81                                                            bool biased = false, int dim = -1) {
82            using (ILScope.Enter(A, Weights)) {
83                if (isnull(A))
84                    throw new ILArgumentException("input parameter A must not be null");
85                if (A.IsScalar) {
86                    return zeros<double>(A.S);
87                }
88                if (dim == -1) {
89                    dim = A.S.WorkingDimension();
90                    if (dim < 0)
91                        dim = 0;
92                }
93                if (A.IsEmpty) {
94                    int[] dims = A.S.ToIntArray();
95                    dims[dim] = 1;
96                    return zeros<double>(dims);
97                }
98                int n = A.S[dim];
99                if (isnull(Weights)) {
100                    // unweighted
101                    ILArray<double> tmp = n;
102                    if (!biased && n > 1) {
103                        tmp.a = tmp - 1;
104                    }
105                    ILArray<double> tmpM = sum(A, dim) / n;
106                    ILArray<double> AminTmpM = A - tmpM;
107                    return sum(AminTmpM * AminTmpM, dim) / tmp;
108                } else {
109                    // weighted
110                    if (!Weights.IsVector || Weights.S.NumberOfElements != n) {
111                        throw new ILArgumentException("Weights parameter must be a vector of the length of the working dimension of A");
112                    }
113                    if (any(Weights < 0)) {
114                        throw new ILArgumentException("values of Weights parameter must all be positive");
115                    }
116                    ILArray<double> locWeights;
117                    if (!A.IsMatrix) {
118                        // vector expansion currently only works for vectors on matrices
119                        ILArray<int> wdims = ones<int>(Math.Max(dim, ndims(A)));
120                        wdims[dim] = Weights.Length;
121                        locWeights = reshape(Weights, new ILSize(wdims)) / sumall(Weights);
122                        int[] repDims = A.S.ToIntArray();
123                        repDims[dim] = 1;
124                        locWeights = repmat(locWeights, repDims);
125                    }
126                    ILArray<double> r = A - sum(Weights * A, dim);
127                    return sum(Weights * (r * r), dim);
128                }
129            }
130        }
131
132#region HYCALPER AUTO GENERATED CODE
133
134        /// <summary>
135        /// Variance along dimension of A
136        /// </summary>
137        /// <param name="A">Input array A</param>
138        /// <param name="Weights">[Optional] Vector of scaling factors, same length as working dimension of A, default: no scaling</param>
139        /// <param name="biased">[Optional] true: apply biased normalization to result, default: false (non-biased)</param>
140        /// <param name="dim">[Optional] Index of the dimension to operate along. If omitted operates along the first non singleton dimension (i.e. != 1).</param>
141        /// <returns>Variances</returns>
142        /// <remarks><para>On scalar A a scalar 0 of the same shape as A is returned.</para>
143        /// <para>On empty A an empty array is returned, having the dimension to operate along reduced to length 1.</para>
144        /// <para>The parameters <paramref name="Weights"/>, <paramref name="biased"/> and <paramref name="dim"/> are optional.
145        /// Ommiting either one will choose its respective default value.</para>
146        /// <para> The result for <paramref name="biased"/> = true is computed by the following formula:
147        /// <code>r = (A - mean(A));
148        /// var = sum(r * r) / A.D[dim];</code>
149        /// If <paramref name="biased"/> is false (default) the normalization is done with the length of the working dimension of A as follows:
150        /// <code>r = (A - mean(A));
151        /// var = sum(r * r) / (A.D[dim] - 1); </code>
152        /// If <paramref name="Weights"/> is given, the parameter <paramref name="biased"/> is ignored.</para>
153        /// <para>If <paramref name="Weights"/> is given, the normalization is applied to r as follows:
154        /// <code>w = w / sum(w);
155        /// r = A - sum(w * A);
156        /// var = sum(w * (r * r));
157        /// </code></para></remarks>
158        public static ILRetArray<float> var(ILInArray<float> A,
159                                                            ILInArray<float> Weights = null,
160                                                            bool biased = false, int dim = -1) {
161            using (ILScope.Enter(A, Weights)) {
162                if (isnull(A))
163                    throw new ILArgumentException("input parameter A must not be null");
164                if (A.IsScalar) {
165                    return zeros<float>(A.S);
166                }
167                if (dim == -1) {
168                    dim = A.S.WorkingDimension();
169                    if (dim < 0)
170                        dim = 0;
171                }
172                if (A.IsEmpty) {
173                    int[] dims = A.S.ToIntArray();
174                    dims[dim] = 1;
175                    return zeros<float>(dims);
176                }
177                int n = A.S[dim];
178                if (isnull(Weights)) {
179                    // unweighted
180                    ILArray<float> tmp = n;
181                    if (!biased && n > 1) {
182                        tmp.a = tmp - 1;
183                    }
184                    ILArray<float> tmpM = sum(A, dim) / n;
185                    ILArray<float> AminTmpM = A - tmpM;
186                    return sum(AminTmpM * AminTmpM, dim) / tmp;
187                } else {
188                    // weighted
189                    if (!Weights.IsVector || Weights.S.NumberOfElements != n) {
190                        throw new ILArgumentException("Weights parameter must be a vector of the length of the working dimension of A");
191                    }
192                    if (any(Weights < 0)) {
193                        throw new ILArgumentException("values of Weights parameter must all be positive");
194                    }
195                    ILArray<float> locWeights;
196                    if (!A.IsMatrix) {
197                        // vector expansion currently only works for vectors on matrices
198                        ILArray<int> wdims = ones<int>(Math.Max(dim, ndims(A)));
199                        wdims[dim] = Weights.Length;
200                        locWeights = reshape(Weights, new ILSize(wdims)) / sumall(Weights);
201                        int[] repDims = A.S.ToIntArray();
202                        repDims[dim] = 1;
203                        locWeights = repmat(locWeights, repDims);
204                    }
205                    ILArray<float> r = A - sum(Weights * A, dim);
206                    return sum(Weights * (r * r), dim);
207                }
208            }
209        }
210
211#endregion HYCALPER AUTO GENERATED CODE
212   }
213}
Note: See TracBrowser for help on using the repository browser.