Exemplo n.º 1
0
            /*************************************************************************
            Spearman's rank cross-correlation matrix

            INPUT PARAMETERS:
                X   -   array[N,M1], sample matrix:
                        * J-th column corresponds to J-th variable
                        * I-th row corresponds to I-th observation
                Y   -   array[N,M2], sample matrix:
                        * J-th column corresponds to J-th variable
                        * I-th row corresponds to I-th observation
                N   -   N>=0, number of observations:
                        * if given, only leading N rows of X/Y are used
                        * if not given, automatically determined from input sizes
                M1  -   M1>0, number of variables in X:
                        * if given, only leading M1 columns of X are used
                        * if not given, automatically determined from input size
                M2  -   M2>0, number of variables in Y:
                        * if given, only leading M1 columns of X are used
                        * if not given, automatically determined from input size

            OUTPUT PARAMETERS:
                C   -   array[M1,M2], cross-correlation matrix (zero if N=0 or N=1)

              -- ALGLIB --
                 Copyright 28.10.2010 by Bochkanov Sergey
            *************************************************************************/
            public static void spearmancorrm2(double[,] x,
                double[,] y,
                int n,
                int m1,
                int m2,
                ref double[,] c) {
                int i = 0;
                int j = 0;
                double v = 0;
                double[] t = new double[0];
                double[] x0 = new double[0];
                double[] y0 = new double[0];
                double[] sx = new double[0];
                double[] sy = new double[0];
                bool[] samex = new bool[0];
                bool[] samey = new bool[0];
                apserv.apbuffers buf = new apserv.apbuffers();
                int i_ = 0;

                x = (double[,])x.Clone();
                y = (double[,])y.Clone();
                c = new double[0, 0];

                ap.assert(n >= 0, "SpearmanCorrM2: N<0");
                ap.assert(m1 >= 1, "SpearmanCorrM2: M1<1");
                ap.assert(m2 >= 1, "SpearmanCorrM2: M2<1");
                ap.assert(ap.rows(x) >= n, "SpearmanCorrM2: Rows(X)<N!");
                ap.assert(ap.cols(x) >= m1 | n == 0, "SpearmanCorrM2: Cols(X)<M1!");
                ap.assert(apserv.apservisfinitematrix(x, n, m1), "SpearmanCorrM2: X contains infinite/NAN elements");
                ap.assert(ap.rows(y) >= n, "SpearmanCorrM2: Rows(Y)<N!");
                ap.assert(ap.cols(y) >= m2 | n == 0, "SpearmanCorrM2: Cols(Y)<M2!");
                ap.assert(apserv.apservisfinitematrix(y, n, m2), "SpearmanCorrM2: X contains infinite/NAN elements");

                //
                // N<=1, return zero
                //
                if(n <= 1) {
                    c = new double[m1, m2];
                    for(i = 0; i <= m1 - 1; i++) {
                        for(j = 0; j <= m2 - 1; j++) {
                            c[i, j] = 0;
                        }
                    }
                    return;
                }

                //
                // Allocate
                //
                t = new double[Math.Max(Math.Max(m1, m2), n)];
                x0 = new double[m1];
                y0 = new double[m2];
                sx = new double[m1];
                sy = new double[m2];
                samex = new bool[m1];
                samey = new bool[m2];
                c = new double[m1, m2];

                //
                // Replace data with ranks
                //
                for(j = 0; j <= m1 - 1; j++) {
                    for(i_ = 0; i_ <= n - 1; i_++) {
                        t[i_] = x[i_, j];
                    }
                    basicstatops.rankx(ref t, n, buf);
                    for(i_ = 0; i_ <= n - 1; i_++) {
                        x[i_, j] = t[i_];
                    }
                }
                for(j = 0; j <= m2 - 1; j++) {
                    for(i_ = 0; i_ <= n - 1; i_++) {
                        t[i_] = y[i_, j];
                    }
                    basicstatops.rankx(ref t, n, buf);
                    for(i_ = 0; i_ <= n - 1; i_++) {
                        y[i_, j] = t[i_];
                    }
                }

                //
                // * calculate means of X
                // * center X
                // * if we have constant columns, these columns are
                //   artificially zeroed (they must be zero in exact arithmetics,
                //   but unfortunately floating point ops are not exact).
                // * calculate column variances
                //
                for(i = 0; i <= m1 - 1; i++) {
                    t[i] = 0;
                    samex[i] = true;
                    sx[i] = 0;
                }
                for(i_ = 0; i_ <= m1 - 1; i_++) {
                    x0[i_] = x[0, i_];
                }
                v = (double)1 / (double)n;
                for(i = 0; i <= n - 1; i++) {
                    for(i_ = 0; i_ <= m1 - 1; i_++) {
                        t[i_] = t[i_] + v * x[i, i_];
                    }
                    for(j = 0; j <= m1 - 1; j++) {
                        samex[j] = samex[j] & (double)(x[i, j]) == (double)(x0[j]);
                    }
                }
                for(i = 0; i <= n - 1; i++) {
                    for(i_ = 0; i_ <= m1 - 1; i_++) {
                        x[i, i_] = x[i, i_] - t[i_];
                    }
                    for(j = 0; j <= m1 - 1; j++) {
                        if(samex[j]) {
                            x[i, j] = 0;
                        }
                        sx[j] = sx[j] + x[i, j] * x[i, j];
                    }
                }
                for(j = 0; j <= m1 - 1; j++) {
                    sx[j] = Math.Sqrt(sx[j] / (n - 1));
                }

                //
                // Repeat same steps for Y
                //
                for(i = 0; i <= m2 - 1; i++) {
                    t[i] = 0;
                    samey[i] = true;
                    sy[i] = 0;
                }
                for(i_ = 0; i_ <= m2 - 1; i_++) {
                    y0[i_] = y[0, i_];
                }
                v = (double)1 / (double)n;
                for(i = 0; i <= n - 1; i++) {
                    for(i_ = 0; i_ <= m2 - 1; i_++) {
                        t[i_] = t[i_] + v * y[i, i_];
                    }
                    for(j = 0; j <= m2 - 1; j++) {
                        samey[j] = samey[j] & (double)(y[i, j]) == (double)(y0[j]);
                    }
                }
                for(i = 0; i <= n - 1; i++) {
                    for(i_ = 0; i_ <= m2 - 1; i_++) {
                        y[i, i_] = y[i, i_] - t[i_];
                    }
                    for(j = 0; j <= m2 - 1; j++) {
                        if(samey[j]) {
                            y[i, j] = 0;
                        }
                        sy[j] = sy[j] + y[i, j] * y[i, j];
                    }
                }
                for(j = 0; j <= m2 - 1; j++) {
                    sy[j] = Math.Sqrt(sy[j] / (n - 1));
                }

                //
                // calculate cross-covariance matrix
                //
                ablas.rmatrixgemm(m1, m2, n, (double)1 / (double)(n - 1), x, 0, 0, 1, y, 0, 0, 0, 0.0, ref c, 0, 0);

                //
                // Divide by standard deviations
                //
                for(i = 0; i <= m1 - 1; i++) {
                    for(j = 0; j <= m2 - 1; j++) {
                        if((double)(sx[i]) != (double)(0) & (double)(sy[j]) != (double)(0)) {
                            c[i, j] = c[i, j] / (sx[i] * sy[j]);
                        } else {
                            c[i, j] = 0;
                        }
                    }
                }
            }
Exemplo n.º 2
0
            /*************************************************************************
            Spearman's rank correlation coefficient

            Input parameters:
                X       -   sample 1 (array indexes: [0..N-1])
                Y       -   sample 2 (array indexes: [0..N-1])
                N       -   N>=0, sample size:
                            * if given, only N leading elements of X/Y are processed
                            * if not given, automatically determined from input sizes

            Result:
                Spearman's rank correlation coefficient
                (zero for N=0 or N=1)

              -- ALGLIB --
                 Copyright 09.04.2007 by Bochkanov Sergey
            *************************************************************************/
            public static double spearmancorr2(double[] x,
                double[] y,
                int n) {
                double result = 0;
                apserv.apbuffers buf = new apserv.apbuffers();

                x = (double[])x.Clone();
                y = (double[])y.Clone();

                ap.assert(n >= 0, "SpearmanCorr2: N<0");
                ap.assert(ap.len(x) >= n, "SpearmanCorr2: Length(X)<N!");
                ap.assert(ap.len(y) >= n, "SpearmanCorr2: Length(Y)<N!");
                ap.assert(apserv.isfinitevector(x, n), "SpearmanCorr2: X is not finite vector");
                ap.assert(apserv.isfinitevector(y, n), "SpearmanCorr2: Y is not finite vector");

                //
                // Special case
                //
                if(n <= 1) {
                    result = 0;
                    return result;
                }
                basicstatops.rankx(ref x, n, buf);
                basicstatops.rankx(ref y, n, buf);
                result = pearsoncorr2(x, y, n);
                return result;
            }
Exemplo n.º 3
0
            /*************************************************************************
            Spearman's rank correlation matrix

            INPUT PARAMETERS:
                X   -   array[N,M], sample matrix:
                        * J-th column corresponds to J-th variable
                        * I-th row corresponds to I-th observation
                N   -   N>=0, number of observations:
                        * if given, only leading N rows of X are used
                        * if not given, automatically determined from input size
                M   -   M>0, number of variables:
                        * if given, only leading M columns of X are used
                        * if not given, automatically determined from input size

            OUTPUT PARAMETERS:
                C   -   array[M,M], correlation matrix (zero if N=0 or N=1)

              -- ALGLIB --
                 Copyright 28.10.2010 by Bochkanov Sergey
            *************************************************************************/
            public static void spearmancorrm(double[,] x,
                int n,
                int m,
                ref double[,] c) {
                int i = 0;
                int j = 0;
                apserv.apbuffers buf = new apserv.apbuffers();
                double[] t = new double[0];
                double v = 0;
                double[] x0 = new double[0];
                bool[] same = new bool[0];
                int i_ = 0;

                x = (double[,])x.Clone();
                c = new double[0, 0];

                ap.assert(n >= 0, "SpearmanCorrM: N<0");
                ap.assert(m >= 1, "SpearmanCorrM: M<1");
                ap.assert(ap.rows(x) >= n, "SpearmanCorrM: Rows(X)<N!");
                ap.assert(ap.cols(x) >= m | n == 0, "SpearmanCorrM: Cols(X)<M!");
                ap.assert(apserv.apservisfinitematrix(x, n, m), "SpearmanCorrM: X contains infinite/NAN elements");

                //
                // N<=1, return zero
                //
                if(n <= 1) {
                    c = new double[m, m];
                    for(i = 0; i <= m - 1; i++) {
                        for(j = 0; j <= m - 1; j++) {
                            c[i, j] = 0;
                        }
                    }
                    return;
                }

                //
                // Allocate
                //
                t = new double[Math.Max(n, m)];
                x0 = new double[m];
                same = new bool[m];
                c = new double[m, m];

                //
                // Replace data with ranks
                //
                for(j = 0; j <= m - 1; j++) {
                    for(i_ = 0; i_ <= n - 1; i_++) {
                        t[i_] = x[i_, j];
                    }
                    basicstatops.rankx(ref t, n, buf);
                    for(i_ = 0; i_ <= n - 1; i_++) {
                        x[i_, j] = t[i_];
                    }
                }

                //
                // Calculate means,
                // check for constant columns
                //
                for(i = 0; i <= m - 1; i++) {
                    t[i] = 0;
                    same[i] = true;
                }
                for(i_ = 0; i_ <= m - 1; i_++) {
                    x0[i_] = x[0, i_];
                }
                v = (double)1 / (double)n;
                for(i = 0; i <= n - 1; i++) {
                    for(i_ = 0; i_ <= m - 1; i_++) {
                        t[i_] = t[i_] + v * x[i, i_];
                    }
                    for(j = 0; j <= m - 1; j++) {
                        same[j] = same[j] & (double)(x[i, j]) == (double)(x0[j]);
                    }
                }

                //
                // * center variables;
                // * if we have constant columns, these columns are
                //   artificialy zeroed (they must be zero in exact arithmetics,
                //   but unfortunately floating point is not exact).
                // * calculate upper half of symmetric covariance matrix
                //
                for(i = 0; i <= n - 1; i++) {
                    for(i_ = 0; i_ <= m - 1; i_++) {
                        x[i, i_] = x[i, i_] - t[i_];
                    }
                    for(j = 0; j <= m - 1; j++) {
                        if(same[j]) {
                            x[i, j] = 0;
                        }
                    }
                }
                ablas.rmatrixsyrk(m, n, (double)1 / (double)(n - 1), x, 0, 0, 1, 0.0, ref c, 0, 0, true);

                //
                // force symmetricity
                //
                for(i = 0; i <= m - 2; i++) {
                    for(i_ = i + 1; i_ <= m - 1; i_++) {
                        c[i_, i] = c[i, i_];
                    }
                }

                //
                // Calculate Pearson coefficients
                //
                for(i = 0; i <= m - 1; i++) {
                    t[i] = Math.Sqrt(c[i, i]);
                }
                for(i = 0; i <= m - 1; i++) {
                    for(j = 0; j <= m - 1; j++) {
                        if((double)(t[i]) != (double)(0) & (double)(t[j]) != (double)(0)) {
                            c[i, j] = c[i, j] / (t[i] * t[j]);
                        } else {
                            c[i, j] = 0.0;
                        }
                    }
                }
            }
Exemplo n.º 4
0
        /*************************************************************************
        This function sorts array of real keys by ascending.

        Its results are:
        * sorted array A
        * permutation tables P1, P2

        Algorithm outputs permutation tables using two formats:
        * as usual permutation of [0..N-1]. If P1[i]=j, then sorted A[i]  contains
          value which was moved there from J-th position.
        * as a sequence of pairwise permutations. Sorted A[] may  be  obtained  by
          swaping A[i] and A[P2[i]] for all i from 0 to N-1.
          
        INPUT PARAMETERS:
            A       -   unsorted array
            N       -   array size

        OUPUT PARAMETERS:
            A       -   sorted array
            P1, P2  -   permutation tables, array[N]
            
        NOTES:
            this function assumes that A[] is finite; it doesn't checks that
            condition. All other conditions (size of input arrays, etc.) are not
            checked too.

          -- ALGLIB --
             Copyright 14.05.2008 by Bochkanov Sergey
        *************************************************************************/
        public static void tagsort(ref double[] a,
            int n,
            ref int[] p1,
            ref int[] p2)
        {
            apserv.apbuffers buf = new apserv.apbuffers();

            p1 = new int[0];
            p2 = new int[0];

            tagsortbuf(ref a, n, ref p1, ref p2, buf);
        }
Exemplo n.º 5
0
        /*************************************************************************
        This function finds feasible point of  (NMain+NSlack)-dimensional  problem
        subject to NMain explicit boundary constraints (some  constraints  can  be
        omitted), NSlack implicit non-negativity constraints,  K  linear  equality
        constraints.

        INPUT PARAMETERS
            X           -   array[NMain+NSlack], initial point.
            BndL        -   lower bounds, array[NMain]
                            (may contain -INF, when bound is not present)
            HaveBndL    -   array[NMain], if HaveBndL[i] is False,
                            then i-th bound is not present
            BndU        -   array[NMain], upper bounds
                            (may contain +INF, when bound is not present)
            HaveBndU    -   array[NMain], if HaveBndU[i] is False,
                            then i-th bound is not present
            NMain       -   number of main variables
            NSlack      -   number of slack variables
            CE          -   array[K,NMain+NSlack+1], equality  constraints CE*x=b.
                            Rows contain constraints, first  NMain+NSlack  columns
                            contain coefficients before X[], last  column  contain
                            right part.
            K           -   number of linear constraints
            EpsI        -   infeasibility (error in the right part) allowed in the
                            solution

        OUTPUT PARAMETERS:
            X           -   feasible point or best infeasible point found before
                            algorithm termination
            QPIts       -   number of QP iterations (for debug purposes)
            GPAIts      -   number of GPA iterations (for debug purposes)
            
        RESULT:
            True in case X is feasible, False - if it is infeasible.

          -- ALGLIB --
             Copyright 20.01.2012 by Bochkanov Sergey
        *************************************************************************/
        public static bool findfeasiblepoint(ref double[] x,
            double[] bndl,
            bool[] havebndl,
            double[] bndu,
            bool[] havebndu,
            int nmain,
            int nslack,
            double[,] ce,
            int k,
            double epsi,
            ref int qpits,
            ref int gpaits)
        {
            bool result = new bool();
            int i = 0;
            int j = 0;
            int idx0 = 0;
            int idx1 = 0;
            double[] permx = new double[0];
            double[] xn = new double[0];
            double[] xa = new double[0];
            double[] newtonstep = new double[0];
            double[] g = new double[0];
            double[] pg = new double[0];
            double[,] a = new double[0,0];
            double armijostep = 0;
            double armijobeststep = 0;
            double armijobestfeas = 0;
            double v = 0;
            double mx = 0;
            double feaserr = 0;
            double feasold = 0;
            double feasnew = 0;
            double pgnorm = 0;
            double vn = 0;
            double vd = 0;
            double stp = 0;
            int vartofreeze = 0;
            double valtofreeze = 0;
            double maxsteplen = 0;
            bool werechangesinconstraints = new bool();
            bool stage1isover = new bool();
            bool converged = new bool();
            double[] activeconstraints = new double[0];
            double[] tmpk = new double[0];
            double[] colnorms = new double[0];
            int nactive = 0;
            int nfree = 0;
            int nsvd = 0;
            int[] p1 = new int[0];
            int[] p2 = new int[0];
            apserv.apbuffers buf = new apserv.apbuffers();
            double[] w = new double[0];
            double[] s = new double[0];
            double[,] u = new double[0,0];
            double[,] vt = new double[0,0];
            int itscount = 0;
            int itswithintolerance = 0;
            int maxitswithintolerance = 0;
            int gparuns = 0;
            int maxarmijoruns = 0;
            int i_ = 0;

            ce = (double[,])ce.Clone();
            qpits = 0;
            gpaits = 0;

            maxitswithintolerance = 3;
            maxarmijoruns = 5;
            qpits = 0;
            gpaits = 0;
            
            //
            // Initial enforcement of the feasibility with respect to boundary constraints
            // NOTE: after this block we assume that boundary constraints are consistent.
            //
            if( !enforceboundaryconstraints(ref x, bndl, havebndl, bndu, havebndu, nmain, nslack) )
            {
                result = false;
                return result;
            }
            if( k==0 )
            {
                
                //
                // No linear constraints, we can exit right now
                //
                result = true;
                return result;
            }
            
            //
            // Scale rows of CE in such way that max(CE[i,0..nmain+nslack-1])=1 for any i=0..k-1
            //
            for(i=0; i<=k-1; i++)
            {
                v = 0.0;
                for(j=0; j<=nmain+nslack-1; j++)
                {
                    v = Math.Max(v, Math.Abs(ce[i,j]));
                }
                if( (double)(v)!=(double)(0) )
                {
                    v = 1/v;
                    for(i_=0; i_<=nmain+nslack;i_++)
                    {
                        ce[i,i_] = v*ce[i,i_];
                    }
                }
            }
            
            //
            // Allocate temporaries
            //
            xn = new double[nmain+nslack];
            xa = new double[nmain+nslack];
            permx = new double[nmain+nslack];
            g = new double[nmain+nslack];
            pg = new double[nmain+nslack];
            tmpk = new double[k];
            a = new double[k, nmain+nslack];
            activeconstraints = new double[nmain+nslack];
            newtonstep = new double[nmain+nslack];
            s = new double[nmain+nslack];
            colnorms = new double[nmain+nslack];
            for(i=0; i<=nmain+nslack-1; i++)
            {
                s[i] = 1.0;
                colnorms[i] = 0.0;
                for(j=0; j<=k-1; j++)
                {
                    colnorms[i] = colnorms[i]+math.sqr(ce[j,i]);
                }
            }
            
            //
            // K>0, we have linear equality constraints combined with bound constraints.
            //
            // Try to find feasible point as minimizer of the quadratic function
            //     F(x) = 0.5*||CE*x-b||^2 = 0.5*x'*(CE'*CE)*x - (b'*CE)*x + 0.5*b'*b
            // subject to boundary constraints given by BL, BU and non-negativity of
            // the slack variables. BTW, we drop constant term because it does not
            // actually influences on the solution.
            //
            // Below we will assume that K>0.
            //
            itswithintolerance = 0;
            itscount = 0;
            while( true )
            {
                
                //
                // Stage 0: check for exact convergence
                //
                converged = true;
                feaserr = 0;
                for(i=0; i<=k-1; i++)
                {
                    
                    //
                    // Calculate:
                    // * V - error in the right part
                    // * MX - maximum term in the left part
                    //
                    // Terminate if error in the right part is not greater than 100*Eps*MX.
                    //
                    // IMPORTANT: we must perform check for non-strict inequality, i.e. to use <= instead of <.
                    //            it will allow us to easily handle situations with zero rows of CE.
                    //
                    mx = 0;
                    v = -ce[i,nmain+nslack];
                    for(j=0; j<=nmain+nslack-1; j++)
                    {
                        mx = Math.Max(mx, Math.Abs(ce[i,j]*x[j]));
                        v = v+ce[i,j]*x[j];
                    }
                    feaserr = feaserr+math.sqr(v);
                    converged = converged && (double)(Math.Abs(v))<=(double)(100*math.machineepsilon*mx);
                }
                feaserr = Math.Sqrt(feaserr);
                if( converged )
                {
                    result = (double)(feaserr)<=(double)(epsi);
                    return result;
                }
                
                //
                // Stage 1: equality constrained quadratic programming
                //
                // * treat active bound constraints as equality ones (constraint is considered 
                //   active when we are at the boundary, independently of the antigradient direction)
                // * calculate unrestricted Newton step to point XM (which may be infeasible)
                //   calculate MaxStepLen = largest step in direction of XM which retains feasibility.
                // * perform bounded step from X to XN:
                //   a) XN=XM                  (if XM is feasible)
                //   b) XN=X-MaxStepLen*(XM-X) (otherwise)
                // * X := XN
                // * if XM (Newton step subject to currently active constraints) was feasible, goto Stage 2
                // * repeat Stage 1
                //
                // NOTE 1: in order to solve constrained qudratic subproblem we will have to reorder
                //         variables in such way that ones corresponding to inactive constraints will
                //         be first, and active ones will be last in the list. CE and X are now
                //                                                       [ xi ]
                //         separated into two parts: CE = [CEi CEa], x = [    ], where CEi/Xi correspond
                //                                                       [ xa ]
                //         to INACTIVE constraints, and CEa/Xa correspond to the ACTIVE ones.
                //
                //         Now, instead of F=0.5*x'*(CE'*CE)*x - (b'*CE)*x + 0.5*b'*b, we have
                //         F(xi) = 0.5*(CEi*xi,CEi*xi) + (CEa*xa-b,CEi*xi) + (0.5*CEa*xa-b,CEa*xa).
                //         Here xa is considered constant, i.e. we optimize with respect to xi, leaving xa fixed.
                //
                //         We can solve it by performing SVD of CEi and calculating pseudoinverse of the
                //         Hessian matrix. Of course, we do NOT calculate pseudoinverse explicitly - we
                //         just use singular vectors to perform implicit multiplication by it.
                //
                //
                while( true )
                {
                    
                    //
                    // Calculate G - gradient subject to equality constraints,
                    // multiply it by inverse of the Hessian diagonal to obtain initial
                    // step vector.
                    //
                    // Bound step subject to constraints which can be activated,
                    // run Armijo search with increasing step size.
                    // Search is terminated when feasibility error stops to decrease.
                    //
                    // NOTE: it is important to test for "stops to decrease" instead
                    // of "starts to increase" in order to correctly handle cases with
                    // zero CE.
                    //
                    armijobeststep = 0.0;
                    armijobestfeas = 0.0;
                    for(i=0; i<=nmain+nslack-1; i++)
                    {
                        g[i] = 0;
                    }
                    for(i=0; i<=k-1; i++)
                    {
                        v = 0.0;
                        for(i_=0; i_<=nmain+nslack-1;i_++)
                        {
                            v += ce[i,i_]*x[i_];
                        }
                        v = v-ce[i,nmain+nslack];
                        armijobestfeas = armijobestfeas+math.sqr(v);
                        for(i_=0; i_<=nmain+nslack-1;i_++)
                        {
                            g[i_] = g[i_] + v*ce[i,i_];
                        }
                    }
                    armijobestfeas = Math.Sqrt(armijobestfeas);
                    for(i=0; i<=nmain-1; i++)
                    {
                        if( havebndl[i] && (double)(x[i])==(double)(bndl[i]) )
                        {
                            g[i] = 0.0;
                        }
                        if( havebndu[i] && (double)(x[i])==(double)(bndu[i]) )
                        {
                            g[i] = 0.0;
                        }
                    }
                    for(i=0; i<=nslack-1; i++)
                    {
                        if( (double)(x[nmain+i])==(double)(0.0) )
                        {
                            g[nmain+i] = 0.0;
                        }
                    }
                    v = 0.0;
                    for(i=0; i<=nmain+nslack-1; i++)
                    {
                        if( (double)(math.sqr(colnorms[i]))!=(double)(0) )
                        {
                            newtonstep[i] = -(g[i]/math.sqr(colnorms[i]));
                        }
                        else
                        {
                            newtonstep[i] = 0.0;
                        }
                        v = v+math.sqr(newtonstep[i]);
                    }
                    if( (double)(v)==(double)(0) )
                    {
                        
                        //
                        // Constrained gradient is zero, QP iterations are over
                        //
                        break;
                    }
                    calculatestepbound(x, newtonstep, 1.0, bndl, havebndl, bndu, havebndu, nmain, nslack, ref vartofreeze, ref valtofreeze, ref maxsteplen);
                    if( vartofreeze>=0 && (double)(maxsteplen)==(double)(0) )
                    {
                        
                        //
                        // Can not perform step, QP iterations are over
                        //
                        break;
                    }
                    if( vartofreeze>=0 )
                    {
                        armijostep = Math.Min(1.0, maxsteplen);
                    }
                    else
                    {
                        armijostep = 1;
                    }
                    while( true )
                    {
                        for(i_=0; i_<=nmain+nslack-1;i_++)
                        {
                            xa[i_] = x[i_];
                        }
                        for(i_=0; i_<=nmain+nslack-1;i_++)
                        {
                            xa[i_] = xa[i_] + armijostep*newtonstep[i_];
                        }
                        enforceboundaryconstraints(ref xa, bndl, havebndl, bndu, havebndu, nmain, nslack);
                        feaserr = 0.0;
                        for(i=0; i<=k-1; i++)
                        {
                            v = 0.0;
                            for(i_=0; i_<=nmain+nslack-1;i_++)
                            {
                                v += ce[i,i_]*xa[i_];
                            }
                            v = v-ce[i,nmain+nslack];
                            feaserr = feaserr+math.sqr(v);
                        }
                        feaserr = Math.Sqrt(feaserr);
                        if( (double)(feaserr)>=(double)(armijobestfeas) )
                        {
                            break;
                        }
                        armijobestfeas = feaserr;
                        armijobeststep = armijostep;
                        armijostep = 2.0*armijostep;
                    }
                    for(i_=0; i_<=nmain+nslack-1;i_++)
                    {
                        x[i_] = x[i_] + armijobeststep*newtonstep[i_];
                    }
                    enforceboundaryconstraints(ref x, bndl, havebndl, bndu, havebndu, nmain, nslack);
                    
                    //
                    // Determine number of active and free constraints
                    //
                    nactive = 0;
                    for(i=0; i<=nmain-1; i++)
                    {
                        activeconstraints[i] = 0;
                        if( havebndl[i] && (double)(x[i])==(double)(bndl[i]) )
                        {
                            activeconstraints[i] = 1;
                        }
                        if( havebndu[i] && (double)(x[i])==(double)(bndu[i]) )
                        {
                            activeconstraints[i] = 1;
                        }
                        if( (double)(activeconstraints[i])>(double)(0) )
                        {
                            nactive = nactive+1;
                        }
                    }
                    for(i=0; i<=nslack-1; i++)
                    {
                        activeconstraints[nmain+i] = 0;
                        if( (double)(x[nmain+i])==(double)(0.0) )
                        {
                            activeconstraints[nmain+i] = 1;
                        }
                        if( (double)(activeconstraints[nmain+i])>(double)(0) )
                        {
                            nactive = nactive+1;
                        }
                    }
                    nfree = nmain+nslack-nactive;
                    if( nfree==0 )
                    {
                        break;
                    }
                    qpits = qpits+1;
                    
                    //
                    // Reorder variables
                    //
                    tsort.tagsortbuf(ref activeconstraints, nmain+nslack, ref p1, ref p2, buf);
                    for(i=0; i<=k-1; i++)
                    {
                        for(j=0; j<=nmain+nslack-1; j++)
                        {
                            a[i,j] = ce[i,j];
                        }
                    }
                    for(j=0; j<=nmain+nslack-1; j++)
                    {
                        permx[j] = x[j];
                    }
                    for(j=0; j<=nmain+nslack-1; j++)
                    {
                        if( p2[j]!=j )
                        {
                            idx0 = p2[j];
                            idx1 = j;
                            for(i=0; i<=k-1; i++)
                            {
                                v = a[i,idx0];
                                a[i,idx0] = a[i,idx1];
                                a[i,idx1] = v;
                            }
                            v = permx[idx0];
                            permx[idx0] = permx[idx1];
                            permx[idx1] = v;
                        }
                    }
                    
                    //
                    // Calculate (unprojected) gradient:
                    // G(xi) = CEi'*(CEi*xi + CEa*xa - b)
                    //
                    for(i=0; i<=nfree-1; i++)
                    {
                        g[i] = 0;
                    }
                    for(i=0; i<=k-1; i++)
                    {
                        v = 0.0;
                        for(i_=0; i_<=nmain+nslack-1;i_++)
                        {
                            v += a[i,i_]*permx[i_];
                        }
                        tmpk[i] = v-ce[i,nmain+nslack];
                    }
                    for(i=0; i<=k-1; i++)
                    {
                        v = tmpk[i];
                        for(i_=0; i_<=nfree-1;i_++)
                        {
                            g[i_] = g[i_] + v*a[i,i_];
                        }
                    }
                    
                    //
                    // Calculate Newton step using SVD of CEi:
                    //     F(xi)  = 0.5*xi'*H*xi + g'*xi    (Taylor decomposition)
                    //     XN     = -H^(-1)*g               (new point, solution of the QP subproblem)
                    //     H      = CEi'*CEi                
                    //     CEi    = U*W*V'                  (SVD of CEi)
                    //     H      = V*W^2*V'                 
                    //     H^(-1) = V*W^(-2)*V'
                    //     step     = -V*W^(-2)*V'*g          (it is better to perform multiplication from right to left)
                    //
                    // NOTE 1: we do NOT need left singular vectors to perform Newton step.
                    //
                    nsvd = Math.Min(k, nfree);
                    if( !svd.rmatrixsvd(a, k, nfree, 0, 1, 2, ref w, ref u, ref vt) )
                    {
                        result = false;
                        return result;
                    }
                    for(i=0; i<=nsvd-1; i++)
                    {
                        v = 0.0;
                        for(i_=0; i_<=nfree-1;i_++)
                        {
                            v += vt[i,i_]*g[i_];
                        }
                        tmpk[i] = v;
                    }
                    for(i=0; i<=nsvd-1; i++)
                    {
                        
                        //
                        // It is important to have strict ">" in order to correctly 
                        // handle zero singular values.
                        //
                        if( (double)(math.sqr(w[i]))>(double)(math.sqr(w[0])*(nmain+nslack)*math.machineepsilon) )
                        {
                            tmpk[i] = tmpk[i]/math.sqr(w[i]);
                        }
                        else
                        {
                            tmpk[i] = 0;
                        }
                    }
                    for(i=0; i<=nmain+nslack-1; i++)
                    {
                        newtonstep[i] = 0;
                    }
                    for(i=0; i<=nsvd-1; i++)
                    {
                        v = tmpk[i];
                        for(i_=0; i_<=nfree-1;i_++)
                        {
                            newtonstep[i_] = newtonstep[i_] - v*vt[i,i_];
                        }
                    }
                    for(j=nmain+nslack-1; j>=0; j--)
                    {
                        if( p2[j]!=j )
                        {
                            idx0 = p2[j];
                            idx1 = j;
                            v = newtonstep[idx0];
                            newtonstep[idx0] = newtonstep[idx1];
                            newtonstep[idx1] = v;
                        }
                    }
                    
                    //
                    // NewtonStep contains Newton step subject to active bound constraints.
                    //
                    // Such step leads us to the minimizer of the equality constrained F,
                    // but such minimizer may be infeasible because some constraints which
                    // are inactive at the initial point can be violated at the solution.
                    //
                    // Thus, we perform optimization in two stages:
                    // a) perform bounded Newton step, i.e. step in the Newton direction
                    //    until activation of the first constraint
                    // b) in case (MaxStepLen>0)and(MaxStepLen<1), perform additional iteration
                    //    of the Armijo line search in the rest of the Newton direction.
                    //
                    calculatestepbound(x, newtonstep, 1.0, bndl, havebndl, bndu, havebndu, nmain, nslack, ref vartofreeze, ref valtofreeze, ref maxsteplen);
                    if( vartofreeze>=0 && (double)(maxsteplen)==(double)(0) )
                    {
                        
                        //
                        // Activation of the constraints prevent us from performing step,
                        // QP iterations are over
                        //
                        break;
                    }
                    if( vartofreeze>=0 )
                    {
                        v = Math.Min(1.0, maxsteplen);
                    }
                    else
                    {
                        v = 1.0;
                    }
                    for(i_=0; i_<=nmain+nslack-1;i_++)
                    {
                        xn[i_] = v*newtonstep[i_];
                    }
                    for(i_=0; i_<=nmain+nslack-1;i_++)
                    {
                        xn[i_] = xn[i_] + x[i_];
                    }
                    postprocessboundedstep(ref xn, x, bndl, havebndl, bndu, havebndu, nmain, nslack, vartofreeze, valtofreeze, v, maxsteplen);
                    if( (double)(maxsteplen)>(double)(0) && (double)(maxsteplen)<(double)(1) )
                    {
                        
                        //
                        // Newton step was restricted by activation of the constraints,
                        // perform Armijo iteration.
                        //
                        // Initial estimate for best step is zero step. We try different
                        // step sizes, from the 1-MaxStepLen (residual of the full Newton
                        // step) to progressively smaller and smaller steps.
                        //
                        armijobeststep = 0.0;
                        armijobestfeas = 0.0;
                        for(i=0; i<=k-1; i++)
                        {
                            v = 0.0;
                            for(i_=0; i_<=nmain+nslack-1;i_++)
                            {
                                v += ce[i,i_]*xn[i_];
                            }
                            v = v-ce[i,nmain+nslack];
                            armijobestfeas = armijobestfeas+math.sqr(v);
                        }
                        armijobestfeas = Math.Sqrt(armijobestfeas);
                        armijostep = 1-maxsteplen;
                        for(j=0; j<=maxarmijoruns-1; j++)
                        {
                            for(i_=0; i_<=nmain+nslack-1;i_++)
                            {
                                xa[i_] = xn[i_];
                            }
                            for(i_=0; i_<=nmain+nslack-1;i_++)
                            {
                                xa[i_] = xa[i_] + armijostep*newtonstep[i_];
                            }
                            enforceboundaryconstraints(ref xa, bndl, havebndl, bndu, havebndu, nmain, nslack);
                            feaserr = 0.0;
                            for(i=0; i<=k-1; i++)
                            {
                                v = 0.0;
                                for(i_=0; i_<=nmain+nslack-1;i_++)
                                {
                                    v += ce[i,i_]*xa[i_];
                                }
                                v = v-ce[i,nmain+nslack];
                                feaserr = feaserr+math.sqr(v);
                            }
                            feaserr = Math.Sqrt(feaserr);
                            if( (double)(feaserr)<(double)(armijobestfeas) )
                            {
                                armijobestfeas = feaserr;
                                armijobeststep = armijostep;
                            }
                            armijostep = 0.5*armijostep;
                        }
                        for(i_=0; i_<=nmain+nslack-1;i_++)
                        {
                            xa[i_] = xn[i_];
                        }
                        for(i_=0; i_<=nmain+nslack-1;i_++)
                        {
                            xa[i_] = xa[i_] + armijobeststep*newtonstep[i_];
                        }
                        enforceboundaryconstraints(ref xa, bndl, havebndl, bndu, havebndu, nmain, nslack);
                    }
                    else
                    {
                        
                        //
                        // Armijo iteration is not performed
                        //
                        for(i_=0; i_<=nmain+nslack-1;i_++)
                        {
                            xa[i_] = xn[i_];
                        }
                    }
                    stage1isover = (double)(maxsteplen)>=(double)(1) || (double)(maxsteplen)==(double)(0);
                    
                    //
                    // Calculate feasibility errors for old and new X.
                    // These quantinies are used for debugging purposes only.
                    // However, we can leave them in release code because performance impact is insignificant.
                    //
                    // Update X. Exit if needed.
                    //
                    feasold = 0;
                    feasnew = 0;
                    for(i=0; i<=k-1; i++)
                    {
                        v = 0.0;
                        for(i_=0; i_<=nmain+nslack-1;i_++)
                        {
                            v += ce[i,i_]*x[i_];
                        }
                        feasold = feasold+math.sqr(v-ce[i,nmain+nslack]);
                        v = 0.0;
                        for(i_=0; i_<=nmain+nslack-1;i_++)
                        {
                            v += ce[i,i_]*xa[i_];
                        }
                        feasnew = feasnew+math.sqr(v-ce[i,nmain+nslack]);
                    }
                    feasold = Math.Sqrt(feasold);
                    feasnew = Math.Sqrt(feasnew);
                    if( (double)(feasnew)>=(double)(feasold) )
                    {
                        break;
                    }
                    for(i_=0; i_<=nmain+nslack-1;i_++)
                    {
                        x[i_] = xa[i_];
                    }
                    if( stage1isover )
                    {
                        break;
                    }
                }
                
                //
                // Stage 2: gradient projection algorithm (GPA)
                //
                // * calculate feasibility error (with respect to linear equality constraints)
                // * calculate gradient G of F, project it into feasible area (G => PG)
                // * exit if norm(PG) is exactly zero or feasibility error is smaller than EpsC
                // * let XM be exact minimum of F along -PG (XM may be infeasible).
                //   calculate MaxStepLen = largest step in direction of -PG which retains feasibility.
                // * perform bounded step from X to XN:
                //   a) XN=XM              (if XM is feasible)
                //   b) XN=X-MaxStepLen*PG (otherwise)
                // * X := XN
                // * stop after specified number of iterations or when no new constraints was activated
                //
                // NOTES:
                // * grad(F) = (CE'*CE)*x - (b'*CE)^T
                // * CE[i] denotes I-th row of CE
                // * XM = X+stp*(-PG) where stp=(grad(F(X)),PG)/(CE*PG,CE*PG).
                //   Here PG is a projected gradient, but in fact it can be arbitrary non-zero 
                //   direction vector - formula for minimum of F along PG still will be correct.
                //
                werechangesinconstraints = false;
                for(gparuns=1; gparuns<=k; gparuns++)
                {
                    
                    //
                    // calculate feasibility error and G
                    //
                    feaserr = 0;
                    for(i=0; i<=nmain+nslack-1; i++)
                    {
                        g[i] = 0;
                    }
                    for(i=0; i<=k-1; i++)
                    {
                        
                        //
                        // G += CE[i]^T * (CE[i]*x-b[i])
                        //
                        v = 0.0;
                        for(i_=0; i_<=nmain+nslack-1;i_++)
                        {
                            v += ce[i,i_]*x[i_];
                        }
                        v = v-ce[i,nmain+nslack];
                        feaserr = feaserr+math.sqr(v);
                        for(i_=0; i_<=nmain+nslack-1;i_++)
                        {
                            g[i_] = g[i_] + v*ce[i,i_];
                        }
                    }
                    
                    //
                    // project G, filter it (strip numerical noise)
                    //
                    for(i_=0; i_<=nmain+nslack-1;i_++)
                    {
                        pg[i_] = g[i_];
                    }
                    projectgradientintobc(x, ref pg, bndl, havebndl, bndu, havebndu, nmain, nslack);
                    filterdirection(ref pg, x, bndl, havebndl, bndu, havebndu, s, nmain, nslack, 1.0E-9);
                    for(i=0; i<=nmain+nslack-1; i++)
                    {
                        if( (double)(math.sqr(colnorms[i]))!=(double)(0) )
                        {
                            pg[i] = pg[i]/math.sqr(colnorms[i]);
                        }
                        else
                        {
                            pg[i] = 0.0;
                        }
                    }
                    
                    //
                    // Check GNorm and feasibility.
                    // Exit when GNorm is exactly zero.
                    //
                    pgnorm = 0.0;
                    for(i_=0; i_<=nmain+nslack-1;i_++)
                    {
                        pgnorm += pg[i_]*pg[i_];
                    }
                    feaserr = Math.Sqrt(feaserr);
                    pgnorm = Math.Sqrt(pgnorm);
                    if( (double)(pgnorm)==(double)(0) )
                    {
                        result = (double)(feaserr)<=(double)(epsi);
                        return result;
                    }
                    
                    //
                    // calculate planned step length
                    //
                    vn = 0.0;
                    for(i_=0; i_<=nmain+nslack-1;i_++)
                    {
                        vn += g[i_]*pg[i_];
                    }
                    vd = 0;
                    for(i=0; i<=k-1; i++)
                    {
                        v = 0.0;
                        for(i_=0; i_<=nmain+nslack-1;i_++)
                        {
                            v += ce[i,i_]*pg[i_];
                        }
                        vd = vd+math.sqr(v);
                    }
                    stp = vn/vd;
                    
                    //
                    // Calculate step bound.
                    // Perform bounded step and post-process it
                    //
                    calculatestepbound(x, pg, -1.0, bndl, havebndl, bndu, havebndu, nmain, nslack, ref vartofreeze, ref valtofreeze, ref maxsteplen);
                    if( vartofreeze>=0 && (double)(maxsteplen)==(double)(0) )
                    {
                        result = false;
                        return result;
                    }
                    if( vartofreeze>=0 )
                    {
                        v = Math.Min(stp, maxsteplen);
                    }
                    else
                    {
                        v = stp;
                    }
                    for(i_=0; i_<=nmain+nslack-1;i_++)
                    {
                        xn[i_] = x[i_];
                    }
                    for(i_=0; i_<=nmain+nslack-1;i_++)
                    {
                        xn[i_] = xn[i_] - v*pg[i_];
                    }
                    postprocessboundedstep(ref xn, x, bndl, havebndl, bndu, havebndu, nmain, nslack, vartofreeze, valtofreeze, v, maxsteplen);
                    
                    //
                    // update X
                    // check stopping criteria
                    //
                    werechangesinconstraints = werechangesinconstraints || numberofchangedconstraints(xn, x, bndl, havebndl, bndu, havebndu, nmain, nslack)>0;
                    for(i_=0; i_<=nmain+nslack-1;i_++)
                    {
                        x[i_] = xn[i_];
                    }
                    gpaits = gpaits+1;
                    if( !werechangesinconstraints )
                    {
                        break;
                    }
                }
                
                //
                // Stage 3: decide to stop algorithm or not to stop
                //
                // 1. we can stop when last GPA run did NOT changed constraints status.
                //    It means that we've found final set of the active constraints even
                //    before GPA made its run. And it means that Newton step moved us to
                //    the minimum subject to the present constraints.
                //    Depending on feasibility error, True or False is returned.
                //
                feaserr = 0;
                for(i=0; i<=k-1; i++)
                {
                    v = 0.0;
                    for(i_=0; i_<=nmain+nslack-1;i_++)
                    {
                        v += ce[i,i_]*x[i_];
                    }
                    v = v-ce[i,nmain+nslack];
                    feaserr = feaserr+math.sqr(v);
                }
                feaserr = Math.Sqrt(feaserr);
                if( (double)(feaserr)<=(double)(epsi) )
                {
                    itswithintolerance = itswithintolerance+1;
                }
                else
                {
                    itswithintolerance = 0;
                }
                if( !werechangesinconstraints || itswithintolerance>=maxitswithintolerance )
                {
                    result = (double)(feaserr)<=(double)(epsi);
                    return result;
                }
                itscount = itscount+1;
            }
            return result;
        }
Exemplo n.º 6
0
        /*************************************************************************
        This function replaces data in XY by their CENTERED ranks:
        * XY is processed row-by-row
        * rows are processed separately
        * tied data are correctly handled (tied ranks are calculated)
        * centered ranks are just usual ranks, but centered in such way  that  sum
          of within-row values is equal to 0.0.
        * centering is performed by subtracting mean from each row, i.e it changes
          mean value, but does NOT change higher moments

        SMP EDITION OF ALGLIB:

          ! This function can utilize multicore capabilities of  your system.  In
          ! order to do this you have to call version with "smp_" prefix,   which
          ! indicates that multicore code will be used.
          ! 
          ! This note is given for users of SMP edition; if you use GPL  edition,
          ! or commercial edition of ALGLIB without SMP support, you  still  will
          ! be able to call smp-version of this function,  but  all  computations
          ! will be done serially.
          !
          ! We recommend you to carefully read ALGLIB Reference  Manual,  section
          ! called 'SMP support', before using parallel version of this function.
          !
          ! You should remember that starting/stopping worker thread always  have
          ! non-zero cost. Although  multicore  version  is  pretty  efficient on
          ! large problems, we do not recommend you to use it on small problems -
          ! ones where expected operations count is less than 100.000

        INPUT PARAMETERS:
            XY      -   array[NPoints,NFeatures], dataset
            NPoints -   number of points
            NFeatures-  number of features

        OUTPUT PARAMETERS:
            XY      -   data are replaced by their within-row ranks;
                        ranking starts from 0, ends at NFeatures-1

          -- ALGLIB --
             Copyright 18.04.2013 by Bochkanov Sergey
        *************************************************************************/
        public static void rankdatacentered(double[,] xy,
            int npoints,
            int nfeatures)
        {
            apserv.apbuffers buf0 = new apserv.apbuffers();
            apserv.apbuffers buf1 = new apserv.apbuffers();
            int basecasecost = 0;
            alglib.smp.shared_pool pool = new alglib.smp.shared_pool();

            alglib.ap.assert(npoints>=0, "RankData: NPoints<0");
            alglib.ap.assert(nfeatures>=1, "RankData: NFeatures<1");
            alglib.ap.assert(alglib.ap.rows(xy)>=npoints, "RankData: Rows(XY)<NPoints");
            alglib.ap.assert(alglib.ap.cols(xy)>=nfeatures || npoints==0, "RankData: Cols(XY)<NFeatures");
            alglib.ap.assert(apserv.apservisfinitematrix(xy, npoints, nfeatures), "RankData: XY contains infinite/NAN elements");
            
            //
            // Basecase cost is a maximum cost of basecase problems.
            // Problems harded than that cost will be split.
            //
            // Problem cost is assumed to be NPoints*NFeatures*log2(NFeatures),
            // which is proportional, but NOT equal to number of FLOPs required
            // to solve problem.
            //
            basecasecost = 10000;
            
            //
            // Try to use serial code, no SMP functionality, no shared pools.
            //
            if( (double)(apserv.inttoreal(npoints)*apserv.inttoreal(nfeatures)*apserv.logbase2(nfeatures))<(double)(basecasecost) )
            {
                rankdatabasecase(xy, 0, npoints, nfeatures, true, buf0, buf1);
                return;
            }
            
            //
            // Parallel code
            //
            alglib.smp.ae_shared_pool_set_seed(pool, buf0);
            rankdatarec(xy, 0, npoints, nfeatures, true, pool, basecasecost);
        }
Exemplo n.º 7
0
 public minqpstate()
 {
     densea = new double[0,0];
     diaga = new double[0];
     b = new double[0];
     bndl = new double[0];
     bndu = new double[0];
     havebndl = new bool[0];
     havebndu = new bool[0];
     xorigin = new double[0];
     startx = new double[0];
     xc = new double[0];
     gc = new double[0];
     activeconstraints = new int[0];
     prevactiveconstraints = new int[0];
     workbndl = new double[0];
     workbndu = new double[0];
     tmp0 = new double[0];
     tmp1 = new double[0];
     itmp0 = new int[0];
     p2 = new int[0];
     bufa = new double[0,0];
     bufb = new double[0];
     bufx = new double[0];
     buf = new apserv.apbuffers();
 }
Exemplo n.º 8
0
        /*************************************************************************
        Spearman's rank correlation matrix

        SMP EDITION OF ALGLIB:

          ! This function can utilize multicore capabilities of  your system.  In
          ! order to do this you have to call version with "smp_" prefix,   which
          ! indicates that multicore code will be used.
          ! 
          ! This note is given for users of SMP edition; if you use GPL  edition,
          ! or commercial edition of ALGLIB without SMP support, you  still  will
          ! be able to call smp-version of this function,  but  all  computations
          ! will be done serially.
          !
          ! We recommend you to carefully read ALGLIB Reference  Manual,  section
          ! called 'SMP support', before using parallel version of this function.
          !
          ! You should remember that starting/stopping worker thread always  have
          ! non-zero cost. Although  multicore  version  is  pretty  efficient on
          ! large problems, we do not recommend you to use it on small problems -
          ! with correlation matrices smaller than 128*128.

        INPUT PARAMETERS:
            X   -   array[N,M], sample matrix:
                    * J-th column corresponds to J-th variable
                    * I-th row corresponds to I-th observation
            N   -   N>=0, number of observations:
                    * if given, only leading N rows of X are used
                    * if not given, automatically determined from input size
            M   -   M>0, number of variables:
                    * if given, only leading M columns of X are used
                    * if not given, automatically determined from input size

        OUTPUT PARAMETERS:
            C   -   array[M,M], correlation matrix (zero if N=0 or N=1)

          -- ALGLIB --
             Copyright 28.10.2010 by Bochkanov Sergey
        *************************************************************************/
        public static void spearmancorrm(double[,] x,
            int n,
            int m,
            ref double[,] c)
        {
            int i = 0;
            int j = 0;
            apserv.apbuffers buf = new apserv.apbuffers();
            double[,] xc = new double[0,0];
            double[] t = new double[0];
            double v = 0;
            double vv = 0;
            double x0 = 0;
            bool b = new bool();

            c = new double[0,0];

            alglib.ap.assert(n>=0, "SpearmanCorrM: N<0");
            alglib.ap.assert(m>=1, "SpearmanCorrM: M<1");
            alglib.ap.assert(alglib.ap.rows(x)>=n, "SpearmanCorrM: Rows(X)<N!");
            alglib.ap.assert(alglib.ap.cols(x)>=m || n==0, "SpearmanCorrM: Cols(X)<M!");
            alglib.ap.assert(apserv.apservisfinitematrix(x, n, m), "SpearmanCorrM: X contains infinite/NAN elements");
            
            //
            // N<=1, return zero
            //
            if( n<=1 )
            {
                c = new double[m, m];
                for(i=0; i<=m-1; i++)
                {
                    for(j=0; j<=m-1; j++)
                    {
                        c[i,j] = 0;
                    }
                }
                return;
            }
            
            //
            // Allocate
            //
            t = new double[Math.Max(n, m)];
            c = new double[m, m];
            
            //
            // Replace data with ranks
            //
            xc = new double[m, n];
            ablas.rmatrixtranspose(n, m, x, 0, 0, xc, 0, 0);
            rankdata(xc, m, n);
            
            //
            // 1. Calculate means, check for constant columns
            // 2. Center variables, constant  columns are
            //   artificialy zeroed (they must be zero in exact arithmetics,
            //   but unfortunately floating point is not exact).
            //
            for(i=0; i<=m-1; i++)
            {
                
                //
                // Calculate:
                // * V - mean value of I-th variable
                // * B - True in case all variable values are same
                //
                v = 0;
                b = true;
                x0 = xc[i,0];
                for(j=0; j<=n-1; j++)
                {
                    vv = xc[i,j];
                    v = v+vv;
                    b = b && (double)(vv)==(double)(x0);
                }
                v = v/n;
                
                //
                // Center/zero I-th variable
                //
                if( b )
                {
                    
                    //
                    // Zero
                    //
                    for(j=0; j<=n-1; j++)
                    {
                        xc[i,j] = 0.0;
                    }
                }
                else
                {
                    
                    //
                    // Center
                    //
                    for(j=0; j<=n-1; j++)
                    {
                        xc[i,j] = xc[i,j]-v;
                    }
                }
            }
            
            //
            // Calculate upper half of symmetric covariance matrix
            //
            ablas.rmatrixsyrk(m, n, (double)1/(double)(n-1), xc, 0, 0, 0, 0.0, c, 0, 0, true);
            
            //
            // Calculate Pearson coefficients (upper triangle)
            //
            for(i=0; i<=m-1; i++)
            {
                if( (double)(c[i,i])>(double)(0) )
                {
                    t[i] = 1/Math.Sqrt(c[i,i]);
                }
                else
                {
                    t[i] = 0.0;
                }
            }
            for(i=0; i<=m-1; i++)
            {
                v = t[i];
                for(j=i; j<=m-1; j++)
                {
                    c[i,j] = c[i,j]*v*t[j];
                }
            }
            
            //
            // force symmetricity
            //
            ablas.rmatrixenforcesymmetricity(c, m, true);
        }
Exemplo n.º 9
0
        /*************************************************************************
        Spearman's rank cross-correlation matrix

        SMP EDITION OF ALGLIB:

          ! This function can utilize multicore capabilities of  your system.  In
          ! order to do this you have to call version with "smp_" prefix,   which
          ! indicates that multicore code will be used.
          ! 
          ! This note is given for users of SMP edition; if you use GPL  edition,
          ! or commercial edition of ALGLIB without SMP support, you  still  will
          ! be able to call smp-version of this function,  but  all  computations
          ! will be done serially.
          !
          ! We recommend you to carefully read ALGLIB Reference  Manual,  section
          ! called 'SMP support', before using parallel version of this function.
          !
          ! You should remember that starting/stopping worker thread always  have
          ! non-zero cost. Although  multicore  version  is  pretty  efficient on
          ! large problems, we do not recommend you to use it on small problems -
          ! with correlation matrices smaller than 128*128.

        INPUT PARAMETERS:
            X   -   array[N,M1], sample matrix:
                    * J-th column corresponds to J-th variable
                    * I-th row corresponds to I-th observation
            Y   -   array[N,M2], sample matrix:
                    * J-th column corresponds to J-th variable
                    * I-th row corresponds to I-th observation
            N   -   N>=0, number of observations:
                    * if given, only leading N rows of X/Y are used
                    * if not given, automatically determined from input sizes
            M1  -   M1>0, number of variables in X:
                    * if given, only leading M1 columns of X are used
                    * if not given, automatically determined from input size
            M2  -   M2>0, number of variables in Y:
                    * if given, only leading M1 columns of X are used
                    * if not given, automatically determined from input size

        OUTPUT PARAMETERS:
            C   -   array[M1,M2], cross-correlation matrix (zero if N=0 or N=1)

          -- ALGLIB --
             Copyright 28.10.2010 by Bochkanov Sergey
        *************************************************************************/
        public static void spearmancorrm2(double[,] x,
            double[,] y,
            int n,
            int m1,
            int m2,
            ref double[,] c)
        {
            int i = 0;
            int j = 0;
            double v = 0;
            double v2 = 0;
            double vv = 0;
            bool b = new bool();
            double[] t = new double[0];
            double x0 = 0;
            double y0 = 0;
            double[] sx = new double[0];
            double[] sy = new double[0];
            double[,] xc = new double[0,0];
            double[,] yc = new double[0,0];
            apserv.apbuffers buf = new apserv.apbuffers();

            c = new double[0,0];

            alglib.ap.assert(n>=0, "SpearmanCorrM2: N<0");
            alglib.ap.assert(m1>=1, "SpearmanCorrM2: M1<1");
            alglib.ap.assert(m2>=1, "SpearmanCorrM2: M2<1");
            alglib.ap.assert(alglib.ap.rows(x)>=n, "SpearmanCorrM2: Rows(X)<N!");
            alglib.ap.assert(alglib.ap.cols(x)>=m1 || n==0, "SpearmanCorrM2: Cols(X)<M1!");
            alglib.ap.assert(apserv.apservisfinitematrix(x, n, m1), "SpearmanCorrM2: X contains infinite/NAN elements");
            alglib.ap.assert(alglib.ap.rows(y)>=n, "SpearmanCorrM2: Rows(Y)<N!");
            alglib.ap.assert(alglib.ap.cols(y)>=m2 || n==0, "SpearmanCorrM2: Cols(Y)<M2!");
            alglib.ap.assert(apserv.apservisfinitematrix(y, n, m2), "SpearmanCorrM2: X contains infinite/NAN elements");
            
            //
            // N<=1, return zero
            //
            if( n<=1 )
            {
                c = new double[m1, m2];
                for(i=0; i<=m1-1; i++)
                {
                    for(j=0; j<=m2-1; j++)
                    {
                        c[i,j] = 0;
                    }
                }
                return;
            }
            
            //
            // Allocate
            //
            t = new double[Math.Max(Math.Max(m1, m2), n)];
            sx = new double[m1];
            sy = new double[m2];
            c = new double[m1, m2];
            
            //
            // Replace data with ranks
            //
            xc = new double[m1, n];
            yc = new double[m2, n];
            ablas.rmatrixtranspose(n, m1, x, 0, 0, xc, 0, 0);
            ablas.rmatrixtranspose(n, m2, y, 0, 0, yc, 0, 0);
            rankdata(xc, m1, n);
            rankdata(yc, m2, n);
            
            //
            // 1. Calculate means, variances, check for constant columns
            // 2. Center variables, constant  columns are
            //   artificialy zeroed (they must be zero in exact arithmetics,
            //   but unfortunately floating point is not exact).
            //
            // Description of variables:
            // * V - mean value of I-th variable
            // * V2- variance
            // * VV-temporary
            // * B - True in case all variable values are same
            //
            for(i=0; i<=m1-1; i++)
            {
                v = 0;
                v2 = 0.0;
                b = true;
                x0 = xc[i,0];
                for(j=0; j<=n-1; j++)
                {
                    vv = xc[i,j];
                    v = v+vv;
                    b = b && (double)(vv)==(double)(x0);
                }
                v = v/n;
                if( b )
                {
                    for(j=0; j<=n-1; j++)
                    {
                        xc[i,j] = 0.0;
                    }
                }
                else
                {
                    for(j=0; j<=n-1; j++)
                    {
                        vv = xc[i,j];
                        xc[i,j] = vv-v;
                        v2 = v2+(vv-v)*(vv-v);
                    }
                }
                sx[i] = Math.Sqrt(v2/(n-1));
            }
            for(i=0; i<=m2-1; i++)
            {
                v = 0;
                v2 = 0.0;
                b = true;
                y0 = yc[i,0];
                for(j=0; j<=n-1; j++)
                {
                    vv = yc[i,j];
                    v = v+vv;
                    b = b && (double)(vv)==(double)(y0);
                }
                v = v/n;
                if( b )
                {
                    for(j=0; j<=n-1; j++)
                    {
                        yc[i,j] = 0.0;
                    }
                }
                else
                {
                    for(j=0; j<=n-1; j++)
                    {
                        vv = yc[i,j];
                        yc[i,j] = vv-v;
                        v2 = v2+(vv-v)*(vv-v);
                    }
                }
                sy[i] = Math.Sqrt(v2/(n-1));
            }
            
            //
            // calculate cross-covariance matrix
            //
            ablas.rmatrixgemm(m1, m2, n, (double)1/(double)(n-1), xc, 0, 0, 0, yc, 0, 0, 1, 0.0, c, 0, 0);
            
            //
            // Divide by standard deviations
            //
            for(i=0; i<=m1-1; i++)
            {
                if( (double)(sx[i])!=(double)(0) )
                {
                    sx[i] = 1/sx[i];
                }
                else
                {
                    sx[i] = 0.0;
                }
            }
            for(i=0; i<=m2-1; i++)
            {
                if( (double)(sy[i])!=(double)(0) )
                {
                    sy[i] = 1/sy[i];
                }
                else
                {
                    sy[i] = 0.0;
                }
            }
            for(i=0; i<=m1-1; i++)
            {
                v = sx[i];
                for(j=0; j<=m2-1; j++)
                {
                    c[i,j] = c[i,j]*v*sy[j];
                }
            }
        }
Exemplo n.º 10
0
 public override void init()
 {
     xy = new double[0,0];
     d = new double[0,0];
     tmpd = new double[0,0];
     distbuf = new apserv.apbuffers();
     kmeanstmp = new kmeansbuffers();
 }
Exemplo n.º 11
0
        /*************************************************************************
        This function returns distance matrix for dataset

        COMMERCIAL EDITION OF ALGLIB:

          ! Commercial version of ALGLIB includes two  important  improvements  of
          ! this function, which can be used from C++ and C#:
          ! * Intel MKL support (lightweight Intel MKL is shipped with ALGLIB)
          ! * multicore support
          !
          ! Agglomerative  hierarchical  clustering  algorithm  has  two   phases:
          ! distance matrix calculation  and  clustering  itself. Only first phase
          ! (distance matrix calculation) is accelerated by Intel MKL  and  multi-
          ! threading. Thus, acceleration is significant only for  medium or high-
          ! dimensional problems.
          !
          ! We recommend you to read 'Working with commercial version' section  of
          ! ALGLIB Reference Manual in order to find out how to  use  performance-
          ! related features provided by commercial edition of ALGLIB.

        INPUT PARAMETERS:
            XY      -   array[NPoints,NFeatures], dataset
            NPoints -   number of points, >=0
            NFeatures-  number of features, >=1
            DistType-   distance function:
                        *  0    Chebyshev distance  (L-inf norm)
                        *  1    city block distance (L1 norm)
                        *  2    Euclidean distance  (L2 norm, non-squared)
                        * 10    Pearson correlation:
                                dist(a,b) = 1-corr(a,b)
                        * 11    Absolute Pearson correlation:
                                dist(a,b) = 1-|corr(a,b)|
                        * 12    Uncentered Pearson correlation (cosine of the angle):
                                dist(a,b) = a'*b/(|a|*|b|)
                        * 13    Absolute uncentered Pearson correlation
                                dist(a,b) = |a'*b|/(|a|*|b|)
                        * 20    Spearman rank correlation:
                                dist(a,b) = 1-rankcorr(a,b)
                        * 21    Absolute Spearman rank correlation
                                dist(a,b) = 1-|rankcorr(a,b)|

        OUTPUT PARAMETERS:
            D       -   array[NPoints,NPoints], distance matrix
                        (full matrix is returned, with lower and upper triangles)

        NOTE:  different distance functions have different performance penalty:
               * Euclidean or Pearson correlation distances are the fastest ones
               * Spearman correlation distance function is a bit slower
               * city block and Chebyshev distances are order of magnitude slower
               
               The reason behing difference in performance is that correlation-based
               distance functions are computed using optimized linear algebra kernels,
               while Chebyshev and city block distance functions are computed using
               simple nested loops with two branches at each iteration.

          -- ALGLIB --
             Copyright 10.07.2012 by Bochkanov Sergey
        *************************************************************************/
        public static void clusterizergetdistances(double[,] xy,
            int npoints,
            int nfeatures,
            int disttype,
            ref double[,] d)
        {
            apserv.apbuffers buf = new apserv.apbuffers();

            d = new double[0,0];

            alglib.ap.assert(nfeatures>=1, "ClusterizerGetDistances: NFeatures<1");
            alglib.ap.assert(npoints>=0, "ClusterizerGetDistances: NPoints<1");
            alglib.ap.assert((((((((disttype==0 || disttype==1) || disttype==2) || disttype==10) || disttype==11) || disttype==12) || disttype==13) || disttype==20) || disttype==21, "ClusterizerGetDistances: incorrect DistType");
            alglib.ap.assert(alglib.ap.rows(xy)>=npoints, "ClusterizerGetDistances: Rows(XY)<NPoints");
            alglib.ap.assert(alglib.ap.cols(xy)>=nfeatures, "ClusterizerGetDistances: Cols(XY)<NFeatures");
            alglib.ap.assert(apserv.apservisfinitematrix(xy, npoints, nfeatures), "ClusterizerGetDistances: XY contains NAN/INF");
            clusterizergetdistancesbuf(buf, xy, npoints, nfeatures, disttype, ref d);
        }
Exemplo n.º 12
0
 public override void init()
 {
     ct = new double[0,0];
     ctbest = new double[0,0];
     xycbest = new int[0];
     xycprev = new int[0];
     d2 = new double[0];
     csizes = new int[0];
     initbuf = new apserv.apbuffers();
     updatepool = new alglib.smp.shared_pool();
 }
Exemplo n.º 13
0
        /*************************************************************************
        K-means++ initialization

        INPUT PARAMETERS:
            Buf         -   special reusable structure which stores previously allocated
                            memory, intended to avoid memory fragmentation when solving
                            multiple subsequent problems. Must be initialized prior to
                            usage.

        OUTPUT PARAMETERS:
            Buf         -   initialized structure

          -- ALGLIB --
             Copyright 24.07.2015 by Bochkanov Sergey
        *************************************************************************/
        public static void kmeansinitbuf(kmeansbuffers buf)
        {
            apserv.apbuffers updateseed = new apserv.apbuffers();

            alglib.smp.ae_shared_pool_set_seed(buf.updatepool, updateseed);
        }
Exemplo n.º 14
0
            /*************************************************************************
            This function returns distance matrix for dataset

            FOR USERS OF SMP EDITION:

              ! This function can utilize multicore capabilities of  your system.  In
              ! order to do this you have to call version with "smp_" prefix,   which
              ! indicates that multicore code will be used.
              ! 
              ! This note is given for users of SMP edition; if you use GPL  edition,
              ! or commercial edition of ALGLIB without SMP support, you  still  will
              ! be able to call smp-version of this function,  but  all  computations
              ! will be done serially.
              !
              ! We recommend you to carefully read ALGLIB Reference  Manual,  section
              ! called 'SMP support', before using parallel version of this function.
              !
              ! You should remember that starting/stopping worker thread always  have
              ! non-zero  cost.  Multicore  version  is  pretty  efficient  on  large
              ! problems  which  need  more  than  1.000.000 operations to be solved,
              ! gives  moderate  speed-up in mid-range (from 100.000 to 1.000.000 CPU
              ! cycles), but gives no speed-up for small problems (less than  100.000
              ! operations).

            INPUT PARAMETERS:
                XY      -   array[NPoints,NFeatures], dataset
                NPoints -   number of points, >=0
                NFeatures-  number of features, >=1
                DistType-   distance function:
                            *  0    Chebyshev distance  (L-inf norm)
                            *  1    city block distance (L1 norm)
                            *  2    Euclidean distance  (L2 norm)
                            * 10    Pearson correlation:
                                    dist(a,b) = 1-corr(a,b)
                            * 11    Absolute Pearson correlation:
                                    dist(a,b) = 1-|corr(a,b)|
                            * 12    Uncentered Pearson correlation (cosine of the angle):
                                    dist(a,b) = a'*b/(|a|*|b|)
                            * 13    Absolute uncentered Pearson correlation
                                    dist(a,b) = |a'*b|/(|a|*|b|)
                            * 20    Spearman rank correlation:
                                    dist(a,b) = 1-rankcorr(a,b)
                            * 21    Absolute Spearman rank correlation
                                    dist(a,b) = 1-|rankcorr(a,b)|

            OUTPUT PARAMETERS:
                D       -   array[NPoints,NPoints], distance matrix
                            (full matrix is returned, with lower and upper triangles)

            NOTES: different distance functions have different performance penalty:
                   * Euclidean or Pearson correlation distances are the fastest ones
                   * Spearman correlation distance function is a bit slower
                   * city block and Chebyshev distances are order of magnitude slower
               
                   The reason behing difference in performance is that correlation-based
                   distance functions are computed using optimized linear algebra kernels,
                   while Chebyshev and city block distance functions are computed using
                   simple nested loops with two branches at each iteration.

              -- ALGLIB --
                 Copyright 10.07.2012 by Bochkanov Sergey
            *************************************************************************/
            public static void clusterizergetdistances(double[,] xy,
                int npoints,
                int nfeatures,
                int disttype,
                ref double[,] d)
            {
                int i = 0;
                int j = 0;
                double v = 0;
                double vv = 0;
                double vr = 0;
                double[,] tmpxy = new double[0, 0];
                double[] tmpx = new double[0];
                double[] tmpy = new double[0];
                double[] diagbuf = new double[0];
                apserv.apbuffers buf = new apserv.apbuffers();
                int i_ = 0;

                d = new double[0, 0];

                alglib.ap.assert(nfeatures >= 1, "ClusterizerGetDistances: NFeatures<1");
                alglib.ap.assert(npoints >= 0, "ClusterizerGetDistances: NPoints<1");
                alglib.ap.assert((((((((disttype == 0 || disttype == 1) || disttype == 2) || disttype == 10) || disttype == 11) || disttype == 12) || disttype == 13) || disttype == 20) || disttype == 21, "ClusterizerGetDistances: incorrect DistType");
                alglib.ap.assert(alglib.ap.rows(xy) >= npoints, "ClusterizerGetDistances: Rows(XY)<NPoints");
                alglib.ap.assert(alglib.ap.cols(xy) >= nfeatures, "ClusterizerGetDistances: Cols(XY)<NFeatures");
                alglib.ap.assert(apserv.apservisfinitematrix(xy, npoints, nfeatures), "ClusterizerGetDistances: XY contains NAN/INF");

                //
                // Quick exit
                //
                if (npoints == 0)
                {
                    return;
                }
                if (npoints == 1)
                {
                    d = new double[1, 1];
                    d[0, 0] = 0;
                    return;
                }

                //
                // Build distance matrix D.
                //
                if (disttype == 0 || disttype == 1)
                {

                    //
                    // Chebyshev or city-block distances:
                    // * recursively calculate upper triangle (with main diagonal)
                    // * copy it to the bottom part of the matrix
                    //
                    d = new double[npoints, npoints];
                    evaluatedistancematrixrec(xy, nfeatures, disttype, d, 0, npoints, 0, npoints);
                    ablas.rmatrixenforcesymmetricity(d, npoints, true);
                    return;
                }
                if (disttype == 2)
                {

                    //
                    // Euclidean distance
                    //
                    // NOTE: parallelization is done within RMatrixSYRK
                    //
                    d = new double[npoints, npoints];
                    tmpxy = new double[npoints, nfeatures];
                    tmpx = new double[nfeatures];
                    diagbuf = new double[npoints];
                    for (j = 0; j <= nfeatures - 1; j++)
                    {
                        tmpx[j] = 0.0;
                    }
                    v = (double)1 / (double)npoints;
                    for (i = 0; i <= npoints - 1; i++)
                    {
                        for (i_ = 0; i_ <= nfeatures - 1; i_++)
                        {
                            tmpx[i_] = tmpx[i_] + v * xy[i, i_];
                        }
                    }
                    for (i = 0; i <= npoints - 1; i++)
                    {
                        for (i_ = 0; i_ <= nfeatures - 1; i_++)
                        {
                            tmpxy[i, i_] = xy[i, i_];
                        }
                        for (i_ = 0; i_ <= nfeatures - 1; i_++)
                        {
                            tmpxy[i, i_] = tmpxy[i, i_] - tmpx[i_];
                        }
                    }
                    ablas.rmatrixsyrk(npoints, nfeatures, 1.0, tmpxy, 0, 0, 0, 0.0, d, 0, 0, true);
                    for (i = 0; i <= npoints - 1; i++)
                    {
                        diagbuf[i] = d[i, i];
                    }
                    for (i = 0; i <= npoints - 1; i++)
                    {
                        d[i, i] = 0.0;
                        for (j = i + 1; j <= npoints - 1; j++)
                        {
                            v = Math.Sqrt(Math.Max(diagbuf[i] + diagbuf[j] - 2 * d[i, j], 0.0));
                            d[i, j] = v;
                        }
                    }
                    ablas.rmatrixenforcesymmetricity(d, npoints, true);
                    return;
                }
                if (disttype == 10 || disttype == 11)
                {

                    //
                    // Absolute/nonabsolute Pearson correlation distance
                    //
                    // NOTE: parallelization is done within PearsonCorrM, which calls RMatrixSYRK internally
                    //
                    d = new double[npoints, npoints];
                    diagbuf = new double[npoints];
                    tmpxy = new double[npoints, nfeatures];
                    for (i = 0; i <= npoints - 1; i++)
                    {
                        v = 0.0;
                        for (j = 0; j <= nfeatures - 1; j++)
                        {
                            v = v + xy[i, j];
                        }
                        v = v / nfeatures;
                        for (j = 0; j <= nfeatures - 1; j++)
                        {
                            tmpxy[i, j] = xy[i, j] - v;
                        }
                    }
                    ablas.rmatrixsyrk(npoints, nfeatures, 1.0, tmpxy, 0, 0, 0, 0.0, d, 0, 0, true);
                    for (i = 0; i <= npoints - 1; i++)
                    {
                        diagbuf[i] = d[i, i];
                    }
                    for (i = 0; i <= npoints - 1; i++)
                    {
                        d[i, i] = 0.0;
                        for (j = i + 1; j <= npoints - 1; j++)
                        {
                            v = d[i, j] / Math.Sqrt(diagbuf[i] * diagbuf[j]);
                            if (disttype == 10)
                            {
                                v = 1 - v;
                            }
                            else
                            {
                                v = 1 - Math.Abs(v);
                            }
                            v = Math.Max(v, 0.0);
                            d[i, j] = v;
                        }
                    }
                    ablas.rmatrixenforcesymmetricity(d, npoints, true);
                    return;
                }
                if (disttype == 12 || disttype == 13)
                {

                    //
                    // Absolute/nonabsolute uncentered Pearson correlation distance
                    //
                    // NOTE: parallelization is done within RMatrixSYRK
                    //
                    d = new double[npoints, npoints];
                    diagbuf = new double[npoints];
                    ablas.rmatrixsyrk(npoints, nfeatures, 1.0, xy, 0, 0, 0, 0.0, d, 0, 0, true);
                    for (i = 0; i <= npoints - 1; i++)
                    {
                        diagbuf[i] = d[i, i];
                    }
                    for (i = 0; i <= npoints - 1; i++)
                    {
                        d[i, i] = 0.0;
                        for (j = i + 1; j <= npoints - 1; j++)
                        {
                            v = d[i, j] / Math.Sqrt(diagbuf[i] * diagbuf[j]);
                            if (disttype == 13)
                            {
                                v = Math.Abs(v);
                            }
                            v = Math.Min(v, 1.0);
                            d[i, j] = 1 - v;
                        }
                    }
                    ablas.rmatrixenforcesymmetricity(d, npoints, true);
                    return;
                }
                if (disttype == 20 || disttype == 21)
                {

                    //
                    // Spearman rank correlation
                    //
                    // NOTE: parallelization of correlation matrix is done within
                    //       PearsonCorrM, which calls RMatrixSYRK internally
                    //
                    d = new double[npoints, npoints];
                    diagbuf = new double[npoints];
                    tmpxy = new double[npoints, nfeatures];
                    ablas.rmatrixcopy(npoints, nfeatures, xy, 0, 0, ref tmpxy, 0, 0);
                    basestat.rankdatacentered(tmpxy, npoints, nfeatures);
                    ablas.rmatrixsyrk(npoints, nfeatures, 1.0, tmpxy, 0, 0, 0, 0.0, d, 0, 0, true);
                    for (i = 0; i <= npoints - 1; i++)
                    {
                        if ((double)(d[i, i]) > (double)(0))
                        {
                            diagbuf[i] = 1 / Math.Sqrt(d[i, i]);
                        }
                        else
                        {
                            diagbuf[i] = 0.0;
                        }
                    }
                    for (i = 0; i <= npoints - 1; i++)
                    {
                        v = diagbuf[i];
                        d[i, i] = 0.0;
                        for (j = i + 1; j <= npoints - 1; j++)
                        {
                            vv = d[i, j] * v * diagbuf[j];
                            if (disttype == 20)
                            {
                                vr = 1 - vv;
                            }
                            else
                            {
                                vr = 1 - Math.Abs(vv);
                            }
                            if ((double)(vr) < (double)(0))
                            {
                                vr = 0.0;
                            }
                            d[i, j] = vr;
                        }
                    }
                    ablas.rmatrixenforcesymmetricity(d, npoints, true);
                    return;
                }
                alglib.ap.assert(false);
            }
Exemplo n.º 15
0
 public minqpstate()
 {
     densea = new double[0, 0];
     b = new double[0];
     bndl = new double[0];
     bndu = new double[0];
     havebndl = new bool[0];
     havebndu = new bool[0];
     xorigin = new double[0];
     startx = new double[0];
     xc = new double[0];
     gc = new double[0];
     dc = new double[0];
     xcand0 = new double[0];
     xcand1 = new double[0];
     xn = new double[0];
     pg = new double[0];
     activeset = new bool[0];
     workbndl = new double[0];
     workbndu = new double[0];
     tmp0 = new double[0];
     tmp1 = new double[0];
     itmp0 = new int[0];
     p2 = new int[0];
     bufa = new double[0, 0];
     bufb = new double[0];
     bufx = new double[0];
     buf = new apserv.apbuffers();
 }
Exemplo n.º 16
0
        /*************************************************************************
        This function returns distance matrix for dataset

        INPUT PARAMETERS:
            XY      -   array[NPoints,NFeatures], dataset
            NPoints -   number of points, >=0
            NFeatures-  number of features, >=1
            DistType-   distance function:
                        *  0    Chebyshev distance  (L-inf norm)
                        *  1    city block distance (L1 norm)
                        *  2    Euclidean distance  (L2 norm)
                        * 10    Pearson correlation:
                                dist(a,b) = 1-corr(a,b)
                        * 11    Absolute Pearson correlation:
                                dist(a,b) = 1-|corr(a,b)|
                        * 12    Uncentered Pearson correlation (cosine of the angle):
                                dist(a,b) = a'*b/(|a|*|b|)
                        * 13    Absolute uncentered Pearson correlation
                                dist(a,b) = |a'*b|/(|a|*|b|)
                        * 20    Spearman rank correlation:
                                dist(a,b) = 1-rankcorr(a,b)
                        * 21    Absolute Spearman rank correlation
                                dist(a,b) = 1-|rankcorr(a,b)|

        OUTPUT PARAMETERS:
            D       -   array[NPoints,NPoints], distance matrix
                        (full matrix is returned, with lower and upper triangles)

        NOTES: different distance functions have different performance penalty:
               * Euclidean or Pearson correlation distances are the fastest ones
               * Spearman correlation distance function is a bit slower
               * city block and Chebyshev distances are order of magnitude slower
               
               The reason behing difference in performance is that correlation-based
               distance functions are computed using optimized linear algebra kernels,
               while Chebyshev and city block distance functions are computed using
               simple nested loops with two branches at each iteration.

          -- ALGLIB --
             Copyright 10.07.2012 by Bochkanov Sergey
        *************************************************************************/
        public static void clusterizergetdistances(double[,] xy,
            int npoints,
            int nfeatures,
            int disttype,
            ref double[,] d)
        {
            int i = 0;
            int j = 0;
            int k = 0;
            double v = 0;
            double vv = 0;
            double[,] tmpxy = new double[0,0];
            double[] tmpx = new double[0];
            double[] tmpy = new double[0];
            apserv.apbuffers buf = new apserv.apbuffers();
            int i_ = 0;

            d = new double[0,0];

            alglib.ap.assert(nfeatures>=1, "ClusterizerGetDistances: NFeatures<1");
            alglib.ap.assert(npoints>=0, "ClusterizerGetDistances: NPoints<1");
            alglib.ap.assert((((((((disttype==0 || disttype==1) || disttype==2) || disttype==10) || disttype==11) || disttype==12) || disttype==13) || disttype==20) || disttype==21, "ClusterizerGetDistances: incorrect DistType");
            alglib.ap.assert(alglib.ap.rows(xy)>=npoints, "ClusterizerGetDistances: Rows(XY)<NPoints");
            alglib.ap.assert(alglib.ap.cols(xy)>=nfeatures, "ClusterizerGetDistances: Cols(XY)<NFeatures");
            alglib.ap.assert(apserv.apservisfinitematrix(xy, npoints, nfeatures), "ClusterizerGetDistances: XY contains NAN/INF");
            
            //
            // Quick exit
            //
            if( npoints==0 )
            {
                return;
            }
            if( npoints==1 )
            {
                d = new double[1, 1];
                d[0,0] = 0;
                return;
            }
            
            //
            // Build distance matrix D.
            //
            if( disttype==0 )
            {
                
                //
                // Chebyshev distance
                //
                d = new double[npoints, npoints];
                for(i=0; i<=npoints-1; i++)
                {
                    d[i,i] = 0.0;
                    for(j=i+1; j<=npoints-1; j++)
                    {
                        v = 0.0;
                        for(k=0; k<=nfeatures-1; k++)
                        {
                            vv = xy[i,k]-xy[j,k];
                            if( (double)(vv)<(double)(0) )
                            {
                                vv = -vv;
                            }
                            if( (double)(vv)>(double)(v) )
                            {
                                v = vv;
                            }
                        }
                        d[i,j] = v;
                        d[j,i] = v;
                    }
                }
                return;
            }
            if( disttype==1 )
            {
                
                //
                // City block distance
                //
                d = new double[npoints, npoints];
                for(i=0; i<=npoints-1; i++)
                {
                    d[i,i] = 0.0;
                    for(j=i+1; j<=npoints-1; j++)
                    {
                        v = 0.0;
                        for(k=0; k<=nfeatures-1; k++)
                        {
                            vv = xy[i,k]-xy[j,k];
                            if( (double)(vv)<(double)(0) )
                            {
                                vv = -vv;
                            }
                            v = v+vv;
                        }
                        v = v/nfeatures;
                        d[i,j] = v;
                        d[j,i] = v;
                    }
                }
                return;
            }
            if( disttype==2 )
            {
                
                //
                // Euclidean distance
                //
                d = new double[npoints, npoints];
                tmpxy = new double[npoints, nfeatures];
                tmpx = new double[nfeatures];
                for(j=0; j<=nfeatures-1; j++)
                {
                    tmpx[j] = 0.0;
                }
                v = (double)1/(double)npoints;
                for(i=0; i<=npoints-1; i++)
                {
                    for(i_=0; i_<=nfeatures-1;i_++)
                    {
                        tmpx[i_] = tmpx[i_] + v*xy[i,i_];
                    }
                }
                for(i=0; i<=npoints-1; i++)
                {
                    for(i_=0; i_<=nfeatures-1;i_++)
                    {
                        tmpxy[i,i_] = xy[i,i_];
                    }
                    for(i_=0; i_<=nfeatures-1;i_++)
                    {
                        tmpxy[i,i_] = tmpxy[i,i_] - tmpx[i_];
                    }
                }
                ablas.rmatrixsyrk(npoints, nfeatures, 1.0, tmpxy, 0, 0, 0, 0.0, ref d, 0, 0, true);
                for(i=0; i<=npoints-1; i++)
                {
                    for(j=i+1; j<=npoints-1; j++)
                    {
                        v = Math.Sqrt(Math.Max(d[i,i]+d[j,j]-2*d[i,j], 0.0));
                        d[i,j] = v;
                        d[j,i] = v;
                    }
                }
                for(i=0; i<=npoints-1; i++)
                {
                    d[i,i] = 0.0;
                }
                return;
            }
            if( disttype==10 || disttype==11 )
            {
                
                //
                // Absolute/nonabsolute Pearson correlation distance
                //
                d = new double[npoints, npoints];
                tmpxy = new double[nfeatures, npoints];
                for(i=0; i<=npoints-1; i++)
                {
                    for(i_=0; i_<=nfeatures-1;i_++)
                    {
                        tmpxy[i_,i] = xy[i,i_];
                    }
                }
                basestat.pearsoncorrm(tmpxy, nfeatures, npoints, ref d);
                for(i=0; i<=npoints-1; i++)
                {
                    for(j=i+1; j<=npoints-1; j++)
                    {
                        if( disttype==10 )
                        {
                            v = 1-d[i,j];
                        }
                        else
                        {
                            v = 1-Math.Abs(d[i,j]);
                        }
                        v = Math.Max(v, 0.0);
                        d[i,j] = v;
                        d[j,i] = v;
                    }
                }
                for(i=0; i<=npoints-1; i++)
                {
                    d[i,i] = 0.0;
                }
                return;
            }
            if( disttype==12 || disttype==13 )
            {
                
                //
                // Absolute/nonabsolute uncentered Pearson correlation distance
                //
                d = new double[npoints, npoints];
                ablas.rmatrixsyrk(npoints, nfeatures, 1.0, xy, 0, 0, 0, 0.0, ref d, 0, 0, true);
                for(i=0; i<=npoints-1; i++)
                {
                    for(j=i+1; j<=npoints-1; j++)
                    {
                        v = d[i,j]/Math.Sqrt(d[i,i]*d[j,j]);
                        if( disttype==13 )
                        {
                            v = Math.Abs(v);
                        }
                        v = Math.Min(v, 1.0);
                        d[i,j] = 1-v;
                        d[j,i] = 1-v;
                    }
                }
                for(i=0; i<=npoints-1; i++)
                {
                    d[i,i] = 0.0;
                }
                return;
            }
            if( disttype==20 || disttype==21 )
            {
                
                //
                // Spearman rank correlation
                //
                d = new double[npoints, npoints];
                tmpx = new double[nfeatures];
                tmpy = new double[nfeatures];
                tmpxy = new double[nfeatures, npoints];
                for(i=0; i<=npoints-1; i++)
                {
                    for(i_=0; i_<=nfeatures-1;i_++)
                    {
                        tmpx[i_] = xy[i,i_];
                    }
                    basicstatops.rankx(ref tmpx, nfeatures, buf);
                    for(i_=0; i_<=nfeatures-1;i_++)
                    {
                        tmpxy[i_,i] = tmpx[i_];
                    }
                }
                basestat.pearsoncorrm(tmpxy, nfeatures, npoints, ref d);
                for(i=0; i<=npoints-1; i++)
                {
                    for(j=i+1; j<=npoints-1; j++)
                    {
                        if( disttype==20 )
                        {
                            v = 1-d[i,j];
                        }
                        else
                        {
                            v = 1-Math.Abs(d[i,j]);
                        }
                        v = Math.Max(v, 0.0);
                        d[i,j] = v;
                        d[j,i] = v;
                    }
                }
                for(i=0; i<=npoints-1; i++)
                {
                    d[i,i] = 0.0;
                }
                return;
            }
            alglib.ap.assert(false);
        }