Skip to content

Commit 5f66945

Browse files
committed
simulation fixes
1 parent 13b4120 commit 5f66945

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

src/mssmViz/sim.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pandas as pd
44
from mssm.models import *
55
from mssm.src.python.smooths import convolve_event
6+
from mssm.src.python.gamm_solvers import cpp_cholP,compute_Linv,apply_eigen_perm
67

78
################################## Contains simulations to simulate for GAMM & GAMMLSS models ##################################
89

@@ -37,7 +38,7 @@ def sim1(sim_size,sim_sigma = 5.5,sim_lam = 1e-4,sim_weak_nonlin = 0.5,random_se
3738
# Get matrix for time effects
3839
sim_dat = pd.DataFrame({"Time":time_pred,
3940
"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)})
4142

4243
sim_formula = Formula(lhs("y"),
4344
[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
5253

5354
# Get fixed time effects (+intercept)
5455
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)
5657
fixed3 = np.zeros_like(fixed2)
5758
fixed_sim_time_coefs = [fixed1,fixed2,fixed3]
5859

@@ -61,7 +62,16 @@ def sim1(sim_size,sim_sigma = 5.5,sim_lam = 1e-4,sim_weak_nonlin = 0.5,random_se
6162

6263
# Prepare random smooth sampler
6364
# 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
6575

6676
# Get matrix for x effects
6777
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
188198
sim_dat = pd.DataFrame({"Time":time_pred,
189199
"x":x_pred,
190200
"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)})
192202

193203
sim_formula = Formula(lhs("y"),
194204
[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
209219

210220
# Prepare random smooth sampler
211221
# 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
213232

214233
# Get matrix for x effects
215234
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
222241
sim_mat2 = sim_model2.get_mmat()
223242

224243
# 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)
226245

227246
# Get matrix for z effects
228247
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
235254
sim_mat3 = sim_model3.get_mmat()
236255

237256
# 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)
239258

240259
# Simulation seed
241260
np_gen = np.random.default_rng(random_seed)

0 commit comments

Comments
 (0)