ningshuxia
2022-12-12 e78f5936fee9ab4fff600515bb20a41a28f329c4
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
// <copyright file="ExcelFunctions.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
 
using System;
using IStation.Numerics.Distributions;
using IStation.Numerics.Statistics;
 
#if !NETSTANDARD1_3
using System.Runtime;
#endif
 
// ReSharper disable InconsistentNaming
 
namespace IStation.Numerics
{
    /// <summary>
    /// Collection of functions equivalent to those provided by Microsoft Excel
    /// but backed instead by Math.NET Numerics.
    /// We do not recommend to use them except in an intermediate phase when
    /// porting over solutions previously implemented in Excel.
    /// </summary>
    public static class ExcelFunctions
    {
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public static double NormSDist(double z)
        {
            return Normal.CDF(0d, 1d, z);
        }
 
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public static double NormSInv(double probability)
        {
            return Normal.InvCDF(0d, 1d, probability);
        }
 
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public static double NormDist(double x, double mean, double standardDev, bool cumulative)
        {
            return cumulative ? Normal.CDF(mean, standardDev, x) : Normal.PDF(mean, standardDev, x);
        }
 
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public static double NormInv(double probability, double mean, double standardDev)
        {
            return Normal.InvCDF(mean, standardDev, probability);
        }
 
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public static double TDist(double x, int degreesFreedom, int tails)
        {
            switch (tails)
            {
                case 1:
                    return 1d - StudentT.CDF(0d, 1d, degreesFreedom, x);
                case 2:
                    return 1d - StudentT.CDF(0d, 1d, degreesFreedom, x) + StudentT.CDF(0d, 1d, degreesFreedom, -x);
                default:
                    throw new ArgumentOutOfRangeException(nameof(tails));
            }
        }
 
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public static double TInv(double probability, int degreesFreedom)
        {
            return -StudentT.InvCDF(0d, 1d, degreesFreedom, probability/2);
        }
 
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public static double FDist(double x, int degreesFreedom1, int degreesFreedom2)
        {
            return 1d - FisherSnedecor.CDF(degreesFreedom1, degreesFreedom2, x);
        }
 
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public static double FInv(double probability, int degreesFreedom1, int degreesFreedom2)
        {
            return FisherSnedecor.InvCDF(degreesFreedom1, degreesFreedom2, 1d - probability);
        }
 
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public static double BetaDist(double x, double alpha, double beta)
        {
            return Beta.CDF(alpha, beta, x);
        }
 
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public static double BetaInv(double probability, double alpha, double beta)
        {
            return Beta.InvCDF(alpha, beta, probability);
        }
 
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public static double GammaDist(double x, double alpha, double beta, bool cumulative)
        {
            return cumulative ? Gamma.CDF(alpha, 1/beta, x) : Gamma.PDF(alpha, 1/beta, x);
        }
 
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public static double GammaInv(double probability, double alpha, double beta)
        {
            return Gamma.InvCDF(alpha, 1/beta, probability);
        }
 
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public static double Quartile(double[] array, int quant)
        {
            switch (quant)
            {
                case 0:
                    return ArrayStatistics.Minimum(array);
                case 1:
                    return array.QuantileCustom(0.25, QuantileDefinition.Excel);
                case 2:
                    return array.QuantileCustom(0.5, QuantileDefinition.Excel);
                case 3:
                    return array.QuantileCustom(0.75, QuantileDefinition.Excel);
                case 4:
                    return ArrayStatistics.Maximum(array);
                default:
                    throw new ArgumentOutOfRangeException(nameof(quant));
            }
        }
 
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public static double Percentile(double[] array, double k)
        {
            return array.QuantileCustom(k, QuantileDefinition.Excel);
        }
 
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public static double PercentRank(double[] array, double x)
        {
            return array.QuantileRank(x, RankDefinition.Min);
        }
    }
}
 
// ReSharper restore InconsistentNaming