Esempio n. 1
0
        public void testGammaFunction()
        {
            // Testing Gamma function

            double expected   = 0.0;
            double calculated = GammaFunction.logValue(1);

            if (Math.Abs(calculated) > 1.0e-15)
            {
                Assert.Fail("GammaFunction(1)\n"
                            + "    calculated: " + calculated + "\n"
                            + "    expected:   " + expected);
            }

            for (int i = 2; i < 9000; i++)
            {
                expected  += Math.Log(i);
                calculated = GammaFunction.logValue((i + 1));
                if (Math.Abs(calculated - expected) / expected > 1.0e-9)
                {
                    Assert.Fail("GammaFunction(" + i + ")\n"
                                + "    calculated: " + calculated + "\n"
                                + "    expected:   " + expected + "\n"
                                + "    rel. error: "
                                + Math.Abs(calculated - expected) / expected);
                }
            }
        }
        public double Evaluate(double a, double b)
        {
            var gamma  = new GammaFunction();
            var result = (gamma.EvaluateWithLanczos(a) * gamma.EvaluateWithLanczos(b)) / (gamma.EvaluateWithLanczos(a + b));

            return(result);
        }
        public override double GetPDF(double x)
        {
            double nu = mDegreesOfFreedom;
            double v1 = GammaFunction.GetGamma((nu + 1.0) / 2.0);
            double v2 = GammaFunction.GetGamma(nu / 2.0);

            return(v1 * System.Math.Pow(1 + x * x / nu, -(nu + 1.0) / 2.0) / (System.Math.Sqrt(nu * System.Math.PI) * v2));
        }
Esempio n. 4
0
        //private IncompleteBetaFunctionFraction inverseFraction;

        /// Constructor method.
        /// @param a1 double
        /// @param a2 double
        public IncompleteBetaFunction(double a1, double a2)
        {
            _alpha1  = a1;
            _alpha2  = a2;
            _logNorm = GammaFunction.LogGamma(_alpha1 + _alpha2)
                       - GammaFunction.LogGamma(_alpha1)
                       - GammaFunction.LogGamma(_alpha2);
        }
        public static double GetPDF(double x, int df)
        {
            double nu = df;
            double v1 = GammaFunction.GetGamma((nu + 1.0) / 2.0);
            double v2 = GammaFunction.GetGamma(nu / 2.0);

            return(v1 * System.Math.Pow(1 + x * x / nu, -(nu + 1.0) / 2.0) / (System.Math.Sqrt(nu * System.Math.PI) * v2));
        }
Esempio n. 6
0
 /// Assigns new values to the parameters.
 /// This method assumes that the parameters have been already checked.
 private void DefineParameters(double shape1, double shape2)
 {
     _alpha1 = shape1;
     _alpha2 = shape2;
     _norm   = GammaFunction.LogBeta(_alpha1, _alpha2);
     _gamma1 = null;
     _gamma2 = null;
     _incompleteBetaFunction = null;
 }
        public void When_Solve_Gamma_Function_With_Lanczos_Approximation()
        {
            var gammaFunction = new GammaFunction();
            var firstResult   = gammaFunction.EvaluateWithLanczos(2.5);
            var secondResult  = gammaFunction.EvaluateWithLanczos(-0.5);

            Assert.Equal(1.3293403881791388, firstResult);
            Assert.Equal(-3.5449077018110287, secondResult);
        }
Esempio n. 8
0
        public IActionResult ComputeGammaFunction([FromBody] GetGammaRequest request)
        {
            var gamma  = new GammaFunction();
            var result = gamma.EvaluateWithLanczos(request.A);

            return(new OkObjectResult(new FunctionResponse {
                Value = result
            }));
        }
Esempio n. 9
0
        public void testGammaValues()
        {
            // Testing Gamma values

            // reference results are calculated with R
            double[][] tasks =
            {
                new double[3] {
                    0.0001, 9999.422883231624, 1e3
                },
                new double[3] {
                    1.2, 0.9181687423997607, 1e3
                },
                new double[3] {
                    7.3, 1271.4236336639089586, 1e3
                },
                new double[3] {
                    -1.1, 9.7148063829028946, 1e3
                },
                new double[3] {
                    -4.001, -41.6040228304425312, 1e3
                },
                new double[3] {
                    -4.999, -8.347576090315059, 1e3
                },
                new double[3] {
                    -19.000001, 8.220610833201313e-12, 1e8
                },
                new double[3] {
                    -19.5, 5.811045977502255e-18, 1e3
                },
                new double[3] {
                    -21.000001, 1.957288098276488e-14, 1e8
                },
                new double[3] {
                    -21.5, 1.318444918321553e-20, 1e6
                }
            };

            for (int i = 0; i < tasks.Length; ++i)
            {
                double x          = tasks[i][0];
                double expected   = tasks[i][1];
                double calculated = GammaFunction.value(x);
                double tol        = tasks[i][2] * Const.QL_EPSILON * Math.Abs(expected);

                if (Math.Abs(calculated - expected) > tol)
                {
                    Assert.Fail("GammaFunction(" + x + ")\n"
                                + "    calculated: " + calculated + "\n"
                                + "    expected:   " + expected + "\n"
                                + "    rel. error: "
                                + Math.Abs(calculated - expected) / expected);
                }
            }
        }
Esempio n. 10
0
        public void FailWhenGivenANegativeInteger()
        {
            var function = new GammaFunction();

            var inputs = function.GetInputs();

            Assert.Throws <ArgumentException>(() =>
            {
                inputs[0].Value = -5;
            });
        }
Esempio n. 11
0
        public double Pdf(double x)
        {
            double df2 = (df + 1) / 2;

            // Ln( Г((df + 1) / 2) / Г(df / 2) )
            double term1 = GammaFunction.LogValue(df2) - GammaFunction.LogValue(df / 2);

            // Ln( (1 + x^2 / df) ^ (-(df + 1) / 2) )
            double term2 = Log(1 + x.Sqr() / df) * -df2;

            return(Exp(term1 + term2) / Sqrt(PI * df));
        }
        public void SuccessfullySetFunctionInfo()
        {
            var function = new GammaFunction();

            Assert.NotNull(function.FunctionInfo);
            Assert.Equal("Gamma", function.FunctionInfo.Name);
            Assert.Equal(new Version("1.0.0"), function.FunctionInfo.Version);
            Assert.Equal("Apply the gamma function to a number.", function.FunctionInfo.Description);
            Assert.Collection(function.FunctionInfo.Tags,
                              i => Assert.Equal("algebra", i),
                              i => Assert.Equal("gamma", i));
        }
Esempio n. 13
0
        /// Assigns new degrees of freedom to the receiver.
        /// Compute the norm of the distribution after a change of parameters.
        /// @param n1 int	first degree of freedom
        /// @param n2 int	second degree of freedom
        public void DefineParameters(int n1, int n2)
        {
            _dof1 = n1;
            _dof2 = n2;
            double nn1 = 0.5 * n1;
            double nn2 = 0.5 * n2;

            _norm = nn1 * Math.Log(n1) + nn2 * Math.Log(n2)
                    - GammaFunction.LogBeta(nn1, nn2);
            _incompleteBetaFunction = null;
            _chiSquareDistribution1 = null;
            _chiSquareDistribution2 = null;
        }
Esempio n. 14
0
        public void GammaFunctionValue()
        {
            for (int i = 0; i < knownX.Length; i++)
            {
                double expected = knownY[i];
                double actual   = GammaFunction.Value(knownX[i]);

                output.WriteLine($"X = {knownX[i]}");
                output.WriteLine($"Expected = {expected}");
                output.WriteLine($"Actual   = {actual}");
                output.WriteLine("");

                Assert.True(Math.Abs(expected - actual) < expected * 0.0001);
            }
        }
Esempio n. 15
0
        public void Factorial()
        {
            double actual = 1.0;

            for (int n = 1; n <= 30; n++)
            {
                actual *= n;
                double expected = GammaFunction.Value(n + 1);

                output.WriteLine($"N = {n}");
                output.WriteLine($"Expected = {expected}");
                output.WriteLine($"Actual   = {actual}");

                Assert.True(Math.Abs(expected - actual) < expected * 0.0001);
            }
        }
Esempio n. 16
0
        public void GammaFunctionLogValue()
        {
            var comparer = new AbsoluteEqualityComparer(0.0001);

            for (int i = 0; i < knownX.Length; i++)
            {
                double expected = Math.Log(knownY[i]);
                double actual   = GammaFunction.LogValue(knownX[i]);

                output.WriteLine($"X = {knownX[i]}");
                output.WriteLine($"Expected = {expected}");
                output.WriteLine($"Actual   = {actual}");
                output.WriteLine("");

                Assert.Equal(expected, actual, comparer);
            }
        }
Esempio n. 17
0
 /// Assigns new values to the parameters.
 /// This method assumes that the parameters have been already checked.
 public void DefineParameters(double shape, double scale)
 {
     _alpha = shape;
     _beta  = scale;
     _norm  = Math.Log(_beta) * _alpha + GammaFunction.LogGamma(_alpha);
     if (_alpha < 1)
     {
         _b = (Math.E + _alpha) / Math.E;
     }
     else if (_alpha > 1)
     {
         _a = Math.Sqrt(2 * _alpha - 1);
         _b = _alpha - Math.Log(4.0);
         _q = _alpha + 1 / _a;
         _d = 1 + Math.Log(4.5);
     }
     _incompleteGammaFunction = null;
 }
Esempio n. 18
0
        public void SuccessfullyReturnValueGivenAPositiveInteger()
        {
            var function = new GammaFunction();

            var inputs = function.GetInputs();

            Assert.Single(inputs);

            inputs[0].Value = 5;

            var result = function.Calculate(inputs);

            Assert.NotNull(result);
            Assert.Collection(result,
                              i =>
            {
                Assert.Equal(typeof(double), i.Value.GetType());
                Assert.Equal(24, TypeConverter.ToObject <int>(i.Value));
            });
        }
Esempio n. 19
0
        public void SuccessfullyReturnNaNGivenZero()
        {
            var function = new GammaFunction();

            var inputs = function.GetInputs();

            Assert.Single(inputs);

            inputs[0].Value = 0;

            var result = function.Calculate(inputs);

            Assert.NotNull(result);
            Assert.Collection(result,
                              i =>
            {
                Assert.Equal(typeof(double), i.Value.GetType());
                Assert.True(Double.IsNaN(TypeConverter.ToObject <double>(i.Value)));
            });
        }
Esempio n. 20
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(GammaFunction obj) {
   return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
 }
Esempio n. 21
0
 /// Constructor method.
 public IncompleteGammaFunction(double a)
 {
     _alpha         = a;
     _alphaLogGamma = GammaFunction.LogGamma(_alpha);
 }
Esempio n. 22
0
 /// @param n int	degree of freedom
 public void DefineParameters(int n)
 {
     _dof  = n;
     _norm = -(Math.Log(_dof) * 0.5
               + GammaFunction.LogBeta(_dof * 0.5, 0.5));
 }
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(GammaFunction obj)
 {
     return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr);
 }