zhao
2021-07-07 2fdf959ac739edd6de84aa8053b8b9683dce8e8b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Licensed to the Apache Software Foundation (ASF) Under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for Additional information regarding copyright ownership.
* The ASF licenses this file to You Under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed Under the License is distributed on an "AS Is" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations Under the License.
*/
/*
 * Created on May 30, 2005
 *
 */
namespace HH.WMS.Utils.NPOI.SS.Formula.Functions
{
    using HH.WMS.Utils.NPOI.Util;
    using System;
 
    /**
     * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
     *
     * Library for common statistics functions
     */
    public class StatsLib
    {
 
        private StatsLib() { }
 
 
        /**
         * returns the mean of deviations from mean.
         * @param v
         */
        public static double avedev(double[] v)
        {
            double r = 0;
            double m = 0;
            double s = 0;
            for (int i = 0, iSize = v.Length; i < iSize; i++)
            {
                s += v[i];
            }
            m = s / v.Length;
            s = 0;
            for (int i = 0, iSize = v.Length; i < iSize; i++)
            {
                s += Math.Abs(v[i] - m);
            }
            r = s / v.Length;
            return r;
        }
 
        public static double stdev(double[] v)
        {
            double r = double.NaN;
            if (v != null && v.Length > 1)
            {
                r = Math.Sqrt(devsq(v) / (v.Length - 1));
            }
            return r;
        }
        public static double var(double[] v)
        {
            double r = Double.NaN;
            if (v != null && v.Length > 1)
            {
                r = devsq(v) / (v.Length - 1);
            }
            return r;
        }
 
        public static double varp(double[] v)
        {
            double r = Double.NaN;
            if (v != null && v.Length > 1)
            {
                r = devsq(v) / v.Length;
            }
            return r;
        }
        /**
         * if v Is zero Length or Contains no duplicates, return value
         * Is double.NaN. Else returns the value that occurs most times
         * and if there Is a tie, returns the first such value. 
         * @param v
         */
        public static double mode(double[] v)
        {
            double r = double.NaN;
 
            // very naive impl, may need to be optimized
            if (v != null && v.Length > 1)
            {
                int[] Counts = new int[v.Length];
                Arrays.Fill(Counts, 1);
                for (int i = 0, iSize = v.Length; i < iSize; i++)
                {
                    for (int j = i + 1, jSize = v.Length; j < jSize; j++)
                    {
                        if (v[i] == v[j]) Counts[i]++;
                    }
                }
                double maxv = 0;
                int maxc = 0;
                for (int i = 0, iSize = Counts.Length; i < iSize; i++)
                {
                    if (Counts[i] > maxc)
                    {
                        maxv = v[i];
                        maxc = Counts[i];
                    }
                }
                r = (maxc > 1) ? maxv : double.NaN; // "no-dups" Check
            }
            return r;
        }
 
        public static double median(double[] v)
        {
            double r = double.NaN;
 
            if (v != null && v.Length >= 1)
            {
                int n = v.Length;
                Array.Sort(v);
                r = (n % 2 == 0)
                    ? (v[n / 2] + v[n / 2 - 1]) / 2
                    : v[n / 2];
            }
 
            return r;
        }
 
 
        public static double devsq(double[] v)
        {
            double r = double.NaN;
            if (v != null && v.Length >= 1)
            {
                double m = 0;
                double s = 0;
                int n = v.Length;
                for (int i = 0; i < n; i++)
                {
                    s += v[i];
                }
                m = s / n;
                s = 0;
                for (int i = 0; i < n; i++)
                {
                    s += (v[i] - m) * (v[i] - m);
                }
 
                r = (n == 1)
                        ? 0
                        : s;
            }
            return r;
        }
 
        /*
         * returns the kth largest element in the array. Duplicates
         * are considered as distinct values. Hence, eg.
         * for array {1,2,4,3,3} & k=2, returned value Is 3.
         * <br/>
         * k <= 0 & k >= v.Length and null or empty arrays
         * will result in return value double.NaN
         * @param v
         * @param k
         */
        public static double kthLargest(double[] v, int k)
        {
            double r = double.NaN;
            k--; // since arrays are 0-based
            if (v != null && v.Length > k && k >= 0)
            {
                Array.Sort(v);
                r = v[v.Length - k - 1];
            }
            return r;
        }
 
        /*
         * returns the kth smallest element in the array. Duplicates
         * are considered as distinct values. Hence, eg.
         * for array {1,1,2,4,3,3} & k=2, returned value Is 1.
         * <br/>
         * k <= 0 & k >= v.Length or null array or empty array
         * will result in return value double.NaN
         * @param v
         * @param k
         */
        public static double kthSmallest(double[] v, int k)
        {
            double r = double.NaN;
            k--; // since arrays are 0-based
            if (v != null && v.Length > k && k >= 0)
            {
                Array.Sort(v);
                r = v[k];
            }
            return r;
        }
    }
}