Exemplo n.º 1
0
 public void Diffuse()
 {
     this.Position = new PointF
     {
         X = this.Position.X + 25 * (float)normalDistribution.Generate(),
         Y = this.Position.Y + 25 * (float)normalDistribution.Generate(),
     };
 }
Exemplo n.º 2
0
        //生成样本
        public void GenerateSamples(int nCount, int nDimension)
        {
            // Create a new uniform continuous distribution from 0.42 to 1.1
            // var uniform = new UniformContinuousDistribution(a: -1, b: 1);
            var uniform = new NormalDistribution(0, 2);

            double[][] temp_Samples = new double[nCount][];
            for (int index = 0; index < nCount; index++)
            {
                double[] temp_smp = new double[nDimension];
                for (int i = 0; i < nDimension; i++)
                {
                    temp_smp[i] = uniform.Generate();
                }
                temp_Samples[index] = temp_smp;
            }
            //异常点
            {
                //double[] temp_smp = new double[nDimension];
                //for (int i = 0; i < nDimension; i++)
                //{

                //    temp_smp[i] = 1.2;
                //}
                temp_Samples[0][0] = 1.2;
            }
            m_samples = temp_Samples;
        }
        // initialise the neural network
        public NeuralNetwork(int inputNodes, int hiddenNodes, int outputNodes, double learningRate)
        {
            // set number of nodes in each input, hidden, output layer
            HiddenNodes = hiddenNodes;
            InputNodes  = inputNodes;
            OutputNodes = outputNodes;

            // Create weigth matrices
            Wih = new double[HiddenNodes, InputNodes];
            Who = new double[OutputNodes, HiddenNodes];

            // Initialize the weight matrices.
            // Sample their start values from a normal probability distribution centred around zero
            // and with a standard deviation that is related to the number of incoming links into a
            // node, 1 /√(number of incoming links).
            NormalDistribution distribution = new NormalDistribution(0.0, 1.0 / Math.Sqrt(HiddenNodes));

            for (int i = 0; i < Wih.Rows(); i++)
            {
                Wih.SetRow(i, distribution.Generate(InputNodes));
            }

            distribution = new NormalDistribution(0.0, 1.0 / Math.Sqrt(OutputNodes));

            for (int i = 0; i < Who.Rows(); i++)
            {
                Who.SetRow(i, distribution.Generate(HiddenNodes));
            }

            // learning rate
            LearningRate = learningRate;

            // activation function is the sigmoid function
            ActivationFunction = Sigmoid;
        }
        public static void GenerateValues(int howMany, double meanA, double meanB, double meanC, double[,] R)
        {
            TestChol();
            var disitrubtion = new NormalDistribution(0, Math.Sqrt(1));

            var mean = new double[] { disitrubtion.InverseDistributionFunction(meanA), disitrubtion.InverseDistributionFunction(meanB), disitrubtion.InverseDistributionFunction(meanC) };

            var randomValues = disitrubtion.Generate(howMany * 3);

            var aSamples = new double[howMany];
            var bSamples = new double[howMany];
            var cSamples = new double[howMany];

            int randomValueCount = 0;

            for (int i = 0; i < howMany; i++)
            {
                //generating one sample
                var z       = new[] { randomValues[randomValueCount++], randomValues[randomValueCount++], randomValues[randomValueCount++] };
                var product = z.Dot(R);
                var y       = mean.Add(product);
                var samples = new double[] { y[0] > 0 ? 1 : 0, y[1] > 0 ? 1 : 0, y[2] > 0 ? 1 : 0 };

                aSamples[i] = samples[0];
                bSamples[i] = samples[1];
                cSamples[i] = samples[2];

                Trace.WriteLine($"{aSamples[i]}, {bSamples[i]}, {cSamples[i]}");
            }

            var resultCorrAb = CalculateCorrelation(aSamples, bSamples);
            var resultCorrAc = CalculateCorrelation(aSamples, cSamples);
            var resultCorrBc = CalculateCorrelation(bSamples, cSamples);
        }
Exemplo n.º 5
0
        public static Bitmap AWGNNoise(Bitmap bmp, int intensity)
        {
            Bitmap img    = new Bitmap(bmp.Width, bmp.Height);
            var    normal = new NormalDistribution(mean: 0, stdDev: 1);

            double[] noise = normal.Generate(img.Width * img.Height);
            int      l     = 0;

            for (int i = 0; i < bmp.Height; i++)
            {
                for (int j = 0; j < bmp.Width; j++)
                {
                    Color c  = bmp.GetPixel(j, i);
                    int   c1 = c.R + ((int)Math.Ceiling(noise[l]) * intensity); //(int)noise + intensity;//(int)Math.Sqrt(noise() * 255) + intensity;
                    int   c2 = c.G + ((int)Math.Ceiling(noise[l]) * intensity); //(int)Math.Sqrt(noise() * 255) + intensity;
                    int   c3 = c.B + ((int)Math.Ceiling(noise[l]) * intensity); //(int)Math.Sqrt(noise() * 255) + intensity;
                    c1 = c1 > 255 ? 255 : c1;
                    c1 = c1 < 0 ? 0 : c1;
                    c2 = c2 > 255 ? 255 : c2;
                    c2 = c2 < 0 ? 0 : c2;
                    c3 = c3 > 255 ? 255 : c3;
                    c3 = c3 < 0 ? 0 : c3;
                    Color newC = Color.FromArgb(c1, c2, c3);
                    img.SetPixel(j, i, newC);
                    l++;
                }
            }
            return(img);
        }
Exemplo n.º 6
0
        protected override double[] GenerateRandomInternal(Random rnd)
        {
            double[,] ltf = Chol.LeftTriangularFactor;
            double[] result = new double[Dimension];
            double[] u      = Means;

            // TODO: I Don't remember, what happens here. I should check it out.
            // OK. I checked, it doesn't work properly...
            for (int i = 0; i < Dimension; i++)
            {
                result[i] = BaseNormal.Generate(rnd);

                // result[i] = _baseNormal.Generate(rnd) / Math.Sqrt(_baseGamma.Generate(rnd) / DegreesOfFreedom);
            }

            result = Matrix.Dot(ltf, result);

            double[] gammaSqrt = baseGamma.Generate(Dimension, rnd).Divide(DegreesOfFreedom).Sqrt();

            // from MATHlab function MVTRND(C,DF,N).
            result = Elementwise.Divide(result, gammaSqrt);

            // double[] chiSqrt = _baseChi.Generate(Dimension, rnd).Divide(DegreesOfFreedom).Sqrt();

            // result = Elementwise.Divide(result, chiSqrt); //from R func R/rmvt.R
            result = Elementwise.Add(result, u);

            return(result);
        }
            public override double Generate(Random source)
            {
                double x = baseNormal.Generate(source);
                double y = gammaDistr.Generate(source);

                return((x / Math.Sqrt(y / DegreesOfFreedom) * ScaleCoefficient) + mean);
            }
Exemplo n.º 8
0
        double[] GenerateRandomData()    //随机生成样本
        {
            int    mCount        = 1000; ///样本总数量
            int    inflextion1   = 300;  //拐点1所在位置
            int    inflextion2   = 600;  //拐点2所在位置
            double slope         = 1;    //斜率
            int    windWidth     = 200;  //稳态检测窗口长度
            int    intervalWidth = 40;   //每次窗口滑动的间隔长度

            double[]           samples = new double[mCount];
            NormalDistribution normal  = new NormalDistribution(0, 20);

            double[] Normalsamples = normal.Generate(mCount);///生成正态分布样本
            //  UniformContinuousDistribution normal = new UniformContinuousDistribution(-10, 10);
            // double[] Normalsamples = normal.Generate(mCount);

            ///拐点1前的样本
            for (int index = 0; index < inflextion1; index++)
            {
                samples[index] = Normalsamples[index];
            }
            ///拐点1和拐点2之间的样本
            for (int index = 0; index < inflextion2 - inflextion1; index++)
            {
                double offset = index * slope;
                samples[index + inflextion1] = Normalsamples[index + inflextion1] + offset;
            }
            ///拐点2之后的样本
            for (int index = 0; index < mCount - inflextion2; index++)
            {
                samples[index + inflextion2] = Normalsamples[index + inflextion2] + samples[inflextion2 - 1];
            }
            return(samples);
        }
Exemplo n.º 9
0
        public static double[] NormalDistributionSamples(int numberOfSamples, double mean = 0, double stdDev = 1)
        {
            // Create a Normal with mean and sigma
            var normal = new NormalDistribution(mean, stdDev);

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

            return(samples);
        }
Exemplo n.º 10
0
        public ConstantVelocity2DModel GetNoisyState(double accelerationNoise)
        {
            var processNoiseMat = ConstantVelocity2DModel.GetProcessNoise(accelerationNoise);
            var noise           = normalDistribution.Generate(ConstantVelocity2DModel.Dimension).Multiply(processNoiseMat);

            return(new ConstantVelocity2DModel
            {
                Position = new PointF
                {
                    X = currentState.Position.X + (float)noise[0],
                    Y = currentState.Position.Y + (float)noise[2]
                },

                Velocity = new PointF
                {
                    X = currentState.Velocity.X + (float)noise[1],
                    Y = currentState.Velocity.Y + (float)noise[3]
                }
            });
        }
Exemplo n.º 11
0
        public void GenerateTest()
        {
            NormalDistribution target = new NormalDistribution(2, 5);

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

            var actual = NormalDistribution.Estimate(samples);

            Assert.AreEqual(2, actual.Mean, 0.01);
            Assert.AreEqual(5, actual.StandardDeviation, 0.01);
        }
Exemplo n.º 12
0
        public DataPoint[] GeneratePoints(int pointsCount, double standardDeviation)
        {
            var nd     = new NormalDistribution(0, standardDeviation);
            var values = nd.Generate(pointsCount * 2);
            var points = new DataPoint[pointsCount];

            for (int i = 0; i < pointsCount; i++)
            {
                points[i] = new DataPoint((float)values[i * 2], (float)values[i * 2 + 1]);
            }
            return(points);
        }
Exemplo n.º 13
0
        public static double[] generateVector(int n)
        {
            NormalDistribution randomizer = new NormalDistribution(0, 1);

            double[] vector = new double[n];

            for (int i = 0; i < vector.Length; i++)
            {
                vector[i] = randomizer.Generate();
            }
            return(vector);
        }
Exemplo n.º 14
0
 public override void RunSimulation(int simNumber)
 {
     var W = _dist.Generate(_allDates.Count - 1);
     _r = new double[_allDates.Count];
     _bankAccount = new double[_allDates.Count];
     _r[0] = _r0;
     _bankAccount[0] = 1;
     for (var i = 0; i < _allDates.Count - 1; i++)
     {
         var dt = (_allDates[i + 1] - _allDates[i]) / 365.0;
         _r[i + 1] = _r[i] + (Theta(_allDates[i + 1]) - _a * _r[i]) * dt + _vol * Math.Sqrt(dt) * W[i];
         _bankAccount[i + 1] = _bankAccount[i] * Math.Exp(_r[i] * dt);
     }
 }
Exemplo n.º 15
0
        private void radButton2_Click(object sender, EventArgs e)
        {
            // чистим данные
            Id.Clear();
            X.Clear();

            // чистим графики
            scatterplotView1.Graph.GraphPane.CurveList.Clear();
            radChartView1.Series[0].DataPoints.Clear();
            radChartView2.Series[0].DataPoints.Clear();

            // параметиры для генерации выборки
            int    N   = this.labeledIntValue2.Value;
            double Mx  = this.labeledDoubleValye1.Value;
            double Std = this.labeledDoubleValye2.Value;

            // генерируем индекс (можно быстрее, но так нагляднее)
            for (int i = 0; i < N; i++)
            {
                Id.Add(i);
            }

            // генератор
            NormalDistribution norm = new NormalDistribution(Mx, Std);

            // создание выборки объемом N
            X.AddRange(norm.Generate(N));

            // визуализация - скаттерплот
            scatterplotView1.DataSource = X.ToArray();

            // визуализация - гистограмма
            Histogram histogram = new Histogram();

            histogram.Compute(X.ToArray());
            histogramView1.DataSource = histogram;

            for (int i = 0; i < N; i++)
            {
                radChartView1.Series[0].DataPoints.Add(new ScatterDataPoint(Id[i], X[i]));
            }

            foreach (HistogramBin bin in histogram.Bins)
            {
                string b = $"{bin.Range.Min}-{bin.Range.Max}";
                radChartView2.Series[0].DataPoints.Add(new CategoricalDataPoint(bin.Value, b));
            }
        }
Exemplo n.º 16
0
        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(10000000);

            // 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.00, σ² = 25.00)", result);
        }
Exemplo n.º 17
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);
        }
Exemplo n.º 18
0
        public override void RunSimulation(int simNumber)
        {
            NormalDistribution dist = new NormalDistribution();

            Generator.Seed = -1585814591 * simNumber; // This magic number is: "HW1FSimulator".GetHashCode();
            double[] W = dist.Generate(allDates.Count - 1);
            r              = new double[allDates.Count];
            bankAccount    = new double[allDates.Count];
            r[0]           = r0;
            bankAccount[0] = 1;
            for (int i = 0; i < allDates.Count - 1; i++)
            {
                double dt = (allDates[i + 1] - allDates[i]) / 365.0;
                r[i + 1]           = r[i] + (theta(allDates[i + 1]) - a * r[i]) * dt + vol * Math.Sqrt(dt) * W[i];
                bankAccount[i + 1] = bankAccount[i] * Math.Exp(r[i] * dt);
            }
        }
Exemplo n.º 19
0
        protected override double[] GenerateRandomInternal(Random rnd)
        {
            double[,] ltf = Chol.LeftTriangularFactor;
            double[] result = new double[Dimension];
            double[] u      = Means;

            for (int i = 0; i < Dimension; i++)
            {
                result[i] = BaseNormal.Generate(rnd);
            }

            result = Matrix.Dot(ltf, result);

            result = Elementwise.Add(result, u);

            return(result);
        }
Exemplo n.º 20
0
        /// <summary>
        /// Run a simulation and store the results for later use by <see cref="GetIndices(MarketObservable, List{Date})"/>
        /// </summary>
        /// <param name="simNumber"></param>
        public override void RunSimulation(int simNumber)
        {
            _simulation = new Dictionary <int, double>();
            var    spot     = _fxSource.GetRate(_anchorDate);
            var    simRate  = spot;
            var    oldFxFwd = spot;
            double newFXfwd;

            // Simulate the default
            var normal  = new NormalDistribution();
            var uniform = new UniformContinuousDistribution();
            var hazEst  = _survivalProbSource.GetSP(_survivalProbSource.getAnchorDate().AddTenor(Tenor.FromYears(1)));

            hazEst         = -Math.Log(hazEst);
            Generator.Seed =
                -533776581 * simNumber; // This magic number is: "DeterministicCreditWithFXJump".GetHashCode();
            var tau = uniform.Generate();

            tau             = Math.Log(tau) / -hazEst;
            _simDefaultTime = _anchorDate.value + tau * 365;

            for (var timeCounter = 0; timeCounter < _allRequiredDates.Count; timeCounter++)
            {
                double dt = timeCounter > 0
                    ? _allRequiredDates[timeCounter] - _allRequiredDates[timeCounter - 1]
                    : _allRequiredDates[timeCounter] - _anchorDate.value;
                newFXfwd = _fxSource.GetRate(new Date(_anchorDate.value + dt));

                dt = dt / 365.0;
                var sdt = Math.Sqrt(dt);
                var dW  = normal.Generate();
                // TODO: drift needs to be adjusted for default rate * jump size
                simRate = simRate * newFXfwd / oldFxFwd * Math.Exp(-0.5 * _fxVol * _fxVol * dt + _fxVol * sdt * dW);
                if (_simDefaultTime < _allRequiredDates[timeCounter])
                {
                    _simulation[_allRequiredDates[timeCounter]] = simRate * (1 + _relJumpSizeInDefault);
                }
                else
                {
                    _simulation[_allRequiredDates[timeCounter]] = simRate;
                }
            }
        }
Exemplo n.º 21
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            double mean    = double.NaN;
            double dev     = double.NaN;
            int    samples = 0;
            int    seed    = 0;

            DA.GetData(0, ref mean);
            DA.GetData(1, ref dev);
            DA.GetData(2, ref samples);
            DA.GetData(3, ref seed);

            Accord.Math.Random.Generator.Seed = seed;

            NormalDistribution dist = new NormalDistribution(mean, dev);
            Tensor             tens = new Tensor(samples);

            tens.TensorData = dist.Generate(samples);

            DA.SetData(0, tens);
        }
        protected virtual void GenerateShaderCompositions(MaterialGeneratorContext context, ShaderMixinSource shaderSource)
        {
            if (Fresnel != null)
            {
                shaderSource.AddComposition("fresnelFunction", Fresnel.Generate(context));
            }

            if (Visibility != null)
            {
                shaderSource.AddComposition("geometricShadowingFunction", Visibility.Generate(context));
            }

            if (NormalDistribution != null)
            {
                shaderSource.AddComposition("normalDistributionFunction", NormalDistribution.Generate(context));
            }

            if (Environment != null)
            {
                shaderSource.AddComposition("environmentFunction", Environment.Generate(context));
            }
        }
Exemplo n.º 23
0
        /// <summary>
        /// Run a simulation and store the results for later use by <see cref="GetIndices(MarketObservable, List{Date})"/>
        /// </summary>
        /// <param name="simNumber"></param>
        public override void RunSimulation(int simNumber)
        {
            simulation = new Dictionary <int, double>();
            double simRate  = spot;
            double oldFxFwd = spot;
            double newFXfwd;

            // Simulate the default
            NormalDistribution            normal  = new NormalDistribution();
            UniformContinuousDistribution uniform = new UniformContinuousDistribution();
            double hazEst = survivalProbSource.GetSP(survivalProbSource.getAnchorDate().AddTenor(Tenor.Years(1)));

            hazEst         = -Math.Log(hazEst);
            Generator.Seed = -533776581 * simNumber; // This magic number is: "DeterministicCreditWithFXJump".GetHashCode();
            double tau = uniform.Generate();

            tau            = Math.Log(tau) / (-hazEst);
            simDefaultTime = anchorDate.value + tau * 365;

            for (int timeCounter = 0; timeCounter < allRequiredDates.Count; timeCounter++)
            {
                double dt = timeCounter > 0 ? allRequiredDates[timeCounter] - allRequiredDates[timeCounter - 1] : allRequiredDates[timeCounter] - anchorDate.value;
                newFXfwd = fxSource.GetRate(new Date(anchorDate.value + dt));

                dt = dt / 365.0;
                double sdt = Math.Sqrt(dt);
                double dW  = normal.Generate();
                // TODO: drift needs to be adjusted for default rate * jump size
                simRate = simRate * newFXfwd / oldFxFwd * Math.Exp((-0.5 * fxVol * fxVol) * dt + fxVol * sdt * dW);
                if (simDefaultTime < allRequiredDates[timeCounter])
                {
                    simulation[allRequiredDates[timeCounter]] = simRate * (1 + relJumpSizeInDefault);
                }
                else
                {
                    simulation[allRequiredDates[timeCounter]] = simRate;
                }
            }
        }
Exemplo n.º 24
0
        public void ConstructorTest3()
        {
            Accord.Math.Tools.SetupGenerator(0);

            // Create a normal distribution with mean 2 and sigma 3
            var normal = new NormalDistribution(mean: 2, stdDev: 3);

            // In a normal distribution, the median and
            // the mode coincide with the mean, so

            double mean   = normal.Mean;   // 2
            double mode   = normal.Mode;   // 2
            double median = normal.Median; // 2

            // The variance is the square of the standard deviation
            double variance = normal.Variance; // 3² = 9

            // Let's check what is the cumulative probability of
            // a value less than 3 occurring in this distribution:
            double cdf = normal.DistributionFunction(3); // 0.63055

            // Finally, let's generate 1000 samples from this distribution
            // and check if they have the specified mean and standard dev.

            double[] samples = normal.Generate(10000);


            double sampleMean = samples.Mean();              // 2.00
            double sampleDev  = samples.StandardDeviation(); // 3.00

            Assert.AreEqual(2, mean);
            Assert.AreEqual(2, mode);
            Assert.AreEqual(2, median);

            Assert.AreEqual(9, variance);
            Assert.AreEqual(10000, samples.Length);
            Assert.AreEqual(2.000, sampleMean, 5e-3);
            Assert.AreEqual(3.000, sampleDev, 5e-3);
        }
        private int[] DistributeToBuckets(int numberOfBuckets, int total)
        {
            if (numberOfBuckets == 0)
            {
                return(null);
            }

            var result = new int[numberOfBuckets];

            while (total > 0)
            {
                var distributionFunction = new NormalDistribution(mean: (double)total / numberOfBuckets);
                var samples = distributionFunction.Generate(numberOfBuckets);

                for (int i = 0; i < numberOfBuckets; i++)
                {
                    result[i] += (int)samples[i];
                    total     -= result[i];
                }
            }

            return(result);
        }
Exemplo n.º 26
0
        static void Run(int numPlayers, int numReentries, double eloDistribution)
        {
            // This doesn't really matter but I picked from the same source as my default distribution
            // https://www.mtgcommunityreview.com/single-post/2018/06/12/Luck-Skill-and-Magic
            double eloMean = 1678.632;
            // I'm not actually going to adjust anyone's elo as they go - I'm using it as a representation of "true" skill
            var playerDistribution = new NormalDistribution(eloMean, eloDistribution);
            var players            = new List <Player>();

            for (var index = 0; index < numPlayers; index++)
            {
                var player = new Player();
                player.Elo       = (decimal)playerDistribution.Generate();
                player.GemsSpent = 4000;
                players.Add(player);
            }

            var dayTwoPlayers = RunDayOne(players, numReentries);

            foreach (var player in dayTwoPlayers)
            {
                player.CurrentWins   = 0;
                player.CurrentLosses = 0;
            }
            RunDayTwo(dayTwoPlayers);

            var dayOneAvgElo     = players.Average(p => p.Elo);
            var dayTwoAvgElo     = dayTwoPlayers.Average(p => p.Elo);
            var dayTwoWinPercent = EloCalculator.PredictResult(dayTwoAvgElo, (decimal)eloMean)[0] * 100;

            Console.WriteLine($"Day one average Elo: {dayOneAvgElo}.");
            Console.WriteLine($"Day two average Elo: {dayTwoAvgElo} ({dayTwoWinPercent,3:0.0}% win percent).");

            var numBuckets = 100;
            var bucketSize = numPlayers / numBuckets;

            if (numPlayers % numBuckets != 0)
            {
                Console.WriteLine("Warning - please use a multiple of " + numBuckets + " for more accurate top end performance");
            }

            var eloSorted = players.OrderBy(p => p.Elo).ToList();
            var skip      = 0;

            while (skip + bucketSize <= eloSorted.Count)
            {
                var nextBucket      = eloSorted.Skip(skip).Take(bucketSize).ToList();
                var minElo          = (int)nextBucket.Min(p => p.Elo);
                var maxElo          = (int)nextBucket.Max(p => p.Elo);
                var avgElo          = (int)nextBucket.Average(p => p.Elo);
                var avgGems         = nextBucket.Average(p => p.GemsWon - p.GemsSpent);
                var avgDayOneWins   = nextBucket.Average(p => p.DayOneWins);
                var avgDayOneLosses = nextBucket.Average(p => p.DayOneLosses);
                var avgDayTwoWins   = nextBucket.Average(p => p.DayTwoWins);
                var avgDayTwoLosses = nextBucket.Average(p => p.DayTwoLosses);
                var winPercentage   = EloCalculator.PredictResult(avgElo, (decimal)eloMean)[0] * 100;
                var bucketNumber    = skip / bucketSize + 1;

                Console.WriteLine($"{avgElo} ({minElo}-{maxElo}) ({winPercentage,3:0.0}% game win%, bucket #{bucketNumber}): {avgGems} gems, " +
                                  $"{avgDayOneWins,3:0.0}-{avgDayOneLosses,3:0.0} on day one, " +
                                  $"{avgDayTwoWins,3:0.0}-{avgDayTwoLosses,3:0.0} on day two.");
                skip += bucketSize;
            }
        }
        public static void GenerateValues2(int howMany)
        {
            var disitrubtion = new NormalDistribution(0, Math.Sqrt(1));
            var randomValues = disitrubtion.Generate(howMany * 2);

            var meanA = 0.7;
            var meanB = 0.2;
            var sigma = new double[2, 2];

            sigma[0, 0] = 1;
            sigma[0, 1] = 0.7;
            sigma[1, 0] = 0.7;
            sigma[1, 1] = 1;

            var randomValuesMatrix = new double[howMany, 2];
            var verticalIndex      = 0;

            for (int i = 0; i < howMany; i = i + 2)
            {
                randomValuesMatrix[verticalIndex, 0] = randomValues.ElementAt(i) - meanA;
                randomValuesMatrix[verticalIndex, 1] = randomValues.ElementAt(i + 1) - meanB;

                randomValuesMatrix[verticalIndex, 0] = randomValuesMatrix[verticalIndex, 0] > 0 ? 1 : 0;
                randomValuesMatrix[verticalIndex, 1] = randomValuesMatrix[verticalIndex, 1] > 0 ? 1 : 0;

                verticalIndex++;
            }

            var randomValuesMatrixCov = randomValuesMatrix.Covariance(); //new [] {meanA, meanB}
            var cholCovX    = new CholeskyDecomposition(randomValuesMatrixCov).LeftTriangularFactor.Transpose();
            var invCholCovX = cholCovX.Inverse();

            var dottedInverse = randomValuesMatrix.Dot(invCholCovX);
            var result        = dottedInverse.Dot(new CholeskyDecomposition(sigma).LeftTriangularFactor.Transpose());

            var resultSigma = result.Covariance();

            verticalIndex = 0;
            for (int i = 0; i < howMany; i++)
            {
                result[verticalIndex, 0] = result[verticalIndex, 0] > 0 ? 1 : 0;
                result[verticalIndex, 1] = result[verticalIndex, 1] > 0 ? 1 : 0;

                verticalIndex++;
            }

            var booleansigma = result.Covariance();

            int randomValueCount = 0;

            //for (int i = 0; i < howMany; i++)
            //{
            //    //generating one sample
            //    var z = new[] { randomValues[randomValueCount++]-meanA, randomValues[randomValueCount++]-meanB };
            //    var product = z.Dot(R);
            //    var y = mean.Add(product);
            //    var samples = new double[] { y[0] > 0 ? 1 : 0, y[1] > 0 ? 1 : 0 };

            //    aSamples[i] = samples[0];
            //    bSamples[i] = samples[1];

            //    Trace.WriteLine($"{aSamples[i]}, {bSamples[i]}");
            //}
        }
Exemplo n.º 28
0
        public double[,] _TSNE()
        {
            //# Initialize variables
            //    X = pca(X, initial_dims).real
            var X        = PCA();
            var n        = X.GetLength(0);
            var d        = X.GetLength(1);
            var max_iter = 1000;

            var initial_momentum = 0.5;
            var final_momentum   = 0.8;
            var eta      = 500;
            var min_gain = 0.01;

            // Create a Normal with mean 2 and sigma 5
            var normal = new NormalDistribution(0, 1);

            // Generate samples from it
            double[] samples = normal.Generate(n * no_dims);

            var Y = samples.Reshape(n, no_dims);

            var dY    = Matrix.Create(n, no_dims, 0.0);
            var iY    = Matrix.Create(n, no_dims, 0.0);
            var gains = Matrix.Create(n, no_dims, 1.0);

            //# Compute P-values
            //P = x2p(X, 1e-5, perplexity)
            //P = P + np.transpose(P)
            //P = P / np.sum(P)
            //P = P * 4.									# early exaggeration
            //P = np.maximum(P, 1e-12)

            var P = X2P();

            P = P.Add(P.Transpose());
            P = P.Divide(P.Sum());
            P = P.Multiply(4.0);
            P = NP_MAXIMUM(P, 1e-12);



            for (var iter = 0; iter < max_iter; iter++)
            {
                //# Compute pairwise affinities
                //sum_Y = np.sum(np.square(Y), 1)
                //num = -2. * np.dot(Y, Y.T)
                //num = 1. / (1. + np.add(np.add(num, sum_Y).T, sum_Y))
                //num[range(n), range(n)] = 0.
                //Q = num / np.sum(num)
                //Q = np.maximum(Q, 1e-12)
                var sum_Y = Y.Multiply(Y).Sum(1);
                var num   = Y.Dot(Y.Transpose()).Multiply(-2.0);



                num = NP_ADD(NP_ADD(num, sum_Y).Transpose(), sum_Y).Add(1.0).Pow(-1);

                for (var i = 0; i < num.GetLength(0); i++)
                {
                    num[i, i] = 0.0;
                }



                var Q = num.Divide(num.Sum());
                Q = NP_MAXIMUM(Q, 1e-12);
                var momentum = 0.0;

                //# Compute gradient
                //PQ = P - Q
                //for i in range(n):
                //    dY[i, :] = np.sum(np.tile(PQ[:, i] * num[:, i], (no_dims, 1)).T * (Y[i, :] - Y), 0)
                var PQ = P.Subtract(Q);


                for (var i = 0; i < n; i++)
                {
                    var YY = Matrix.Create(Y.GetLength(0), Y.GetLength(1), 0.0);
                    for (var r = 0; r < YY.GetLength(0); r++)
                    {
                        for (var c = 0; c < YY.GetLength(1); c++)
                        {
                            YY[r, c] = Y[i, c] - Y[r, c];
                        }
                    }

                    var T = NP_TILE(PQ.GetColumn(i).Multiply(num.GetColumn(i)), no_dims, 1).Transpose().Multiply(YY).Sum(0);



                    for (var c = 0; c < dY.GetLength(1); c++)
                    {
                        dY[i, c] = T[c];
                    }
                }



                if (iter < 20)
                {
                    momentum = initial_momentum;
                }

                else
                {
                    momentum = final_momentum;
                }

                //gains = (gains + 0.2) * ((dY > 0.) != (iY > 0.)) + \
                //(gains * 0.8) * ((dY > 0.) == (iY > 0.))
                // gains[gains < min_gain] = min_gain

                var RowNum = gains.GetLength(0);
                var ColNum = gains.GetLength(1);
                for (var r = 0; r < RowNum; r++)
                {
                    for (var c = 0; c < ColNum; c++)
                    {
                        if ((dY[r, c] > 0.0 && iY[r, c] <= 0) || (dY[r, c] <= 0.0 && iY[r, c] > 0))
                        {
                            gains[r, c] = gains[r, c] + 0.2;
                        }
                        else
                        {
                            gains[r, c] = gains[r, c] * 0.8;
                        }

                        if (gains[r, c] < min_gain)
                        {
                            gains[r, c] = min_gain;
                        }
                        // iY = momentum * iY - eta * (gains * dY)
                        iY[r, c] = momentum * iY[r, c] - eta * (gains[r, c] * dY[r, c]);
                        Y[r, c]  = Y[r, c] + iY[r, c];
                    }
                }
                // Y = Y - np.tile(np.mean(Y, 0), (n, 1))

                Y = Y.Subtract(NP_TILE(Y.Sum(0).Divide(Y.GetLength(0)), n, 1));



                //# Compute current value of cost function
                //if (iter + 1) % 10 == 0:
                //    C = np.sum(P * np.log(P / Q))
                //    print("Iteration %d: error is %f" % (iter + 1, C))
                if ((iter + 1) % 10 == 0)
                {
                    var C = P.Multiply(P.Divide(Q).Log()).Sum();
                    Console.WriteLine("Iteration {0}: error is {1}", (iter + 1), C);
                }


                //# Stop lying about P-values
                //if iter == 100:
                //    P = P / 4.
                if (iter == 100)
                {
                    P = P.Divide(4.0);
                }
            }
            return(Y);
        }
Exemplo n.º 29
0
        public static List <double> GenerateStandardNormalRandomNumbers(int numberOfDraws)
        {
            NormalDistribution normalDistribution = new NormalDistribution();

            return(normalDistribution.Generate(numberOfDraws).ToList());
        }