3
3
import pandas as pd
4
4
from mssm .models import *
5
5
from mssm .src .python .smooths import convolve_event
6
+ from mssm .src .python .gamm_solvers import cpp_cholP ,compute_Linv ,apply_eigen_perm
6
7
7
8
################################## Contains simulations to simulate for GAMM & GAMMLSS models ##################################
8
9
@@ -37,7 +38,7 @@ def sim1(sim_size,sim_sigma = 5.5,sim_lam = 1e-4,sim_weak_nonlin = 0.5,random_se
37
38
# Get matrix for time effects
38
39
sim_dat = pd .DataFrame ({"Time" :time_pred ,
39
40
"x" :x_pred ,
40
- "y" :scp .stats .norm .rvs (size = len (time_pred ))})
41
+ "y" :scp .stats .norm .rvs (size = len (time_pred ), random_state = 20 )})
41
42
42
43
sim_formula = Formula (lhs ("y" ),
43
44
[i (),f (["Time" ],nk = 15 )],
@@ -52,7 +53,7 @@ def sim1(sim_size,sim_sigma = 5.5,sim_lam = 1e-4,sim_weak_nonlin = 0.5,random_se
52
53
53
54
# Get fixed time effects (+intercept)
54
55
fixed1 = np .array ([5 ,* scp .stats .norm .rvs (size = (sim_S .shape [1 ]- 1 ),scale = 5 ,random_state = fixed_seed )]).reshape (- 1 ,1 )
55
- fixed2 = np .array ([- 5 ,* scp .stats .norm .rvs (size = (sim_S .shape [1 ]- 1 ),scale = 5 ,random_state = fixed_seed * 3 )]).reshape (- 1 ,1 )
56
+ fixed2 = np .array ([- 5 ,* scp .stats .norm .rvs (size = (sim_S .shape [1 ]- 1 ),scale = 5 ,random_state = int ( fixed_seed * 3 ) )]).reshape (- 1 ,1 )
56
57
fixed3 = np .zeros_like (fixed2 )
57
58
fixed_sim_time_coefs = [fixed1 ,fixed2 ,fixed3 ]
58
59
@@ -61,7 +62,16 @@ def sim1(sim_size,sim_sigma = 5.5,sim_lam = 1e-4,sim_weak_nonlin = 0.5,random_se
61
62
62
63
# Prepare random smooth sampler
63
64
# Based on Wood (2017, 6.10)
64
- V = scp .sparse .linalg .spsolve (sim_mat .T @ sim_mat + sim_S ,scp .sparse .eye ((sim_S .shape [1 ]),format = 'csc' )) * sim_sigma
65
+ nH = sim_mat .T @ sim_mat + sim_S
66
+ Lp , Pr , code = cpp_cholP (nH .tocsc ())
67
+
68
+ if code != 0 :
69
+ raise ValueError ("Cholesky failed." )
70
+
71
+ LVp = compute_Linv (Lp ,1 )
72
+ LV = apply_eigen_perm (Pr ,LVp )
73
+ V = (LV .T @ LV ) * sim_sigma
74
+ #V = scp.sparse.linalg.spsolve(sim_mat.T @ sim_mat + sim_S,scp.sparse.eye((sim_S.shape[1]),format='csc')) * sim_sigma
65
75
66
76
# Get matrix for x effects
67
77
sim_formula2 = Formula (lhs ("y" ),
@@ -188,7 +198,7 @@ def sim2(sim_size,sim_sigma = 5.5,sim_lam = 1e-4,set_zero = 1,random_seed=None,f
188
198
sim_dat = pd .DataFrame ({"Time" :time_pred ,
189
199
"x" :x_pred ,
190
200
"z" :z_pred ,
191
- "y" :scp .stats .norm .rvs (size = len (time_pred ))})
201
+ "y" :scp .stats .norm .rvs (size = len (time_pred ), random_state = 20 )})
192
202
193
203
sim_formula = Formula (lhs ("y" ),
194
204
[i (),f (["Time" ],nk = 15 )],
@@ -209,7 +219,16 @@ def sim2(sim_size,sim_sigma = 5.5,sim_lam = 1e-4,set_zero = 1,random_seed=None,f
209
219
210
220
# Prepare random smooth sampler
211
221
# Based on Wood (2017, 6.10)
212
- V = scp .sparse .linalg .spsolve (sim_mat .T @ sim_mat + sim_S ,scp .sparse .eye ((sim_S .shape [1 ]),format = 'csc' )) * sim_sigma
222
+ nH = sim_mat .T @ sim_mat + sim_S
223
+ Lp , Pr , code = cpp_cholP (nH .tocsc ())
224
+
225
+ if code != 0 :
226
+ raise ValueError ("Cholesky failed." )
227
+
228
+ LVp = compute_Linv (Lp ,1 )
229
+ LV = apply_eigen_perm (Pr ,LVp )
230
+ V = (LV .T @ LV ) * sim_sigma
231
+ #V = scp.sparse.linalg.spsolve(sim_mat.T @ sim_mat + sim_S,scp.sparse.eye((sim_S.shape[1]),format='csc')) * sim_sigma
213
232
214
233
# Get matrix for x effects
215
234
sim_formula2 = Formula (lhs ("y" ),
@@ -222,7 +241,7 @@ def sim2(sim_size,sim_sigma = 5.5,sim_lam = 1e-4,set_zero = 1,random_seed=None,f
222
241
sim_mat2 = sim_model2 .get_mmat ()
223
242
224
243
# Get fixed x effects
225
- fixed_x = np .array ([0 ,* scp .stats .norm .rvs (size = (5 ),scale = 5 ,random_state = fixed_seed * 6 )]).reshape (- 1 ,1 )
244
+ fixed_x = np .array ([0 ,* scp .stats .norm .rvs (size = (5 ),scale = 5 ,random_state = int ( fixed_seed * 6 ) )]).reshape (- 1 ,1 )
226
245
227
246
# Get matrix for z effects
228
247
sim_formula3 = Formula (lhs ("y" ),
@@ -235,7 +254,7 @@ def sim2(sim_size,sim_sigma = 5.5,sim_lam = 1e-4,set_zero = 1,random_seed=None,f
235
254
sim_mat3 = sim_model3 .get_mmat ()
236
255
237
256
# Get fixed z effects
238
- fixed_z = np .array ([0 ,* scp .stats .norm .rvs (size = (10 ),scale = 5 ,random_state = fixed_seed * 15 )]).reshape (- 1 ,1 )
257
+ fixed_z = np .array ([0 ,* scp .stats .norm .rvs (size = (10 ),scale = 5 ,random_state = int ( fixed_seed * 15 ) )]).reshape (- 1 ,1 )
239
258
240
259
# Simulation seed
241
260
np_gen = np .random .default_rng (random_seed )
0 commit comments