INLA_DIST
Loading...
Searching...
No Matches
generate_regression_data.cpp
1// probably don't need all of them.
2#include <random>
3#include <vector>
4#include <iostream>
5#include <fstream>
6#include <math.h>
7#include <time.h>
8#include <stdlib.h>
9#include <stdio.h>
10
11#include <Eigen/Core>
12#include <Eigen/Dense>
13
14#include <iostream>
15#include <LBFGS.h>
16
17#include <optional>
18
19#include <armadillo>
20
21
22using Eigen::VectorXd;
23using Eigen::MatrixXd;
24
25typedef Eigen::VectorXd Vect;
26
27// for now use armadillo ... do better once we switch to binary
28
29/*MatrixXd read_matrix(const std::string filename, int n_row, int n_col){
30
31 arma::mat X(n_row, n_col);
32 X.load(filename, arma::raw_ascii);
33 // X.print();
34
35 return Eigen::Map<MatrixXd>(X.memptr(), X.n_rows, X.n_cols);
36}*/
37
38/*void file_exists(std::string file_name)
39{
40 if (std::fstream{file_name}) ;
41 else {
42 std::cerr << file_name << " couldn\'t be opened (not existing or failed to open)\n";
43 exit(1);
44 }
45
46}*/
47
48
49void rnorm_gen(int no, double mean, double sd, Vect& x, int seed){
50 // unsigned int seed = 2;
51 std::default_random_engine generator(seed);
52 std::normal_distribution<double> distribution (mean, sd);
53
54 for (int i=0; i< x.size(); ++i){
55 x[i] = distribution(generator);
56 }
57
58}
59
60
61void generate_ex_regression_constr(size_t nb, size_t no, double& tau, MatrixXd& D, Vect& e, MatrixXd& Prec, MatrixXd& B, Vect& b, Vect& y){
62
63
64 if(e.sum() != 0){
65 std::cout << "For now only SUM-TO-ZERO constraints possible!!" << std::endl;
66 exit(1);
67 }
68
69 // generate covariance matrix
70 MatrixXd M = MatrixXd::Random(nb,nb);
71 Prec = M*M.transpose();
72
73 LLT<MatrixXd> lltOfA(Prec); // compute the Cholesky decomposition of A
74 MatrixXd L = lltOfA.matrixL();
75
76 int seed = 4;
77 Vect z(nb);
78 rnorm_gen(nb, 0.0, 1.0, z, seed);
79 //std::cout << "z = " << z.transpose() << std::endl;
80
81 b = L.triangularView<Lower>().solve(z);
82 //std::cout << "norm(L*b - z) = " << (L*b - z).norm() << std::endl;
83
84 MatrixXd V = lltOfA.solve(D.transpose());
85 MatrixXd W = D*V;
86 MatrixXd U = W.inverse()*V.transpose();
87 Vect c = D*b - e;
88 b = b - U.transpose()*c;
89 //std::cout << "D*b = " << D*b << ", b = " << b.transpose() << std::endl;
90
91 B.resize(no, nb);
92 B << Vect::Ones(no), MatrixXd::Random(no, nb-1);
93 //std::cout << "B = \n" << B << std::endl;
94
95 Vect noise_vec(no);
96 double sd = 1/sqrt(exp(tau));
97 // use different seeds!!
98 rnorm_gen(no, 0.0, sd, noise_vec, seed+1);
99
100 // generates data y, using fixed effects b with b ~ N(0,Sigma*) where D*b = e,
101 // Sigma* constraint covariance matrix (see GMRF book p.38)
102 y = B*b + noise_vec;
103
104
105}
106
107void generate_ex_regression( size_t nb, size_t no, double& tau, Eigen::MatrixXd& B, Vect& b, Vect& y){
108
109 std::cout << "generates random sample" << std::endl;
110
111 /* ---------------------- construct random matrix of covariates --------------------- */
112
113 // require different random seed here than in noise -> otherwise cancels each other out
114 // val_l will then equal val_d ...
115 Vect B_random(no*(nb-1));
116 rnorm_gen(no, 0.0, 1, B_random, 2);
117
118 Vect B_tmp(no*nb);
119 B_tmp << Vect::Ones(no), B_random;
120 //std::cout << B_tmp << std::endl;
121
122 // TODO: fix this!
123 Eigen::Map<Eigen::MatrixXd> tmp(B_tmp.data(), no,nb);
124 B = tmp;
125 //*B(B_tmp.data());
126 //Eigen::MatrixXd::Map(*B) = B_tmp.data();
127 //std::cout << *B << std::endl;
128
129 /* ------- construct random solution vector of fixed effects & observations -------- */
130 b = 2*(Vect::Random(nb) + Vect::Ones(nb));
131
132 double mean = 0.0;
133 double sd = 1/sqrt(exp(tau));
134 Vect noise_vec(no);
135
136 rnorm_gen(no, mean, sd, noise_vec, 4);
137
138 y = B*b + noise_vec;
139
140 std::cout << "b : " << b.transpose() << std::endl;
141
142 /*std::cout << "noise vec " << std::endl;
143 std::cout << noise_vec << std::endl;
144
145 std::cout << "B*b " << std::endl;
146 std::cout << (*B)*(*b) << std::endl;
147
148 std::cout << "y " << std::endl;
149 std::cout << *y << std::endl; */
150
151}
152
153// theta vector should be 3-dimensional : precision_observations, kappa, tau
154void generate_ex_spatial_constr(size_t ns, size_t nb, size_t no, Vect& theta, MatrixXd& Qs, SpMat& Ax, MatrixXd& Ds, Vect& e, MatrixXd& Prec, MatrixXd& B, Vect& b, Vect& u, Vect& y){
155
156
157 if(e.sum() != 0){
158 std::cout << "For now only SUM-TO-ZERO constraints possible!!" << std::endl;
159 exit(1);
160 }
161
162 // fixed effects remain UNCONSTRAINT
163 // generate random covariance matrix for fixed effects
164 MatrixXd M = MatrixXd::Random(nb,nb);
165 Prec = M*M.transpose();
166
167 LLT<MatrixXd> lltOfA(Prec); // compute the Cholesky decomposition of A
168 MatrixXd L_b = lltOfA.matrixL();
169
170 int seed = 4;
171 Vect z_b(nb);
172 rnorm_gen(nb, 0.0, 1.0, z_b, seed);
173 //std::cout << "z = " << z.transpose() << std::endl;
174
175 b = L_b.triangularView<Lower>().solve(z_b);
176 //std::cout << "norm(L*b - z) = " << (L*b - z).norm() << std::endl;
177
178 // generate random covariates
179 B.resize(no, nb);
180 B << Vect::Ones(no), MatrixXd::Random(no, nb-1);
181 //std::cout << "B = \n" << B << std::endl;
182
183 // handle CONSTRAINED spatial part
184
185 LLT<MatrixXd> lltOfQ(Qs); // compute the Cholesky decomposition of A
186 MatrixXd L_s = lltOfQ.matrixL();
187
188 Vect z_s(ns);
189 rnorm_gen(nb, 0.0, 1.0, z_s, seed+1);
190 //std::cout << "z = " << z.transpose() << std::endl;
191
192 u = L_s.triangularView<Lower>().solve(z_s);
193
194 MatrixXd V = lltOfQ.solve(Ds.transpose());
195 MatrixXd W = Ds*V;
196 MatrixXd U = W.inverse()*V.transpose();
197 Vect c = Ds*u - e;
198 u = u - U.transpose()*c;
199 std::cout << "Ds*u = " << Ds*u << std::endl;
200 // ", b = " << b.transpose()
201
202 Vect noise_vec(no);
203 double sd = 1/sqrt(exp(theta[0]));
204 // use different seeds!!
205 rnorm_gen(no, 0.0, sd, noise_vec, seed+2);
206
207 // generates data y, using fixed effects b with b ~ N(0,Sigma*) where D*b = e,
208 // Sigma* constraint covariance matrix (see GMRF book p.38)
209 Vect x(ns+nb);
210 x << u, b;
211 y = Ax*x + noise_vec;
212
213}
214
215
216// theta vector should be 4-dimensional
217void generate_ex_spatial_temporal_constr(size_t ns, size_t nt, size_t nb, size_t no, Vect& theta, MatrixXd& Qst, SpMat& Ax, MatrixXd& Dst, Vect& e, MatrixXd& Prec, Vect& b, Vect& u, Vect& y){
218
219
220 if(e.sum() != 0){
221 std::cout << "For now only SUM-TO-ZERO constraints possible!!" << std::endl;
222 exit(1);
223 }
224
225 // fixed effects remain UNCONSTRAINT
226 // generate random covariance matrix for fixed effects
227 MatrixXd M = MatrixXd::Random(nb,nb);
228 Prec = M*M.transpose();
229
230 LLT<MatrixXd> lltOfA(Prec); // compute the Cholesky decomposition of A
231 MatrixXd L_b = lltOfA.matrixL();
232
233 int seed = 199;
234 Vect z_b(nb);
235 rnorm_gen(nb, 0.0, 1.0, z_b, seed);
236 //std::cout << "z_b = " << z_b.transpose() << std::endl;
237
238 b = L_b.triangularView<Lower>().solve(z_b);
239 //std::cout << "b : " << b.transpose() << std::endl;
240 //std::cout << "norm(L*b - z) = " << (L_b*b - z_b).norm() << std::endl;
241
242 // handle CONSTRAINED spatial part
243
244 LLT<MatrixXd> lltOfQ(Qst); // compute the Cholesky decomposition of A
245 MatrixXd L_st = lltOfQ.matrixL();
246
247 Vect z_st(ns*nt);
248 rnorm_gen(nb, 0.0, 1.0, z_st, seed+1);
249 //std::cout << "z = " << z_st.transpose() << std::endl;
250
251 u = L_st.triangularView<Lower>().solve(z_st);
252
253 MatrixXd V = lltOfQ.solve(Dst.transpose());
254 MatrixXd W = Dst*V;
255 MatrixXd U = W.inverse()*V.transpose();
256 Vect c = Dst*u - e;
257 u = u - U.transpose()*c;
258 if((Dst*u).norm() > 1e-10){
259 std::cout << "Dst*u = " << Dst*u << std::endl;
260 std::cout << "This should be zero?!" << std::endl;
261 exit(1);
262 }
263 // ", b = " << b.transpose()
264
265 Vect noise_vec(no);
266 double sd = 1/sqrt(exp(theta[0]));
267 // use different seeds!!
268 rnorm_gen(no, 0.0, sd, noise_vec, seed+2);
269
270 // generates data y, using fixed effects b with b ~ N(0,Sigma*) where D*b = e,
271 // Sigma* constraint covariance matrix (see GMRF book p.38)
272 Vect x(ns*nt+nb);
273 x << u, b;
274 y = Ax*x + noise_vec;
275
276}
277
278
279
280
281