Exemplo n.º 1
0
        public void Statistics_Mode_Single_Value()
        {
            double[] na    = new double[] { 1 };
            double   value = Dispersion.Mode(na);

            Assert.AreEqual(1, value);
        }
Exemplo n.º 2
0
        public void Statistics_Standard_Deviation_Population_precomputed_mean()
        {
            double mean  = Dispersion.Mean(ta);
            double value = Dispersion.StandardDeviationPopulation(ta, mean);

            Assert.IsTrue((6.81 <= value && value <= 6.83));
        }
Exemplo n.º 3
0
        public void Statictics_CoVariance_1()
        {
            InitData_dataset_pca_example();
            double value = Dispersion.CoVarianceSample(_trainingData[0], _trainingData[1]);

            Assert.IsTrue(SupportFunctions.DoubleCompare(value, .6154));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Compute the SD of each column
        ///
        /// </summary>
        /// <param name="classInputMatrix"></param>
        /// <returns>Returns a list of 2D array.
        /// Even though there is 1 rowm the 2D array is returned
        /// for use in any future matrix computations</returns>
        protected List <double[][]> GetClassStandardDeviationMatrix(List <double[][]>
                                                                    classInputMatrix, List <double[][]>
                                                                    classMeanMatrix)
        {
            if (classInputMatrix == null || classMeanMatrix == null)
            {
                throw new InvalidMatrixException();
            }
            List <double[][]> sdMatrix = new List <double[][]>();

            int noOfAttributes = classInputMatrix[0].Length;

            //foreach (double[][] cim in classInputMatrix)
            for (int ii = 0; ii < classInputMatrix.Count; ii++)
            {
                double[][] cim = classInputMatrix[ii];
                double[][] sd  = new double[noOfAttributes][];

                //For each attribute
                Parallel.For(0, sd.Length, idx =>
                {
                    sd[idx] = new double[1];
                    //Do not call SD functionin Statistic routine
                    //as mean is already known and it will be double computation
                    sd[idx][0] =
                        Dispersion.StandardDeviationPopulation(
                            cim[idx], classMeanMatrix[ii][idx][0]);
                });

                sdMatrix.Add(sd);
            }

            return(sdMatrix);
        }
Exemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="classInputMatrix"></param>
        /// <returns>Returns a list of 2D array.
        /// Even though there is 1 rowm the 2D array is returned
        /// for use in any future matrix computations</returns>
        protected List <double[][]> GetClassMeanMatrix(List <double[][]>
                                                       classInputMatrix)
        {
            if (classInputMatrix == null)
            {
                throw new InvalidMatrixException();
            }
            List <double[][]> meanMatrix = new List <double[][]>();

            int noOfAttributes = classInputMatrix[0].Length;

            foreach (double[][] cim in classInputMatrix)
            {
                double[][] mean = new double[noOfAttributes][];

                Parallel.For(0, mean.Length, idx =>
                {
                    mean[idx]    = new double[1];
                    mean[idx][0] =
                        Dispersion.Mean(cim[idx]);
                });

                meanMatrix.Add(mean);
            }

            return(meanMatrix);
        }
 void Lists_Inicializations()    //Chamada no Start, inicia as listas, para que valores possam ser atribuidos a elas.
 {
     //--------Iniciando a Lista "PercenteCadence":---				Define a porcentagem de dar o minimo de disparos
     PercenteCadence.Insert(0, 0.7f);                                                                        //70% Aircraft01
     PercenteCadence.Insert(1, 0.6f);                                                                        //60% Aircraft02
     PercenteCadence.Insert(2, 0.5f);                                                                        //50% Aircraft03
     PercenteCadence.Insert(3, 0.3f);                                                                        //30% Aircraft04
     PercenteCadence.Insert(4, 0.25f);                                                                       //25% VTOLBoss
     //--------Iniciando a Lista "PercentageToCompare"-
     for (int i = 0; i < 5; i++)
     {
         PercentageToCompare.Insert(i, Random.value);
     }
     //--------Iniciando a Lista "Cadence":-----------
     Cadence.Insert(0, 0);                                                                                   //Iniciando a cadencia de Aircraft01 no valor minimo
     Cadence.Insert(1, 0);                                                                                   //Iniciando a cadencia de Aircraft02 no valor minimo
     Cadence.Insert(2, 1);                                                                                   //Iniciando a cadencia de Aircraft03 no valor minimo
     Cadence.Insert(3, 1);                                                                                   //Iniciando a cadencia de Aircraft04 no valor minimo
     Cadence.Insert(4, 2);                                                                                   //Iniciando a cadencia de VTOLBoss no valor minimo
     //--------Iniciando a Lista "PercenteHit":-------
     PercenteHit.Insert(0, 0.8f);                                                                            //Se o sorteio de Aircraft01 for maior que PercenteHit[0], a bala acerta o canhao.
     PercenteHit.Insert(1, 0.6f);                                                                            //Se o sorteio de Aircraft02 for maior que PercenteHit[1], a bala acerta o canhao.
     PercenteHit.Insert(2, 0.5f);                                                                            //Se o sorteio de Aircraft03 for maior que PercenteHit[2], a bala acerta o canhao.
     PercenteHit.Insert(3, 0.4f);                                                                            //Se o sorteio de Aircraft04 for maior que PercenteHit[3], a bala acerta o canhao.
     PercenteHit.Insert(4, 0.25f);                                                                           //Se o sorteio de VTOLBoss for maior que PercenteHit[4], a bala acerta o canhao.
     //--------Iniciando a Lista "yDelays":-----------				Como funciona: NavePosition.Y = yDelays[YIndex];
     YDelays.Insert(0, 1);
     YDelays.Insert(1, 1);
     YDelays.Insert(2, Random.Range(1, 2));
     YDelays.Insert(3, Random.Range(1, 2));
     YDelays.Insert(4, Random.Range(1, 6));
     YDelays.Insert(5, 0);
     YDelays.Insert(6, 0);
     //--------Iniciando a Lista "Dispersion":--------
     Dispersion.Insert(0, 1);                                                                // A Aircraft01 pode atirar 0 ou 1 bala cada vez que for atirar
     Dispersion.Insert(1, 1);                                                                // A Aircraft02 vai atirar 1 bala cada vez que for atirar
     Dispersion.Insert(2, Random.Range(1, 2));                                               // A Aircraft03 pode atirar entre 1 e 2 balas cada vez que for atirar
     Dispersion.Insert(3, Random.Range(1, 2));                                               // A Aircraft04 pode atirar entre 1 e 2 balas cada vez que for atirar
     Dispersion.Insert(4, Random.Range(2, 3));                                               // A VTOLBoss pode atirar entre 2 e 3 balas cada vez que for atirar
     //--------Iniciando a Lista "Defense_Rate":------				(Dano do tiro recebido - Defesa da nave)
     Defense_Rate.Insert(0, Random.Range(10, 16));                                           // O valor da defesa da Aircraft01 pode ser entre 10 e 15
     Defense_Rate.Insert(1, Random.Range(10, 16));                                           // O valor da defesa da Aircraft02 pode ser entre 10 e 15
     Defense_Rate.Insert(2, Random.Range(15, 21));                                           // O valor da defesa da Aircraft03 pode ser entre 15 e 20
     Defense_Rate.Insert(3, Random.Range(15, 26));                                           // O valor da defesa da Aircraft04 pode ser entre 15 e 25
     Defense_Rate.Insert(4, Random.Range(25, 41));                                           // O valor da defesa da VTOLBoss pode ser entre 25 e 40
     Defense_Rate.Insert(5, 15);                                                             // O valor da defesa da Cargo_Aircraft pode ser entre 25 e 40
     Defense_Rate.Insert(6, 0);                                                              // O valor da defesa da Passenger_Aircraft pode ser entre 25 e 40
     //--------Iniciando a Lista "YPositions":--------
     YPositions.Insert(0, 17f);
     YPositions.Insert(1, 13.5f);
     YPositions.Insert(2, 10f);
     YPositions.Insert(3, 6.5f);
     YPositions.Insert(4, 3f);
     YPositions.Insert(5, -0.5f);
     YPositions.Insert(5, -4f);
 }
Exemplo n.º 7
0
        public void Statictics_CoVariance_matrix_2_cols_1()
        {
            InitData_dataset_pca_example();
            double[][] matrix = Dispersion.CovarianceMatrixSample(_trainingData);

            Assert.IsTrue(SupportFunctions.DoubleCompare(matrix[0][0], .6165));
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrix[0][1], .6154));
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrix[1][0], .6154));
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrix[1][1], .7165));
        }
Exemplo n.º 8
0
 public void Statictics_CoVariance_Matrix_2_cols_0()
 {
     double[][] data = new double[2][];
     data[0] = ta;
     data[1] = ta2;
     double[][] matrix = Dispersion.CovarianceMatrixSample(data);
     Assert.IsTrue(SupportFunctions.DoubleCompare(matrix[0][0], 52.36));
     Assert.IsTrue(SupportFunctions.DoubleCompare(matrix[0][1], 21.12));
     Assert.IsTrue(SupportFunctions.DoubleCompare(matrix[1][0], 21.12));
     Assert.IsTrue(SupportFunctions.DoubleCompare(matrix[1][1], 55.75));
 }
Exemplo n.º 9
0
 //Return mean matrix of dataset
 //Private members are not being used due to unit test purposes
 /// <summary>
 /// Assume target attributes is last column in Training Data
 /// </summary>
 /// <param name="trainingData"></param>
 /// <param name="indexTargetAttribute"></param>
 /// <param name="noOfAttributes"></param>
 /// <returns></returns>
 protected double[] GetDataSetMeanMatrix()
 {
     double[] mean = new double[_noOfAttributes];
     //int idx = 0;
     //Assumption: Target Attributer is last column
     Parallel.For(0, _trainingData.Length - 1, colIdx => //For each column
     {
         //if (colIdx != indexTargetAttribute)
         //{
         mean[colIdx] = Dispersion.Mean(_trainingData[colIdx]);
         //}
     });
     return(mean);
 }
Exemplo n.º 10
0
        public void Statictics_ZeroCentered_2_rows_0()
        {
            InitData_dataset_pca_example();

            double[][] matrix = Dispersion.GetZeroCenteredData(_trainingData, false);

            Assert.IsTrue(SupportFunctions.DoubleCompare(matrix[0][0], .69));
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrix[0][1], -1.31));
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrix[0][2], .39));
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrix[0][3], .09));
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrix[0][4], 1.29));
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrix[1][0], .49));
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrix[1][1], -1.21));
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrix[1][2], .99));
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrix[1][3], .29));
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrix[1][4], 1.09));
        }
Exemplo n.º 11
0
        double RunModelForSingleData(double[] data)
        {
            VerifyDataForRun(data);
            double[] values =
                new double[_allTrees.Length];
            int idx = 0;

            //Iterate through all trees
            foreach (ModelCART modelCart in _allTrees)
            {
                values[idx++] =
                    modelCart.RunModelForSingleData(data);
            }

            double finalValue = Dispersion.Mode(values);

            return(finalValue);
        }
Exemplo n.º 12
0
        public override double RunModelForSingleData(double[] data)
        {
            double value = 0;

            VerifyDataForRun(data);

            if (data.Length != _data.Length - 1)
            {
                throw new InvalidDataException();
            }

            KeyValuePair <int, double>[] allValues =
                new KeyValuePair <int, double> [_data[0].Length];

            //Compute distance per Row
            Parallel.For(0, _data[0].Length, new ParallelOptions {
                MaxDegreeOfParallelism = _maxParallelThreads
            }, row =>
            {
                double[] tArray = SupportFunctions.GetLinearArray(_data, row, _data.Length - 2);
                double distance = _distanceMeasure.getDistanceVector(tArray, data);
                allValues[row]  = new KeyValuePair <int, double>(row, distance);
            });

            List <KeyValuePair <int, double> > distList =
                SupportFunctions.GetSortedKeyValuePair(allValues);

            double[] targetValues = new double[_k];
            //Get lowest k distancesPerRow and their count
            //for (int ii= 0; ii < _k;ii++)
            Parallel.For(0, _k, new ParallelOptions {
                MaxDegreeOfParallelism = _maxParallelThreads
            }, ii =>
            {
                int row          = distList[ii].Key;
                targetValues[ii] = _data[_origTargetAttributeIndex][row];
            });

            //Get the mode
            value = Dispersion.Mode(targetValues);
            return(value);
        }
Exemplo n.º 13
0
        //Training Data Should have only Two Attributes
        public override Common.MLCore.ModelBase BuildModel(double[][] trainingData,
                                                           string[] attributeHeaders,
                                                           int indexTargetAttribute)
        {
            VerifyData(trainingData, attributeHeaders, indexTargetAttribute);

            double[]        x, y;
            ModelLinearBase model = new ModelLinearBase(_missingValue,
                                                        _indexTargetAttribute, _trainingData.Length - 1);

            //Additional Checks
            if (trainingData.Length != 2)
            {
                throw new InvalidTrainingDataException();
            }
            if (indexTargetAttribute == 0)
            {
                x = trainingData[1];
                y = trainingData[0];
            }
            else
            {
                x = trainingData[0];
                y = trainingData[1];
            }

            //x,y are arrays
            model.B1 = Dispersion.CorrelationPearson(x, y)
                       * (Dispersion.StandardDeviationSample(y) /
                          Dispersion.StandardDeviationSample(x));

            model.B0 = Dispersion.Mean(y) -
                       model.B1 * Dispersion.Mean(x);

            return(model);
        }
Exemplo n.º 14
0
        public void Statistics_Mode()
        {
            double value = Dispersion.Mode(ta);

            Assert.AreEqual(Double.NaN, value);
        }
Exemplo n.º 15
0
        public void Statistics_Standard_Deviation_Sample()
        {
            double value = Dispersion.StandardDeviationSample(ta);

            Assert.IsTrue((7.236 <= value && value <= 7.237));
        }
Exemplo n.º 16
0
        public void Statictics_CoVariance_0()
        {
            double value = Dispersion.CoVarianceSample(ta, ta2);

            Assert.IsTrue(SupportFunctions.DoubleCompare(value, 21.125));
        }
Exemplo n.º 17
0
        public void Statistics_Correlation_Two()
        {
            double value = Dispersion.CorrelationPearson(ta, ta2);

            Assert.IsTrue((.389 <= value && value <= .391));
        }
Exemplo n.º 18
0
        public void Statistics_Mean()
        {
            double value = Dispersion.Mean(ta);

            Assert.IsTrue((9.887 <= value && value <= 9.889));
        }
Exemplo n.º 19
0
 private void FContainer_Load(object sender, EventArgs e)
 {
     this.WindowState = FormWindowState.Maximized;
     Dispersion.PerformClick();
 }
Exemplo n.º 20
0
        public void Statistics_Sum()
        {
            double value = Dispersion.Sum(ta);

            Assert.AreEqual(value, 89);
        }
Exemplo n.º 21
0
        public void Statistics_Standard_Deviation_Population()
        {
            double value = Dispersion.StandardDeviationPopulation(ta);

            Assert.IsTrue((6.81 <= value && value <= 6.83));
        }
Exemplo n.º 22
0
        public void Statistics_VariancePopulation()
        {
            double value = Dispersion.VariancePopulation(ta);

            Assert.IsTrue((46.53 <= value && value <= 46.55));
        }
Exemplo n.º 23
0
        public void Statistics_VarianceSample()
        {
            double value = Dispersion.VarianceSample(ta);

            Assert.IsTrue((52.360 <= value && value <= 52.362));
        }
Exemplo n.º 24
0
        public void Statistics_Range()
        {
            double value = Dispersion.Range(ta);

            Assert.IsTrue((24 <= value && value <= 24));
        }
Exemplo n.º 25
0
        public void Statistics_Correlation_One()
        {
            double value = Dispersion.CorrelationPearson(ta, ta);

            Assert.IsTrue(value == 1);
        }
Exemplo n.º 26
0
        private static void DisplayResults(string parameters, int criteriaItemsAmount, List <double> results,
                                           ProbabilityFunction probabilityFunction, MathematicalExpectation mathematicalExpectation, Dispersion dispersion)
        {
            Console.WriteLine(Environment.NewLine +
                              $"-----{dispersion.Method.Name.Replace("Dispersion", "")} distribution ({parameters})-----");
            results.Take(30).ToList().ForEach(x => Console.Write($"{x:F0} "));
            Console.Write(Environment.NewLine + $"{N} items: ");
            for (int i = 0; i < Math.Min(7, criteriaItemsAmount); i++)
            {
                Console.Write($"{i}({results.Count(x => x == i)}) ");
            }
            Console.WriteLine();
            Console.WriteLine($"Practical  : Average={results.Average():F5}, Dispersion={CalculateDispersion(results.ToList()):F5}");
            Console.WriteLine($"Theoretical: Average={mathematicalExpectation():F5}, Dispersion={dispersion():F5}");
            CheckPearsonCriteria(GetSortedList(results), probabilityFunction, criteriaItemsAmount);

            double CalculateDispersion(List <double> list) => list.Sum(x => Math.Pow(x - list.Average(), 2)) / list.Count;
            List <double> GetSortedList(List <double> list) => list.OrderBy(x => x).ToList();
        }
Exemplo n.º 27
0
        public double[][] GetPrincipleFeatures(double[][] trainingData,
                                               string[] attributeHeaders,
                                               ref double[] standardDeviations,
                                               ref double[][] rotationMatrix,
                                               int maxParallelThreads   = -1,
                                               int indexTargetAttribute = -1)
        {
            if (trainingData.Length !=
                attributeHeaders.Length)
            {
                throw new AttributesCountMismatchException();
            }

            bool ignoreLastColumn = indexTargetAttribute >= 0?true:false;

            double[][] zeroCenteredData = Dispersion.GetZeroCenteredData(trainingData,
                                                                         ignoreLastColumn,
                                                                         maxParallelThreads);
            if (Rank > zeroCenteredData.Length)
            {
                Rank = zeroCenteredData.Length;
            }

            //Generate Covariance Matrix from NewData
            double[][] coVarMatrix = Dispersion.CovarianceMatrixSample(zeroCenteredData, maxParallelThreads, true);

            //Compute EigenValue/Vectors of coVarMatrix
            double[][] eigenVectors = null;
            double[]   eigenValues  = null;

            MatrixOperations mo = new MatrixOperations();

            mo.EigenValues(coVarMatrix, ref eigenValues,
                           ref eigenVectors);

            //Form Feature Vector
            //Sort indexes by EigenValues
            //List<int> A = new List<int>() { 3, 2, 1 };

            var sorted = eigenValues
                         .Select((x, i) => new KeyValuePair <double, int>(x, i))
                         .OrderByDescending(x => x.Key)
                         .ToList();

            //List<int> B = sorted.Select(x => x.Key).ToList();
            List <int> idxSorted = sorted.Select(x => x.Value).ToList();

            //Convert SD Matrix
            List <double> standardDevList = new List <double>();

            for (int idx = 0; idx < Rank; idx++)
            {
                if (eigenValues[idxSorted[idx]] >= 0)
                {
                    double sd = System.Math.Sqrt(eigenValues[idxSorted[idx]]);
                    if (sd >= Tolerance)
                    {
                        standardDevList.Add(sd);
                    }
                }
            }

            standardDeviations = standardDevList.ToArray();

            //EigenValues are already sorted in decreasing order
            //Hence idxSorted[0] already has highest eigenvalue/std dev
            rotationMatrix = new double[standardDeviations.Length][];

            for (int idx = 0; idx < standardDeviations.Length; idx++)
            {
                rotationMatrix[idx] = eigenVectors[idxSorted[idx]];
            }

            //Derive New Data Set
            double[][] finalPCAData = mo.Multiply(mo.Transpose(rotationMatrix),
                                                  mo.Transpose(zeroCenteredData));
            finalPCAData = mo.Transpose(finalPCAData);
            return(finalPCAData);
        }