Skip to content

Commit 17993e4

Browse files
authored
Merge pull request #8 from JoKra1/main
v0.1.24
2 parents 3fbb15a + f539872 commit 17993e4

File tree

8 files changed

+912
-89
lines changed

8 files changed

+912
-89
lines changed

LICENSE

+674
Large diffs are not rendered by default.

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Description
44

5-
``mssm`` is a toolbox to estimate Generalized Additive Mixed Models (GAMMs) semi Markov-switching GAMMs (sMs-GAMMs) and sMs Impulse Response GAMMs (sMs-IR-GAMMs). The ``main`` branch is updated frequently to reflect new developments. The ``stable`` branch should reflect the latest releases. if you don't need the newest functionality, you should install from the ``stable`` branch (see below for instructions).
5+
``mssm`` is a toolbox to estimate Generalized Additive Mixed Models (GAMMs) semi Markov-switching GAMMs (sMs-GAMMs) and sMs Impulse Response GAMMs (sMs-IR-GAMMs). The ``main`` branch is updated frequently to reflect new developments. The ``stable`` branch should reflect the latest releases. If you don't need the newest functionality, you should install from the ``stable`` branch (see below for instructions).
66

77
## Installation
88

@@ -28,8 +28,12 @@ pip install -i https://test.pypi.org/simple/ mssm
2828

2929
### Building from source
3030

31-
You can also build directly from source. This requires ``conda`` or an installation of [eigen](https://eigen.tuxfamily.org/index.php?title=Main_Page)(``setup.py`` then expects ``eigen`` in "usr/local/include/eigen3". This will probably not work on windows.). Once you have ``conda`` installed,
32-
[install eigen from conda-forge](https://anaconda.org/conda-forge/eigen). After cloning and navigating into the downloaded repository you can then install via ``pip install . ``.
31+
You can also build directly from source. This requires ``conda`` or an installation of [eigen](https://eigen.tuxfamily.org/index.php?title=Main_Page) (``setup.py`` then expects ``eigen`` in "usr/local/include/eigen3". This will probably not work on windows - the ``conda`` strategy should.). Once you have ``conda`` installed,
32+
[install eigen from conda-forge](https://anaconda.org/conda-forge/eigen). After cloning and navigating into the downloaded repository you can then install via:
33+
34+
```
35+
pip install .
36+
```
3337

3438
## To get started
3539

pyproject.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ classifiers = [
2525
# https://pypi.org/project/setuptools-scm/ - automatic version number based on tag
2626
dynamic = ["version"]
2727

28-
[tool.setuptools_scm]
28+
[tool.setuptools_scm]
29+
# https://github.com/pypa/setuptools_scm/issues/342
30+
local_scheme = "no-local-version"

src/mssm/models.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,11 @@ def fit(self,burn_in=100,maxiter_inner=30,m_avg=15,conv_tol=1e-7,extend_lambda=T
602602
temp = temp_schedule[iter]
603603

604604
durs,states,llks = self.__propose_all_states(pool,cov,temp,n_pi,n_TR,s_log_o_probs,dur_log_probs,var_map)
605-
states_flat = np.array([st for s in states for st in s],dtype=int)
605+
606+
if not self.mvar_by is None:
607+
states_flat = np.array([st for s in states for _ in range(len(factor_levels[self.mvar_by])) for st in s],dtype=int)
608+
else:
609+
states_flat = np.array([st for s in states for st in s],dtype=int)
606610

607611
### Convergence control ###
608612

@@ -764,7 +768,11 @@ def fit(self,burn_in=100,maxiter_inner=30,m_avg=15,conv_tol=1e-7,extend_lambda=T
764768
# Now we can decode.
765769
with mp.Pool(processes=self.cpus) as pool:
766770
_,states_max,_ = self.__decode_all_states(pool,cov,self.__pi,self.__TR,s_log_o_probs,dur_log_probs,var_map)
767-
max_states_flat = np.array([st for s in states_max for st in s],dtype=int)
771+
772+
if not self.mvar_by is None:
773+
max_states_flat = np.array([st for s in states_max for _ in range(len(factor_levels[self.mvar_by])) for st in s],dtype=int)
774+
else:
775+
max_states_flat = np.array([st for s in states_max for st in s],dtype=int)
768776

769777
return llk_hist,max_states_flat
770778

src/mssm/src/python/gamm_solvers.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,20 @@ def step_fellner_schall_sparse(gInv,emb_SJ,Bps,cCoef,cLam,scale,verbose=False):
2828
# equal to zero: every coefficient of a term is penalized away. In that
2929
# case num /denom is not defined so we return directly.
3030
if denom <= 0: # Prevent overflow
31-
return 1e+7
32-
nLam = scale * (num / denom) * cLam
33-
nLam = max(nLam,1e-7) # Prevent Lambda going to zero
34-
nLam = min(nLam,1e+7) # Prevent overflow
31+
nLam = 1e+7
32+
else:
33+
nLam = scale * (num / denom) * cLam
34+
35+
nLam = max(nLam,1e-7) # Prevent Lambda going to zero
36+
nLam = min(nLam,1e+7) # Prevent overflow
3537

3638
if verbose:
3739
print(f"Num = {(gInv @ emb_SJ).trace()} - {Bps} == {num}\nDenom = {denom}; Lambda = {nLam}")
3840

39-
return nLam-cLam
41+
# Compute lambda delta
42+
delta_lam = nLam-cLam
43+
44+
return delta_lam
4045

4146
def grad_lambda(gInv,emb_SJ,Bps,cCoef,scale):
4247
# P. Deriv of restricted likelihood with respect to lambda.

src/mssm/src/python/sem.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def compute_log_probs(n_j,n_obs,has_scale_split,
218218
if not mvar_by is None:
219219
# We need to split the s_log_o_probs from every series by the multivariate factor
220220
# and then sum the log-probs together. This is a strong independence assumption (see Langrock, 2021)
221-
n_by_mvar = factor_levels[mvar_by]
221+
n_by_mvar = len(factor_levels[mvar_by])
222222
s_log_o_probs = [s_prob.reshape(n_j,n_by_mvar,-1).sum(axis=1) for s_prob in s_log_o_probs]
223223

224224
return s_log_o_probs,dur_log_probs

src/mssm/src/python/smooths.py

-7
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ def B_spline_basis(i, cov, state_est, nk, drop_outer_k=False, convolve=False, mi
5353
# Setup basis with even knot locations.
5454
# Code based on Eilers, P., & Marx, B. (2010). Splines, knots, and penalties. https://doi.org/10.1002/WICS.125
5555
# See also: Eilers, P. H. C., & Marx, B. D. (1996). Flexible smoothing with B-splines and penalties. https://doi.org/10.1214/ss/1038425655
56-
# However, knot location calculation was adapted to match mgcv (Wood, 2017)
5756

5857
xl = min(cov)
5958
xr = max(cov)
@@ -64,12 +63,6 @@ def B_spline_basis(i, cov, state_est, nk, drop_outer_k=False, convolve=False, mi
6463
if not min_c is None:
6564
xl = min_c
6665

67-
rg = xr - xl
68-
69-
# MGCV adjustment.
70-
xr += 0.001*rg
71-
xl -= 0.001*rg
72-
7366
# ndx is equal to n in Eilers & Marx (2011)
7467
# So there will be n-1 knots (without expansion)
7568
# n + 1 + 2*deg knots with expansion

tutorials/1) GAMMs.ipynb

+207-70
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)