Exemplo n.º 1
0
        /// <summary>
        ///   Generates a random vector of observations from the current distribution.
        /// </summary>
        ///
        /// <returns>A random vector of observations drawn from this distribution.</returns>
        ///
        public override double Generate()
        {
            var g = new Accord.Math.Random.GaussianGenerator(
                (float)mean, (float)stdDev, Accord.Math.Random.Generator.Random.Next());

            return(g.Next());
        }
        /// <summary>
        ///   Generates a random vector of observations from the
        ///   Inverse Gaussian distribution with the given parameters.
        /// </summary>
        ///
        /// <param name="mean">The mean parameter mu.</param>
        /// <param name="shape">The shape parameter lambda.</param>
        /// <param name="samples">The number of samples to generate.</param>
        ///
        /// <returns>An array of double values sampled from the specified Uniform distribution.</returns>
        ///
        public static double[] Random(double mean, double shape, int samples)
        {
            var u = Accord.Math.Random.Generator.Random;
            var g = new Accord.Math.Random.GaussianGenerator(0, 1, u.Next());

            double[] r = new double[samples];
            for (int i = 0; i < r.Length; i++)
            {
                double v = g.Next();
                double y = v * v;
                double x = mean + (mean * mean * y) / (2 * shape) - (mean / (2 * shape)) * Math.Sqrt(4 * mean * shape * y + mean * mean * y * y);

                double t = u.NextDouble();

                if (t <= (mean) / (mean + x))
                {
                    r[i] = x;
                }
                else
                {
                    r[i] = (mean * mean) / x;
                }
            }

            return(r);
        }
Exemplo n.º 3
0
        public static Matrix Random(int rows, int cols, double initParamsStdDev, Random rng)
        {
            Accord.Math.Random.GaussianGenerator gen = new Accord.Math.Random.GaussianGenerator(0, (float)initParamsStdDev, rng.Next());

            Matrix result = new Matrix(rows, cols);

            for (int i = 0; i < result.W.Length; i++)
            {
                result.W[i] = gen.Next();
            }
            return(result);
        }
Exemplo n.º 4
0
        /// <summary>
        ///   Generates a random vector of observations from the current distribution.
        /// </summary>
        ///
        /// <param name="samples">The number of samples to generate.</param>
        ///
        /// <returns>A random vector of observations drawn from this distribution.</returns>
        ///
        public override double[] Generate(int samples)
        {
            double[] r = new double[samples];

            var g = new Accord.Math.Random.GaussianGenerator(
                (float)mean, (float)stdDev, Accord.Math.Random.Generator.Random.Next());

            for (int i = 0; i < r.Length; i++)
            {
                r[i] = g.Next();
            }

            return(r);
        }
        /// <summary>
        ///   Generates a random observation from the
        ///   Inverse Gaussian distribution with the given parameters.
        /// </summary>
        ///
        /// <param name="mean">The mean parameter mu.</param>
        /// <param name="shape">The shape parameter lambda.</param>
        ///
        /// <returns>A random double value sampled from the specified Uniform distribution.</returns>
        ///
        public static double Random(double mean, double shape)
        {
            var u = Accord.Math.Random.Generator.Random;
            var g = new Accord.Math.Random.GaussianGenerator(0, 1, u.Next());

            double v = g.Next();
            double y = v * v;
            double x = mean + (mean * mean * y) / (2 * shape) - (mean / (2 * shape)) * Math.Sqrt(4 * mean * shape * y + mean * mean * y * y);

            double t = u.NextDouble();

            if (t <= (mean) / (mean + x))
            {
                return(x);
            }

            return((mean * mean) / x);
        }
Exemplo n.º 6
0
        private void FillNaNs(double[][] data)
        {
            string prefix = "Filling NaNs...";

            Utils.DrawMessage(prefix, Utils.CreateProgressBar(Utils.ProgressBarLength, 0), ConsoleColor.Gray);

            int drawEvery = Utils.PercentIntervalByLength(data[0].Length);

            Random currentRandom = new Random(DataManager.Config.Random.Next());

            // Loop through all columns
            int row = 0;

            for (int col = 0; col < data[0].Length; ++col)
            {
                // Najdeme si statisticke vlastnosti
                double[] d    = data.Select(x => x[col]).ToArray();
                double[] diff = new double[d.Length - 1];
                for (int i = 1; i < d.Length; ++i)
                {
                    diff[i - 1] = d[i] - d[i - 1];
                }
                diff = diff.Where(x => !double.IsNaN(x)).ToArray();
                double mean   = diff.Mean();
                double stddev = diff.StandardDeviation(mean);
                if (diff.Length <= 1)
                {
                    mean   = 0;
                    stddev = 1;
                }

                // Nahodny generator
                Accord.Math.Random.GaussianGenerator gen = new Accord.Math.Random.GaussianGenerator((float)mean, (float)stddev, currentRandom.Next());

                // Doplnime zacatek
                for (row = 0; row < data.Length; ++row)
                {
                    if (!double.IsNaN(data[row][col]))
                    {
                        for (row = row - 1; row >= 0; --row)
                        {
                            data[row][col] = data[row + 1][col] + gen.Next();
                        }
                        break;
                    }
                }

                // Doplnime konec
                for (row = data.Length - 1; row >= 0; --row)
                {
                    if (!double.IsNaN(data[row][col]))
                    {
                        for (row = row + 1; row < data.Length; ++row)
                        {
                            data[row][col] = data[row - 1][col] + gen.Next();
                        }
                        break;
                    }
                }

                // Doplnime mezery
                for (row = 0; row < data.Length; ++row)
                {
                    if (double.IsNaN(data[row][col]))
                    {
                        int start = row - 1;
                        for (row = row + 1; row < data.Length; ++row)
                        {
                            if (!double.IsNaN(data[row][col]))
                            {
                                int end = row;

                                // Doplnime od start do end
                                for (int i = start + 1; i < end; ++i)
                                {
                                    // Najdeme hodnotu pri aproximaci
                                    double aprox = data[start][col] + (data[end][col] - data[start][col]) / (end - start) * (i - start);

                                    data[i][col] = aprox + gen.Next();
                                }

                                break;
                            }
                        }
                    }
                }

                // Pro jistotu rozkopirovani
                for (row = 1; row < data.Length; ++row)
                {
                    if (double.IsNaN(data[row][col]))
                    {
                        data[row][col] = data[row - 1][col];
                    }
                }
                for (row = data.Length - 2; row >= 0; --row)
                {
                    if (double.IsNaN(data[row][col]))
                    {
                        data[row][col] = data[row + 1][col];
                    }
                }

                if (col % drawEvery == 0)
                {
                    Utils.DrawMessage(prefix, Utils.CreateProgressBar(Utils.ProgressBarLength, (double)col / data[0].Length * 100.0), ConsoleColor.Gray);
                }
            }

            Utils.DrawMessage(prefix, Utils.CreateProgressBar(Utils.ProgressBarLength, 100), ConsoleColor.Green);
            Console.WriteLine();
        }