corr.src

来自「没有说明」· SRC 代码 · 共 178 行

SRC
178
字号
/*
** corr.src
** (C) Copyright 1988-1998 by Aptech Systems, Inc.
** All Rights Reserved.
**
** This Software Product is PROPRIETARY SOURCE CODE OF APTECH
** SYSTEMS, INC.    This File Header must accompany all files using
** any portion, in whole or in part, of this Source Code.   In
** addition, the right to create such files is strictly limited by
** Section 2.A. of the GAUSS Applications License Agreement
** accompanying this Software Product.
**
** If you wish to distribute any portion of the proprietary Source
** Code, in whole or in part, you must first obtain written
** permission from Aptech Systems.
**
**  Format            Purpose                                        Line
** =========================================================================
** cx = CORRM(m);     correlation matrix from moment matrix.          27
** cx = CORRVC(vc);   correlation matrix from var-covar matrix.       61
** cx = CORRX(x);     correlation matrix from data matrix.            89
** vc = VCM(m);       var-covar matrix from moment matrix.           119
** vc = VCX(x);       var-covar matrix from data matrix.             154
*/

/*
**> corrm
**
**  Purpose:    Computes correlation matrix from moment (x'x) matrix.
**
**  Format:     cx = corrm(m);
**
**  Input:      m     KxK moment (x'x) matrix. Constant term MUST have been
**                    the first variable when moment matrix was computed.
**
**  Output:     cx    KxK correlation matrix.
*/

proc corrm(m);
    local cc,xx,n,xbar,vv,vc,std;
    /* check for complex input */
    if iscplx(m);
        if hasimag(m);
            errorlog "ERROR: Not implemented for complex matrices.";
            end;
        else;
            m = real(m);
        endif;
    endif;
    cc = seqa(2,1,cols(m)-1);
    xx = m[cc,cc];                  /* Pull out K-1xK-1 submatrix */
    n = m[1,1];                     /* Number of observations. */
    xbar = m[cc,1]/n;               /* Vector of means */
    vv = xbar*xbar';
    vc = (xx-n*vv)/(n-1);           /* VC matrix */
    std = sqrt(diag(vc));
    retp( vc./(std.*std') );
endp;

/*
**> corrvc
**
**  Purpose:    Computes a correlation matrix from a
**              variance-covariance matrix.
**
**  Format:     cx = corrvc(vc);
**
**  Input:      vc    KxK variance-covariance matrix (of data or parameters).
**
**  Output:     cx    KxK correlation matrix.
*/

proc corrvc(vc);
    local std;
    /* check for complex input */
    if iscplx(vc);
        if hasimag(vc);
            errorlog "ERROR: Not implemented for complex matrices.";
            end;
        else;
            vc = real(vc);
        endif;
    endif;
    std = sqrt(diag(vc));
    retp( vc./(std.*std') );
endp;

/*
**> corrx
**
**  Purpose:    Computes correlation matrix.
**
**  Format:     cx = corrx(x);
**
**  Input:      x     NxK matrix of data.
**
**  Output:     cx    KxK correlation matrix of columns of x.
*/

proc corrx(x);
    local vc, std;
    /* check for complex input */
    if iscplx(x);
        if hasimag(x);
            errorlog "ERROR: Not implemented for complex matrices.";
            end;
        else;
            x = real(x);
        endif;
    endif;
    vc = moment(x-meanc(x)',0)/(rows(x)-1);         /* variance-covariance
                                                    :: matrix
                                                    */
    std = sqrt(diag(vc));           /* standard deviations */
    retp( vc./(std.*std') );
endp;

/*
**> vcm
**
**  Purpose:    Computes a variance-covariance matrix from
**              a moment matrix.
**
**  Format:     vc = vcm(m);
**
**  Input:      m    KxK moment (x'x) matrix. Constant term MUST have
**                   been first variable when moment matrix was computed.
**
**  Output:     vc   KxK variance-covariance matrix.
*/

proc vcm(m);
    local cc, xx, n, xbar, vv;

    /* check for complex input */
    if iscplx(m);
        if hasimag(m);
            errorlog "ERROR: Not implemented for complex matrices.";
            end;
        else;
            m = real(m);
        endif;
    endif;

    cc = seqa(2,1,cols(m)-1);
    xx = m[cc,cc];                  /* Pull out K-1xK-1 submatrix */
    n = m[1,1];                     /* Number of observations. */
    xbar = m[cc,1]/n;               /* Vector of means */
    vv = xbar*xbar';
    retp( (xx-n*(vv))/(n-1) );
endp;

/*
**> vcx
**
**  Purpose:    Computes a variance-covariance matrix from a data matrix.
**
**  Format:     vc = vcx(x);
**
**  Input:      x    NxK matrix of data.
**
**  Output:     vc   KxK var-covar matrix.
*/

proc vcx(x);
    /* check for complex input */
    if iscplx(x);
        if hasimag(x);
            errorlog "ERROR: Not implemented for complex matrices.";
            end;
        else;
            x = real(x);
        endif;
    endif;

    retp( moment( x-meanc(x)',0) / ( rows(x) - 1 ) );
endp;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?