-
Notifications
You must be signed in to change notification settings - Fork 1
/
Hhat.cpp
58 lines (43 loc) · 1.38 KB
/
Hhat.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
#include <Rcpp.h>
using namespace Rcpp;
double ker(double x){
double anss = -x/sqrt(2*PI)*exp(-pow(x,2)/2);
return(anss);
}
double score_fnc(NumericVector y, NumericVector x1, NumericVector x2, double beta){
int n = y.size();
double obj = 0.0;
for (int i=0; i < n; i++){
obj += (2.0*y[i]-1.0)*( x1[i]+x2[i]*beta >= 0 ) ;
}
obj = obj/n;
return(obj);
}
double hat_H_ms(NumericVector y, NumericVector x1, NumericVector x2, double bw, double beta){
int n = y.size();
double out = 0.0;
for (int i = 0; i < n; i++){
out += (2.0*y[i] - 1.0)*ker( (x1[i] + x2[i]*beta)/bw )/pow(bw, 2)*pow(x2[i], 2);
}
out = out/n;
return(out);
}
//[[Rcpp::export]]
NumericVector H_ms(NumericVector y, NumericVector x1, NumericVector x2, NumericVector bws, double beta){
int lenh = bws.size();
NumericVector out(lenh,0.0);
for (int i =0; i < lenh; i++){
out[i] = hat_H_ms(y, x1, x2, bws[i], beta);
}
return(out);
}
//[[Rcpp::export]]
NumericVector H(NumericVector y, NumericVector x1, NumericVector x2, NumericVector grds, double beta){
int len = grds.size();
NumericVector out(len, 0.0);
for (int i = 0; i < len; i++){
out[i] = ( score_fnc(y,x1,x2,beta + grds[i]) + score_fnc(y,x1,x2,beta - grds[i])
- 2*score_fnc(y,x1,x2,beta) )/pow(grds[i], 2);
}
return(out);
}