INLA_DIST
Loading...
Searching...
No Matches
PostTheta.h
1#ifndef POST_THETA_H
2#define POST_THETA_H
3
4#include <random>
5#include <vector>
6#include <iostream>
7#include <fstream>
8#include <math.h>
9#include <time.h>
10#include <stdlib.h>
11#include <stdio.h>
12
13#include <omp.h>
14#include "mpi.h"
15
16// std::setwd print out
17#include <iomanip>
18
19#include <Eigen/Dense>
20#include <Eigen/Core>
21#include <Eigen/SparseCholesky>
22//#include <Eigen/CholmodSupport>
23#include <unsupported/Eigen/KroneckerProduct>
24
25//#include "../read_write_functions.h"
26
27//#include "solver_cholmod.h" -> pardiso can do inversion now
28#include "PardisoSolver.h"
29#include "BTASolver.h"
30//#include "BTASolver_dummy.h"
31#include "EigenCholSolver.h"
32
33//#include "Hyperparameters.h"
34
35//#define SMART_GRAD
36
37//#define PRINT_MSG
38//#define PRINT_TIMES
39//#define RECORD_TIMES
40//#define DATA_SYNTHETIC
41
42using namespace Eigen;
43using namespace std;
44
45// typedef Eigen::Matrix<double, Dynamic, Dynamic> Mat;
46typedef Eigen::SparseMatrix<double> SpMat;
47typedef Eigen::SparseMatrix<double, RowMajor> SpRmMat;
48typedef Eigen::VectorXd Vect;
49
50
56
57 private:
58
62 int threads_level2;
63
64
65 int ns;
66 int nt;
67 int nss;
68 int nb;
69 int no;
70 int nst;
71 int nu;
72 int n;
74 size_t nnz_Qst;
75 size_t nnz_Qs;
76
77 int dim_th;
78 int dim_spatial_domain;
79 string manifold;
83 Solver* solverQ;
84 Solver* solverQst;
85
86 int threadID_solverQst;
87 int threadID_solverQ;
88
89 string likelihood;
90 Vect extraCoeffVecLik;
91
92 string solver_type;
93
94 std::string prior;
98 int iter_acc;
99 Vect y;
103 Vector3i dimList;
104#if 0
105 Hyperparameters* theta_prior_test;
106 Hyperparameters* theta_test;
107 //Hyperparameters theta_prior_test = new Hyperparameters(dim_spatial_domain, manifold, dimList, theta_prior_param, theta_prior_param);
108#endif
109 // either Ax or B used
110 SpMat Ax;
113 MatrixXd B;
116 // used in spatial and spatial-temporal case
117 SpMat c0;
118 SpMat g1;
119 SpMat g2;
121 // only used in spatial-temporal case
122 SpMat g3;
123 SpMat M0;
124 SpMat M1;
126 SpMat M2;
128 SpMat Qb;
129 SpMat Qu;
130 SpMat Qst;
131 SpMat Qs;
132 SpMat Qx;
133 SpMat Qxy;
135 double yTy;
136 Vect BTy;
137 Vect AxTy;
138 SpMat AxTAx;
139 Vect mu_initial;
140 Vect mu;
142 // new attempt, store previous mu for each stencil point
143 // figure out later what to do for Hessian ...
144 MatrixXd mu_matrix;
147 Vect t_grad;
148 double min_f_theta;
150 double w_sum;
153 ArrayXi task_to_rank_list_grad;
154
155 MatrixXd G;
158#ifdef SMART_GRAD
159 bool thetaDiff_initialized;
160 VectorXd theta_prev;
161 MatrixXd ThetaDiff;
162#endif
163
164 const bool constr;
165 const MatrixXd Dx;
166 const MatrixXd Dxy;
168 //MatrixXd V; /**< V = Q^-1 * t(D) */
169 //MatrixXd W; /**< W = A*V */
170 //Vect U; /**< U = W^-1 * t(V) */
171
172 const bool validate;
173 const Vect w;
174
175 bool printed_eps_flag = false;
176
177#ifdef RECORD_TIMES
178 std::string log_file_name;
179 // to record times
180 double t_Ftheta_ext;
181 double t_priorHyp;
182 double t_priorLat;
183 double t_priorLatAMat;
184 double t_likel;
185 double t_condLat;
186 double t_condLatAMat;
187 double t_thread_nom;
188 double t_thread_denom;
189#endif
190
191 // these have to always be defined otherwise compile error.
192 // always get measured but not written to file
193 double t_priorLatChol;
194 double t_condLatChol;
195 double t_condLatSolve;
196
197 double t_bfgs_iter;
198
199 public:
210 PostTheta(int ns, int nt, int nb, int no,
211 MatrixXd B, Vect y,
212 Vect theta_prior,
213 //Hyperparameters* theta_prior_test,
214 Vect mu_initial,
215 string likelihood, Vect extraCoeffVecLik,
216 string solver_type,
217 const bool constr, const MatrixXd Dxy,
218 const bool validate, const Vect w);
231 PostTheta(int ns, int nt, int nb, int no,
232 SpMat Ax, Vect y, SpMat c0, SpMat g1, SpMat g2,
233 Vect theta_prior,
234 Vect mu_initial,
235 string likelihood, Vect extraCoeffVecLik,
236 string solver_type,
237 int dim_spatial_domain, string manifold,
238 const bool constr, const MatrixXd Dx, const MatrixXd Dxy,
239 const bool validate, const Vect w);
240
263 PostTheta(int ns, int nt, int nb, int no,
264 SpMat Ax, Vect y, SpMat c0, SpMat g1, SpMat g2, SpMat g3,
265 SpMat M0, SpMat M1, SpMat M2,
266 Vect theta_prior,
267 Vect mu_initial,
268 string likelihood, Vect extraCoeffVecLik,
269 string solver_type,
270 int dim_spatial_domain, string manifold,
271 const bool constr, const MatrixXd Dx, const MatrixXd Dxy,
272 const bool validate, const Vect w);
273
292 PostTheta(int ns, int nt, int nss, int nb, int no,
293 SpMat Ax, Vect y, SpMat c0, SpMat g1, SpMat g2, SpMat g3,
294 SpMat M0, SpMat M1, SpMat M2,
295 Vect theta_prior_param,
296 Vect mu_initial,
297 string likelihood, Vect extraCoeffVecLik,
298 string solver_type,
299 int dim_spatial_domain, string manifold,
300 const bool constr, const MatrixXd Dx, const MatrixXd Dxy,
301 const bool validate, const Vect w);
302
303
311 double operator()(Vect& theta, Vect& grad);
312
313#ifdef DATA_SYNTHETIC
314 double compute_error_bfgs(Vect& theta);
315#endif
316
317 //Hyperparameters create_hp(Vect param, char scale);
318
322 void computeG(Vect& theta);
323
324 int get_fct_count();
325
326 // ============================================================================================ //
327 // CONVERT MODEL PARAMETRISATION TO INTERPRETABLE PARAMETRISATION & VICE VERSA
328
329 void convert_theta2interpret(Vect& theta, Vect& theta_interpret);
330
331 void convert_interpret2theta(Vect& theta_interpret, Vect& theta);
332
343 void convert_theta2interpret_spatTemp(double lgamE, double lgamS, double lgamT, double& sigU, double& ranS, double& ranT);
344
355 void convert_interpret2theta_spatTemp(double sigU, double ranS, double ranT, double& lgamE, double& lgamS, double& lgamT);
356
365 void convert_interpret2theta_spat(double lranS, double lsigU, double& lgamS, double& lgamE);
366
375 void convert_theta2interpret_spat(double lgamS, double lgamE, double& lranS, double& lsigU);
376
377
378 // ============================================================================================ //
379 // FUNCTIONS TO BE CALLED AFTER THE BFGS SOLVER CONVERGED
380
386 void get_mu(Vect& theta, Vect& mu_);
387
388 #if 0
394 void get_mu(Vect& mu);
395 #endif
396
401 Vect get_grad();
402
403 void get_Qprior(Vect theta, SpMat& Qprior);
404
412 MatrixXd get_Covariance(Vect theta, double eps);
413
414 MatrixXd get_Cov_interpret_param(Vect interpret_theta, double eps);
415
416 double f_eval(Vect& theta);
417
424 void get_marginals_f(Vect& theta, Vect& mu, Vect& vars);
425
432 void get_fullFact_marginals_f(Vect& theta, SpMat& Qinv);
433
434 void compute_fullInverseQ(Vect& theta, MatrixXd& Qinv);
435
443 MatrixXd hess_eval(Vect theta, double eps);
444
445 MatrixXd hess_eval_interpret_theta(Vect interpret_theta, double eps);
446
451 void check_pos_def(MatrixXd &hess);
452
453 // ============================================================================================ //
454 // ALL FOLLOWING FUNCTIONS CONTRIBUT TO THE EVALUATION OF F(THETA) & GRADIENT
455
462 double eval_post_theta(Vect& theta, Vect& mu);
463
472 void eval_log_gaussian_prior_hp(Vect& theta_param, Vect& theta_prior_param, double& log_prior);
473
481 void eval_log_pc_prior_hp(double& log_sum, Vect& lambda, Vect& theta_interpret);
482
483 void update_mean_constr(const MatrixXd& D, Vect& e, Vect& sol, MatrixXd& V, MatrixXd& W, MatrixXd& U, Vect& updated_sol);
484
485 void eval_log_dens_constr(Vect& x, Vect& mu, SpMat&Q, double& log_det_Q, const MatrixXd& D, MatrixXd& W, double& val_log_dens);
486
493 void eval_log_prior_lat(Vect& theta, Vect& mu, double &val);
494
501 void eval_likelihood(Vect& theta, Vect& mu, double &log_det, double &val);
502
508 void construct_Q_spatial(Vect& theta, SpMat& Qs);
509
515 void construct_Q_spat_temp(Vect& theta, SpMat& Qst);
516
517 void construct_Qprior(Vect& theta, SpMat& Qx);
518
525 void construct_Q(Vect& theta, Vect& mu, SpMat& Q);
526
532 void construct_b(Vect& theta, Vect &rhs);
533
534 //void update_mean_constr(MatrixXd& D, Vect& e, Vect& sol, MatrixXd& V, MatrixXd& W);
535
544 void eval_denominator(Vect& theta, double& val, SpMat& Q, Vect& rhs, Vect& mu);
545
546 // ============================================================================================ //
547 // INNER ITERATION & everything that is needed for it
548
554 double cond_LogPriorLat(SpMat& Qprior, Vect& x);
555
560 double cond_LogPoisLik(Vect& eta);
561
566 double cond_negLogPoisLik(Vect& eta);
567
572 Vect grad_cond_negLogPoisLik(Vect& eta);
573
578 Vect diagHess_cond_negLogPoisLik(Vect& eta);
579
580
586 double cond_negLogPois(SpMat& Qprior, Vect& x);
587
592 void link_f_sigmoid(Vect& x, Vect& sigmoidX);
593
599 double cond_negLogBinomLik(Vect& eta);
600
607 double cond_negLogBinom(SpMat& Qprior, Vect& x);
608
616 double cond_negLogDist(SpMat &Qprior, Vect& x, function<double(Vect&, Vect&)> lik_func);
617
624 void FD_gradient(Vect& eta, Vect& grad);
625
632 void FD_diag_hessian(Vect& eta, Vect& diag_hess);
633
641 void NewtonIter(Vect& theta, Vect& x, SpMat& Q, double& log_det);
642
643
644 // measure times within each iterationstd::string file_name, int& iter_count, double& t_Ftheta_ext, double& t_priorHyp,
645 void record_times(std::string file_name, int iter_count, double t_Ftheta_ext, double t_thread_nom, double t_priorHyp,
646 double t_priorLat, double t_priorLatAMat, double t_priorLatChol, double t_likel,
647 double t_thread_denom, double t_condLat, double t_condLatAMat, double t_condLatChol, double t_condLatSolve);
648
649
653 ~PostTheta();
654
655};
656
657#endif
658
659
Definition Hyperparameters.h:26
Computes the Posterior of the hyperparameters theta.
Definition PostTheta.h:55
int dim_grad_loop
Definition PostTheta.h:80
int nst
Definition PostTheta.h:70
SpMat M0
Definition PostTheta.h:123
SpMat Qxy
Definition PostTheta.h:133
void convert_interpret2theta_spat(double lranS, double lsigU, double &lgamS, double &lgamE)
convert hyperparameters theta from the interpretable parametrisation to the model parametrisation ie....
Definition PostTheta.cpp:1430
int n
Definition PostTheta.h:72
void check_pos_def(MatrixXd &hess)
check if Hessian positive definite (matrix assumed to be dense & small since dim(theta) small)
Definition PostTheta.cpp:2141
void link_f_sigmoid(Vect &x, Vect &sigmoidX)
link function. vectorized evaluation of sigmoid function for each entry
Definition PostTheta.cpp:3384
void construct_Q_spatial(Vect &theta, SpMat &Qs)
spatial model : SPDE discretisation – matrix construction
Definition PostTheta.cpp:2864
void computeG(Vect &theta)
overwriting G every time, not explicitly listed, better way to do this? needs to be stored after ever...
MatrixXd hess_eval(Vect theta, double eps)
computes the hessian at theta using second order finite difference. Is used be get_Covariance.
Definition PostTheta.cpp:1660
void eval_log_prior_lat(Vect &theta, Vect &mu, double &val)
evaluate log prior of random effects
Definition PostTheta.cpp:2670
double min_f_theta
Definition PostTheta.h:148
SpMat AxTAx
Definition PostTheta.h:138
double operator()(Vect &theta, Vect &grad)
structure required by BFGS solver, requires : theta, gradient theta
Definition PostTheta.cpp:834
double eval_post_theta(Vect &theta, Vect &mu)
Core function. Evaluate posterior of theta. mu are latent parameters.
Definition PostTheta.cpp:2172
string manifold
Definition PostTheta.h:79
Vect theta_prior_param
Definition PostTheta.h:100
int ns
Definition PostTheta.h:65
int threads_level1
Definition PostTheta.h:61
double cond_negLogPois(SpMat &Qprior, Vect &x)
evaluate negative condiational log Poisson + Gaussian prior
Definition PostTheta.cpp:3371
void get_marginals_f(Vect &theta, Vect &mu, Vect &vars)
Compute the marginal variances of the latent parameters at theta. Using selected inversion procedure.
Definition PostTheta.cpp:1564
const MatrixXd Dxy
Definition PostTheta.h:166
void eval_denominator(Vect &theta, double &val, SpMat &Q, Vect &rhs, Vect &mu)
Evaluate denominator: conditional probability of Qx|y.
Definition PostTheta.cpp:3184
SpMat Qx
Definition PostTheta.h:132
void eval_log_pc_prior_hp(double &log_sum, Vect &lambda, Vect &theta_interpret)
evaluate log prior using PC prior
Definition PostTheta.cpp:2553
void get_mu(Vect &theta, Vect &mu_)
get conditional mean mu for theta – Gaussian case.
Definition PostTheta.cpp:1446
Vect get_grad()
returns current gradient of theta.
Definition PostTheta.cpp:1482
SpMat Qu
Definition PostTheta.h:129
int iter_count
Definition PostTheta.h:97
SpMat c0
Definition PostTheta.h:117
~PostTheta()
class destructor. Frees memory allocated by PostTheta class.
Definition PostTheta.cpp:3646
Vect AxTy
Definition PostTheta.h:137
void construct_Q(Vect &theta, Vect &mu, SpMat &Q)
construct precision matrix. Calls spatial, spatial-temporal, etc.
Definition PostTheta.cpp:3053
Vect y
Definition PostTheta.h:99
void FD_gradient(Vect &eta, Vect &grad)
compute finite difference gradient. 1st order central difference. currently stepsize h fixed.
Definition PostTheta.cpp:3427
int dim_th
Definition PostTheta.h:77
int MPI_size
Definition PostTheta.h:59
int nss
Definition PostTheta.h:67
int nu
Definition PostTheta.h:71
double w_sum
Definition PostTheta.h:150
SpMat Ax
Definition PostTheta.h:110
int nb
Definition PostTheta.h:68
SpMat M2
Definition PostTheta.h:126
void FD_diag_hessian(Vect &eta, Vect &diag_hess)
compute finite difference diagonal of hessian. 2nd order central difference. currently stepsize h fix...
Definition PostTheta.cpp:3470
void eval_log_gaussian_prior_hp(Vect &theta_param, Vect &theta_prior_param, double &log_prior)
evaluate log prior of the hyperparameters using original theta value
Definition PostTheta.cpp:2543
Vect grad_cond_negLogPoisLik(Vect &eta)
evaluate analytical negative gradient log Poisson likelihood
Definition PostTheta.cpp:3351
SpMat Qb
Definition PostTheta.h:128
void convert_interpret2theta_spatTemp(double sigU, double ranS, double ranT, double &lgamE, double &lgamS, double &lgamT)
convert hyperparameters theta from the interpretable parametrisation to the model parametrisation ie....
Definition PostTheta.cpp:1324
Vect t_grad
Definition PostTheta.h:147
int no_f_eval
Definition PostTheta.h:152
Vect mu
Definition PostTheta.h:140
double cond_negLogBinom(SpMat &Qprior, Vect &x)
evaluate negative condiational log Poisson + Gaussian prior
Definition PostTheta.cpp:3403
int MPI_rank
Definition PostTheta.h:60
MatrixXd get_Covariance(Vect theta, double eps)
Compute Covariance matrix of hyperparameters theta, at theta.
Definition PostTheta.cpp:1494
Vect mu_midpoint
Definition PostTheta.h:141
Vect BTy
Definition PostTheta.h:136
void construct_Q_spat_temp(Vect &theta, SpMat &Qst)
spatial temporal model : SPDE discretisation. DEMF(1,2,1) model.
Definition PostTheta.cpp:2908
SpMat g3
Definition PostTheta.h:122
int nt
Definition PostTheta.h:66
int num_solvers
Definition PostTheta.h:81
MatrixXd mu_matrix
Definition PostTheta.h:144
double cond_LogPriorLat(SpMat &Qprior, Vect &x)
evaluate Gaussian log prior (without log determinant!!), mean assumed to be zero
Definition PostTheta.cpp:3325
MatrixXd B
Definition PostTheta.h:113
int fct_count
Definition PostTheta.h:96
PostTheta(int ns, int nt, int nb, int no, MatrixXd B, Vect y, Vect theta_prior, Vect mu_initial, string likelihood, Vect extraCoeffVecLik, string solver_type, const bool constr, const MatrixXd Dxy, const bool validate, const Vect w)
constructor for regression model (no random effects).
Definition PostTheta.cpp:7
double cond_negLogDist(SpMat &Qprior, Vect &x, function< double(Vect &, Vect &)> lik_func)
evaluate negative condiational log likelihood + Gaussian prior
Definition PostTheta.cpp:3417
Vect diagHess_cond_negLogPoisLik(Vect &eta)
evaluate analytical negative diagonal Hessian of log Poisson likelihood
Definition PostTheta.cpp:3366
const bool constr
Definition PostTheta.h:164
void NewtonIter(Vect &theta, Vect &x, SpMat &Q, double &log_det)
Newton iteration to find optimum of conditional distribution latent parameters of prior & likelihood.
Definition PostTheta.cpp:3519
double yTy
Definition PostTheta.h:135
SpMat g1
Definition PostTheta.h:118
SpMat g2
Definition PostTheta.h:119
std::string prior
Definition PostTheta.h:94
string likelihood
Definition PostTheta.h:89
double cond_negLogBinomLik(Vect &eta)
evaluate negative log Binomial likelihood
Definition PostTheta.cpp:3390
void convert_theta2interpret_spat(double lgamS, double lgamE, double &lranS, double &lsigU)
convert hyperparameters theta from the interpretable parametrisation to the model parametrisation ie....
Definition PostTheta.cpp:1411
void eval_likelihood(Vect &theta, Vect &mu, double &log_det, double &val)
compute log likelihood : log_det tau*no and value -theta*yTy
Definition PostTheta.cpp:2815
double cond_negLogPoisLik(Vect &eta)
evaluate negative log Poisson likelihood
Definition PostTheta.cpp:3343
int no
Definition PostTheta.h:69
void convert_theta2interpret_spatTemp(double lgamE, double lgamS, double lgamT, double &sigU, double &ranS, double &ranT)
convert hyperparameters theta from the model parametrisation to the interpretable parametrisation ie....
Definition PostTheta.cpp:1286
double cond_LogPoisLik(Vect &eta)
evaluate log Poisson likelihood
Definition PostTheta.cpp:3336
void construct_b(Vect &theta, Vect &rhs)
Assemble right-handside.
Definition PostTheta.cpp:3172
const MatrixXd Dx
Definition PostTheta.h:165
SpMat M1
Definition PostTheta.h:124
MatrixXd G
Definition PostTheta.h:155
void get_fullFact_marginals_f(Vect &theta, SpMat &Qinv)
Compute the marginal variances of the latent parameters at theta. Using selected inversion procedure.
abstract base solver class to enable to be able two switch between solvers (current options: PARDISO ...
Definition Solver.h:27