public void BinomialCreateFailsWithBadParameters(double p, int n)
 {
     var bernoulli = new Binomial(p,n);
 }
 public void CanSample()
 {
     var n = new Binomial(0.3, 5);
     n.Sample();
 }
Beispiel #3
0
    public static Distribution FromDoc(Distribution existing, DocNode doc) {
        // a single number means we want just a fixed value instead of a random distribution
        if (doc.Type == DocNodeType.Scalar) {
            var fixedValue = System.Convert.ToSingle(doc.StringValue);
            if (existing != null && existing is Range) {
                ((Range)existing).Start = fixedValue;
                ((Range)existing).End = fixedValue;
                return existing;
            } else {
                return new Range(fixedValue, fixedValue);
            }
        }

        var first = doc[0].StringValue;
        var resultType = typeof(Range);
        int startingIndex = 1;
        if (first == "vary") {
            resultType = typeof(Vary);
        } else if (first == "gaussian") {
            resultType = typeof(Gaussian);
        } else if (first == "binomial") {
            resultType = typeof(Binomial);
        } else if (first == "exponential") {
            resultType = typeof(Exponential);
        } else if (first == "range") {
            resultType = typeof(Range);
        } else {
            // we haven't found a tag we know about, so assume that the first value is a number and it's a Range
            startingIndex = 0;
        }

        // all these distributions take two floats as arguments, so let's parse those out first
        float firstNum = System.Convert.ToSingle(doc[startingIndex].StringValue);
        float secondNum = System.Convert.ToSingle(doc[startingIndex + 1].StringValue);

        // set the numbers appropriate to the class
        if (resultType == typeof(Range)) {
            if (existing != null && existing is Range) {
                ((Range)existing).Start = firstNum;
                ((Range)existing).End = secondNum;
            } else {
                existing = new Range(firstNum, secondNum);
            }
        } else if (resultType == typeof(Vary)) {
            if (existing != null && existing is Vary) {
                ((Vary)existing).Mean = firstNum;
                ((Vary)existing).Proportion = secondNum;
            } else {
                existing = new Vary(firstNum, secondNum);
            }
        } else if (resultType == typeof(Gaussian)) {
            if (existing != null && existing is Gaussian) {
                ((Gaussian)existing).Mean = firstNum;
                ((Gaussian)existing).StdDev = secondNum;
            } else {
                existing = new Gaussian(firstNum, secondNum);
            }
        } else if (resultType == typeof(Binomial)) {
            if (existing != null && existing is Binomial) {
                ((Binomial)existing).FlipProb = firstNum;
                ((Binomial)existing).Max = secondNum;
            } else {
                existing = new Binomial(firstNum, secondNum);
            }
        } else if (resultType == typeof(Exponential)) {
            if (existing != null && existing is Exponential) {
                ((Exponential)existing).InvLambda = firstNum;
                ((Exponential)existing).Minimum = secondNum;
            } else {
                existing = new Exponential(firstNum, secondNum);
            }
        }
        return existing;
    }
 public void ValidateProbability(double p, int n, int x, double d)
 {
     var b = new Binomial(p, n);
     AssertHelpers.AlmostEqual(d, b.Probability(x), 14);
 }
 public void ValidateSkewness(double p, int n)
 {
     var b = new Binomial(p, n);
     Assert.AreEqual((1.0 - (2.0 * p)) / Math.Sqrt(n * p * (1.0 - p)), b.Skewness);
 }
        public void ValidateSkewness(double p, int n)
        {
            var b = new Binomial(p, n);

            Assert.AreEqual((1.0 - (2.0 * p)) / Math.Sqrt(n * p * (1.0 - p)), b.Skewness);
        }
 public void CanSampleStatic()
 {
     Binomial.Sample(new System.Random(0), 0.3, 5);
 }
        public void ValidateMinimum()
        {
            var b = new Binomial(0.3, 10);

            Assert.AreEqual <int>(0, b.Minimum);
        }
        public void ValidateProbability(double p, int n, int x, double d)
        {
            var b = new Binomial(p, n);

            AssertHelpers.AlmostEqual(d, b.Probability(x), 14);
        }
Beispiel #10
0
        public void SetProbabilityOfOneFails(double p)
        {
            var b = new Binomial(0.3, 1);

            Assert.Throws <ArgumentOutOfRangeException>(() => b.P = p);
        }
Beispiel #11
0
 public void FailSampleSequenceStatic()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => Binomial.Samples(new Random(), -1.0, 5).First());
 }
Beispiel #12
0
 public void BinomTest()
 {
     Assert.AreEqual(1, Binomial.Calc(2, 0));
     Assert.AreEqual(2, Binomial.Calc(2, 1));
     Assert.AreEqual(3, Binomial.Calc(3, 2));
 }
Beispiel #13
0
 public void FailSampleStatic()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => Binomial.Sample(new Random(0), -1.0, 5));
 }
    public static void sparse_grid_hermite(int dim_num, int level_max, int point_num,
                                           ref double[] grid_weight, ref double[] grid_point)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    SPARSE_GRID_HERMITE computes a sparse grid of Gauss-Hermite points.
    //
    //  Discussion:
    //
    //    The quadrature rule is associated with a sparse grid derived from
    //    a Smolyak construction using a 1D Gauss-Hermite quadrature rule.
    //
    //    The user specifies:
    //    * the spatial dimension of the quadrature region,
    //    * the level that defines the Smolyak grid.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    05 July 2008
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Reference:
    //
    //    Fabio Nobile, Raul Tempone, Clayton Webster,
    //    A Sparse Grid Stochastic Collocation Method for Partial Differential
    //    Equations with Random Input Data,
    //    SIAM Journal on Numerical Analysis,
    //    Volume 46, Number 5, 2008, pages 2309-2345.
    //
    //  Parameters:
    //
    //    Input, int DIM_NUM, the spatial dimension.
    //
    //    Input, int LEVEL_MAX, controls the size of the final sparse grid.
    //
    //    Input, int POINT_NUM, the number of points in the grid, as determined
    //    by SPARSE_GRID_HERM_SIZE.
    //
    //    Output, double GRID_WEIGHT[POINT_NUM], the weights.
    //
    //    Output, double GRID_POINT[DIM_NUM*POINT_NUM], the points.
    //
    {
        int level;
        int point;
        int point3 = 0;

        for (point = 0; point < point_num; point++)
        {
            grid_weight[point] = 0.0;
        }

        //
        //  The outer loop generates LEVELs from LEVEL_MIN to LEVEL_MAX.
        //
        int point_num2 = 0;

        int level_min = Math.Max(0, level_max + 1 - dim_num);

        int[] grid_base2 = new int[dim_num];
        int[] level_1d   = new int[dim_num];
        int[] order_1d   = new int[dim_num];

        for (level = level_min; level <= level_max; level++)
        {
            //
            //  The middle loop generates the next partition LEVEL_1D(1:DIM_NUM)
            //  that adds up to LEVEL.
            //
            bool more = false;
            int  h    = 0;
            int  t    = 0;

            for (;;)
            {
                Comp.comp_next(level, dim_num, ref level_1d, ref more, ref h, ref t);
                //
                //  Transform each 1D level to a corresponding 1D order.
                //  The relationship is the same as for other OPEN rules.
                //  The GL rule differs from the other OPEN rules only in the nesting behavior.
                //
                ClenshawCurtis.level_to_order_open(dim_num, level_1d, ref order_1d);

                int dim;
                for (dim = 0; dim < dim_num; dim++)
                {
                    grid_base2[dim] = (order_1d[dim] - 1) / 2;
                }

                //
                //  The product of the 1D orders gives us the number of points in this grid.
                //
                int order_nd = typeMethods.i4vec_product(dim_num, order_1d);
                //
                //  Compute the weights for this product grid.
                //
                double[] grid_weight2 = HermiteQuadrature.product_weight_hermite(dim_num, order_1d, order_nd);
                //
                //  Now determine the coefficient of the weight.
                //
                int coeff = (int)(Math.Pow(-1, level_max - level)
                                  * Binomial.choose(dim_num - 1, level_max - level));
                //
                //  The inner (hidden) loop generates all points corresponding to given grid.
                //  The grid indices will be between -M to +M, where 2*M + 1 = ORDER_1D(DIM).
                //
                int[] grid_index2 = Multigrid.multigrid_index_z(dim_num, order_1d, order_nd);
                //
                //  Determine the first level of appearance of each of the points.
                //  This allows us to flag certain points as being repeats of points
                //  generated on a grid of lower level.
                //
                //  This is SLIGHTLY tricky.
                //
                int[] grid_level = HermiteQuadrature.index_level_hermite(level, level_max, dim_num, order_nd,
                                                                         grid_index2, grid_base2);
                //
                //  Only keep those points which first appear on this level.
                //
                for (point = 0; point < order_nd; point++)
                {
                    //
                    //  Either a "new" point (increase count, create point, create weight)
                    //
                    if (grid_level[point] == level)
                    {
                        HermiteQuadrature.hermite_abscissa(dim_num, 1, grid_index2,
                                                           grid_base2, ref grid_point, gridIndex: +point * dim_num,
                                                           gridPointIndex: +point_num2 * dim_num);

                        grid_weight[point_num2] = coeff * grid_weight2[point];

                        point_num2 += 1;
                    }
                    //
                    //  or an already existing point (create point temporarily, find match,
                    //  add weight to matched point's weight).
                    //
                    else
                    {
                        double[] grid_point_temp = new double[dim_num];

                        HermiteQuadrature.hermite_abscissa(dim_num, 1, grid_index2,
                                                           grid_base2, ref grid_point_temp, gridIndex: +point * dim_num);

                        int point2;
                        for (point2 = 0; point2 < point_num2; point2++)
                        {
                            point3 = point2;
                            for (dim = 0; dim < dim_num; dim++)
                            {
                                if (!(Math.Abs(grid_point[dim + point2 * dim_num] - grid_point_temp[dim]) >
                                      double.Epsilon))
                                {
                                    continue;
                                }

                                point3 = -1;
                                break;
                            }

                            if (point3 == point2)
                            {
                                break;
                            }
                        }

                        switch (point3)
                        {
                        case -1:
                            Console.WriteLine("");
                            Console.WriteLine("SPARSE_GRID_HERM - Fatal error!");
                            Console.WriteLine("  Could not match point.");
                            return;

                        default:
                            grid_weight[point3] += coeff * grid_weight2[point];
                            break;
                        }
                    }
                }

                if (!more)
                {
                    break;
                }
            }
        }
    }
Beispiel #15
0
            public void Initialize()
            {
                // DO NOT make this a constructor, because it makes the test not notice complete lack of serialization as an empty object is set up exactly as the thing
                // you are trying to deserialize.
                this.pareto  = new Pareto(1.2, 3.5);
                this.poisson = new Poisson(2.3);
                this.wishart = new Wishart(20, new PositiveDefiniteMatrix(new double[, ] {
                    { 22, 21 }, { 21, 23 }
                }));
                this.vectorGaussian = new VectorGaussian(Vector.FromArray(13, 14), new PositiveDefiniteMatrix(new double[, ] {
                    { 16, 15 }, { 15, 17 }
                }));
                this.unnormalizedDiscrete = UnnormalizedDiscrete.FromLogProbs(DenseVector.FromArray(5.1, 5.2, 5.3));
                this.pointMass            = PointMass <double> .Create(1.1);

                this.gaussian             = new Gaussian(11.0, 12.0);
                this.nonconjugateGaussian = new NonconjugateGaussian(1.2, 2.3, 3.4, 4.5);
                this.gamma              = new Gamma(9.0, 10.0);
                this.gammaPower         = new GammaPower(5.6, 2.8, 3.4);
                this.discrete           = new Discrete(6.0, 7.0, 8.0);
                this.conjugateDirichlet = new ConjugateDirichlet(1.2, 2.3, 3.4, 4.5);
                this.dirichlet          = new Dirichlet(3.0, 4.0, 5.0);
                this.beta      = new Beta(2.0, 1.0);
                this.binomial  = new Binomial(5, 0.8);
                this.bernoulli = new Bernoulli(0.6);

                this.sparseBernoulliList    = SparseBernoulliList.Constant(4, new Bernoulli(0.1));
                this.sparseBernoulliList[1] = new Bernoulli(0.9);
                this.sparseBernoulliList[3] = new Bernoulli(0.7);

                this.sparseBetaList    = SparseBetaList.Constant(5, new Beta(2.0, 2.0));
                this.sparseBetaList[0] = new Beta(3.0, 4.0);
                this.sparseBetaList[1] = new Beta(5.0, 6.0);

                this.sparseGaussianList    = SparseGaussianList.Constant(6, Gaussian.FromMeanAndPrecision(0.1, 0.2));
                this.sparseGaussianList[4] = Gaussian.FromMeanAndPrecision(0.3, 0.4);
                this.sparseGaussianList[5] = Gaussian.FromMeanAndPrecision(0.5, 0.6);

                this.sparseGammaList = SparseGammaList.Constant(1, Gamma.FromShapeAndRate(1.0, 2.0));

                this.truncatedGamma    = new TruncatedGamma(1.2, 2.3, 3.4, 4.5);
                this.truncatedGaussian = new TruncatedGaussian(1.2, 3.4, 5.6, 7.8);
                this.wrappedGaussian   = new WrappedGaussian(1.2, 2.3, 3.4);

                ga = Distribution <double> .Array(new[] { this.gaussian, this.gaussian });

                vga = Distribution <Vector> .Array(new[] { this.vectorGaussian, this.vectorGaussian });

                ga2D = Distribution <double> .Array(new[, ] {
                    { this.gaussian, this.gaussian }, { this.gaussian, this.gaussian }
                });

                vga2D = Distribution <Vector> .Array(new[, ] {
                    { this.vectorGaussian, this.vectorGaussian }, { this.vectorGaussian, this.vectorGaussian }
                });

                gaJ = Distribution <double> .Array(new[] { new[] { this.gaussian, this.gaussian }, new[] { this.gaussian, this.gaussian } });

                vgaJ = Distribution <Vector> .Array(new[] { new[] { this.vectorGaussian, this.vectorGaussian }, new[] { this.vectorGaussian, this.vectorGaussian } });

                var gp    = new GaussianProcess(new ConstantFunction(0), new SquaredExponential(0));
                var basis = Util.ArrayInit(2, i => Vector.FromArray(1.0 * i));

                this.sparseGp = new SparseGP(new SparseGPFixed(gp, basis));

                this.quantileEstimator = new QuantileEstimator(0.01);
                this.quantileEstimator.Add(5);

                this.stringDistribution1 = StringDistribution.String("aa").Append(StringDistribution.OneOf("b", "ccc")).Append("dddd");
                this.stringDistribution2 = new StringDistribution();
                this.stringDistribution2.SetToProduct(StringDistribution.OneOf("a", "b"), StringDistribution.OneOf("b", "c"));
            }
 public void CanSampleStatic()
 {
     var d = Binomial.Sample(new Random(), 0.3, 5);
 }
        public void ValidateToString()
        {
            var b = new Binomial(0.3, 2);

            Assert.AreEqual("Binomial(p = 0.3, n = 2)", b.ToString());
        }
 public void CanSampleSequenceStatic()
 {
     var ied = Binomial.Samples(new Random(), 0.3, 5);
     var arr = ied.Take(5).ToArray();
 }
        public void ValidateMaximum()
        {
            var b = new Binomial(0.3, 10);

            Assert.AreEqual(10, b.Maximum);
        }
 public void FailSampleStatic()
 {
     var d = Binomial.Sample(new Random(), -1.0, 5);
 }
 public void FailSampleSequenceStatic()
 {
     Assert.That(() => Binomial.Samples(new System.Random(0), -1.0, 5).First(), Throws.ArgumentException);
 }
 public void FailSampleSequenceStatic()
 {
     var ied = Binomial.Samples(new Random(), -1.0, 5).First();
 }
 public void SetProbabilityOfOneFails(double p)
 {
     var b = new Binomial(0.3, 1);
     Assert.Throws<ArgumentOutOfRangeException>(() => b.P = p);
 }
 public void CanSampleSequence()
 {
     var n   = new Binomial(0.3, 5);
     var ied = n.Samples();
     var e   = ied.Take(5).ToArray();
 }
 public void ValidateMaximum()
 {
     var b = new Binomial(0.3, 10);
     Assert.AreEqual(10, b.Maximum);
 }
 public void BinomialCreateFailsWithBadParameters(double p, int n)
 {
     var bernoulli = new Binomial(p, n);
 }
 public void CanCreateBinomial(double p, int n)
 {
     var bernoulli = new Binomial(p, n);
     Assert.AreEqual(p, bernoulli.P);
 }
        public void ValidateToString()
        {
            var b = new Binomial(0.3, 2);

            Assert.AreEqual(String.Format("Binomial(Success Probability = {0}, Number of Trials = {1})", b.P, b.N), b.ToString());
        }
 public void ValidateToString()
 {
     var b = new Binomial(0.3, 2);
     Assert.AreEqual<string>("Binomial(Success Probability = 0.3, Number of Trials = 2)", b.ToString());
 }
        public void CanSetSuccessProbability(double p, int n)
        {
            var b = new Binomial(0.3, n);

            b.P = p;
        }
 public void CanSetSuccessProbability(double p, int n)
 {
     var b = new Binomial(0.3, n);
     b.P = p;
 }
        public void SetProbabilityOfOneFails(double p, int n)
        {
            var b = new Binomial(0.3, n);

            b.P = p;
        }
        public void CanCreateBinomial(double p, int n)
        {
            var bernoulli = new Binomial(p, n);

            Assert.AreEqual(p, bernoulli.P);
        }
Beispiel #34
0
        public double[] GetSampleData(string distType, double mostLikelyEstimate,
                                      double lowEstimate, double highEstimate)
        {
            if (Iterations > 10000)
            {
                Iterations = 10000;
            }
            if (Iterations <= 2)
            {
                Iterations = 1000;
            }
            if (this.CILevel < 10)
            {
                this.CILevel = 90;
            }
            if (this.CILevel > 99)
            {
                this.CILevel = 99;
            }
            Random rnd = new Random(Random);

            mostLikelyEstimate = Math.Round(mostLikelyEstimate, 4);
            lowEstimate        = Math.Round(lowEstimate, 4);
            highEstimate       = Math.Round(highEstimate, 4);
            var sampledata = new double[Iterations];

            if (distType == Calculator1.RUC_TYPES.triangle.ToString())
            {
                if (lowEstimate >= mostLikelyEstimate || lowEstimate == 0)
                {
                    //arbitrary rules (25%)
                    lowEstimate = mostLikelyEstimate * .75;
                    //no errors: lowEstimate = 0 is often the case
                    //sb.AppendLine(Errors.GetMessage("DATA_BADDISTRIBUTION"));
                }
                if (highEstimate <= mostLikelyEstimate || highEstimate == 0)
                {
                    //arbitrary rules (25%)
                    highEstimate = mostLikelyEstimate * 1.25;
                }
                if (Random != 0)
                {
                    //generate samples of the Triangular(low, high, mode) distribution;
                    Triangular.Samples(rnd, sampledata, lowEstimate, highEstimate, mostLikelyEstimate);
                }
                else
                {
                    //generate samples of the Triangular(low, high, mode) distribution;
                    Triangular.Samples(sampledata, lowEstimate, highEstimate, mostLikelyEstimate);
                }
            }
            else if (distType == Calculator1.RUC_TYPES.normal.ToString())
            {
                //generate samples of the Normal(mean, sd) distribution;
                if (Random != 0)
                {
                    Normal.Samples(rnd, sampledata, lowEstimate, highEstimate);
                }
                else
                {
                    Normal.Samples(sampledata, lowEstimate, highEstimate);
                }
            }
            else if (distType == Calculator1.RUC_TYPES.lognormal.ToString())
            {
                if (Random != 0)
                {
                    LogNormal.Samples(rnd, sampledata, lowEstimate, highEstimate);
                }
                else
                {
                    LogNormal.Samples(sampledata, lowEstimate, highEstimate);
                }
            }
            else if (distType == Calculator1.RUC_TYPES.weibull.ToString())
            {
                if (Random != 0)
                {
                    Weibull.Samples(rnd, sampledata, lowEstimate, highEstimate);
                }
                else
                {
                    Weibull.Samples(sampledata, lowEstimate, highEstimate);
                }
            }
            else if (distType == Calculator1.RUC_TYPES.beta.ToString())
            {
                if (Random != 0)
                {
                    Beta.Samples(rnd, sampledata, lowEstimate, highEstimate);
                }
                else
                {
                    Beta.Samples(sampledata, lowEstimate, highEstimate);
                }
            }
            else if (distType == Calculator1.RUC_TYPES.pareto.ToString())
            {
                if (Random != 0)
                {
                    Pareto.Samples(rnd, sampledata, lowEstimate, highEstimate);
                }
                else
                {
                    Pareto.Samples(sampledata, lowEstimate, highEstimate);
                }
            }
            else if (distType == Calculator1.RUC_TYPES.uniform.ToString())
            {
                var sampleints = new int[Iterations];
                int iLower     = CalculatorHelpers.ConvertStringToInt(lowEstimate.ToString());
                int iUpper     = CalculatorHelpers.ConvertStringToInt(highEstimate.ToString());
                if (Random != 0)
                {
                    DiscreteUniform.Samples(rnd, sampleints, iLower, iUpper);
                }
                else
                {
                    DiscreteUniform.Samples(sampleints, iLower, iUpper);
                }
                for (int i = 0; i < sampleints.Count(); i++)
                {
                    sampledata[i] = sampleints[i];
                }
            }
            else if (distType == Calculator1.RUC_TYPES.bernoulli.ToString())
            {
                var sampleints = new int[Iterations];
                if (Random != 0)
                {
                    Bernoulli.Samples(rnd, sampleints, lowEstimate);
                }
                else
                {
                    Bernoulli.Samples(sampleints, lowEstimate);
                }
                for (int i = 0; i < sampleints.Count(); i++)
                {
                    sampledata[i] = sampleints[i];
                }
            }
            else if (distType == Calculator1.RUC_TYPES.poisson.ToString())
            {
                var sampleints = new int[Iterations];
                if (Random != 0)
                {
                    Poisson.Samples(rnd, sampleints, lowEstimate);
                }
                else
                {
                    Poisson.Samples(sampleints, lowEstimate);
                }
                for (int i = 0; i < sampleints.Count(); i++)
                {
                    sampledata[i] = sampleints[i];
                }
            }
            else if (distType == Calculator1.RUC_TYPES.binomial.ToString())
            {
                var sampleints     = new int[Iterations];
                int iUpperEstimate = CalculatorHelpers.ConvertStringToInt(highEstimate.ToString());
                if (Random != 0)
                {
                    Binomial.Samples(rnd, sampleints, lowEstimate, iUpperEstimate);
                }
                else
                {
                    Binomial.Samples(sampleints, lowEstimate, iUpperEstimate);
                }
                for (int i = 0; i < sampleints.Count(); i++)
                {
                    sampledata[i] = sampleints[i];
                }
            }
            else if (distType == Calculator1.RUC_TYPES.gamma.ToString())
            {
                //generate samples of the Gamma(shape, scale) distribution;
                if (Random != 0)
                {
                    Gamma.Samples(rnd, sampledata, lowEstimate, highEstimate);
                }
                else
                {
                    Gamma.Samples(sampledata, lowEstimate, highEstimate);
                }
            }
            else
            {
                //don't force them to use distribution
            }
            //hold for possible infernet use
            //else if (distType == Calculator1.RUC_TYPES.dirichlet.ToString())
            //{
            //    //generate samples of the Dirichlet(random, alpha) distribution;
            //    Dirichlet.Sample(sampledata, lowEstimate);
            //}
            //else if (distType == Calculator1.RUC_TYPES.wishart.ToString())
            //{
            //    //generate samples of the Wishart(random, degrees of freedom, scale) distribution;
            //    Wishart.Sample(sampledata, lowEstimate, highEstimate);
            //}

            //the mathlibrary supports more than a dozen additional distributions

            return(sampledata);
        }
        public void ValidateEntropy(double p, int n, double e)
        {
            var b = new Binomial(p, n);

            AssertHelpers.AlmostEqualRelative(e, b.Entropy, 14);
        }
Beispiel #36
0
        static void Main(string[] args)
        {
            // Binomial
            var p      = 0.174;
            var n      = 6;
            var x      = 5;
            var bCdf   = Binomial.CDF(p, n, x);
            var bPmf   = Binomial.PMF(p, n, x);
            var bPmfLn = Binomial.PMFLn(p, n, x);

            var b       = new Binomial(p, n);
            var _bCdf   = b.CumulativeDistribution(x);
            var _bPmf   = b.Probability(x);
            var _bPmfLn = b.ProbabilityLn(x);

            // Normal
            var mean   = 1012.5;
            var stdDev = 24.8069;
            var x1     = 1000;
            var x2     = 1025;
            var nCdf   = Normal.CDF(mean, stdDev, x2) - Normal.CDF(mean, stdDev, x1);
            var nPdf   = Normal.PDF(mean, stdDev, x2) - Normal.PDF(mean, stdDev, x1);
            var nPdfLn = Normal.PDFLn(mean, stdDev, x2) - Normal.PDFLn(mean, stdDev, x1);

            var resN = new List <double>();

            for (int i = 950; i < 1075; i++)
            {
                //resN.Add(Normal.CDF(mean, stdDev, 1075) - Normal.CDF(mean, stdDev, i));
                resN.Add(Normal.CDF(mean, stdDev, i));
            }

            var _n       = new Normal(mean, stdDev);
            var _nCdf    = b.CumulativeDistribution(x2) - b.CumulativeDistribution(x1);
            var _nPdf    = b.Probability(x2) - b.Probability(x1);
            var _nPdfLn  = b.ProbabilityLn(x2) - b.ProbabilityLn(x1);
            var nSamples = new double[100];

            _n.Samples(nSamples);

            // Discrete Uniform
            var sqrtThree = Math.Sqrt(3);
            var _mean     = 2.1;
            var _stdDev   = 1.465;
            var lower     = (_mean - sqrtThree * _stdDev);
            var upper     = (_mean + sqrtThree * _stdDev);
            var u         = new ContinuousUniform(lower, upper);
            var _p        = u.Density(1.05);

            var h = new Histogram(nSamples, 5);

            Regex _regexBack = new Regex(@"(?:(?<Lower>[\d]+)? *-? *(?:(?<Upper>[\d]+)))");
            var   test0      = "66";

            var select = test0;

            if (_regexBack.IsMatch(select))
            {
                foreach (var match in _regexBack.Matches(select))
                {
                }
            }



            Console.ReadKey();
        }
        public void ValidateMode(double p, int n, int m)
        {
            var b = new Binomial(p, n);

            Assert.AreEqual(m, b.Mode);
        }
 public void SetProbabilityOfOneFails([Values(Double.NaN, -1.0, 2.0)] double p)
 {
     var b = new Binomial(0.3, 1);
     Assert.Throws<ArgumentOutOfRangeException>(() => b.P = p);
 }
        public void ValidateProbabilityLn(double p, int n, int x, double dln)
        {
            var b = new Binomial(p, n);

            AssertHelpers.AlmostEqualRelative(dln, b.ProbabilityLn(x), 14);
        }
 public void ValidateEntropy([Values(0.0, 0.3, 1.0)] double p, [Values(4, 3, 2)] int n, [Values(0.0, 1.1404671643037712668976423399228972051669206536461, 0.0)] double e)
 {
     var b = new Binomial(p, n);
     AssertHelpers.AlmostEqual(e, b.Entropy, 14);
 }
        public void CanSampleSequenceStatic()
        {
            var ied = Binomial.Samples(new System.Random(0), 0.3, 5);

            GC.KeepAlive(ied.Take(5).ToArray());
        }
 public void ValidateSkewness([Values(0.0, 0.3, 1.0)] double p, [Values(4, 3, 2)] int n)
 {
     var b = new Binomial(p, n);
     Assert.AreEqual((1.0 - (2.0 * p)) / Math.Sqrt(n * p * (1.0 - p)), b.Skewness);
 }
        public void CanSample()
        {
            var n = new Binomial(0.3, 5);

            n.Sample();
        }
 public void ValidateMode([Values(0.0, 0.3, 1.0)] double p, [Values(4, 3, 2)] int n, [Values(0, 1, 2)] int m)
 {
     var b = new Binomial(p, n);
     Assert.AreEqual(m, b.Mode);
 }
 public void ValidateToString()
 {
     var b = new Binomial(0.3, 2);
     Assert.AreEqual(String.Format("Binomial(Success Probability = {0}, Number of Trials = {1})", b.P, b.N), b.ToString());
 }
 public void ValidateProbability(
     [Values(0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.300000, 0.300000, 0.300000, 0.300000, 0.300000, 0.300000, 0.300000, 0.300000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000)] double p, 
     [Values(1, 1, 3, 3, 3, 10, 10, 10, 1, 1, 3, 3, 3, 10, 10, 10, 1, 1, 3, 3, 3, 10, 10, 10)] int n, 
     [Values(0, 1, 0, 1, 3, 0, 1, 10, 0, 1, 0, 1, 3, 0, 1, 10, 0, 1, 0, 1, 3, 0, 1, 10)] int x, 
     [Values(1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.69999999999999995559107901499373838305473327636719, 0.2999999999999999888977697537484345957636833190918, 0.34299999999999993471888615204079956461021032657166, 0.44099999999999992772448109690231306411849135972008, 0.026999999999999997002397833512077451789759292859569, 0.02824752489999998207939855277004937778546385011091, 0.12106082099999992639752977030555903089040470780077, 0.0000059048999999999978147480206303047454017251032868501, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0)] double d)
 {
     var b = new Binomial(p, n);
     AssertHelpers.AlmostEqual(d, b.Probability(x), 14);
 }
 public void ValidateEntropy(double p, int n, double e)
 {
     var b = new Binomial(p, n);
     AssertHelpers.AlmostEqualRelative(e, b.Entropy, 14);
 }
 public void ValidateProbabilityLn(
     [Values(0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.300000, 0.300000, 0.300000, 0.300000, 0.300000, 0.300000, 0.300000, 0.300000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000)] double p, 
     [Values(1, 1, 3, 3, 3, 10, 10, 10, 1, 1, 3, 3, 3, 10, 10, 10, 1, 1, 3, 3, 3, 10, 10, 10)] int n, 
     [Values(0, 1, 0, 1, 3, 0, 1, 10, 0, 1, 0, 1, 3, 0, 1, 10, 0, 1, 0, 1, 3, 0, 1, 10)] int x, 
     [Values(0.0, Double.NegativeInfinity, 0.0, Double.NegativeInfinity, Double.NegativeInfinity, 0.0, Double.NegativeInfinity, Double.NegativeInfinity, -0.3566749439387324423539544041072745145718090708995, -1.2039728043259360296301803719337238685164245381839, -1.0700248318161973270618632123218235437154272126985, -0.81871040353529122294284394322574719301255212216016, -3.6119184129778080888905411158011716055492736145517, -3.566749439387324423539544041072745145718090708995, -2.1114622067804823267977785542148302920616046876506, -12.039728043259360296301803719337238685164245381839, Double.NegativeInfinity, 0.0, Double.NegativeInfinity, Double.NegativeInfinity, 0.0, Double.NegativeInfinity, Double.NegativeInfinity, 0.0)] double dln)
 {
     var b = new Binomial(p, n);
     AssertHelpers.AlmostEqual(dln, b.ProbabilityLn(x), 14);
 }
 public void ValidateMode(double p, int n, int m)
 {
     var b = new Binomial(p, n);
     Assert.AreEqual(m, b.Mode);
 }
 public void CanCreateBinomial([Values(0.0, 0.3, 1.0)] double p, [Values(4, 3, 2)] int n)
 {
     var bernoulli = new Binomial(p, n);
     Assert.AreEqual(p, bernoulli.P);
 }
 public void ValidateProbabilityLn(double p, int n, int x, double dln)
 {
     var b = new Binomial(p, n);
     AssertHelpers.AlmostEqualRelative(dln, b.ProbabilityLn(x), 14);
 }
 public void SetProbabilityOfOneFails(double p, int n)
 {
     var b = new Binomial(0.3, n);
     b.P = p;
 }
 public void CanSampleSequence()
 {
     var n = new Binomial(0.3, 5);
     var ied = n.Samples();
     ied.Take(5).ToArray();
 }
 public void ValidateMinimum()
 {
     var b = new Binomial(0.3, 10);
     Assert.AreEqual<int>(0, b.Minimum);
 }
 public void ValidateToString()
 {
     var b = new Binomial(0.3, 2);
     Assert.AreEqual("Binomial(p = 0.3, n = 2)", b.ToString());
 }
Beispiel #56
0
 public void SetProbabilityOfOneFails(double p)
 {
     var b = new Binomial(0.3, 1);
     Assert.That(() => b.P = p, Throws.ArgumentException);
 }