Example #1
0
        public void CanSampleSequence()
        {
            var n   = new LogNormal(1.0, 2.0);
            var ied = n.Samples();

            ied.Take(5).ToArray();
        }
Example #2
0
        public void CanEstimateParameters(double mu, double sigma)
        {
            var original  = new LogNormal(mu, sigma, new Random(100));
            var estimated = LogNormal.Estimate(original.Samples().Take(10000));

            AssertHelpers.AlmostEqual(mu, estimated.Mu, 2);
            AssertHelpers.AlmostEqual(sigma, estimated.Sigma, 2);
        }
Example #3
0
        public PortfolioPath(int yearsUntilRetirement, double expectedReturn, double variance, double initialPortfolioValue, double annualContribution, double incomeDraw, int yearsPlannedRetirement)
        {
            this.folioReturn           = expectedReturn;
            this.folioStDev            = Math.Sqrt(variance) / 100;
            this.annualContribution    = annualContribution;
            this.incomeDraw            = incomeDraw;
            this.initialPortfolioValue = initialPortfolioValue;
            this.retirement            = yearsUntilRetirement;
            this.nSteps             = retirement + yearsPlannedRetirement;
            this.portfolioValueList = new List <decimal>();
            this.portfolioValueList.Add((decimal)initialPortfolioValue);
            LogNormal            lognormal = LogNormal.WithMuSigma(folioReturn, folioStDev);
            IEnumerable <double> returns   = lognormal.Samples().Take(nSteps);

            this.endingPortfolioValue = returns.Aggregate(initialPortfolioValue, ComputeNextPortfolioValue);
        }
Example #4
0
 public void FailSampleSequenceStatic()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => { var ied = LogNormal.Samples(new Random(), 0.0, -1.0).First(); });
 }
Example #5
0
        public void CanSampleSequenceStatic()
        {
            var ied = LogNormal.Samples(new Random(), 0.0, 1.0);

            ied.Take(5).ToArray();
        }
 public void FailSampleSequenceStatic()
 {
     Assert.That(() => { var ied = LogNormal.Samples(new Random(0), 0.0, -1.0).First(); }, Throws.ArgumentException);
 }
        public void CanSampleSequenceStatic()
        {
            var ied = LogNormal.Samples(new Random(0), 0.0, 1.0);

            GC.KeepAlive(ied.Take(5).ToArray());
        }
Example #8
0
 public void FailSampleSequenceStatic()
 {
     var ied = LogNormal.Samples(new Random(), 0.0, -1.0).First();
 }
Example #9
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 CanSampleSequence()
 {
     var n = new LogNormal(1.0, 2.0);
     var ied = n.Samples();
     var e = ied.Take(5).ToArray();
 }
Example #11
0
        public void CanEstimateParameters(double mu, double sigma)
        {
            var original = new LogNormal(mu, sigma, new Random(100));
            var estimated = LogNormal.Estimate(original.Samples().Take(10000));

            AssertHelpers.AlmostEqual(mu, estimated.Mu, 2);
            AssertHelpers.AlmostEqual(sigma, estimated.Sigma, 2);
        }
Example #12
0
        /// <summary>
        /// Run example
        /// </summary>
        /// <a href="http://en.wikipedia.org/wiki/Log-normal_distribution">LogNormal distribution</a>
        public void Run()
        {
            // 1. Initialize the new instance of the LogNormal distribution class with parameters Mu = 0, Sigma = 1
            var logNormal = new LogNormal(0, 1);

            Console.WriteLine(@"1. Initialize the new instance of the LogNormal distribution class with parameters Mu = {0}, Sigma = {1}", logNormal.Mu, logNormal.Sigma);
            Console.WriteLine();

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

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

            // Probability density
            Console.WriteLine(@"{0} - Probability density at location '0.3'", logNormal.Density(0.3).ToString(" #0.00000;-#0.00000"));

            // Log probability density
            Console.WriteLine(@"{0} - Log probability density at location '0.3'", logNormal.DensityLn(0.3).ToString(" #0.00000;-#0.00000"));

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

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

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

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

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

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

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

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

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

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

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

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

            LogNormal.Samples(data, 0.0, 1.0);
            ConsoleHelper.DisplayHistogram(data);
            Console.WriteLine();

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

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