35 const arma::Mat<T> &a,
36 const arma::Mat<T> &x1,
37 const arma::Mat<T> &x2,
38 const arma::Mat<T> &x3,
39 const arma::Col<T> ¶meter,
40 const arma::Col<T> &weights) :
Target(y, a, x1, x2, x3, parameter) {
41 this->weights(weights);
53 for (
unsigned i=0; i < alpha.n_elem; i++) {
54 alpha[i] = parameter[i];
57 for (
unsigned i=0; i < beta.n_elem; i++) {
58 beta[i] = parameter[i+pos];
61 if (parameter.n_elem == pos+gamma.n_elem) {
62 for (
unsigned i=0; i < gamma.n_elem; i++) {
63 gamma[i] = parameter[i+pos];
71 const arma::Mat<T> &a,
72 const arma::Mat<T> &x1,
73 const arma::Mat<T> &x2,
74 const arma::Mat<T> &x3,
75 const arma::Col<T> ¶meter) {
76 this->update_data(y, a, x1, x2, x3);
77 alpha = arma::Col<T>(x1.n_cols);
78 beta = arma::Col<T>(x2.n_cols);
79 gamma = arma::Col<T>(x3.n_cols);
85 const arma::Mat<T> &a,
86 const arma::Mat<T> &x1,
87 const arma::Mat<T> &x2,
88 const arma::Col<T> ¶meter,
89 const arma::Col<T> &weights) :
Target(y, a, x1, x2, x2, parameter) {
90 this->weights(weights);
95 const arma::Mat<T> &a,
96 const arma::Mat<T> &x1,
97 const arma::Mat<T> &x2,
98 const arma::Col<T> ¶meter) :
Target(y, a, x1, x2, x2, parameter) {
103 template <
typename T>
111 this->propensity = target::expit(this->propensity);
124 template <
typename T>
126 arma::Mat<T> res(this->Y().n_elem, 5);
127 res.col(1) = this->p(0);
128 res.col(2) = this->p(1);
129 res.col(0) = (1-this->A())%res.col(1) + this->A()%res.col(2);
130 res.col(3) = this->target;
131 res.col(4) = this->nuisance;
141 template <
typename T>
148 if (indiv)
return(logl);
149 return(sum(logl, 0));
169 template <
typename T>
171 const arma::Col<T> &propensity) {
172 arma::Col<T> p0 = this->p(0);
173 for (
unsigned i=0; i < alpha.n_elem; i++) this->alpha[i] = alpha[i];
174 calculate(
true,
false,
false);
176 arma::Col<T> H = this->H();
177 arma::Col<T> S = (this->A()-propensity)%(H-p0);
178 S %= this->weights();
179 arma::Mat<T> U(S.n_elem, alpha.n_elem);
180 for (
unsigned i=0; i < alpha.n_elem; i++) {
181 U.col(i) = S % this->X1().col(i);
186 template <
typename T>
188 calculate(
false,
false,
true);
199 template <
typename T>
201 arma::Col<T> phat = this->p(0) % (1-this->A().col(0)) +
202 this->p(1) % this->A().col(0);
203 arma::Mat<T> dp_dlp = dp();
204 arma::Col<T> S = (this->Y()-phat) / (phat%(1-phat));
205 S %= this->weights();
206 for (
unsigned i=0; i < dp_dlp.n_cols; i++)
208 if (indiv)
return(dp_dlp);
209 return(sum(dp_dlp, 0));
213 template <
typename T>
219 this->nuisance = exp(this->nuisance);
224 template <
typename T>
226 const arma::Mat<T> &a,
227 const arma::Mat<T> &x1,
228 const arma::Mat<T> &x2,
229 const arma::Mat<T> &x3,
230 const arma::Col<T> ¶meter,
231 const arma::Col<T> &weights) :
236 template <
typename T>
238 const arma::Mat<T> &a,
239 const arma::Mat<T> &x1,
240 const arma::Mat<T> &x2,
241 const arma::Mat<T> &x3,
242 const arma::Col<T> ¶meter,
243 const arma::Col<T> &weights) :
248 template <
typename T>
252 this->target = tanh(this->target);
253 if (target || nuisance)
254 this->pr =
rd2prob(this->target, this->nuisance);
257 template <
typename T>
261 this->target = exp(this->target);
262 if (target || nuisance)
263 this->pr =
rr2prob(this->target, this->nuisance);
266 template <
typename T>
271 arma::Col<T> dp0_rd = -s0/(s0 + s1);
272 arma::Col<T> d0 = (dp0_rd + a) % (1 - rd()%rd());
274 for (
unsigned i=0; i < d_alpha.n_cols; i++) d_alpha.col(i) %= d0;
275 arma::Col<T> dp0_op = s0 % s1/(s0 + s1);
277 for (
unsigned i=0; i < d_beta.n_cols; i++) d_beta.col(i) %= dp0_op;
278 return ( join_rows(d_alpha, d_beta) );
281 template <
typename T>
285 arma::Col<T> tmp = 1 - a + a%rr();
290 for (
unsigned i=0; i < d_alpha.n_cols; i++) d_alpha.col(i) %= dp_rr;
295 for (
unsigned i=0; i < d_beta.n_cols; i++) d_beta.col(i) %= dp_op;
296 return ( join_rows(d_alpha, d_beta) );
306 template <
typename T>
307 arma::Mat<T>
rd2prob(
const arma::Col<T> &rd,
const arma::Col<T> &op) {
308 arma::Col<T> a = op-1;
309 arma::Col<T> b = -op%(2-rd)-rd;
310 arma::Col<T> p0 = (-b - sqrt(b%b - 4*op%(1.0 - rd)%a)) / (2*a);
311 for (
unsigned i=0; i < p0.size(); i++) {
312 if (std::abs(op[i]-1.0) < 1e-16) p0[i] = 0.5*(1.0 - rd[i]);
314 arma::Mat<T> res(rd.n_elem, 2);
327 template <
typename T>
328 arma::Mat<T>
rr2prob(
const arma::Col<T> &rr,
const arma::Col<T> &op) {
329 arma::Mat<T> res(rr.n_elem, 2);
330 arma::Col<T> a = rr%(1.0 - op);
331 arma::Col<T> b = op%(1.0 + rr);
332 arma::Col<T> p0 = (-b + sqrt(b%b + 4*a%op))/(2*a);
333 for (
unsigned i=0; i < p0.size(); i++) {
334 if (std::abs(op[i]-1.0) < 1e-16) p0[i] = (T)1/(1.0 + rr[i]);
358 const arma::cx_mat &a,
359 const arma::cx_mat &x2,
360 const arma::cx_mat &x3,
361 const arma::cx_vec ¶meter,
362 const arma::cx_vec &weights,
364 Target<cx_dbl>(y, a, arma::cx_mat(1, 1), x2, x3, parameter, weights) {
366 ACE::calculate(
true,
true);
373 const arma::vec ¶meter,
374 const arma::vec &weights,
376 ACE(arma::conv_to<arma::cx_vec>::from(y),
377 arma::conv_to<arma::cx_vec>::from(a),
378 arma::conv_to<arma::cx_mat>::from(x2),
379 arma::conv_to<arma::cx_mat>::from(x3),
380 arma::conv_to<arma::cx_vec>::from(parameter),
381 arma::conv_to<arma::cx_vec>::from(weights),
384 void ACE::calculate(
bool target,
bool nuisance,
bool propensity) {
386 if (nuisance && this->binary) {
387 this->nuisance = target::expit(this->nuisance);
391 arma::cx_mat ACE::est(
bool indiv,
const cx_dbl &value) {
392 arma::cx_vec a1 = A().as_col();
393 for (
unsigned i=0; i < a1.n_elem; i++) a1[i] = (a1[i] == value);
394 arma::cx_vec U = Y()%a1/(propensity) -
395 (a1-propensity)%nuisance/(propensity) - alpha[0];
397 if (indiv)
return( std::move(U) );
401 arma::cx_mat ACE::est(arma::cx_vec par,
bool indiv,
const cx_dbl &value) {
404 return(ACE::est(indiv, value));
407 void ACE::update_par(arma::cx_vec par) {
411 void ACE::update_par(arma::vec par) {
412 arma::cx_vec parc = arma::conv_to<arma::cx_vec>::from(par);
424 unsigned n1 = this->alpha.n_elem;
425 unsigned n2 = this->beta.n_elem;
426 unsigned n3 = this->gamma.n_elem;
427 unsigned p = n1+n2+n3;
430 res[0] = -sum(real(weights()));
431 double eps = std::numeric_limits<float>::denorm_min();
432 cx_dbl delta(0, eps);
436 for (
unsigned i=0; i < n2; i++) {
438 this->beta[i] += delta;
439 ACE::calculate(
true,
true,
false);
441 res[i+pos] = std::imag(ACE::est(
false, value)[0])/eps;
444 for (
unsigned i=0; i < n3; i++) {
445 tmp = this->gamma[i];
446 this->gamma[i] += delta;
447 ACE::calculate(i == 0,
true);
448 this->gamma[i] = tmp;
449 res[i+pos] = std::imag(ACE::est(
false, value)[0])/eps;
451 return( std::move(res) );
arma::mat deriv(const cx_dbl &value=1)
deriv Calculate derivative of estimating function for AIPW estimator
Classes for targeted inference models.
virtual arma::Mat< T > est(arma::Col< T > alpha, const arma::Col< T > &propensity)
Estimating function for double robust estimator.
void update_par(const arma::Col< T > ¶meter)
update_par -
arma::Mat< T > rr2prob(const arma::Col< T > &rd, const arma::Col< T > &op)
rr2prob - Computes risk probabilities given exposure 0 or 1.
arma::Mat< T > rd2prob(const arma::Col< T > &rd, const arma::Col< T > &op)
rd2prob - Computes risk probabilities given exposure 0 or 1.
Utility functions for Generalized Linear Models.
ACE(const arma::cx_vec &y, const arma::cx_mat &a, const arma::cx_mat &x2, const arma::cx_mat &x3, const arma::cx_vec ¶meter, const arma::cx_vec &weights, bool binary=true)
ACE AIPW for Average Causal Effects.
virtual arma::Col< T > loglik(bool indiv=false)
log likelihood
virtual arma::Mat< T > score(bool indiv=false)
score function
virtual arma::Mat< T > pa()
Calculate risk probabilities conditional on exposure.