Example #1
0
 public void ValidateSkewness(double lambda)
 {
     var d = new Poisson(lambda);
     Assert.AreEqual(1.0 / Math.Sqrt(lambda), d.Skewness);
 }
        /// <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 double[100000];
            for (var i = 0; i < data.Length; i++)
            {
                data[i] = poisson.Sample();
            }

            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.Lambda = 4;
            for (var i = 0; i < data.Length; i++)
            {
                data[i] = poisson.Sample();
            }

            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.Lambda = 10;
            for (var i = 0; i < data.Length; i++)
            {
                data[i] = poisson.Sample();
            }

            ConsoleHelper.DisplayHistogram(data);
        }
Example #3
0
 public void ValidateToString()
 {
     var d = new Poisson(0.3);
     Assert.AreEqual(String.Format("Poisson(λ = {0})", 0.3), d.ToString());
 }
Example #4
0
 public void ValidateEntropy(double lambda)
 {
     var d = new Poisson(lambda);
     Assert.AreEqual((0.5 * Math.Log(2 * Math.PI * Math.E * lambda)) - (1.0 / (12.0 * lambda)) - (1.0 / (24.0 * lambda * lambda)) - (19.0 / (360.0 * lambda * lambda * lambda)), d.Entropy);
 }
Example #5
0
 public void ValidateCumulativeDistribution(double lambda, int x, double result)
 {
     var d = new Poisson(lambda);
     Assert.AreEqual(d.CumulativeDistribution(x), result, 1e-12);
 }
Example #6
0
 public void CanCreatePoisson(double lambda)
 {
     var d = new Poisson(lambda);
     Assert.AreEqual(lambda, d.Lambda);
 }
Example #7
0
 public void CanSample()
 {
     var d = new Poisson(0.3);
     d.Sample();
 }
Example #8
0
 public void CanSampleSequence()
 {
     var d = new Poisson(0.3);
     var ied = d.Samples();
     GC.KeepAlive(ied.Take(5).ToArray());
 }
Example #9
0
 public void ValidateMaximum()
 {
     var d = new Poisson(0.3);
     Assert.AreEqual(int.MaxValue, d.Maximum);
 }
Example #10
0
 public void ValidateProbabilityLn(double lambda, int x, double result)
 {
     var d = new Poisson(lambda);
     Assert.AreEqual(d.ProbabilityLn(x), Math.Log(result), 1e-12);
 }
Example #11
0
 public void ValidateMinimum()
 {
     var d = new Poisson(0.3);
     Assert.AreEqual(0, d.Minimum);
 }
Example #12
0
 public void ValidateMedian(double lambda)
 {
     var d = new Poisson(lambda);
     Assert.AreEqual((int)Math.Floor(lambda + (1.0 / 3.0) - (0.02 / lambda)), d.Median);
 }
Example #13
0
 public void ValidateMode(double lambda)
 {
     var d = new Poisson(lambda);
     Assert.AreEqual((int)Math.Floor(lambda), d.Mode);
 }
Example #14
0
        public static void GenerateShortReadsFromDonorGenome(string donorGenomeFile, string readsFile, int readLength, double coverage, long limit)
        {
            Poisson poisson = new Poisson(coverage / readLength) { RandomSource = new MersenneTwister() };

            using (var donorFile = File.OpenRead(donorGenomeFile))
            using (var file = File.Open(readsFile, FileMode.Create))
            {
                var writer = new BinaryWriter(file);
                var reader = new BinaryReader(donorFile);

                writer.Write(readLength);

                for (long donorIndex = 0, numReads = 0; donorIndex <= donorFile.Length - readLength && numReads < limit; donorIndex++)
                {
                    var numReadsAtPos = poisson.Sample();
                    numReads += numReadsAtPos;

                    if (numReadsAtPos > 0)
                    {
                        donorFile.Seek(donorIndex, SeekOrigin.Begin);
                        var chars = reader.ReadChars(readLength);
                        string read = new string(chars);
                        DnaSequence dna = DnaSequence.CreateGenomeFromString(read);
                        for (int i = 0; i < numReadsAtPos; i++)
                            writer.Write(dna.Bytes);
                    }
                }
            }
        }
Example #15
0
        public JsonResult SpotPriceSimulation(
            int[] timeStepsInLevels, double[] priceLevels,
            double timeStep, double reversionRate, double volatility,
            int numberOfSimulations)
        {
            if (timeStepsInLevels.Length != priceLevels.Length)
                throw new Exception("Lengths not consistent");

            //hourly simulation
            var horizon = timeStepsInLevels.Sum()*24; //
            
            //TODO: add daily/hourly resolution... if it makes sense... but not really.
            
            //estimate an hourly ARMA...
            var d = AppData.GetHistoricalSeries(PricesController._timeSeries);

            var data = d
                .Select(x => x.Value != null ? (double)x.Value : 0) //0 for now, when interpolation is done... etc..
                .ToArray();

            //just take last 3 years... mhm...
            var threeYears = 24 * 7 * 4 * 12 * 3;
            if (data.Length > threeYears)
            {
                data = data.Skip(data.Length - threeYears).Take(threeYears).ToArray();
            }
            //detrend to make stationary
            data = TimeSeries.toStationary(data);

            //hourly
            //var seasons = new int[] { 24, 24 * 7, 24 * 7 * 4, 24 * 7 * 4 * 6, 24 * 7 * 4 * 12};
            //var arSes = new int[] { 1, 2, 24, 25 };

            //daily
            //var seasons = new int[] { 7, 7*4, 7*4*12 };
            //var arSes = new int[] { 1, 7, 7*4 };

            //var arma = TimeSeries.ARMASimple2(data, arSes, seasons);

            //fitted model from matlab
            var arlags = new int[] { 1, 2, 24, 25 };
            //var ar = new TimeSeries.LagOp(new double[] { 1.04216, -0.125323, 0.777886, -0.699448 }, arlags);
            var malags = new int[] { 24, 168 };
            //var ma = new TimeSeries.LagOp(new double[] { -0.44595, 0.175135 }, malags);
            //var xarma = TimeSeries.ARMASimple3(ar, ma, 0.166685, 2.80023);

            //our fitting procedure.. difference...? not much...
            var arma = TimeSeries.ARMASimple2(data, arlags, malags);
            arma = TimeSeries.ARMASimple3(arma.AR, arma.MA, arma.Const, volatility);

            var inSampleRes = TimeSeries.Infer(arma, data);

            var sims = arma.Simulate(numberOfSimulations, horizon, data, inSampleRes);
                        
            var hoursSteps = timeStepsInLevels.Select(x => x * 24).ToArray();

            var desezonalizedData = Desezonalize(data, 168);

            //add the spikes
            //Then perform the spike estimation 
            var spikesThreshold = 1.7;
            var spikeIndices = EstimateSpikesOnMovSDs(desezonalizedData, 24, 2, spikesThreshold);

            //select hour
            var peakHour = 16;
            var peakhourData = TakeShortPeriods(data, 1, peakHour, 24);
            var peakSpikeIndices = EstimateSpikesOnMovSDs(peakhourData, 7, 2, spikesThreshold);
            var distrib = EstimateSpikesDistribution(peakhourData, peakSpikeIndices, Forecast.SpikePreprocess.SimilarDay, spikesThreshold);

            //for testing..
            var n = new Normal(distrib.Item1.Mean, distrib.Item1.Variance);
            var p = new Poisson(distrib.Item2.Lambda * 24);

            //replace spikes now...
            //TODO: replace at the right hour... yes...
            sims = Simulations.spikeSimulationsReplace(sims, p, n);

            //fit the forward curve
            sims = Simulations.liftSeriesToPriceCurve(sims, hoursSteps, priceLevels);

            return Json(sims);
        }