コード例 #1
0
ファイル: ClusterX.cs プロジェクト: cechovsky/DiplomaThesis
        public double GetSDNLL_MDF(ILArray <double> mdfVector)
        {
            double firstPart  = 0;
            double secondPart = 0;
            double thirdPart  = 0;

            if (this.items.Count < 2)
            {
                this.covarianceMatrixMDF = this.GetVarianceMatrix_MDF();
            }

            int q = this.meanMDF.Length;

            // get matrix W
            ILArray <double> W        = this.GetMatrixW_MDF();
            ILArray <double> WInverse = this.GetInverseMatrixOfMatrix(W, q);

            /*if (Double.IsNaN(WInverse.ToArray()[0]))
             * {
             *  //throw new InvalidDataException("inverse of covariance matrix MDF is NaN");
             *  WInverse = ILMath.eye(this.parent.VarianceMDF.Length, this.parent.VarianceMDF.Length);
             *  Debug.Assert(true, "Problem with covariance gaussian matrix.");
             * }*/


            ILArray <double> vector1  = mdfVector - this.meanMDF;
            ILArray <double> tmpArray = ILMath.multiply(WInverse, vector1);

            firstPart  = 0.5 * ILMath.multiply(vector1.T, tmpArray).ToArray()[0];
            secondPart = q / 2 * ILMath.log(2 * Math.PI).ToArray()[0];
            thirdPart  = 0.5 * ILMath.log(ILMath.det(W)).ToArray()[0];

            return(firstPart + secondPart + thirdPart);
        }
コード例 #2
0
        private double calcVarianceFunctionIL(double[] xi, double[] xj, ILArray <double> dispersion)
        {
            ILArray <double> xiIL = xi;
            ILArray <double> xjIL = xj;

            return((double)ILMath.multiply(ILMath.multiply(xiIL.T, dispersion), xjIL));
        }
コード例 #3
0
        private double calcVarianceForRun(double[] run, double[,] candidateMatrix)
        {
            ILArray <double> vector  = run;
            ILArray <double> matrix  = candidateMatrix;
            ILArray <double> inverse = ILMath.pinv(ILMath.multiply(matrix, matrix.T));

            return((double)ILMath.multiply(ILMath.multiply(vector.T, inverse), vector));
        }
コード例 #4
0
        /// <summary>
        /// Multidimensional scaling/PCoA: transform distances to points in a coordinate system.
        /// </summary>
        /// <param name="input">A matrix of pairwise distances. Zero indicates identical objects.</param>
        /// <returns>A matrix, the columns of which are coordinates in the nth dimension.
        /// The rows are in the same order as the input.</returns>
        public static ILArray <double> Scale(ILArray <double> input)
        {
            int n = input.Length;

            ILArray <double> p = ILMath.eye <double>(n, n) - ILMath.repmat(1.0 / n, n, n);

            ILArray <double> a = -.5 * ILMath.multiplyElem(input, input);
            ILArray <double> b = ILMath.multiply(p, a, p);

            ILArray <complex> V = ILMath.empty <complex>();
            ILArray <complex> E = ILMath.eig((b + b.T) / 2, V);

            ILArray <int>    i = ILMath.empty <int>();
            ILArray <double> e = ILMath.sort(ILMath.diag(ILMath.real(E)), i);

            e = ILMath.flipud(e);
            i = ILMath.toint32(ILMath.flipud(ILMath.todouble(i)));

            ILArray <int> keep = ILMath.empty <int>();

            for (int j = 0; j < e.Length; j++)
            {
                if (e[j] > 0.000000001)
                {
                    keep.SetValue(j, keep.Length);
                }
            }

            ILArray <double> Y;

            if (ILMath.isempty(keep))
            {
                Y = ILMath.zeros(n, 1);
            }
            else
            {
                Y = ILMath.zeros <double>(V.S[0], keep.Length);
                for (int j = 0; j < keep.Length; j++)
                {
                    Y[ILMath.full, j] = ILMath.todouble(-V[ILMath.full, i[keep[j]]]);
                }
                Y = ILMath.multiply(Y, ILMath.diag(ILMath.sqrt(e[keep])));
            }

            ILArray <int> maxind = ILMath.empty <int>();

            ILMath.max(ILMath.abs(Y), maxind, 0);
            int              d       = Y.S[1];
            ILArray <int>    indices = maxind + ILMath.toint32(ILMath.array <int>(SteppedRange(0, n, (d - 1) * n)));
            ILArray <double> colsign = ILMath.sign(Y[indices]);

            for (int j = 0; j < Y.S[1]; j++)
            {
                Y[ILMath.full, j] = Y[ILMath.full, j] * colsign[j];
            }

            return(Y);
        }
コード例 #5
0
        private double calcVarianceFunction(double[] xi, double[] xj, double[,] matrix)
        {
            ILArray <double> xiIL     = xi;
            ILArray <double> xjIL     = xj;
            ILArray <double> matrixIL = matrix;
            ILArray <double> inverse  = ILMath.pinv(ILMath.multiply(matrixIL, matrixIL.T));

            return((double)ILMath.multiply(ILMath.multiply(xiIL.T, inverse), xjIL));
        }
コード例 #6
0
        private double calcVarianceFunction(double[] xi, double[] xj, double[,] matrix)
        {
            ILArray <double> xiIL     = xi;
            ILArray <double> xjIL     = xj;
            ILArray <double> matrixIL = matrix;
            ILArray <double> inverse  = ((double[, ])MachineLearning.Learning.Regression.FeatureSubsetSelection.toSystemMatrix <double>((ILMath.multiply(matrixIL, matrixIL.T)))).PseudoInverse();

            return((double)ILMath.multiply(ILMath.multiply(xiIL.T, inverse), xjIL));
        }
コード例 #7
0
ファイル: Vector.cs プロジェクト: cechovsky/DiplomaThesis
        public double GetMDFDistance(ILArray <double> vector)
        {
            ILArray <double> delta  = this.valuesMDF - vector;
            double           result = ILMath.multiply(delta.T, delta).ToArray()[0];

            if (result == 0)
            {
                return(0);
            }
            return(Math.Sqrt(result));
        }
コード例 #8
0
        private double calcVarianceForRun(double[] run, double[,] candidateMatrix)
        {
            ILArray <double> vector = run;
            ILArray <double> matrix = candidateMatrix;

            ILArray <double> matrixTimes = ILMath.multiply(matrix, matrix.T);



            ILArray <double> inverse = ((double[, ])MachineLearning.Learning.Regression.FeatureSubsetSelection.toSystemMatrix <double>(matrixTimes)).PseudoInverse();

            return((double)ILMath.multiply(ILMath.multiply(vector.T, inverse), vector));
        }
コード例 #9
0
        /// <summary>
        /// Calculates the eigenvalues and the eigenfaces.
        /// </summary>
        private void CalculateEigenValues()
        {
            //25 = magicnumber, just needed to initialize double array
            this._eigenValues = new double[25];
            _s.Diagonal.ExportValues(ref this._eigenValues);
            var eigenValuesPow = new double[_eigenValues.Length];

            for (var i = 0; i < eigenValuesPow.Length; i++)
            {
                eigenValuesPow[i] = (double)Math.Pow((double)_eigenValues[i], -0.5);
            }

            this._eigenVectors.Clear();

            for (var i = 0; i < eigenValuesPow.Length; i++)
            {
                var oneEigenVector = (ILArray <double>)_v.Subarray(new string[]
                {
                    ":",
                    i.ToString()
                });
                var oneFace  = ILMath.multiply(_a, oneEigenVector);
                var oneFace2 = ILMath.multiply(oneFace, eigenValuesPow[i]);

                //25 = magicnumber, just needed to initialize double array
                var eigenVectorArray = new double[25];
                oneFace2.ExportValues(ref eigenVectorArray);

                var distance = 0.0;
                for (var j = 0; j < eigenVectorArray.Length; j++)
                {
                    distance += Math.Pow((double)eigenVectorArray[j], 2.0);
                }

                distance = Math.Sqrt(distance);
                for (var j = 0; j < eigenVectorArray.Length; j++)
                {
                    eigenVectorArray[j] /= (float)distance;
                }
                this._eigenVectors.Add(eigenVectorArray);
            }

            this._eigenWeights.Clear();

            for (var i = 0; i < this._vectorSet.Count; i++)
            {
                var existingWeight = this.GetEigenWeight((byte[])this._vectorSet[i], this._eigenVectors.Count);
                this._eigenWeights.Add(existingWeight);
            }
        }
コード例 #10
0
        private bool fitModel(List <Feature> newModel)
        {
            ILArray <double> DM = createDataMatrix(newModel);

            if (DM.Size.NumberOfElements == 0)
            {
                return(false);
            }
            //   ILArray<double> DMT = DM.T;
            ILArray <double> temparray = null;

            double[,] fixSVDwithACCORD;
            //var exp = toSystemMatrix<double>(DM.T);
            // fixSVDwithACCORD = (double[,])exp;
            fixSVDwithACCORD = ((double[, ])toSystemMatrix <double>(DM.T)).PseudoInverse();
            temparray        = fixSVDwithACCORD;

            ILArray <double> constants;

            if (temparray.IsEmpty)
            {
                constants = ILMath.multiply(DM, Y_learning.T);
            }
            else
            {
                constants = ILMath.multiply(temparray, Y_learning.T);
            }
            double[] fittedConstant = constants.ToArray <double>();
            for (int i = 0; i < constants.Length; i++)
            {
                newModel[i].Constant = fittedConstant[i];
                //constants.GetValue(i);
            }
            //the fitting found no further influence
            if (newModel[constants.Length - 1].Constant == 0)
            {
                return(false);
            }
            return(true);
        }
コード例 #11
0
ファイル: ClusterX.cs プロジェクト: cechovsky/DiplomaThesis
        public void UpdateCovarianceMatrixMDF_NonParametric(ILArray <double> vector)
        {
#warning this must be remake according to F. Amnesic average with parameters t1, t2

            // newCov = t-1/t * cov(t-1) + 1/t * (newVector - mean(t)) * (newVector - mean(t))T
            // oldPart = t-1/t * cov(t-1)
            // incrementalPart = 1/t * (newVector - mean(t)) * (newVector - mean(t))T
            // vector1 = (newVector - mean(t))
            // vector2 = (newVector - mean(t))T
            // newCovPart = vector1 * vector2

            //newVector - mean(t)
            //Vector v1 = new Vector(vector.ToArray());
            //v1.Subtract(this.meanMD);

            ILArray <double> vector1 = (vector - this.meanMDF).ToArray();
            ILArray <double> vector2 = vector1.ToArray();
            // transpone
            vector2 = vector2.T;

#warning need to edit update covariance matrix
            double t         = (double)this.items.Count;
            double fragment1 = (t - 2) / (t - 1);
            double fragment2 = t / (Math.Pow((t - 1), 2));

            //DenseMatrix oldPart = fragment1 * this.covarianceMatrix;
            //DenseMatrix newCovPart = vector1 * vector2;

            try
            {
                //DenseMatrix incrementalPart = newCovPart * fragment2;
                this.covarianceMatrixMDF = (this.covarianceMatrixMDF * fragment1) + (ILMath.multiply(vector1, vector2) * fragment2);
            }
            catch (Exception ee)
            {
                throw new InvalidCastException();
            }
        }
コード例 #12
0
        private double calcVarianceForRunIL(double[] run, ILArray <double> dispersion)
        {
            ILArray <double> vector = run;

            return((double)ILMath.multiply(ILMath.multiply(vector.T, dispersion), vector));
        }
コード例 #13
0
 /// <summary>
 /// Creates the small Covariancematrix.
 /// </summary>
 private void CreateCovarianceMatrix()
 {
     _a          = _columnVectorIntMatrix;
     _covariance = ILMath.multiply(_a.T, _a);
 }
コード例 #14
0
        private ILArray <double> calculateDispersion(double[,] matrix)
        {
            ILArray <double> tmp = matrix;

            return(((double[, ])MachineLearning.Learning.Regression.FeatureSubsetSelection.toSystemMatrix <double>((ILMath.multiply(tmp, tmp.T)))).PseudoInverse());
        }
コード例 #15
0
        public void Test_MatrixMultiply()
        {
            int errorCode = 0;

            // success?
            try {
                ILPerformer p       = new ILPerformer();
                double[]    data1   = new double[120];
                double[]    data2   = new double[120];
                double[]    results = new double[36] {
                    177840, 180120, 182400, 184680, 186960, 189240, 180120, 182440,
                    184760, 187080, 189400, 191720, 182400, 184760, 187120, 189480, 191840, 194200, 184680, 187080,
                    189480, 191880, 194280, 196680, 186960, 189400, 191840, 194280, 196720, 199160, 189240, 191720,
                    194200, 196680, 199160, 201640
                };
                for (int i = 0; i < 120; i++)
                {
                    data1[i] = i;
                    data2[i] = i * 2;
                }
                ILDA A            = new ILDA(data1, 6, 20);
                ILDA BR           = new ILDA(data2, 6, 20);
                ILDA ResultExpect = new ILDA(results, 6, 6);
                ILDA Result       = null;
                ILDA AR           = A.T.T;
                ILDA B            = BR.T;
                B.Detach();
                BR        = B.T.T;
                errorCode = 0;
                p.Tic();
                Result = ILMath.multiply(A, B);
                p.Toc();
                errorCode = 1;
                if (Result.Dimensions.NumberOfDimensions != 2)
                {
                    throw new Exception("Wrong number of results dimensions!");
                }
                errorCode = 2;
                if (Result.Dimensions[0] != 6 || Result.Dimensions[1] != 6)
                {
                    throw new Exception("Wrong result's size!");
                }
                errorCode = 3;
                if (!Result.Equals(ResultExpect))
                {
                    throw new Exception("Wrong results values!");
                }
                Info("Test_MatrixMultiplyDouble succeeded: phy/phy " + p.ToString() + "ms needed");
                errorCode = 4;
                p.Tic();
                if (!ResultExpect.Equals(ILMath.multiply(AR, BR)))
                {
                    throw new Exception("Wrong data value on ref/ref");
                }
                p.Toc();
                Info("Test_MatrixMultiplyDouble succeeded: ref/ref " + p.ToString() + "ms needed");
                // test if wrong dimensions throw exceptions
                try {
                    ILMath.multiply(A, AR);
                    throw new InvalidOperationException();
                } catch (Exception e) {
                    if (e is InvalidOperationException)
                    {
                        throw new Exception("Dimensions should be forced to match!");
                    }
                }
                data1 = new double[1000 * 2000];
                A     = new ILDA(data1, 1000, 2000);
                B     = new ILDA(data1, 2000, 1000);
                p.Tic();
                Result = ILMath.multiply(A, B);
                p.Toc();
                Info("Test_MatrixMultiply 1000x2000: phy/phy " + p.ToString() + "ms needed");
                Info("TODO: Implement test MatrixMultiply with reference storages!!");

                errorCode = 5;
                ILArray <float> fA = new ILArray <float>(new float[12] {
                    1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f, 11f, 12f
                }, 4, 3);
                ILArray <float> fB = new ILArray <float>(new float[12] {
                    17f, 18f, 19f, 20f, 21f, 22f, 23f, 24f, 25f, 26f, 27f, 28f
                }, 3, 4);
                ILArray <float> fRes = new ILArray <float>(new float[16] {
                    278f, 332f, 386f, 440f, 323f, 386f, 449f, 512f, 368f, 440, 512f, 584f, 413f, 494f, 575f, 656f
                }, 4, 4);
                if (ILMath.norm(fRes - ILMath.multiply(fA, fB)) > 1.0e-10)
                {
                    throw new Exception("Test_MatrixMultiply: invalid values detected!");
                }

                errorCode = 6;
                ILArray <complex> zA = ILMath.real2complex(ILMath.vector(1, 12), ILMath.vector(-1, -1, -12));
                ILArray <complex> zB = ILMath.real2complex(ILMath.vector(17, 28), ILMath.vector(-17, -1, -28));
                //{new complex (0 , 0.5560), new complex (0, 0.6640), new complex (0, 0.7720), new complex (0, 0.8800), new complex (0,  0.6460), new complex (0,  0.7720), new complex (0,  0.8980), new complex (0,  1.0240), new complex (0,  0.7360), new complex (0,  0.8800),new complex(0,1.0240),new complex(0,1.1680),new complex(0,0.8260),new complex(0,0.9880),new complex(0,1.1500),new complex(0,1.3120)}
                ILArray <complex> zRes = ILMath.real2complex(ILMath.zeros(1, 16), new ILArray <double>(new double[] { 556, 664, 772, 880, 646, 772, 898, 1024, 736, 880, 1024, 1168, 826, 988, 1150, 1312 }) * (-1));
                zA   = ILMath.reshape(zA, 4, 3);
                zB   = ILMath.reshape(zB, 3, 4);
                zRes = ILMath.reshape(zRes, 4, 4);

                if (ILMath.norm(zRes - ILMath.multiply(zA, zB)) > 1.0e-10)
                {
                    throw new Exception("Test_MatrixMultiply: invalid values detected!");
                }

                errorCode = 7;
                ILArray <fcomplex> cA   = ILMath.real2fcomplex(ILMath.vector(1, 12), ILMath.vector(-1, -1, -12));
                ILArray <fcomplex> cB   = ILMath.real2fcomplex(ILMath.vector(17, 28), ILMath.vector(-17, -1, -28));
                ILArray <fcomplex> cRes = ILMath.real2fcomplex(ILMath.zeros(1, 16), new ILArray <double>(new double[] { 556, 664, 772, 880, 646, 772, 898, 1024, 736, 880, 1024, 1168, 826, 988, 1150, 1312 }) * (-1));
                cA   = ILMath.reshape(cA, 4, 3);
                cB   = ILMath.reshape(cB, 3, 4);
                cRes = ILMath.reshape(cRes, 4, 4);

                if (ILMath.norm(cRes - ILMath.multiply(cA, cB)) > 1.0e-10)
                {
                    throw new Exception("Test_MatrixMultiply: invalid values detected!");
                }

                Success("Test_MatrixMultiply successful.");
            } catch (Exception e) {
                Error("Test_MatrixMultiply failed at step: " + errorCode + " Msg: " + e.Message);
            }
        }
コード例 #16
0
 /// <summary>
 /// Matrix multiplication
 /// </summary>
 /// <param name="star">'*'</param>
 protected static ILArray <double> _m(ILArray <double> ilArray1, char star, ILArray <double> ilArray2)
 {
     return(ILMath.multiply(ilArray1, ilArray2));
 }
コード例 #17
0
ファイル: Vector.cs プロジェクト: cechovsky/DiplomaThesis
        public void CountMDF(ILArray <double> GSOmanifold, ILArray <double> C)
        {
            ILArray <double> scaterPart = this.values - C;

            this.valuesMDF = ILMath.multiply(GSOmanifold.T, scaterPart);
        }
コード例 #18
0
ファイル: Vector.cs プロジェクト: cechovsky/DiplomaThesis
        public static double GetNormalisationNum(ILArray <double> vector)
        {
            double sum = ILMath.multiply(vector.T, vector).ToArray()[0];

            return(Math.Sqrt(sum));
        }
コード例 #19
0
        private ILArray <double> calculateDispersion(double[,] matrix)
        {
            ILArray <double> tmp = matrix;

            return(ILMath.pinv(ILMath.multiply(tmp, tmp.T)));
        }
コード例 #20
0
ファイル: ClusterX.cs プロジェクト: cechovsky/DiplomaThesis
        public void UpdateCovarianceMatrixMDF(ILArray <double> vector)
        {
            // newCov = t-1/t * cov(t-1) + 1/t * (newVector - mean(t)) * (newVector - mean(t))T
            // oldPart = t-1/t * cov(t-1)
            // incrementalPart = 1/t * (newVector - mean(t)) * (newVector - mean(t))T
            // vector1 = (newVector - mean(t))
            // vector2 = (newVector - mean(t))T
            // newCovPart = vector1 * vector2

            //newVector - mean(t)
            //Vector v1 = new Vector(vector.ToArray());
            //v1.Subtract(this.meanMD);

            ILArray <double> vector1 = (vector - this.meanMDF).ToArray();
            ILArray <double> vector2 = vector1.ToArray();

            // transpone
            vector2 = vector2.T;

            double t = (double)this.items.Count;
            //double fragment1 = (t - 2) / (t - 1);
            //double fragment2 = t / (Math.Pow((t - 1), 2));

            double fragment1 = (t - 1 - this.GetAmnesicParameter(t)) / t;
            double fragment2 = (1 + this.GetAmnesicParameter(t)) / t;

            try
            {
                this.covarianceMatrixMDF = (this.covarianceMatrixMDF * fragment1) + (fragment2 * ILMath.multiply(vector1, vector2));
            }
            catch (Exception ee)
            {
                throw new InvalidCastException();
            }
        }