Switch to unified view

a b/partyMod/src/TestStatistic.c
1
2
/**
3
    Test statistics for conditional inference
4
    *\file TestStatistic.c
5
    *\author $Author$
6
    *\date $Date$
7
*/
8
                
9
#include "party.h"
10
11
12
/**
13
    Standardizes a statistic t of length pq with mean mu and covariance Sigma
14
    for variances > tol \n
15
    *\param t the vector of statistics
16
    *\param mu expectations
17
    *\param Sigma covariance matrix
18
    *\param pq dimension of t
19
    *\param tol tolerance for variances
20
    *\param ans return value; a pointer to a REALSXP-vector of length pq
21
*/
22
23
void C_standardize(const double *t, const double *mu, const double *Sigma, 
24
                   int pq, double tol, double *ans) {
25
                   
26
    int i;
27
    double sd;
28
    
29
    for (i = 0; i < pq; i++) {
30
        sd = Sigma[i*pq + i]; 
31
        if (sd > tol)
32
            ans[i] = (t[i] - mu[i])/sqrt(sd);
33
        else
34
            ans[i] = 0.0;
35
    }
36
}
37
38
39
/**
40
    Absolute values of standardized statistics
41
    *\param t the vector of statistics
42
    *\param mu expectations
43
    *\param Sigma covariance matrix
44
    *\param pq dimension of t
45
    *\param tol tolerance for variances
46
    *\param ans return value; a pointer to a REALSXP-vector of length pq
47
*/
48
49
void C_absstandardize(const double *t, const double *mu, const double *Sigma,
50
                      int pq, double tol, double *ans) {
51
                      
52
    C_standardize(t, mu, Sigma, pq, tol, ans);
53
    C_abs(ans, pq);
54
}
55
56
57
/**
58
    Maximum absolute values of standardized statistics
59
    *\param t the vector of statistics
60
    *\param mu expectations
61
    *\param Sigma covariance matrix
62
    *\param pq dimension of t
63
    *\param tol tolerance for variances
64
*/
65
66
double C_maxabsTestStatistic(const double *t, const double *mu, const double *Sigma,
67
                             int pq, double tol) {
68
                           
69
     double *mem, ans;
70
     
71
     mem = Calloc(pq, double);
72
     C_absstandardize(t, mu, Sigma, pq, tol, mem);
73
     ans = C_max(mem, pq);
74
     Free(mem);
75
     return(ans);
76
}
77
78
79
/**
80
    R-interface to C_maxabsTestStatistic
81
    *\param t the vector of statistics
82
    *\param mu expectations
83
    *\param Sigma covariance matrix
84
    *\param tol tolerance for variances
85
*/
86
87
SEXP R_maxabsTestStatistic(SEXP t, SEXP mu, SEXP Sigma, SEXP tol) {
88
89
     SEXP ans;
90
     int pq;
91
     
92
     pq = LENGTH(t);
93
     
94
     PROTECT(ans = allocVector(REALSXP, 1));
95
     REAL(ans)[0] = C_maxabsTestStatistic(REAL(t), REAL(mu), REAL(Sigma), pq, 
96
                                          REAL(tol)[0]);
97
     UNPROTECT(1);
98
     return(ans);
99
}
100
101
102
/**
103
    Quadratic form t(t - mu) SigmaPlus (t - mu) \n
104
    *\param t the vector of statistics
105
    *\param mu expectations
106
    *\param SigmaPlus Moore-Penrose inverse
107
    *\param pq dimension of t
108
*/
109
                                
110
double C_quadformTestStatistic(const double *t, const double *mu, 
111
                               const double *SigmaPlus, int pq) {
112
113
    int i, j;
114
    double quadform = 0.0, *tmmu, *tmmuSigmaPlus;
115
116
    tmmu = Calloc(pq, double);
117
    for (i = 0; i < pq; i++)
118
        tmmu[i] = t[i] - mu[i];
119
    
120
    tmmuSigmaPlus = Calloc(pq, double);
121
    for (i = 0; i < pq; i++)  {
122
        tmmuSigmaPlus[i] = 0.0;
123
        for (j = 0; j < pq; j++)
124
            tmmuSigmaPlus[i] += tmmu[j] * SigmaPlus[i * pq + j];
125
        quadform += tmmuSigmaPlus[i] * tmmu[i];
126
    }
127
128
    Free(tmmu); Free(tmmuSigmaPlus);
129
    return(quadform);
130
}
131
132
133
/**
134
    R-interface to C_quadformTestStatistic \n
135
    *\param t the vector of statistics
136
    *\param mu expectations
137
    *\param SigmaPlus Moore-Penrose inverse
138
*/
139
    
140
SEXP R_quadformTestStatistic(SEXP t, SEXP mu, SEXP SigmaPlus) {
141
142
    SEXP ans;
143
    int pq;
144
    
145
    pq = LENGTH(t);
146
    PROTECT(ans = allocVector(REALSXP, 1));
147
    REAL(ans)[0] = C_quadformTestStatistic(REAL(t), 
148
                                           REAL(mu), REAL(SigmaPlus), pq);
149
    UNPROTECT(1);
150
    return(ans);
151
}