void CDCStatsticsMethod::conduct_cdc_screening(std::vector<std::vector<double>> &x, std::vector<std::vector<double>> &distance_y, std::vector<std::vector<double>> &kernel, double index) { std::vector<std::vector<double>> distance_x; ConditionDistanceCovarianceStats conditionDistanceCovarianceStats = ConditionDistanceCovarianceStats(distance_x, distance_y, kernel, 2); for (std::vector<double> univariate_x : x) { distance_x = Euclidean_distance(univariate_x, index); conditionDistanceCovarianceStats.setDistance_x(distance_x); conditionDistanceCovarianceStats.compute_stats(); this->cdc_statistic.push_back(conditionDistanceCovarianceStats.getCondition_distance_covariance_stats()); } }
EXPORT double DTW::Distance(const std::vector< std::vector<double> > &actual, const std::vector< std::vector<double> > &training) { int m = actual.size(); int n = training.size(); double cost[m][n]; cost[0][0] = Euclidean_distance(actual[0], training[0]); for(int i = 1; i < m; i++) cost[i][0] = cost[i-1][0] + Euclidean::Euclidean_distance(actual[i], training[0]); for(int j = 1; j < n; j++) cost[0][j] = cost[0][j-1] + Euclidean::Euclidean_distance(actual[0], training[j]); for(int i = 1; i < m; i++) for(int j = 1; j < n; j++) cost[i][j] = std::min(cost[i-1][j], std::min(cost[i][j-1], cost[i-1][j-1])) + Euclidean::Euclidean_distance(actual[i],training[j]); return cost[m-1][n-1]; }
void dCOVtest(double *x, double *y, int *byrow, int *dims, double *index, double *reps, double *DCOV, double *pval) { /* computes dCov(x,y), dCor(x,y), dVar(x), dVar(y) V-statistic is n*dCov^2 where n*dCov^2 --> Q dims[0] = n (sample size) dims[1] = p (dimension of X) dims[2] = q (dimension of Y) dims[3] = dst (logical, TRUE if x, y are distances) dims[4] = R (number of replicates) index : exponent for distance DCOV : vector [dCov, dCor, dVar(x), dVar(y), mean(A), mean(B)] */ int i, j, k, n, n2, p, q, r, J, K, M, R; int dst; int* perm; double **Dx, **Dy, **A, **B; double dcov, V; n = dims[0]; p = dims[1]; q = dims[2]; dst = dims[3]; R = dims[4]; if (*byrow == FALSE) { /* avoid this step: use as.double(t(x)) in R */ roworder(x, byrow, n, p); *byrow = FALSE; /* false for y */ roworder(y, byrow, n, q); } /* critical to pass correct flag dst from R */ Dx = alloc_matrix(n, n); Dy = alloc_matrix(n, n); if (dst) { vector2matrix(x, Dx, n, n, 1); vector2matrix(y, Dy, n, n, 1); } else { Euclidean_distance(x, Dx, n, p); Euclidean_distance(y, Dy, n, q); } index_distance(Dx, n, *index); index_distance(Dy, n, *index); A = alloc_matrix(n, n); B = alloc_matrix(n, n); Akl(Dx, A, n); Akl(Dy, B, n); free_matrix(Dx, n, n); free_matrix(Dy, n, n); n2 = ((double) n) * n; /* compute dCov(x,y), dVar(x), dVar(y) */ for (k=0; k<4; k++) DCOV[k] = 0.0; for (k=0; k<n; k++) for (j=0; j<n; j++) { DCOV[0] += A[k][j]*B[k][j]; DCOV[2] += A[k][j]*A[k][j]; DCOV[3] += B[k][j]*B[k][j]; } for (k=0; k<4; k++) { DCOV[k] /= n2; if (DCOV[k] > 0) DCOV[k] = sqrt(DCOV[k]); else DCOV[k] = 0.0; } /* compute dCor(x, y) */ V = DCOV[2]*DCOV[3]; if (V > DBL_EPSILON) DCOV[1] = DCOV[0] / sqrt(V); else DCOV[1] = 0.0; if (R > 0) { /* compute the replicates */ if (DCOV[1] > 0.0) { perm = Calloc(n, int); M = 0; for (i=0; i<n; i++) perm[i] = i; for (r=0; r<R; r++) { permute(perm, n); dcov = 0.0; for (k=0; k<n; k++) { K = perm[k]; for (j=0; j<n; j++) { J = perm[j]; dcov += A[k][j]*B[K][J]; } } dcov /= n2; dcov = sqrt(dcov); reps[r] = dcov; if (dcov >= DCOV[0]) M++; } *pval = (double) (M+1) / (double) (R+1); Free(perm); } else {
void undCOV(double *x, double *y, int *byrow, int *dims, double *index, double *idx, double *DCOV) { /* computes dCov(x,y), dCor(x,y), dVar(x), dVar(y) V-statistic is n*dCov^2 where n*dCov^2 --> Q dims[0] = n (sample size) dims[1] = p (dimension of X) dims[2] = q (dimension of Y) dims[3] = dst (logical, TRUE if x, y are distances) index : exponent for distance idx : index vector, a permutation of sample indices DCOV : vector [dCov, dCor, dVar(x), dVar(y)] */ int j, k, n, n2, p, q, dst; double **Dx, **Dy, **A, **B; double V; n = dims[0]; p = dims[1]; q = dims[2]; dst = dims[3]; if (*byrow == FALSE) { /* avoid this step: use as.double(t(x)) in R */ roworder(x, byrow, n, p); *byrow = FALSE; /* false for y */ roworder(y, byrow, n, q); } /*So why do you write codes above?*/ /* critical to pass correct flag dst from R */ Dx = alloc_matrix(n, n);//n: Dy = alloc_matrix(n, n); if (dst) { vector2matrix(x, Dx, n, n, 1); vector2matrix(y, Dy, n, n, 1); } else { Euclidean_distance(x, Dx, n, p); Euclidean_distance(y, Dy, n, q); } index_distance(Dx, n, *index); index_distance(Dy, n, *index); A = alloc_matrix(n, n); B = alloc_matrix(n, n); Akl(Dx, A, n); Akl(Dy, B, n); free_matrix(Dx, n, n); free_matrix(Dy, n, n); n2 = ((double) n) * n; /* compute dCov(x,y), dVar(x), dVar(y) */ for (k=0; k<4; k++) DCOV[k] = 0.0; for (k=0; k<n; k++) for (j=0; j<n; j++) { //What I have changed. if(k != j){ DCOV[0] += A[k][j]*B[k][j]; DCOV[2] += A[k][j]*A[k][j]; DCOV[3] += B[k][j]*B[k][j]; }else{ DCOV[0] -= A[k][j]*B[k][j] * (2 / (n-2)); DCOV[2] -= A[k][j]*A[k][j] * (2 / (n-2)); DCOV[3] -= B[k][j]*B[k][j] * (2 / (n-2)); } } for (k=0; k<4; k++) { DCOV[k] /= ((double) n) * (n - 3); if (DCOV[k] > 0) DCOV[k] = sqrt(DCOV[k]); else DCOV[k] = 0.0; } /* compute dCor(x, y) */ V = DCOV[2]*DCOV[3]; if (V > DBL_EPSILON) DCOV[1] = DCOV[0] / sqrt(V); else DCOV[1] = 0.0; free_matrix(A, n, n); free_matrix(B, n, n); return; }
void indepE(double *x, double *y, int *byrow, int *dims, double *Istat) { /* E statistic for multiv. indep. of X in R^p and Y in R^q statistic returned is I_n^2 [nI_n^2 has a limit dist under indep] dims[0] = n (sample size) dims[1] = p (dimension of X) dims[2] = q (dimension of Y) Istat : the statistic I_n (normalized) */ int i, j, k, m, n, p, q; double Cx, Cy, Cz, C3, C4, n2, n3, n4, v; double **D2x, **D2y; n = dims[0]; p = dims[1]; q = dims[2]; if (*byrow == FALSE) { /* avoid this step: use as.double(t(x)) in R */ roworder(x, byrow, n, p); *byrow = FALSE; /* false for y */ roworder(y, byrow, n, q); } D2x = alloc_matrix(n, n); D2y = alloc_matrix(n, n); Euclidean_distance(x, D2x, n, p); Euclidean_distance(y, D2y, n, q); Cx = Cy = Cz = C3 = C4 = 0.0; n2 = ((double) n) * n; n3 = n2 * n; n4 = n2 * n2; /* compute observed test statistic */ for (i=0; i<n; i++) { for (j=0; j<i; j++) { Cx += (D2x[i][j]); Cy += (D2y[i][j]); Cz += sqrt(D2x[i][j]*D2x[i][j] + D2y[i][j]*D2y[i][j]); } } Cx = 2.0 * Cx / n2; Cy = 2.0 * Cy / n2; Cz = 2.0 * Cz / n2; for (i=0; i<n; i++) { for (j=0; j<n; j++) { for (k=0; k<n; k++) { C3 += sqrt(D2x[k][i]*D2x[k][i] + D2y[k][j]*D2y[k][j]); for (m=0; m<n; m++) C4 += sqrt(D2x[i][k]*D2x[i][k] + D2y[j][m]*D2y[j][m]); } } } C3 /= n3; C4 /= n4; v = Cx + Cy - C4; *Istat = (2.0 * C3 - Cz - C4) / v; free_matrix(D2x, n, n); free_matrix(D2y, n, n); return; }