-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.cpp
118 lines (108 loc) · 1.81 KB
/
helper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double meanC(NumericVector x) {
int n = x.size();
double total = 0;
for(int i = 0; i < n; ++i) {
total += x[i] / n;
}
return total;
}
// [[Rcpp::export]]
double lsa ( NumericVector x , NumericVector y , int n , int d , int D )
{
double Pmax = 0.0 ;
double Nmax = 0.0 ;
// P[i][0] , N[i][0]
for( int i = 0 ; i <= d ; i ++ )
{
double Ptemp = 0.0 ;
double Ntemp = 0.0 ;
for( int k = 0 ; k + i < n ; k ++ )
{
if( Ptemp + x[i+k] * y[k] > 0.0 )
{
Ptemp = Ptemp + x[i+k] * y[k] ;
}
if( Ntemp - x[i+k] * y[k] > 0.0 )
{
Ntemp = Ntemp - x[i+k] * y[k] ;
}
if( Ptemp > Pmax )
{
Pmax = Ptemp ;
if( Pmax > Nmax )
{
D = i ;
}
}
if( Ntemp > Nmax )
{
Nmax = Ntemp ;
if( Nmax > Pmax )
{
D = i ;
}
}
}
}
// P[0][j] , N[0][j]
for( int j = 1 ; j <= d ; j ++ )
{
double Ptemp = 0.0 ;
double Ntemp = 0.0 ;
for( int k = 0 ; k + j < n ; k ++ )
{
if( Ptemp + x[k] * y[j+k] > 0.0 )
{
Ptemp = Ptemp + x[k] * y[j+k] ;
}
if( Ntemp - x[k] * y[j+k] > 0.0 )
{
Ntemp = Ntemp - x[k] * y[j+k] ;
}
if( Ptemp > Pmax )
{
Pmax = Ptemp ;
D = -j ;
}
if( Ntemp > Nmax )
{
Nmax = Ntemp ;
D = -j ;
}
}
}
if( Pmax > Nmax )
{
return( Pmax / n ) ;
}
else
{
return ( - Nmax / n ) ;
}
return( 0.0 ) ;
}
// [[Rcpp::export]]
NumericVector standardize1 ( NumericVector v )
{
int n = v.size();
double mean = 0.0 ;
for( int i = 0 ; i < n ; i ++ )
{
mean = mean + v[i] ;
}
mean = mean / ((double) n ) ;
double sd = 0.0 ;
for( int i = 0 ; i < n ; i ++ )
{
sd = sd + pow( v[i] - mean , 2 ) ;
}
sd = sqrt( sd / ( n - 1 ) ) ;
for( int i = 0 ; i < n ; i ++ )
{
v[i] = ( v[i] - mean ) / sd ;
}
return( v );
}