Beispiel #1
0
        /// <summary>
        /// This will show the poisson distribution
        /// Question 3
        /// Libraries and tool i used: MathNet
        /// install using nugpack from visual studio
        /// I have used code that was provided from
        /// https://rosettacode.org/wiki/Statistics/Normal_distribution#C
        /// I have modified the code to give me a poisson distribution
        /// also comes with a histogram.
        /// I have used the code given in this site for the solution of question 4
        /// </summary>
        /// <param name="numTrials">The number of number to generate</param>
        /// <param name="lambda">The expected value</param>
        static void Question4(int numTrials, int lambda)
        {
            int[] X       = new int[numTrials]; //just an array to store the generated number
            var   poisson = new Poisson(5);     //Create a new poisson object provided from the library

            poisson.Samples(X);                 //Create the sample


            double[] Y = new double[numTrials]; //For this part of the code because the histogram doesn't accept integer array i have to convert them into double

            for (int i = 0; i < numTrials; i++) //By using this for loop and transfering the numbers into a double
            {
                Y[i] = X[i];
            }

            const int numBuckets = 10;                           //number of buckets
            var       histogram  = new Histogram(Y, numBuckets); //The histogram will create a histogram based on the number of buckets and the sample array

            Console.WriteLine("Sample size: {0:N0}", numTrials);
            Console.WriteLine("The lambda value: {0}\n", lambda);
            for (int i = 0; i < numBuckets; i++)
            {
                string bar = new String('#', (int)(histogram[i].Count * MAX_STAR / numTrials));
                Console.WriteLine("bin" + i + "  {0:0.00} \t: {1} {2: 0.00}%", histogram[i].LowerBound, bar, histogram[i].Count / numTrials);
            }
            Console.WriteLine();
        }
Beispiel #2
0
        public void CanSampleSequence()
        {
            var d   = new Poisson(0.3);
            var ied = d.Samples();

            GC.KeepAlive(ied.Take(5).ToArray());
        }
Beispiel #3
0
        public int[] RandomSample(int sample_size)
        {
            int [] sample = new int [sample_size];
            Random random = new Random();

            Poisson.Samples(random, sample, d_lambda);
            return(sample);
        }
        private static void GenerateJD_PriceMatrixValues(double volatility, double riskFreeOptionPrice, MersenneTwister random, double dT,
                                                         List <Tuple <double, double[]> > jdPriceMatrix, double jumpDrift, int simulations)
        {
            for (var i = 1; i < EnvironmentSettings.Instance.TimeIntervals + 1; i++)
            {
                var gaussPrice  = Normal.WithMeanStdDev(0.0, 1.0, random).Samples().Take(simulations).ToArray();
                var gaussJump   = Normal.WithMeanStdDev(0.0, 1.0, random).Samples().Take(simulations).ToArray();
                var poissonJump = Poisson.Samples(EnvironmentSettings.Instance.JumpLambda * dT).Take(simulations).ToArray();

                var S = Simulation_JD(riskFreeOptionPrice, dT, volatility, gaussPrice, gaussJump,
                                      poissonJump, jdPriceMatrix[i - 1], jumpDrift, simulations);

                jdPriceMatrix.Add(new Tuple <double, double[]>(i * dT, S));
            }
        }
 public void CanSampleSequence()
 {
     var d = new Poisson(0.3);
     var ied = d.Samples();
     ied.Take(5).ToArray();
 }
Beispiel #6
0
 public void CanSampleSequence()
 {
     var d   = new Poisson(0.3);
     var ied = d.Samples();
     var e   = ied.Take(5).ToArray();
 }
Beispiel #7
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);
        }
        /// <summary>
        /// Run example
        /// </summary>
        /// <a href="http://en.wikipedia.org/wiki/Poisson_distribution">Poisson distribution</a>
        public void Run()
        {
            // 1. Initialize the new instance of the Poisson distribution class with parameter Lambda = 1
            var poisson = new Poisson(1);

            Console.WriteLine(@"1. Initialize the new instance of the Poisson distribution class with parameter Lambda = {0}", poisson.Lambda);
            Console.WriteLine();

            // 2. Distributuion properties:
            Console.WriteLine(@"2. {0} distributuion properties:", poisson);

            // Cumulative distribution function
            Console.WriteLine(@"{0} - Сumulative distribution at location '3'", poisson.CumulativeDistribution(3).ToString(" #0.00000;-#0.00000"));

            // Probability density
            Console.WriteLine(@"{0} - Probability mass at location '3'", poisson.Probability(3).ToString(" #0.00000;-#0.00000"));

            // Log probability density
            Console.WriteLine(@"{0} - Log probability mass at location '3'", poisson.ProbabilityLn(3).ToString(" #0.00000;-#0.00000"));

            // Entropy
            Console.WriteLine(@"{0} - Entropy", poisson.Entropy.ToString(" #0.00000;-#0.00000"));

            // Largest element in the domain
            Console.WriteLine(@"{0} - Largest element in the domain", poisson.Maximum.ToString(" #0.00000;-#0.00000"));

            // Smallest element in the domain
            Console.WriteLine(@"{0} - Smallest element in the domain", poisson.Minimum.ToString(" #0.00000;-#0.00000"));

            // Mean
            Console.WriteLine(@"{0} - Mean", poisson.Mean.ToString(" #0.00000;-#0.00000"));

            // Median
            Console.WriteLine(@"{0} - Median", poisson.Median.ToString(" #0.00000;-#0.00000"));

            // Mode
            Console.WriteLine(@"{0} - Mode", poisson.Mode.ToString(" #0.00000;-#0.00000"));

            // Variance
            Console.WriteLine(@"{0} - Variance", poisson.Variance.ToString(" #0.00000;-#0.00000"));

            // Standard deviation
            Console.WriteLine(@"{0} - Standard deviation", poisson.StdDev.ToString(" #0.00000;-#0.00000"));

            // Skewness
            Console.WriteLine(@"{0} - Skewness", poisson.Skewness.ToString(" #0.00000;-#0.00000"));
            Console.WriteLine();

            // 3. Generate 10 samples of the Poisson distribution
            Console.WriteLine(@"3. Generate 10 samples of the Poisson distribution");
            for (var i = 0; i < 10; i++)
            {
                Console.Write(poisson.Sample().ToString("N05") + @" ");
            }

            Console.WriteLine();
            Console.WriteLine();

            // 4. Generate 100000 samples of the Poisson(1) distribution and display histogram
            Console.WriteLine(@"4. Generate 100000 samples of the Poisson(1) distribution and display histogram");
            var data = new int[100000];

            Poisson.Samples(data, 1);
            ConsoleHelper.DisplayHistogram(data);
            Console.WriteLine();

            // 5. Generate 100000 samples of the Poisson(4) distribution and display histogram
            Console.WriteLine(@"5. Generate 100000 samples of the Poisson(4) distribution and display histogram");
            Poisson.Samples(data, 4);
            ConsoleHelper.DisplayHistogram(data);
            Console.WriteLine();

            // 6. Generate 100000 samples of the Poisson(10) distribution and display histogram
            Console.WriteLine(@"6. Generate 100000 samples of the Poisson(10) distribution and display histogram");
            Poisson.Samples(data, 10);
            ConsoleHelper.DisplayHistogram(data);
        }