Пример #1
0
        public void ProbabilityFunctionTest()
        {
            var p1 = new NormalDistribution(4.2, 1);
            var p2 = new NormalDistribution(7.0, 2);

            Independent<NormalDistribution> target = new Independent<NormalDistribution>(p1, p2);

            double[] x;
            double actual, expected;

            x = new double[] { 4.2, 7.0 };
            actual = target.ProbabilityDensityFunction(x);
            expected = p1.ProbabilityDensityFunction(x[0]) * p2.ProbabilityDensityFunction(x[1]);
            Assert.AreEqual(expected, actual, 1e-10);
            Assert.IsFalse(double.IsNaN(actual));

            x = new double[] { 0.0, 0.0 };
            actual = target.ProbabilityDensityFunction(x);
            expected = p1.ProbabilityDensityFunction(x[0]) * p2.ProbabilityDensityFunction(x[1]);
            Assert.AreEqual(expected, actual, 1e-10);
            Assert.IsFalse(double.IsNaN(actual));

            x = new double[] { 7.0, 4.2 };
            actual = target.ProbabilityDensityFunction(x);
            expected = p1.ProbabilityDensityFunction(x[0]) * p2.ProbabilityDensityFunction(x[1]);
            Assert.AreEqual(expected, actual, 1e-10);
            Assert.IsFalse(double.IsNaN(actual));
        }
Пример #2
0
        public void ConstructorTest5()
        {
            var normal = new NormalDistribution(mean: 4, stdDev: 4.2);

            double mean = normal.Mean;     // 4.0
            double median = normal.Median; // 4.0
            double var = normal.Variance;  // 17.64

            double cdf = normal.DistributionFunction(x: 1.4); // 0.26794249453351904
            double pdf = normal.ProbabilityDensityFunction(x: 1.4); // 0.078423391448155175
            double lpdf = normal.LogProbabilityDensityFunction(x: 1.4); // -2.5456330358182586

            double ccdf = normal.ComplementaryDistributionFunction(x: 1.4); // 0.732057505466481
            double icdf = normal.InverseDistributionFunction(p: cdf); // 1.4

            double hf = normal.HazardFunction(x: 1.4); // 0.10712736480747137
            double chf = normal.CumulativeHazardFunction(x: 1.4); // 0.31189620872601354

            string str = normal.ToString(CultureInfo.InvariantCulture); // N(x; μ = 4, σ² = 17.64)

            Assert.AreEqual(4.0, mean);
            Assert.AreEqual(4.0, median);
            Assert.AreEqual(17.64, var);
            Assert.AreEqual(0.31189620872601354, chf);
            Assert.AreEqual(0.26794249453351904, cdf);
            Assert.AreEqual(0.078423391448155175, pdf);
            Assert.AreEqual(-2.5456330358182586, lpdf);
            Assert.AreEqual(0.10712736480747137, hf);
            Assert.AreEqual(0.732057505466481, ccdf);
            Assert.AreEqual(1.4, icdf);
            Assert.AreEqual("N(x; μ = 4, σ² = 17.64)", str);
        }
Пример #3
0
        public PinkNoise(IRandomGenerator randomGenerator, float rmsAmplitude)
        {
            if (null == randomGenerator)
                throw new ArgumentNullException("randomGenerator");

            _whiteGenerator = new NormalDistribution(randomGenerator, 0, RmsScale * rmsAmplitude);
        }
Пример #4
0
        public void ConstructorTest()
        {
            var p1 = new NormalDistribution(4.2, 1);
            var p2 = new NormalDistribution(7.0, 2);

            Independent<NormalDistribution> target = new Independent<NormalDistribution>(p1, p2);

            Assert.AreEqual(target.Components[0], p1);
            Assert.AreEqual(target.Components[1], p2);

            Assert.AreEqual(2, target.Dimension);

            Assert.AreEqual(4.2, target.Mean[0]);
            Assert.AreEqual(7.0, target.Mean[1]);

            Assert.AreEqual(1, target.Variance[0]);
            Assert.AreEqual(4, target.Variance[1]);

            Assert.AreEqual(1, target.Covariance[0, 0]);
            Assert.AreEqual(4, target.Covariance[1, 1]);
            Assert.AreEqual(0, target.Covariance[0, 1]);
            Assert.AreEqual(0, target.Covariance[1, 0]);

            var text = target.ToString("N2", System.Globalization.CultureInfo.InvariantCulture);

            Assert.AreEqual("Independent(x0, x1; N(x0; μ = 4.20, σ² = 1.00) + N(x1; μ = 7.00, σ² = 4.00))", text);
        }
        public void ConstructorTest2()
        {
            var original =  new NormalDistribution(mean: 4, stdDev: 4.2);

            var normal = GeneralContinuousDistribution.FromDensityFunction(
                original.Support, original.ProbabilityDensityFunction);

            testNormal(normal);
        }
        public static HiddenMarkovClassifier<Independent> CreateModel1()
        {
            // Create a Continuous density Hidden Markov Model Sequence Classifier
            // to detect a multivariate sequence and the same sequence backwards.
            double[][][] sequences = new double[][][]
            {
                new double[][] 
                { 
                    // This is the first  sequence with label = 0
                    new double[] { 0 },
                    new double[] { 1 },
                    new double[] { 2 },
                    new double[] { 3 },
                    new double[] { 4 },
                }, 

                new double[][]
                {
                     // This is the second sequence with label = 1
                    new double[] { 4 },
                    new double[] { 3 },
                    new double[] { 2 },
                    new double[] { 1 },
                    new double[] { 0 },
                }
            };

            // Labels for the sequences
            int[] labels = { 0, 1 };

            // Creates a sequence classifier containing 2 hidden Markov Models
            //  with 2 states and an underlying Normal distribution as density.
            NormalDistribution component = new NormalDistribution();
            Independent density = new Independent(component);
            var classifier = new HiddenMarkovClassifier<Independent>(2, new Ergodic(2), density);

            // Configure the learning algorithms to train the sequence classifier
            var teacher = new HiddenMarkovClassifierLearning<Independent>(classifier,

                // Train each model until the log-likelihood changes less than 0.001
                modelIndex => new BaumWelchLearning<Independent>(classifier.Models[modelIndex])
                {
                    Tolerance = 0.0001,
                    Iterations = 0
                }
            );

            // Train the sequence classifier using the algorithm
            double logLikelihood = teacher.Run(sequences, labels);

            Assert.AreEqual(-13.271981026832929d, logLikelihood);

            return classifier;
        }
        public void LearnTest1()
        {
            // Create a Continuous density Hidden Markov Model Sequence Classifier
            // to detect a univariate sequence and the same sequence backwards.
            double[][] sequences = new double[][] 
            {
                new double[] { 0,1,2,3,4 }, // This is the first  sequence with label = 0
                new double[] { 4,3,2,1,0 }, // This is the second sequence with label = 1
            };

            // Labels for the sequences
            int[] labels = { 0, 1 };

            // Creates a sequence classifier containing 2 hidden Markov Models
            //  with 2 states and an underlying Normal distribution as density.
            NormalDistribution density = new NormalDistribution();
            var classifier = new HiddenMarkovClassifier<NormalDistribution>(2, new Ergodic(2), density);

            // Configure the learning algorithms to train the sequence classifier
            var teacher = new HiddenMarkovClassifierLearning<NormalDistribution>(classifier,

                // Train each model until the log-likelihood changes less than 0.001
                modelIndex => new BaumWelchLearning<NormalDistribution>(classifier.Models[modelIndex])
                {
                    Tolerance = 0.0001,
                    Iterations = 0
                }
            );

            // Train the sequence classifier using the algorithm
            double logLikelihood = teacher.Run(sequences, labels);


            // Calculate the probability that the given
            //  sequences originated from the model
            double likelihood1, likelihood2;

            // Try to classify the first sequence (output should be 0)
            int c1 = classifier.Compute(sequences[0], out likelihood1);

            // Try to classify the second sequence (output should be 1)
            int c2 = classifier.Compute(sequences[1], out likelihood2);

            Assert.AreEqual(0, c1);
            Assert.AreEqual(1, c2);


            Assert.AreEqual(-13.271981026832929, logLikelihood, 1e-10);
            Assert.AreEqual(0.99999791320102149, likelihood1, 1e-10);
            Assert.AreEqual(0.99999791320102149, likelihood2, 1e-10);
            Assert.IsFalse(double.IsNaN(logLikelihood));
            Assert.IsFalse(double.IsNaN(likelihood1));
            Assert.IsFalse(double.IsNaN(likelihood2));
        }
        public void ConstructorTest5()
        {
            var normal = new NormalDistribution(mean: 4, stdDev: 4.2);

            double mean = normal.Mean;     // 4.0
            double median = normal.Median; // 4.0
            double mode = normal.Mode;     // 4.0
            double var = normal.Variance;  // 17.64

            double cdf = normal.DistributionFunction(x: 1.4); // 0.26794249453351904
            double pdf = normal.ProbabilityDensityFunction(x: 1.4); // 0.078423391448155175
            double lpdf = normal.LogProbabilityDensityFunction(x: 1.4); // -2.5456330358182586

            double ccdf = normal.ComplementaryDistributionFunction(x: 1.4); // 0.732057505466481
            double icdf = normal.InverseDistributionFunction(p: cdf); // 1.4

            double hf = normal.HazardFunction(x: 1.4); // 0.10712736480747137
            double chf = normal.CumulativeHazardFunction(x: 1.4); // 0.31189620872601354

            string str = normal.ToString(CultureInfo.InvariantCulture); // N(x; μ = 4, σ² = 17.64)

            Assert.AreEqual(4.0, mean);
            Assert.AreEqual(4.0, median);
            Assert.AreEqual(4.0, mode);
            Assert.AreEqual(17.64, var);
            Assert.AreEqual(0.31189620872601354, chf);
            Assert.AreEqual(0.26794249453351904, cdf);
            Assert.AreEqual(0.078423391448155175, pdf);
            Assert.AreEqual(-2.5456330358182586, lpdf);
            Assert.AreEqual(0.10712736480747137, hf);
            Assert.AreEqual(0.732057505466481, ccdf);
            Assert.AreEqual(1.4, icdf);
            Assert.AreEqual("N(x; μ = 4, σ² = 17.64)", str);

            Assert.AreEqual(Accord.Math.Normal.Function(normal.ZScore(4.2)), normal.DistributionFunction(4.2));
            Assert.AreEqual(Accord.Math.Normal.Derivative(normal.ZScore(4.2)) / normal.StandardDeviation, normal.ProbabilityDensityFunction(4.2), 1e-16);
            Assert.AreEqual(Accord.Math.Normal.LogDerivative(normal.ZScore(4.2)) - Math.Log(normal.StandardDeviation), normal.LogProbabilityDensityFunction(4.2), 1e-15);

            var range1 = normal.GetRange(0.95);
            var range2 = normal.GetRange(0.99);
            var range3 = normal.GetRange(0.01);

            Assert.AreEqual(-2.9083852331961833, range1.Min);
            Assert.AreEqual(10.908385233196183, range1.Max);
            Assert.AreEqual(-5.7706610709715314, range2.Min);
            Assert.AreEqual(13.770661070971531, range2.Max);
            Assert.AreEqual(-5.7706610709715314, range3.Min);
            Assert.AreEqual(13.770661070971531, range3.Max);
        }
        public void NormalGenerateTest()
        {
            // Create a Normal with mean 2 and sigma 5
            var normal = new NormalDistribution(2, 5);

            // Generate 1000000 samples from it
            double[] samples = normal.Generate(1000000);

            // Try to estimate a new Normal distribution from the 
            // generated samples to check if they indeed match
            var actual = NormalDistribution.Estimate(samples);

            string result = actual.ToString("N2"); // N(x; μ = 2.01, σ² = 25.03)

            Assert.AreEqual("N(x; μ = 2.01, σ² = 25.03)", result);
        }
Пример #10
0
    public static NormalDistribution ByParams(double expectation, double variance)
    {
        if (Double.IsInfinity(expectation) || Double.IsNaN(expectation))
                throw new ArgumentException("The expectation must be a finite number");

            if (variance <= 0 || Double.IsInfinity(expectation) || Double.IsNaN(expectation))
                throw new ArgumentException("The variance must be a positive finite number");

            NormalDistribution distribution = new NormalDistribution();
            distribution.Expectation = expectation;
            distribution.Variance = variance;

            distribution.ComputeInternalParameters();

            return distribution;
    }
    // Use this for initialization
    void Start()
    {
        // Save a pointer to the fusion engine
        fusionEngine = GetComponentInParent<FusionEngine>();

        // Get a pointer to the target
        targets = GameObject.FindGameObjectsWithTag("Target");

        // Noise distribution
        nd = new NormalDistribution(0, 1);
        noiseCovariance = new Matrix(3, 3);
        noiseCovariance[0, 0] = 1e-3;
        noiseCovariance[1, 1] = 1e-3;
        noiseCovariance[2, 2] = 1e-3;

        noiseCovCholT = noiseCovariance.CholeskyDecomposition.TriangularFactor.Clone();
        noiseCovCholT.Transpose();

        // Reset time since the last update
        timeSinceLastUpdateSec = 0.0f;
    }
        public void ConstructorTest1()
        {

            NormalDistribution normal = new NormalDistribution(4.2, 1.2);
            MultivariateNormalDistribution target = new MultivariateNormalDistribution(new[] { 4.2 }, new[,] { { 1.2 * 1.2 } });

            double[] mean = target.Mean;
            double[] median = target.Median;
            double[] var = target.Variance;
            double[,] cov = target.Covariance;

            double apdf1 = target.ProbabilityDensityFunction(new double[] { 2 });
            double apdf2 = target.ProbabilityDensityFunction(new double[] { 4 });
            double apdf3 = target.ProbabilityDensityFunction(new double[] { 3 });
            double alpdf = target.LogProbabilityDensityFunction(new double[] { 3 });
            double acdf = target.DistributionFunction(new double[] { 3 });
            double accdf = target.ComplementaryDistributionFunction(new double[] { 3 });

            double epdf1 = target.ProbabilityDensityFunction(new double[] { 2 });
            double epdf2 = target.ProbabilityDensityFunction(new double[] { 4 });
            double epdf3 = target.ProbabilityDensityFunction(new double[] { 3 });
            double elpdf = target.LogProbabilityDensityFunction(new double[] { 3 });
            double ecdf = target.DistributionFunction(new double[] { 3 });
            double eccdf = target.ComplementaryDistributionFunction(new double[] { 3 });


            Assert.AreEqual(normal.Mean, target.Mean[0]);
            Assert.AreEqual(normal.Median, target.Median[0]);
            Assert.AreEqual(normal.Variance, target.Variance[0]);
            Assert.AreEqual(normal.Variance, target.Covariance[0, 0]);

            Assert.AreEqual(epdf1, apdf1);
            Assert.AreEqual(epdf2, apdf2);
            Assert.AreEqual(epdf3, apdf3);
            Assert.AreEqual(elpdf, alpdf);
            Assert.AreEqual(ecdf, acdf);
            Assert.AreEqual(eccdf, accdf);
            Assert.AreEqual(1.0 - ecdf, eccdf);
        }
Пример #13
0
        public Combat(Unit attacker, Unit defender, bool useRandomisedCombatEfficiency, List<Unit> antiAirUnits = null)
        {
            UseRandomisedCombatEfficiency = useRandomisedCombatEfficiency;
            Generator = new NormalDistribution(GameConstants.CombatEfficiencyMean, GameConstants.CombatEfficiencyDeviation);

            Attacker = new UnitCombatState(attacker, this);
            Defender = new UnitCombatState(defender, this);

            if (attacker.IsAirUnit())
            {
                AttackType = AttackType.AirAttack;
                AntiAirUnits = antiAirUnits.Select((Unit x) => new UnitCombatState(x, this)).ToList();
                foreach (var unit in AntiAirUnits)
                    unit.SetDamage(unit.Unit.Type.Stats.AirAttack.Value);
            }
            else if (attacker.IsArtillery())
                AttackType = AttackType.ArtilleryAttack;
            else
                AttackType = AttackType.GroundAttack;

            // Calculate the outcome right away
            Attack();
        }
Пример #14
0
        public void ConstructorTest()
        {
            var p1 = new NormalDistribution(4.2, 1);
            var p2 = new NormalDistribution(7.0, 2);

            Independent<NormalDistribution> target = new Independent<NormalDistribution>(p1, p2);

            Assert.AreEqual(target.Components[0], p1);
            Assert.AreEqual(target.Components[1], p2);

            Assert.AreEqual(2, target.Dimension);

            Assert.AreEqual(4.2, target.Mean[0]);
            Assert.AreEqual(7.0, target.Mean[1]);

            Assert.AreEqual(1, target.Variance[0]);
            Assert.AreEqual(4, target.Variance[1]);

            Assert.AreEqual(1, target.Covariance[0, 0]);
            Assert.AreEqual(4, target.Covariance[1, 1]);
            Assert.AreEqual(0, target.Covariance[0, 1]);
            Assert.AreEqual(0, target.Covariance[1, 0]);
        }
Пример #15
0
        public void MixtureFitTest()
        {
            var samples1 = new NormalDistribution(mean: -2, stdDev: 0.5).Generate(100000);
            var samples2 = new NormalDistribution(mean: +4, stdDev: 0.5).Generate(100000);

            // Mix the samples from both distributions
            var samples = samples1.Concatenate(samples2);

            // Create a new mixture distribution with two Normal components
            var mixture = new Mixture <NormalDistribution>(new[] { 0.2, 0.8 },
                                                           new NormalDistribution(-1),
                                                           new NormalDistribution(+1));

            // Estimate the distribution
            mixture.Fit(samples, new MixtureOptions
            {
                Iterations = 50,
                Threshold  = 0
            });

            var result = mixture.ToString("N2", System.Globalization.CultureInfo.InvariantCulture);

            Assert.AreEqual("Mixture(x; 0.50*N(x; μ = -2.00, σ² = 0.25) + 0.50*N(x; μ = 4.00, σ² = 0.25))", result);
        }
Пример #16
0
        public void GammaEffectSizeMultimodalTest()
        {
            var random = new Random(42);
            var x      = new NormalDistribution(0, 1).Random(random).Next(200)
                         .Concat(new NormalDistribution(10, 1).Random(random).Next(200))
                         .ToSample();
            var y = new NormalDistribution(0, 1).Random(random).Next(200)
                    .Concat(new NormalDistribution(20, 1).Random(random).Next(200))
                    .ToSample();

            var probabilities = new Probability[] { 0.2, 0.3, 0.4, 0.6, 0.7, 0.8 };
            var minExpected   = new[] { -0.1, -0.1, -0.1, 0.8, 0.8, 0.8 };
            var maxExpected   = new[] { 0.1, 0.1, 0.1, 0.9, 0.9, 0.9 };

            var gammaEffectSizeFunction = GammaEffectSizeFunction.Instance;

            for (int i = 0; i < probabilities.Length; i++)
            {
                var    p     = probabilities[i];
                double gamma = gammaEffectSizeFunction.GetValue(x, y, p);
                output.WriteLine($"{p}: {gamma:0.0000}");
                Assert.True(minExpected[i] < gamma && gamma < maxExpected[i]);
            }
        }
Пример #17
0
        public DataTable CreateNormalData(double mean, double std, double?min = null, double?max = null, int numberOfData = 300)
        {
            var gaussTable         = new DataTable();
            var normalDistribution = new NormalDistribution(mean, std);
            var randomGenerator    = new RandomGenerator();

            gaussTable.AddColumn <double>(Constants.X);
            gaussTable.AddColumn <double>(Constants.Y);

            var minToUse = min ?? mean - 4 * std;
            var maxToUse = max ?? mean + 4 * std;

            for (int i = 0; i < numberOfData; i++)
            {
                var x   = randomGenerator.UniformDeviate(minToUse, maxToUse);
                var y   = normalDistribution.ProbabilityDensityFor(x);
                var row = gaussTable.NewRow();
                row[Constants.X] = x;
                row[Constants.Y] = y;
                gaussTable.Rows.Add(row);
            }

            return(gaussTable);
        }
Пример #18
0
        static public double[,] GenerateData(int smpCount, int n, string exprM, string exprS)
        {
            double[,] data = new double[smpCount, n];
            Parser prsM = new Parser(), prsS = new Parser();

            prsM.SetExpr(exprM);
            prsS.SetExpr(exprS);
            ParserVariable varT = new ParserVariable();

            prsM.DefineVar("t", varT);
            prsS.DefineVar("t", varT);
            NormalDistribution dist = new NormalDistribution();

            for (int i = 0; i < smpCount; i++)
            {
                varT.Value = i + 1;
                dist.SetDistributionParameters(prsM.Eval(), prsS.Eval());
                for (int j = 0; j < n; j++)
                {
                    data[i, j] = dist.NextDouble();
                }
            }
            return(data);
        }
Пример #19
0
        public void FitTest()
        {
            double expectedMean  = 1.125;
            double expectedSigma = 1.01775897605147;

            NormalDistribution target;

            target = new NormalDistribution();
            double[] observations = { 0.10, 0.40, 2.00, 2.00 };
            double[] weights      = { 0.25, 0.25, 0.25, 0.25 };
            target.Fit(observations, weights);

            Assert.AreEqual(expectedMean, target.Mean);
            Assert.AreEqual(expectedSigma, target.StandardDeviation, 1e-6);


            target = new NormalDistribution();
            double[] observations2 = { 0.10, 0.10, 0.40, 2.00 };
            double[] weights2      = { 0.125, 0.125, 0.25, 0.50 };
            target.Fit(observations2, weights2);

            Assert.AreEqual(expectedMean, target.Mean);
            // Assert.AreEqual(expectedSigma, target.StandardDeviation, 1e-6);
        }
Пример #20
0
        /// <summary>
        /// Creates a new instance of the <see cref="ClosingStructuresInput"/> class.
        /// </summary>
        public ClosingStructuresInput()
        {
            factorStormDurationOpenStructure = new RoundedDouble(2, 1.0);
            deviationWaveDirection           = new RoundedDouble(deviationWaveDirectionNumberOfDecimals);

            drainCoefficient = new LogNormalDistribution(2)
            {
                Mean = (RoundedDouble)1,
                StandardDeviation = (RoundedDouble)0.2
            };

            modelFactorSuperCriticalFlow = new NormalDistribution(2)
            {
                Mean = (RoundedDouble)1.1,
                StandardDeviation = (RoundedDouble)0.05
            };

            thresholdHeightOpenWeir       = new NormalDistribution(2);
            areaFlowApertures             = new LogNormalDistribution(2);
            levelCrestStructureNotClosing = new NormalDistribution(2);
            insideWaterLevel = new NormalDistribution(2);

            SetDefaultSchematizationProperties();
        }
        public void SetBlackScholesPrice()
        {
            double d1 = (1 / (AnnualVolatility * Math.Sqrt(ValuationTimeSpan.Years())))
                        * (Math.Log(StockPrice / Strike) + (RiskFreeRate + 0.5 * Math.Pow(AnnualVolatility, 2)) * ValuationTimeSpan.Years());

            double d2 = d1 - AnnualVolatility * Math.Sqrt(ValuationTimeSpan.Years());

            NormalDistribution normalDistribution = new NormalDistribution();

            if (OptionType == Enums.OptionType.Call)
            {
                BlackScholesValue = normalDistribution.DistributionFunction(d1) * StockPrice
                                    - normalDistribution.DistributionFunction(d2) * Strike * Math.Exp(-RiskFreeRate * ValuationTimeSpan.Years());
            }
            else if (OptionType == Enums.OptionType.Put)
            {
                BlackScholesValue = -normalDistribution.DistributionFunction(-d1) * StockPrice
                                    + normalDistribution.DistributionFunction(-d2) * Strike * Math.Exp(-RiskFreeRate * ValuationTimeSpan.Years());
            }
            else
            {
                throw new Exception($"Failed: OptionType {OptionType.ToString()} is not supported");
            }
        }
Пример #22
0
        public void Run()
        {
            var x = new NormalDistribution(mean: 20, stdDev: 2).Random(1).Next(20).Concat(
                new NormalDistribution(mean: 40, stdDev: 2).Random(2).Next(20)).ToArray();
            var y = new NormalDistribution(mean: 20, stdDev: 2).Random(3).Next(20).Concat(
                new NormalDistribution(mean: 80, stdDev: 2).Random(4).Next(20)).ToArray();

            var probabilities = new[] { 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0 };
            var shift         = ShiftFunction.Instance.Values(x, y, probabilities);
            var ratio         = RatioFunction.Instance.Values(x, y, probabilities);

            Console.WriteLine("Probability Shift Ratio");
            for (int i = 0; i < probabilities.Length; i++)
            {
                Console.WriteLine(
                    probabilities[i].ToString("N1").PadRight(11) + " " +
                    shift[i].ToString("N1").PadLeft(5) + " " +
                    ratio[i].ToString("N1").PadLeft(5));
            }

            Console.WriteLine();
            Console.WriteLine("Shift Range: " + ShiftRangeEstimator.Instance.GetRange(x, y));
            Console.WriteLine("Ratio Range: " + RatioRangeEstimator.Instance.GetRange(x, y));
        }
        public static double CalculateCorrelation(double[] set1, double[] set2, bool regulate = true)
        {
            var x = NormalDistribution.Estimate(set1, new NormalOptions()
            {
                Regularization = regulate ? 1 : 0
            });
            var y = NormalDistribution.Estimate(set2, new NormalOptions()
            {
                Regularization = regulate ? 1 : 0
            });

            double totalSum = 0;

            for (int i = 0; i < set1.Length; i++)
            {
                totalSum += (set1[i] - x.Mean) * (set2[i] - y.Mean);
            }

            totalSum /= (set1.Length - 1);

            var corr = totalSum / (x.StandardDeviation * y.StandardDeviation);

            return(corr);
        }
Пример #24
0
        public void MomentMapTest()
        {
            Distribution d = new NormalDistribution();

            for (int n = 1; n < 11; n++)
            {
                double[] K = new double[n + 1];
                K[0] = 1.0;
                if (K.Length > 1)
                {
                    K[1] = 0.0;
                }
                if (K.Length > 2)
                {
                    K[2] = 1.0;
                }
                //for (int m = 1; m < K.Length; m++) {
                //    K[m] = AdvancedIntegerMath.Factorial(m - 1);
                //}

                double M = MomentMath.RawMomentFromCumulants(K);
                Console.WriteLine("{0} {1}", d.Moment(n), M);
            }
        }
Пример #25
0
        //[TestMethod]
        public void TimeNormalGenerators()
        {
            Random rng = new Random(1);
            //IDeviateGenerator nRng = new BoxMullerNormalGenerator();
            //IDeviateGenerator nRng = new PolarRejectionNormalDeviateGenerator();
            //IDeviateGenerator nRng = new RatioOfUniformsNormalGenerator();
            IDeviateGenerator nRng = new LevaNormalGenerator();

            //Sample sample = new Sample();
            ContinuousDistribution nrm = new NormalDistribution();

            Stopwatch timer = Stopwatch.StartNew();
            double    sum   = 0.0;

            for (int i = 0; i < 10000000; i++)
            {
                sum += nrm.InverseLeftProbability(rng.NextDouble());
            }
            timer.Stop();

            //Console.WriteLine(sample.KolmogorovSmirnovTest(new NormalDistribution()).RightProbability);
            Console.WriteLine(sum);
            Console.WriteLine(timer.ElapsedMilliseconds);
        }
        /// <summary>
        /// Plots the sealing slabs.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public override IEnumerable <T> PlotSealingSlabs <T>()
        {
            var sealingSlabs = base.PlotSealingSlabs <T>();

            var randomDerivationOffset        = new NormalDistribution(0, standardDerivationOffset);
            var randomDerivationRadius        = new NormalDistribution(0, standardDerivationRadius);
            var randomDerivationDrillingPoint = new NormalDistribution(0, standardDerivationDrillingPoint);

            foreach (var sealingSlab in sealingSlabs)
            {
                sealingSlab.BaseX = sealingSlab.X;
                sealingSlab.BaseY = sealingSlab.Y;

                sealingSlab.OffsetX = (int)GetRandomDerivationOffsetPixel(randomDerivationOffset);
                sealingSlab.OffsetY = (int)GetRandomDerivationOffsetPixel(randomDerivationOffset);

                sealingSlab.OffsetDrillingPointX = (int)GetRandomDerivationDrillingPointPixel(randomDerivationDrillingPoint);
                sealingSlab.OffsetDrillingPointY = (int)GetRandomDerivationDrillingPointPixel(randomDerivationDrillingPoint);

                sealingSlab.Radius = (int)(sealingSlab.Radius + GetRandomDerivationRadiusPixel(randomDerivationRadius));
            }

            return(sealingSlabs);
        }
Пример #27
0
        public void KolmogorovSmirnovTestConstructorTest2()
        {
            // Test against a Normal distribution

            double[] sample = { 0.621, 0.503, 0.203, 0.477, 0.710, 0.581, 0.329, 0.480, 0.554, 0.382 };

            // The sample is most likely from a uniform distribution, so it would
            // most likely be different from a standard normal distribution.

            NormalDistribution distribution = NormalDistribution.Standard;
            var target = new KolmogorovSmirnovTest(sample, distribution);

            Assert.AreEqual(distribution, target.TheoreticalDistribution);
            Assert.AreEqual(KolmogorovSmirnovTestHypothesis.SampleIsDifferent, target.Hypothesis);
            Assert.AreEqual(DistributionTail.TwoTail, target.Tail);

            Assert.AreEqual(0.580432, target.Statistic, 1e-5);
            Assert.AreEqual(0.000999, target.PValue, 1e-5);
            Assert.IsFalse(Double.IsNaN(target.Statistic));

            // The null hypothesis can be rejected:
            // the sample is not from a standard Normal distribution
            Assert.IsTrue(target.Significant);
        }
Пример #28
0
        public string SetDistributionForBuilding(string name, int?ageQuant)
        {
            if (string.IsNullOrEmpty(Name))
            {
                Name = name;
            }

            if (DistributionFunction == null && DistributionfunctionString != null)
            {
                SetDistributionFunctionFromString();
            }

            if (DistributionFunction != null)
            {
                CanGenerateSamples = true;
                return(string.Empty);
            }

            if (NumberOfSamples == 0 || StDeviation == 0)
            {
                CanGenerateSamples = false;
                return("Stat with name " + (Name) + " and ageQuant= " + (ageQuant ?? -1) + "is marked as CanGenerateSamples=false due to lacking samples");
            }

            try
            {
                DistributionFunction = new NormalDistribution(Mean, StDeviation);
                CanGenerateSamples   = true;
                return("Warning: Found no distribution-function for " + (Name) + " and ageQuant= " + (ageQuant ?? -1) + $". Create one using NormalDistribution using mean={Mean} and stddev={StDeviation}");
            }
            catch (Exception e)
            {
                CanGenerateSamples = false;
                return("Stat with name " + (Name) + " and ageQuant= " + (ageQuant ?? -1) + " is marked as CanGenerateSamples=false due failure to create NormalDistribution: " + e.Message);
            }
        }
Пример #29
0
        public void Constructor_ExpectedValues()
        {
            // Setup
            var mockRepository = new MockRepository();
            var handler        = mockRepository.Stub <IObservablePropertyChangeHandler>();

            mockRepository.ReplayAll();

            var distribution   = new NormalDistribution();
            var designVariable = new NormalDistributionDesignVariable(distribution);

            // Call
            var properties = new NormalDistributionDesignVariableProperties(DistributionReadOnlyProperties.All,
                                                                            designVariable,
                                                                            handler);

            // Assert
            Assert.IsInstanceOf <DesignVariableProperties <NormalDistribution> >(properties);
            Assert.AreSame(distribution, properties.Data);
            Assert.AreEqual(distribution.Mean, properties.Mean);
            Assert.AreEqual(distribution.StandardDeviation, properties.StandardDeviation);
            Assert.AreEqual("Normaal", properties.DistributionType);
            mockRepository.VerifyAll();
        }
Пример #30
0
        public void PhreaticLevelExit_SetNewValue_UpdateMeanAndStandardDeviation()
        {
            // Setup
            var random               = new Random(22);
            var input                = new TestPipingInput();
            var mean                 = (RoundedDouble)(0.01 + random.NextDouble());
            var standardDeviation    = (RoundedDouble)(0.01 + random.NextDouble());
            var expectedDistribution = new NormalDistribution(3)
            {
                Mean = mean,
                StandardDeviation = standardDeviation
            };
            var distributionToSet = new NormalDistribution(5)
            {
                Mean = mean,
                StandardDeviation = standardDeviation
            };

            // Call
            input.PhreaticLevelExit = distributionToSet;

            // Assert
            DistributionTestHelper.AssertDistributionCorrectlySet(input.PhreaticLevelExit, distributionToSet, expectedDistribution);
        }
Пример #31
0
            public static float Calculate(Weapon weapon, float rawDamage)
            {
                var damagePerShot = weapon.DamagePerShot;
                var adjustment    = rawDamage / damagePerShot;
                var variance      = weapon.weaponDef.DamageVariance;
                var roll          = NormalDistribution.Random(
                    new VarianceBounds(
                        damagePerShot - variance,
                        damagePerShot + variance,
                        Core.ModSettings.StandardDeviationSimpleVarianceMultiplier * variance
                        ));
                var variantDamage = roll * adjustment;

                var sb = new StringBuilder();

                sb.AppendLine($"roll: {roll}");
                sb.AppendLine($"damagePerShot: {damagePerShot}");
                sb.AppendLine($"variance: {variance}");
                sb.AppendLine($"adjustment: {adjustment}");
                sb.AppendLine($"variantDamage: {variantDamage}");
                Logger.Debug(sb.ToString());

                return(variantDamage);
            }
Пример #32
0
 /**
  * Generate a new ARTA-Process with the given parameters.
  * */
 public ArProcess(double[] alphas, NormalDistribution whiteNoiseProcess)
 {
     this.alphas            = alphas;
     this.values            = new DoubleHistory(alphas.Length);
     this.whiteNoiseProcess = whiteNoiseProcess;
 }
Пример #33
0
 public static double Sample(StdRandom random, double mu, double sigma) =>
 Math.Exp(NormalDistribution.Sample(random, mu, sigma));
        //---------------------------------------------------------------------
        ///<summary>
        /// Run the Biomass Insects extension at a particular timestep.
        ///</summary>
        public override void Run()
        {

            running = true;
            UI.WriteLine("   Processing landscape for Biomass Insect events ...");

            foreach(IInsect insect in manyInsect)
            {

                if(insect.MortalityYear == Model.Core.CurrentTime)
                    Outbreak.Mortality(insect);

                // Copy the data from current to last
                foreach (ActiveSite site in Model.Core.Landscape)
                    insect.LastYearDefoliation[site] = insect.ThisYearDefoliation[site];

                insect.ThisYearDefoliation.ActiveSiteValues = 0.0;
                //SiteVars.BiomassRemoved.ActiveSiteValues = 0;

                insect.ActiveOutbreak = false;
                insect.SingleOutbreakYear = false;

                NormalDistribution randVar = new NormalDistribution(RandomNumberGenerator.Singleton);
                randVar.Mu = 0;      // mean
                randVar.Sigma = 1;   // std dev
                double randomNum = randVar.NextDouble();

                ExponentialDistribution randVarE = new ExponentialDistribution(RandomNumberGenerator.Singleton);
                randVarE.Lambda = insect.MeanDuration;      // rate
                double randomNumE = randVarE.NextDouble();

                // First, has enough time passed since the last outbreak?
                double timeBetweenOutbreaks = insect.MeanTimeBetweenOutbreaks + (insect.StdDevTimeBetweenOutbreaks * randomNum);
                //double duration = insect.MeanDuration + (insect.StdDevDuration * randomNum);
                double duration = Math.Round(randomNumE + 1);
                
                if (duration > 5) // Limit maximum outbreak duration to 5 years for now.
                    duration = duration - 3;

                double timeAfterDuration = timeBetweenOutbreaks - duration;

                //UI.WriteLine("Calculated time between = {0}.  inputMeanTime={1}, inputStdTime={2}.", timeBetweenOutbreaks, insect.MeanTimeBetweenOutbreaks, insect.StdDevTimeBetweenOutbreaks);
                //UI.WriteLine("Calculated duration     = {0}.  inputMeanDura={1}, inputStdDura={2}.", duration, insect.MeanDuration, insect.StdDevDuration);
                //UI.WriteLine("Insect Start Time = {0}, Stop Time = {1}.", insect.OutbreakStartYear, insect.OutbreakStopYear);


                if(Model.Core.CurrentTime == 1)
                {
                    //UI.WriteLine("   Year 1:  Setting initial start and stop times.");
                    insect.OutbreakStartYear = (int) (timeBetweenOutbreaks / 2.0) + 1;
                    insect.OutbreakStopYear  = insect.OutbreakStartYear + (int) duration - 1;
                    UI.WriteLine("   {0} is not active.  StartYear={1}, StopYear={2}, CurrentYear={3}.", insect.Name, insect.OutbreakStartYear, insect.OutbreakStopYear, Model.Core.CurrentTime);
                }
                else if(insect.OutbreakStartYear <= Model.Core.CurrentTime
                    && insect.OutbreakStopYear > Model.Core.CurrentTime)
                {
                    //UI.WriteLine("   An outbreak starts or continues.  Start and stop time do not change.");
                    insect.ActiveOutbreak = true;
                    UI.WriteLine("   {0} is active.  StartYear={1}, StopYear={2}, CurrentYear={3}.", insect.Name, insect.OutbreakStartYear, insect.OutbreakStopYear, Model.Core.CurrentTime);

                    insect.MortalityYear = Model.Core.CurrentTime + 1;

                }
                //Special case for single year outbreak.
                else if(insect.OutbreakStartYear <= Model.Core.CurrentTime
                    && insect.OutbreakStopYear <= Model.Core.CurrentTime)
                {
                    insect.ActiveOutbreak = true;
                    UI.WriteLine("   {0} is active.  StartYear={1}, StopYear={2}, CurrentYear={3}.", insect.Name, insect.OutbreakStartYear, insect.OutbreakStopYear, Model.Core.CurrentTime);

                    if (insect.OutbreakStartYear == insect.OutbreakStopYear)
                        insect.SingleOutbreakYear = true;
                    insect.MortalityYear = Model.Core.CurrentTime + 1;
                    insect.OutbreakStartYear = Model.Core.CurrentTime + (int)timeBetweenOutbreaks;
                    insect.OutbreakStopYear = insect.OutbreakStartYear + (int)duration - 1;
                }

                else if(insect.OutbreakStopYear <= Model.Core.CurrentTime
                    && timeAfterDuration > Model.Core.CurrentTime - insect.OutbreakStopYear)
                {
                    //UI.WriteLine("   In between outbreaks, reset start and stop times.");
                    insect.ActiveOutbreak = true;
                    UI.WriteLine("   {0} is active.  StartYear={1}, StopYear={2}, CurrentYear={3}.", insect.Name, insect.OutbreakStartYear, insect.OutbreakStopYear, Model.Core.CurrentTime);

                    insect.MortalityYear = Model.Core.CurrentTime + 1;
                    insect.OutbreakStartYear = Model.Core.CurrentTime + (int) timeBetweenOutbreaks;
                    insect.OutbreakStopYear = insect.OutbreakStartYear + (int) duration - 1;
                }
                //UI.WriteLine("  Insect Start Time = {0}, Stop Time = {1}.", insect.OutbreakStartYear, insect.OutbreakStopYear);

                if(insect.ActiveOutbreak)
                {
                    //UI.WriteLine("   OutbreakStartYear={0}.", insect.OutbreakStartYear);

                    if(insect.OutbreakStartYear == Model.Core.CurrentTime || insect.SingleOutbreakYear)
                        // Initialize neighborhoodGrowthReduction with patches
                        Outbreak.InitializeDefoliationPatches(insect);
                    else
                        insect.NeighborhoodDefoliation.ActiveSiteValues = 0;

                    double sumDefoliation = 0.0;
                    int numSites = 0;
                    //foreach(ActiveSite site in Model.Core.Landscape)
                    //{
                    //    sumDefoliation += insect.ThisYearDefoliation[site];
                    //    if(insect.ThisYearDefoliation[site] > 0.0)
                    //        numSites++;

                    //}
                    double meanDefoliation = sumDefoliation / (double) numSites;


                    log.Write("{0},{1},{2},{3},{4},{5}",
                        Model.Core.CurrentTime,
                        insect.Name,
                        insect.OutbreakStartYear,
                        insect.OutbreakStopYear,
                        sumDefoliation,
                        numSites
                        );

                    //foreach (IEcoregion ecoregion in Ecoregions.Dataset)
                    //    log.Write(",{0}", 1);

                    log.WriteLine("");

                }


                //Only write maps if an outbreak is active.
                //if (!insect.ActiveOutbreak)
                //if (insect.OutbreakStartYear <= Model.Core.CurrentTime
                //    && insect.OutbreakStopYear + 1 >= Model.Core.CurrentTime)
                //if (insect.OutbreakStartYear <= Model.Core.CurrentTime)
                //    | insect.MortalityYear = Model.Core.CurrentTime)
                //    continue;

                //----- Write Insect GrowthReduction maps --------
                IOutputRaster<UShortPixel> map = CreateMap((Model.Core.CurrentTime - 1), insect.Name);

                using (map) {
                    UShortPixel pixel = new UShortPixel();
                    foreach (Site site in Model.Core.Landscape.AllSites) {
                        if (site.IsActive)
                                pixel.Band0 = (ushort) (insect.LastYearDefoliation[site] * 100.0);
                        else
                            //  Inactive site
                            pixel.Band0 = 0;

                        map.WritePixel(pixel);
                    }
                }

                //----- Write Initial Patch maps --------
                //IOutputRaster<UShortPixel> map2 = CreateMap(Model.Core.CurrentTime, ("InitialPatchMap" + insect.Name));
                //using (map2) {
                //    UShortPixel pixel = new UShortPixel();
                //    foreach (Site site in Model.Core.Landscape.AllSites) {
                //        if (site.IsActive)
                //        {
                //            if (insect.Disturbed[site])
                //                pixel.Band0 = (ushort) (SiteVars.InitialOutbreakProb[site] * 100);
                //            else
                //                pixel.Band0 = 0;
                //        }
                //        else
                //        {
                //            //  Inactive site
               //             pixel.Band0 = 0;
                //        }
                //        map2.WritePixel(pixel);
                //    }
                //}

                //----- Write Biomass Reduction maps --------
                IOutputRaster<UShortPixel> map3 = CreateMap(Model.Core.CurrentTime, ("BiomassRemoved" + insect.Name));
                using (map3) {
                    UShortPixel pixel = new UShortPixel();
                    foreach (Site site in Model.Core.Landscape.AllSites) {
                        if (site.IsActive)
                        {
                            // TESTING added by RMS
                            if(SiteVars.BiomassRemoved[site] > 0)
                               // UI.WriteLine("  Biomass revoved at {0}/{1}: {2}.", site.Location.Row, site.Location.Column, SiteVars.BiomassRemoved[site]);
                               pixel.Band0 = (ushort) (SiteVars.BiomassRemoved[site] / 100);
                            else
                               pixel.Band0 = 0;

                        }
                        else
                        {
                            //  Inactive site
                            pixel.Band0 = 0;
                        }
                        map3.WritePixel(pixel);
                        //Zero out the BiomassRemoved after the last insect mortality event in a given year.
                        //if (SiteVars.BiomassRemoved[site] > 0 && SiteVars.TimeOfLastEvent[site] < Model.Core.CurrentTime)
                            SiteVars.BiomassRemoved[site] = 0;
                    }
                }
            }

        }
Пример #35
0
        public void DistributionFunctionTest3()
        {
            double expected, actual;

            // Test small variance
            NormalDistribution target = new NormalDistribution(1.0, double.Epsilon);

            expected = 0;
            actual = target.DistributionFunction(0);
            Assert.AreEqual(expected, actual);

            expected = 0.5;
            actual = target.DistributionFunction(1.0);
            Assert.AreEqual(expected, actual);

            expected = 1.0;
            actual = target.DistributionFunction(1.0 + 1e-15);
            Assert.AreEqual(expected, actual);

            expected = 0.0;
            actual = target.DistributionFunction(1.0 - 1e-15);
            Assert.AreEqual(expected, actual);
        }
Пример #36
0
 public NormalRandomGenerator(NormalDistribution distribution)
 {
     this.distribution = distribution;
 }
Пример #37
0
 public NormalRandomGenerator(Random random, NormalDistribution distribution) : base(random)
 {
     this.distribution = distribution;
 }
Пример #38
0
        public void MedianTest()
        {
            NormalDistribution target = new NormalDistribution(0.4, 2.2);

            Assert.AreEqual(target.Median, target.InverseDistributionFunction(0.5));
        }
Пример #39
0
        /// <summary>
        /// Validate inverse cumulative distribution.
        /// </summary>
        /// <param name="x">Input X value.</param>
        /// <param name="p">Expected value.</param>
        public void ValidateInverseCumulativeDistribution(double x, double p)
        {
            var n = new NormalDistribution(5.0, 2.0);

            Assert.AreEqual(x, n.InverseCumulativeDistribution(p), 14);
        }
Пример #40
0
        public void GenerateTest()
        {
            NormalDistribution target = new NormalDistribution(2, 5);

            double[] samples = target.Generate(1000000);

            var actual = NormalDistribution.Estimate(samples);
            actual.Fit(samples);

            Assert.AreEqual(2, actual.Mean, 0.01);
            Assert.AreEqual(5, actual.StandardDeviation, 0.01);
        }
Пример #41
0
        public void GenerateTest2()
        {
            NormalDistribution target = new NormalDistribution(4, 2);

            double[] samples = new double[1000000];
            for (int i = 0; i < samples.Length; i++)
                samples[i] = target.Generate();

            var actual = NormalDistribution.Estimate(samples);
            actual.Fit(samples);

            Assert.AreEqual(4, actual.Mean, 0.01);
            Assert.AreEqual(2, actual.StandardDeviation, 0.01);
        }
Пример #42
0
        public void CloneTest()
        {
            NormalDistribution target = new NormalDistribution(0.5, 4.2);

            NormalDistribution clone = (NormalDistribution)target.Clone();

            Assert.AreNotSame(target, clone);
            Assert.AreEqual(target.Entropy, clone.Entropy);
            Assert.AreEqual(target.Mean, clone.Mean);
            Assert.AreEqual(target.StandardDeviation, clone.StandardDeviation);
            Assert.AreEqual(target.Variance, clone.Variance);
        }
Пример #43
0
        public void ZScoreTest()
        {
            double x = 5;
            double mean = 3;
            double dev = 6;

            NormalDistribution target = new NormalDistribution(mean, dev);

            double expected = (x - 3) / 6;
            double actual = target.ZScore(x);

            Assert.AreEqual(expected, actual);
        }
Пример #44
0
        public void InverseDistributionFunctionTest()
        {
            double[] expected =
            {
                Double.NegativeInfinity, -4.38252, -2.53481, -1.20248,
                -0.0640578, 1.0, 2.06406, 3.20248, 4.53481, 6.38252,
                Double.PositiveInfinity
            };

            NormalDistribution target = new NormalDistribution(1.0, 4.2);

            for (int i = 0; i < expected.Length; i++)
            {
                double x = i / 10.0;
                double actual = target.InverseDistributionFunction(x);
                Assert.AreEqual(expected[i], actual, 1e-5);
                Assert.IsFalse(Double.IsNaN(actual));
            }
        }
Пример #45
0
        ///<summary>
        ///</summary>
        ///<param name="boughtSold"></param>
        ///<param name="settled"></param>
        ///<param name="rateRenamed"></param>
        ///<param name="maturity"></param>
        ///<param name="tenor"></param>
        ///<param name="time"></param>
        ///<param name="ci"></param>
        ///<param name="volatility"></param>
        ///<param name="reversion"></param>
        ///<returns></returns>
        ///<exception cref="Exception"></exception>
        ///<exception cref="NotSupportedException"></exception>
        public static double AltSwaptionPCE(string boughtSold,
                                            string settled,
                                            double rateRenamed,
                                            double maturity,
                                            double tenor,
                                            double time,
                                            double ci,
                                            double volatility,
                                            double reversion)
        {
            //redenominate in years
            double mmaturity = maturity / 365.0;
            double ttime     = time / 365.0;
            double ttenor    = tenor / 365.0;
            double srs       = volatility * Math.Sqrt((1.0 - Math.Exp(-2 * reversion * ttime)) / (2 * reversion));
            //GEIS' U1
            double srt = volatility * Math.Sqrt((1.0 - Math.Exp(-2 * reversion * mmaturity)) / (2 * reversion));
            //GEIS' U2
            double altswaptionPCE;

            switch (boughtSold)
            {
            case "Bought":
                if (time < maturity)
                {
                    altswaptionPCE = Math.Pow((1.0 + rateRenamed), (ttime - mmaturity)) * (1.0 - Math.Pow((1 + rateRenamed), -ttenor)) * (1.0 - Math.Exp(-ci * srs));
                }
                else if ((time >= maturity) && (time < (maturity + tenor)))
                {
                    //branch on settlement type
                    switch (settled)
                    {
                    case "Physical":
                        altswaptionPCE = (1.0 - Math.Pow((1.0 + rateRenamed), (ttime - mmaturity - ttenor))) * (1.0 - Math.Exp(-ci * srs));
                        break;

                    case "Cash":
                        altswaptionPCE = 0.0D;
                        break;

                    default:
                        throw new Exception("Swaption settlement type: " + settled + "  Unknown");
                    }
                }
                else
                {
                    //must be on or beyond maturity + tenor...
                    altswaptionPCE = 0.0D;
                }
                break;

            case "Sold":
                if (time < maturity)
                {
                    altswaptionPCE = 0.0D;
                }
                else if ((time >= maturity) && (time < (maturity + tenor)))
                {
                    switch (settled)
                    {
                    case "Physical":
                        //*****************************************
                        // Tem1 = Application.NormSDist(srt)
                        // *****************************************
                        double tem1 = NormalDistribution.Probability(srt);
                        double srst = volatility * Math.Sqrt((1.0 - Math.Exp(-2 * reversion * (ttime - mmaturity))) / (2 * reversion));
                        double tem2 = Math.Exp(volatility * volatility * mmaturity / 2 - srst * ci);
                        double tem3 = 1.0 - Math.Pow((1.0 + rateRenamed), (ttime - mmaturity - ttenor));
                        altswaptionPCE = (1.0 - 2 * tem1 * tem2) * tem3;
                        if (altswaptionPCE < 0)
                        {
                            altswaptionPCE = 0.0D;
                        }
                        break;

                    case "Cash":
                        altswaptionPCE = 0.0D;
                        break;

                    default:
                        throw new Exception("Swaption settlement type: " + settled + "  Unknown");
                    }
                }
                else
                {
                    //outside range of interest, so.....
                    altswaptionPCE = 0.0D;
                }
                break;

            default:
                throw new NotSupportedException(boughtSold + " not implemented as a Boughtsold value");
            }
            return(altswaptionPCE);
        }
Пример #46
0
        public void RiskStatisticsTest()
        {
            //    ("Testing risk measures...");

            IncrementalGaussianStatistics igs = new IncrementalGaussianStatistics();
            RiskStatistics s = new RiskStatistics();

            double[] averages = { -100.0, -1.0, 0.0, 1.0, 100.0 };
            double[] sigmas = { 0.1, 1.0, 100.0 };
            int      i, j, k, N;

            N = (int)Math.Pow(2, 16) - 1;
            double        dataMin, dataMax;
            List <double> data = new InitializedList <double>(N), weights = new InitializedList <double>(N);

            for (i = 0; i < averages.Length; i++)
            {
                for (j = 0; j < sigmas.Length; j++)
                {
                    NormalDistribution           normal     = new NormalDistribution(averages[i], sigmas[j]);
                    CumulativeNormalDistribution cumulative = new CumulativeNormalDistribution(averages[i], sigmas[j]);
                    InverseCumulativeNormal      inverseCum = new InverseCumulativeNormal(averages[i], sigmas[j]);

                    SobolRsg rng = new SobolRsg(1);
                    dataMin = double.MaxValue;
                    dataMax = double.MinValue;
                    for (k = 0; k < N; k++)
                    {
                        data[k]    = inverseCum.value(rng.nextSequence().value[0]);
                        dataMin    = Math.Min(dataMin, data[k]);
                        dataMax    = Math.Max(dataMax, data[k]);
                        weights[k] = 1.0;
                    }

                    igs.addSequence(data, weights);
                    s.addSequence(data, weights);

                    // checks
                    double calculated, expected;
                    double tolerance;

                    if (igs.samples() != N)
                    {
                        QAssert.Fail("IncrementalGaussianStatistics: "
                                     + "wrong number of samples\n"
                                     + "    calculated: " + igs.samples() + "\n"
                                     + "    expected:   " + N);
                    }
                    if (s.samples() != N)
                    {
                        QAssert.Fail("RiskStatistics: wrong number of samples\n"
                                     + "    calculated: " + s.samples() + "\n"
                                     + "    expected:   " + N);
                    }


                    // weightSum()
                    tolerance  = 1e-10;
                    expected   = weights.Sum();
                    calculated = igs.weightSum();
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("IncrementalGaussianStatistics: "
                                     + "wrong sum of weights\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }
                    calculated = s.weightSum();
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("RiskStatistics: wrong sum of weights\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }


                    // min
                    tolerance  = 1e-12;
                    expected   = dataMin;
                    calculated = igs.min();
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("IncrementalGaussianStatistics: "
                                     + "wrong minimum value\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }
                    calculated = s.min();
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("RiskStatistics: "
                                     + "wrong minimum value\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }


                    // max
                    expected   = dataMax;
                    calculated = igs.max();
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("IncrementalGaussianStatistics: "
                                     + "wrong maximum value\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }
                    calculated = s.max();
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("RiskStatistics: "
                                     + "wrong maximum value\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }


                    // mean
                    expected  = averages[i];
                    tolerance = (expected == 0.0 ? 1.0e-13 :
                                 Math.Abs(expected) * 1.0e-13);
                    calculated = igs.mean();
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("IncrementalGaussianStatistics: "
                                     + "wrong mean value"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }
                    calculated = s.mean();
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("RiskStatistics: wrong mean value"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }


                    // variance
                    expected   = sigmas[j] * sigmas[j];
                    tolerance  = expected * 1.0e-1;
                    calculated = igs.variance();
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("IncrementalGaussianStatistics: "
                                     + "wrong variance"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }
                    calculated = s.variance();
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("RiskStatistics: wrong variance"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }


                    // standardDeviation
                    expected   = sigmas[j];
                    tolerance  = expected * 1.0e-1;
                    calculated = igs.standardDeviation();
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("IncrementalGaussianStatistics: "
                                     + "wrong standard deviation"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }
                    calculated = s.standardDeviation();
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("RiskStatistics: wrong standard deviation"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }


                    // missing errorEstimate() test

                    // skewness
                    expected   = 0.0;
                    tolerance  = 1.0e-4;
                    calculated = igs.skewness();
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("IncrementalGaussianStatistics: "
                                     + "wrong skewness"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }
                    calculated = s.skewness();
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("RiskStatistics: wrong skewness"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }


                    // kurtosis
                    expected   = 0.0;
                    tolerance  = 1.0e-1;
                    calculated = igs.kurtosis();
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("IncrementalGaussianStatistics: "
                                     + "wrong kurtosis"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }
                    calculated = s.kurtosis();
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("RiskStatistics: wrong kurtosis"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }


                    // percentile
                    expected  = averages[i];
                    tolerance = (expected == 0.0 ? 1.0e-3 :
                                 Math.Abs(expected * 1.0e-3));
                    calculated = igs.gaussianPercentile(0.5);
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("IncrementalGaussianStatistics: "
                                     + "wrong Gaussian percentile"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }
                    calculated = s.gaussianPercentile(0.5);
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("RiskStatistics: wrong Gaussian percentile"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }
                    calculated = s.percentile(0.5);
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("RiskStatistics: wrong percentile"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }



                    // potential upside
                    double upper_tail = averages[i] + 2.0 * sigmas[j],
                           lower_tail = averages[i] - 2.0 * sigmas[j];
                    double twoSigma   = cumulative.value(upper_tail);

                    expected  = Math.Max(upper_tail, 0.0);
                    tolerance = (expected == 0.0 ? 1.0e-3 :
                                 Math.Abs(expected * 1.0e-3));
                    calculated = igs.gaussianPotentialUpside(twoSigma);
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("IncrementalGaussianStatistics: "
                                     + "wrong Gaussian potential upside"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }
                    calculated = s.gaussianPotentialUpside(twoSigma);
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("RiskStatistics: wrong Gaussian potential upside"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }
                    calculated = s.potentialUpside(twoSigma);
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("RiskStatistics: wrong potential upside"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }


                    // just to check that GaussianStatistics<StatsHolder> does work
                    StatsHolder h = new StatsHolder(s.mean(), s.standardDeviation());
                    GenericGaussianStatistics <StatsHolder> test = new GenericGaussianStatistics <StatsHolder>(h);
                    expected   = s.gaussianPotentialUpside(twoSigma);
                    calculated = test.gaussianPotentialUpside(twoSigma);
                    if (calculated != expected)
                    {
                        QAssert.Fail("GenericGaussianStatistics<StatsHolder> fails"
                                     + "\n  calculated: " + calculated
                                     + "\n  expected: " + expected);
                    }


                    // value-at-risk
                    expected  = -Math.Min(lower_tail, 0.0);
                    tolerance = (expected == 0.0 ? 1.0e-3 :
                                 Math.Abs(expected * 1.0e-3));
                    calculated = igs.gaussianValueAtRisk(twoSigma);
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("IncrementalGaussianStatistics: "
                                     + "wrong Gaussian value-at-risk"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }
                    calculated = s.gaussianValueAtRisk(twoSigma);
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("RiskStatistics: wrong Gaussian value-at-risk"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }
                    calculated = s.valueAtRisk(twoSigma);
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("RiskStatistics: wrong value-at-risk"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }

                    if (averages[i] > 0.0 && sigmas[j] < averages[i])
                    {
                        // no data will miss the targets:
                        // skip the rest of this iteration
                        igs.reset();
                        s.reset();
                        continue;
                    }


                    // expected shortfall
                    expected = -Math.Min(averages[i]
                                         - sigmas[j] * sigmas[j]
                                         * normal.value(lower_tail) / (1.0 - twoSigma),
                                         0.0);
                    tolerance = (expected == 0.0 ? 1.0e-4
                                            : Math.Abs(expected) * 1.0e-2);
                    calculated = igs.gaussianExpectedShortfall(twoSigma);
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("IncrementalGaussianStatistics: "
                                     + "wrong Gaussian expected shortfall"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }
                    calculated = s.gaussianExpectedShortfall(twoSigma);
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("RiskStatistics: wrong Gaussian expected shortfall"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }
                    calculated = s.expectedShortfall(twoSigma);
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("RiskStatistics: wrong expected shortfall"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }


                    // shortfall
                    expected  = 0.5;
                    tolerance = (expected == 0.0 ? 1.0e-3 :
                                 Math.Abs(expected * 1.0e-3));
                    calculated = igs.gaussianShortfall(averages[i]);
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("IncrementalGaussianStatistics: "
                                     + "wrong Gaussian shortfall"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }
                    calculated = s.gaussianShortfall(averages[i]);
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("RiskStatistics: wrong Gaussian shortfall"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }
                    calculated = s.shortfall(averages[i]);
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("RiskStatistics: wrong shortfall"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }


                    // average shortfall
                    expected   = sigmas[j] / Math.Sqrt(2.0 * Const.M_PI) * 2.0;
                    tolerance  = expected * 1.0e-3;
                    calculated = igs.gaussianAverageShortfall(averages[i]);
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("IncrementalGaussianStatistics: "
                                     + "wrong Gaussian average shortfall"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }
                    calculated = s.gaussianAverageShortfall(averages[i]);
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("RiskStatistics: wrong Gaussian average shortfall"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }
                    calculated = s.averageShortfall(averages[i]);
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("RiskStatistics: wrong average shortfall"
                                     + " for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }


                    // regret
                    expected   = sigmas[j] * sigmas[j];
                    tolerance  = expected * 1.0e-1;
                    calculated = igs.gaussianRegret(averages[i]);
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("IncrementalGaussianStatistics: "
                                     + "wrong Gaussian regret(" + averages[i] + ") "
                                     + "for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }
                    calculated = s.gaussianRegret(averages[i]);
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("RiskStatistics: "
                                     + "wrong Gaussian regret(" + averages[i] + ") "
                                     + "for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }
                    calculated = s.regret(averages[i]);
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("RiskStatistics: "
                                     + "wrong regret(" + averages[i] + ") "
                                     + "for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }


                    // downsideVariance
                    expected  = s.downsideVariance();
                    tolerance = (expected == 0.0 ? 1.0e-3 :
                                 Math.Abs(expected * 1.0e-3));
                    calculated = igs.downsideVariance();
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("IncrementalGaussianStatistics: "
                                     + "wrong downside variance"
                                     + "for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }
                    calculated = igs.gaussianDownsideVariance();
                    if (Math.Abs(calculated - expected) > tolerance)
                    {
                        QAssert.Fail("IncrementalGaussianStatistics: "
                                     + "wrong Gaussian downside variance"
                                     + "for N(" + averages[i] + ", "
                                     + sigmas[j] + ")\n"
                                     + "    calculated: " + calculated + "\n"
                                     + "    expected:   " + expected + "\n"
                                     + "    tolerance:  " + tolerance);
                    }

                    // downsideVariance
                    if (averages[i] == 0.0)
                    {
                        expected   = sigmas[j] * sigmas[j];
                        tolerance  = expected * 1.0e-3;
                        calculated = igs.downsideVariance();
                        if (Math.Abs(calculated - expected) > tolerance)
                        {
                            QAssert.Fail("IncrementalGaussianStatistics: "
                                         + "wrong downside variance"
                                         + "for N(" + averages[i] + ", "
                                         + sigmas[j] + ")\n"
                                         + "    calculated: " + calculated + "\n"
                                         + "    expected:   " + expected + "\n"
                                         + "    tolerance:  " + tolerance);
                        }
                        calculated = igs.gaussianDownsideVariance();
                        if (Math.Abs(calculated - expected) > tolerance)
                        {
                            QAssert.Fail("IncrementalGaussianStatistics: "
                                         + "wrong Gaussian downside variance"
                                         + "for N(" + averages[i] + ", "
                                         + sigmas[j] + ")\n"
                                         + "    calculated: " + calculated + "\n"
                                         + "    expected:   " + expected + "\n"
                                         + "    tolerance:  " + tolerance);
                        }
                        calculated = s.downsideVariance();
                        if (Math.Abs(calculated - expected) > tolerance)
                        {
                            QAssert.Fail("RiskStatistics: wrong downside variance"
                                         + "for N(" + averages[i] + ", "
                                         + sigmas[j] + ")\n"
                                         + "    calculated: " + calculated + "\n"
                                         + "    expected:   " + expected + "\n"
                                         + "    tolerance:  " + tolerance);
                        }
                        calculated = s.gaussianDownsideVariance();
                        if (Math.Abs(calculated - expected) > tolerance)
                        {
                            QAssert.Fail("RiskStatistics: wrong Gaussian downside variance"
                                         + "for N(" + averages[i] + ", "
                                         + sigmas[j] + ")\n"
                                         + "    calculated: " + calculated + "\n"
                                         + "    expected:   " + expected + "\n"
                                         + "    tolerance:  " + tolerance);
                        }
                    }

                    igs.reset();
                    s.reset();
                }
            }
        }
        public void MixtureDistributionExample()
        {
            var samples1 = new NormalDistribution(mean: -2, stdDev: 1).Generate(100000);
            var samples2 = new NormalDistribution(mean: +4, stdDev: 1).Generate(100000);

            // Mix the samples from both distributions
            var samples = samples1.Concatenate(samples2);

            // Create a new mixture distribution with two Normal components
            var mixture = new Mixture<NormalDistribution>(
                new NormalDistribution(-1),
                new NormalDistribution(+1));

            // Estimate the distribution
            mixture.Fit(samples);

            var a = mixture.Components[0].ToString("N2"); // N(x; μ = -2.00, σ² = 1.00)
            var b = mixture.Components[1].ToString("N2"); // N(x; μ =  4.00, σ² = 1.02)

            Assert.AreEqual("N(x; μ = -2.00, σ² = 1.00)", a);
            Assert.AreEqual("N(x; μ = 4.00, σ² = 1.02)", b);
        }
Пример #48
0
        public void FitTest()
        {
            double expectedMean = 1.125;
            double expectedSigma = 1.01775897605147;

            NormalDistribution target;

            target = new NormalDistribution();
            double[] observations = { 0.10, 0.40, 2.00, 2.00 };
            double[] weights = { 0.25, 0.25, 0.25, 0.25 };
            target.Fit(observations, weights);

            Assert.AreEqual(expectedMean, target.Mean);
            Assert.AreEqual(expectedSigma, target.StandardDeviation, 1e-6);


            target = new NormalDistribution();
            double[] observations2 = { 0.10, 0.10, 0.40, 2.00 };
            double[] weights2 = { 0.125, 0.125, 0.25, 0.50 };
            target.Fit(observations2, weights2);

            Assert.AreEqual(expectedMean, target.Mean);
        }
Пример #49
0
 public DerivedNormalDistribution(NormalDistribution distribution)
 {
     Mean = distribution.Mean;
     StandardDeviation = distribution.StandardDeviation;
 }
Пример #50
0
        public void PredictTest2()
        {
            // Create continuous sequences. In the sequence below, there
            // seems to be two states, one for values equal to 1 and another
            // for values equal to 2.
            double[][] sequences = new double[][] 
            {
                new double[] { 1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2 }             
            };

            // Specify a initial normal distribution for the samples.
            NormalDistribution density = new NormalDistribution();

            // Creates a continuous hidden Markov Model with two states organized in a forward
            //  topology and an underlying univariate Normal distribution as probability density.
            var model = new HiddenMarkovModel<NormalDistribution>(new Ergodic(2), density);

            // Configure the learning algorithms to train the sequence classifier until the
            // difference in the average log-likelihood changes only by as little as 0.0001
            var teacher = new BaumWelchLearning<NormalDistribution>(model)
            {
                Tolerance = 0.0001,
                Iterations = 0,

                // However, we will need to specify a regularization constant as the
                //  variance of each state will likely be zero (all values are equal)
                FittingOptions = new NormalOptions() { Regularization = double.Epsilon }
            };

            // Fit the model
            double likelihood = teacher.Run(sequences);


            double a1 = model.Predict(new double[] { 1, 2, 1 });
            double a2 = model.Predict(new double[] { 1, 2, 1, 2 });

            Assert.AreEqual(2, a1, 1e-10);
            Assert.AreEqual(1, a2, 1e-10);
            Assert.IsFalse(Double.IsNaN(a1));
            Assert.IsFalse(Double.IsNaN(a2));

            double p1, p2;
            Mixture<NormalDistribution> d1, d2;
            double b1 = model.Predict(new double[] { 1, 2, 1 }, out p1, out d1);
            double b2 = model.Predict(new double[] { 1, 2, 1, 2 }, out p2, out d2);

            Assert.AreEqual(2, b1, 1e-10);
            Assert.AreEqual(1, b2, 1e-10);
            Assert.IsFalse(Double.IsNaN(b1));
            Assert.IsFalse(Double.IsNaN(b2));

            Assert.AreEqual(0, d1.Coefficients[0]);
            Assert.AreEqual(1, d1.Coefficients[1]);

            Assert.AreEqual(1, d2.Coefficients[0]);
            Assert.AreEqual(0, d2.Coefficients[1]);
        }
Пример #51
0
 public NormalRandomGenerator(int seed, NormalDistribution distribution) : base(seed)
 {
     this.distribution = distribution;
 }
Пример #52
0
        public void LearnTest7()
        {
            // Create continuous sequences. In the sequences below, there
            //  seems to be two states, one for values between 0 and 1 and
            //  another for values between 5 and 7. The states seems to be
            //  switched on every observation.
            double[][] sequences = new double[][] 
            {
                new double[] { 0.1, 5.2, 0.3, 6.7, 0.1, 6.0 },
                new double[] { 0.2, 6.2, 0.3, 6.3, 0.1, 5.0 },
                new double[] { 0.1, 7.0, 0.1, 7.0, 0.2, 5.6 },
            };


            // Specify a initial normal distribution for the samples.
            var density = new NormalDistribution();

            // Creates a continuous hidden Markov Model with two states organized in a forward
            //  topology and an underlying univariate Normal distribution as probability density.
            var model = new HiddenMarkovModel<NormalDistribution>(new Ergodic(2), density);

            // Configure the learning algorithms to train the sequence classifier until the
            // difference in the average log-likelihood changes only by as little as 0.0001
            var teacher = new BaumWelchLearning<NormalDistribution>(model)
            {
                Tolerance = 0.0001,
                Iterations = 0,
            };

            // Fit the model
            double logLikelihood = teacher.Run(sequences);

            // See the log-probability of the sequences learned
            double a1 = model.Evaluate(new[] { 0.1, 5.2, 0.3, 6.7, 0.1, 6.0 }); // -0.12799388666109757
            double a2 = model.Evaluate(new[] { 0.2, 6.2, 0.3, 6.3, 0.1, 5.0 }); // 0.01171157434400194

            // See the probability of an unrelated sequence
            double a3 = model.Evaluate(new[] { 1.1, 2.2, 1.3, 3.2, 4.2, 1.0 }); // -298.7465244473417

            double likelihood = Math.Exp(logLikelihood);
            a1 = Math.Exp(a1); // 0.879
            a2 = Math.Exp(a2); // 1.011
            a3 = Math.Exp(a3); // 0.000

            // We can also ask the model to decode one of the sequences. After
            // this step the resulting sequence will be: { 0, 1, 0, 1, 0, 1 }
            //
            int[] states = model.Decode(new[] { 0.1, 5.2, 0.3, 6.7, 0.1, 6.0 });

            Assert.IsTrue(states.IsEqual(0, 1, 0, 1, 0, 1));

            Assert.AreEqual(1.1341500279562791, likelihood, 1e-10);
            Assert.AreEqual(0.8798587580029778, a1, 1e-10);
            Assert.AreEqual(1.0117804233450216, a2, 1e-10);
            Assert.AreEqual(1.8031545195073828E-130, a3, 1e-10);

            Assert.IsFalse(double.IsNaN(logLikelihood));
            Assert.IsFalse(double.IsNaN(a1));
            Assert.IsFalse(double.IsNaN(a2));
            Assert.IsFalse(double.IsNaN(a3));


            Assert.AreEqual(2, model.Emissions.Length);
            var state1 = (model.Emissions[0] as NormalDistribution);
            var state2 = (model.Emissions[1] as NormalDistribution);
            Assert.AreEqual(0.16666666666666, state1.Mean, 1e-10);
            Assert.AreEqual(6.11111111111111, state2.Mean, 1e-10);
            Assert.IsFalse(Double.IsNaN(state1.Mean));
            Assert.IsFalse(Double.IsNaN(state2.Mean));

            Assert.AreEqual(0.007499999999999, state1.Variance, 1e-10);
            Assert.AreEqual(0.538611111111111, state2.Variance, 1e-10);
            Assert.IsFalse(Double.IsNaN(state1.Variance));
            Assert.IsFalse(Double.IsNaN(state2.Variance));

            Assert.AreEqual(2, model.Transitions.GetLength(0));
            Assert.AreEqual(2, model.Transitions.GetLength(1));

            var A = Matrix.Exp(model.Transitions);
            Assert.AreEqual(0, A[0, 0], 1e-16);
            Assert.AreEqual(1, A[0, 1], 1e-16);
            Assert.AreEqual(1, A[1, 0], 1e-16);
            Assert.AreEqual(0, A[1, 1], 1e-16);

            Assert.IsFalse(A.HasNaN());
        }
Пример #53
0
        public void Compute(OptionPosition option)
        {
            var t = option.TimeToExpiry.TotalDays / 365.0;

            if (Math.Abs(t) < Double.Epsilon)
            {
                return;
            }

            var d1 = Math.Log(option.UnderlyingPrice / option.Strike) + (option.InterestRate - option.DividendRate + Math.Pow(option.Volatility, 2)) / t;
            var d2 = d1 - option.Volatility * Math.Sqrt(t);

            // when calculating option greeks keep in mind that we calculate greek for position,
            // and that's why BS formulas for greeks must be multiplied by -1 in case we're selling options
            // so formulas for greeks are [sign * BSFormula]

            if (option.Type == OptionType.Call)
            {
                option.OptionPrice = option.UnderlyingPrice * NormalDistribution.Phi(d1) -
                                     option.Strike * Math.Exp(-option.InterestRate * t) * NormalDistribution.Phi(d2);

                option.Delta = Math.Exp(-option.DividendRate * t) * NormalDistribution.Phi(d1);

                option.Theta = -Math.Exp(-option.DividendRate * t) * option.UnderlyingPrice * NormalDistribution.Density(d1) *
                               option.Volatility / (2 * Math.Sqrt(t))
                               +
                               option.DividendRate * Math.Exp(-option.DividendRate * t) * option.UnderlyingPrice *
                               NormalDistribution.Phi(d1)
                               -
                               option.InterestRate * option.Strike * Math.Exp(-option.InterestRate * t) *
                               NormalDistribution.Phi(d2);

                option.Rho = t * option.Strike * Math.Exp(-option.InterestRate * t) * NormalDistribution.Phi(d2);
            }
            else
            {
                option.OptionPrice = -option.UnderlyingPrice * NormalDistribution.Phi(-d1) +
                                     option.Strike * Math.Exp(-option.InterestRate * t) * NormalDistribution.Phi(-d2);

                option.Delta = Math.Exp(-option.DividendRate * t) * (NormalDistribution.Phi(d1) - 1);

                option.Theta = -Math.Exp(-option.DividendRate * t) * option.UnderlyingPrice * NormalDistribution.Density(d1) *
                               option.Volatility / (2 * Math.Sqrt(t))
                               -
                               option.DividendRate * Math.Exp(-option.DividendRate * t) * option.UnderlyingPrice *
                               NormalDistribution.Phi(-d1)
                               +
                               option.InterestRate * option.Strike * Math.Exp(-option.InterestRate * t) *
                               NormalDistribution.Phi(-d2);

                option.Rho = -t *option.Strike *Math.Exp(-option.InterestRate *t) * NormalDistribution.Phi(-d2);
            }

            option.Gamma = Math.Exp(-option.DividendRate * t) * NormalDistribution.Density(d1) /
                           (option.UnderlyingPrice * option.Volatility * Math.Sqrt(t));
            option.Vega = Math.Exp(-option.DividendRate * t) * option.UnderlyingPrice * NormalDistribution.Density(d1) *
                          Math.Sqrt(t);

            SideHelper.FixGreeksAccordingToSide(option);
        }
Пример #54
0
        public void LearnTest8()
        {
            // Create continuous sequences. In the sequence below, there
            // seems to be two states, one for values equal to 1 and another
            // for values equal to 2.
            double[][] sequences = new double[][] 
            {
                new double[] { 1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2 }             
            };

            // Specify a initial normal distribution for the samples.
            var density = new NormalDistribution();

            // Creates a continuous hidden Markov Model with two states organized in a forward
            //  topology and an underlying univariate Normal distribution as probability density.
            var model = new HiddenMarkovModel<NormalDistribution>(new Ergodic(2), density);

            // Configure the learning algorithms to train the sequence classifier until the
            // difference in the average log-likelihood changes only by as little as 0.0001
            var teacher = new BaumWelchLearning<NormalDistribution>(model)
            {
                Tolerance = 0.0001,
                Iterations = 0,

                // However, we will need to specify a regularization constant as the
                //  variance of each state will likely be zero (all values are equal)
                FittingOptions = new NormalOptions() { Regularization = double.Epsilon }
            };

            // Fit the model
            double likelihood = teacher.Run(sequences);


            // See the probability of the sequences learned
            double a1 = model.Evaluate(new double[] { 1, 2, 1, 2, 1, 2, 1, 2, 1 }); // exp(a1) = inf
            double a2 = model.Evaluate(new double[] { 1, 2, 1, 2, 1 });             // exp(a2) = inf

            // See the probability of an unrelated sequence
            double a3 = model.Evaluate(new double[] { 1, 2, 3, 2, 1, 2, 1 });          // exp(a3) = 0
            double a4 = model.Evaluate(new double[] { 1.1, 2.2, 1.3, 3.2, 4.2, 1.0 }); // exp(a4) = 0


            Assert.AreEqual(double.PositiveInfinity, System.Math.Exp(likelihood));
            Assert.AreEqual(302.59496915947972, a1);
            Assert.AreEqual(168.26234890650207, a2);
            Assert.AreEqual(0.0, Math.Exp(a3));
            Assert.AreEqual(0.0, Math.Exp(a4));

            Assert.AreEqual(2, model.Emissions.Length);
            var state1 = (model.Emissions[0] as NormalDistribution);
            var state2 = (model.Emissions[1] as NormalDistribution);
            Assert.AreEqual(1.0, state1.Mean, 1e-10);
            Assert.AreEqual(2.0, state2.Mean, 1e-10);
            Assert.IsFalse(Double.IsNaN(state1.Mean));
            Assert.IsFalse(Double.IsNaN(state2.Mean));

            Assert.IsTrue(state1.Variance < 1e-30);
            Assert.IsTrue(state2.Variance < 1e-30);

            var A = Matrix.Exp(model.Transitions);
            Assert.AreEqual(2, A.GetLength(0));
            Assert.AreEqual(2, A.GetLength(1));
            Assert.AreEqual(0, A[0, 0]);
            Assert.AreEqual(1, A[0, 1]);
            Assert.AreEqual(1, A[1, 0]);
            Assert.AreEqual(0, A[1, 1]);
        }
Пример #55
0
        /// <summary>
        ///   Generic learn method implementation that should work for any input type.
        ///   This method is useful for re-using code between methods that accept Bitmap,
        ///   BitmapData, UnmanagedImage, filenames as strings, etc.
        /// </summary>
        ///
        /// <typeparam name="T">The input type.</typeparam>
        ///
        /// <param name="x">The inputs.</param>
        /// <param name="weights">The weights.</param>
        /// <param name="extractor">A function that knows how to process the input
        ///   and extract features from them.</param>
        ///
        /// <returns>The trained model.</returns>
        ///
        protected TModel InnerLearn <T>(T[] x, double[] weights,
                                        Func <T, TExtractor, IEnumerable <TPoint> > extractor)
        {
            var descriptorsPerInstance = new TFeature[x.Length][];
            var totalDescriptorCounts  = new double[x.Length];
            int takenDescriptorCount   = 0;

            // For all instances
            For(0, x.Length, (i, detector) =>
            {
                if (NumberOfDescriptors > 0 && takenDescriptorCount >= NumberOfDescriptors)
                {
                    return;
                }

                TFeature[] desc = extractor(x[i], detector).Select(p => p.Descriptor).ToArray();

                totalDescriptorCounts[i] = desc.Length;

                if (MaxDescriptorsPerInstance > 0)
                {
                    desc = desc.Sample(MaxDescriptorsPerInstance);
                }

                Interlocked.Add(ref takenDescriptorCount, desc.Length);

                descriptorsPerInstance[i] = desc;
            });

            if (NumberOfDescriptors >= 0 && takenDescriptorCount < NumberOfDescriptors)
            {
                throw new InvalidOperationException("There were not enough descriptors to sample the desired amount " +
                                                    "of samples ({0}). Please either increase the number of images, or increase the number of ".Format(NumberOfDescriptors) +
                                                    "descriptors that are sampled from each image by adjusting the MaxSamplesPerImage property ({0}).".Format(MaxDescriptorsPerInstance));
            }

            var totalDescriptors = new TFeature[takenDescriptorCount];
            var totalWeights     = weights != null ? new double[takenDescriptorCount] : null;

            int[] instanceIndices = new int[takenDescriptorCount];

            int c = 0, w = 0;

            for (int i = 0; i < descriptorsPerInstance.Length; i++)
            {
                if (descriptorsPerInstance[i] != null)
                {
                    if (weights != null)
                    {
                        totalWeights[w++] = weights[i];
                    }
                    for (int j = 0; j < descriptorsPerInstance[i].Length; j++)
                    {
                        totalDescriptors[c] = descriptorsPerInstance[i][j];
                        instanceIndices[c]  = i;
                        c++;
                    }
                }
            }

            if (NumberOfDescriptors > 0)
            {
                int[] idx = Vector.Sample(NumberOfDescriptors);
                totalDescriptors = totalDescriptors.Get(idx);
                instanceIndices  = instanceIndices.Get(idx);
            }

            int[] hist = instanceIndices.Histogram();

            Debug.Assert(hist.Sum() == (NumberOfDescriptors > 0 ? NumberOfDescriptors : takenDescriptorCount));

            this.Statistics = new BagOfWordsStatistics()
            {
                TotalNumberOfInstances              = x.Length,
                TotalNumberOfDescriptors            = (int)totalDescriptorCounts.Sum(),
                TotalNumberOfDescriptorsPerInstance = NormalDistribution.Estimate(totalDescriptorCounts, new NormalOptions {
                    Robust = true
                }),
                TotalNumberOfDescriptorsPerInstanceRange = new IntRange((int)totalDescriptorCounts.Min(), (int)totalDescriptorCounts.Max()),

                NumberOfInstancesTaken              = hist.Length,
                NumberOfDescriptorsTaken            = totalDescriptors.Length,
                NumberOfDescriptorsTakenPerInstance = NormalDistribution.Estimate(hist.ToDouble(), new NormalOptions {
                    Robust = true
                }),
                NumberOfDescriptorsTakenPerInstanceRange = new IntRange(hist.Min(), hist.Max())
            };

            return(learn(totalDescriptors, totalWeights));
        }
Пример #56
0
        public void DistributionFunctionTest()
        {
            double x = 3;
            double mean = 7;
            double dev = 5;

            NormalDistribution target = new NormalDistribution(mean, dev);

            double expected = 0.211855398583397;
            double actual = target.DistributionFunction(x);

            Assert.IsFalse(double.IsNaN(actual));
            Assert.AreEqual(expected, actual, 1e-15);
        }
Пример #57
0
        /// <summary>
        /// Validate cumulative distribution.
        /// </summary>
        /// <param name="x">Input X value.</param>
        /// <param name="p">Expected value.</param>
        public void ValidateCumulativeDistribution(double x, double p)
        {
            var n = new NormalDistribution(5.0, 2.0);

            Assert.AreEqual(p, n.CumulativeDistribution(x), 9);
        }
Пример #58
0
        public void ProbabilityDensityFunctionTest2()
        {
            double expected, actual;

            // Test for small variance
            NormalDistribution target = new NormalDistribution(4.2, double.Epsilon);

            expected = 0;
            actual = target.ProbabilityDensityFunction(0);
            Assert.AreEqual(expected, actual);

            expected = double.PositiveInfinity;
            actual = target.ProbabilityDensityFunction(4.2);
            Assert.AreEqual(expected, actual);
        }
Пример #59
0
    private void DiamondSquare(ref float[,] data, int r, int y, int x, float sigma)
    {
        {
            // Center
            float avg = 0;
            avg += data[y, x];
            avg += data[y + r, x];
            avg += data[y, x + r];
            avg += data[y + r, x + r];

            data[y + r / 2, x + r / 2] = NormalDistribution.Rand(avg / 4, sigma / Mathf.Sqrt(2));
        }
        {
            // Left
            float avg = 0;
            avg += data[y, x];
            avg += data[y + r / 2, x + r / 2];
            avg += data[y + r, x];
            if (x > 0)
            {
                avg += data[y, x - r / 2];
                data[y + r / 2, x] = NormalDistribution.Rand(avg / 4, sigma / 2);
            }
            else
            {
                data[y + r / 2, x] = NormalDistribution.Rand(avg / 3, sigma / 2);
            }
        }
        {
            // Right
            float avg = 0;
            avg += data[y, x + r];
            avg += data[y + r / 2, x + r / 2];
            avg += data[y + r, x + r];
            if ((x + r + 1) * (x + r + 1) < data.Length)
            {
                avg += data[y, x + r + r / 2];
                data[y + r / 2, x + r] = NormalDistribution.Rand(avg / 4, sigma / 2);
            }
            else
            {
                data[y + r / 2, x + r] = NormalDistribution.Rand(avg / 3, sigma / 2);
            }
        }
        {
            // Top
            float avg = 0;
            avg += data[y, x];
            avg += data[y + r / 2, x + r / 2];
            avg += data[y, x + r];
            if (y > 0)
            {
                avg += data[y - r / 2, x];
                data[y, x + r / 2] = NormalDistribution.Rand(avg / 4, sigma / 2);
            }
            else
            {
                data[y, x + r / 2] = NormalDistribution.Rand(avg / 3, sigma / 2);
            }
        }
        {
            // Bottom
            float avg = 0;
            avg += data[y + r, x];
            avg += data[y + r / 2, x + r / 2];
            avg += data[y + r, x + r];
            if ((y + r + 1) * (y + r + 1) < data.Length)
            {
                avg += data[y + r + r / 2, x];
                data[y + r, x + r / 2] = NormalDistribution.Rand(avg / 4, sigma / 2);
            }
            else
            {
                data[y + r, x + r / 2] = NormalDistribution.Rand(avg / 3, sigma / 2);
            }
        }
    }
Пример #60
0
        public void LogProbabilityDensityFunctionTest()
        {
            double x = 3;
            double mean = 7;
            double dev = 5;

            NormalDistribution target = new NormalDistribution(mean, dev);

            double expected = System.Math.Log(0.0579383105522966);
            double actual = target.LogProbabilityDensityFunction(x);

            Assert.IsFalse(double.IsNaN(actual));
            Assert.AreEqual(expected, actual, 1e-15);
        }