Пример #1
0
        public void TestNormalDistGetZScore()
        {
            var testSubject = new NormalDistEquation {
                Mean = 0, StdDev = 1
            };

            var testResult = testSubject.GetZScoreFor(0);

            Assert.IsTrue(testResult >= 0.499);

            testResult = testSubject.GetZScoreFor(1);
            Assert.IsTrue(testResult >= 0.34134);
        }
Пример #2
0
        public static double RandomValueInNormalDist(NormalDistEquation eq, int sigma = 3)
        {
            if (eq == null)
            {
                throw new ArgumentNullException(nameof(eq));
            }

            var minRand = eq.Mean - (eq.StdDev * sigma);
            var maxRand = eq.Mean + (eq.StdDev * sigma);

            if (minRand < Int32.MinValue || maxRand > Int32.MaxValue)
            {
                throw new ArgumentException("The random number generator is limited to int max 2^31 value.");
            }

            for (var i = 0; i < 1024; i++)
            {
                //guess some value w/i 3 std dev's
                var someValue = RandomDouble(minRand, maxRand);

                //get the probability of that guess
                var zscore = eq.GetZScoreFor(someValue);

                //zscore
                var attempt = RandomDouble(0, 5) * 0.1;

                //try getting a value with that probability
                var isGe = attempt >= zscore;

                //when succeed - return some value
                if (isGe)
                {
                    return(someValue);
                }
            }
            return(eq.Mean);
        }