|
| 1 | +import dask |
| 2 | +import logging |
1 | 3 | import numpy as np |
2 | 4 | import scipy.sparse |
3 | 5 | from typing import Union |
@@ -71,3 +73,145 @@ def compute_scales_fun(variance, mean): |
71 | 73 | inv_link_fn=invlink_fn, |
72 | 74 | compute_scales_fun=compute_scales_fun |
73 | 75 | ) |
| 76 | + |
| 77 | + |
| 78 | +def init_par( |
| 79 | + input_data, |
| 80 | + init_a, |
| 81 | + init_b, |
| 82 | + init_model |
| 83 | +): |
| 84 | + r""" |
| 85 | + standard: |
| 86 | + Only initialise intercept and keep other coefficients as zero. |
| 87 | +
|
| 88 | + closed-form: |
| 89 | + Initialize with Maximum Likelihood / Maximum of Momentum estimators |
| 90 | +
|
| 91 | + Idea: |
| 92 | + $$ |
| 93 | + \theta &= f(x) \\ |
| 94 | + \Rightarrow f^{-1}(\theta) &= x \\ |
| 95 | + &= (D \cdot D^{+}) \cdot x \\ |
| 96 | + &= D \cdot (D^{+} \cdot x) \\ |
| 97 | + &= D \cdot x' = f^{-1}(\theta) |
| 98 | + $$ |
| 99 | + """ |
| 100 | + train_loc = True |
| 101 | + train_scale = True |
| 102 | + |
| 103 | + if init_model is None: |
| 104 | + groupwise_means = None |
| 105 | + init_a_str = None |
| 106 | + if isinstance(init_a, str): |
| 107 | + init_a_str = init_a.lower() |
| 108 | + # Chose option if auto was chosen |
| 109 | + if init_a.lower() == "auto": |
| 110 | + if isinstance(input_data.design_loc, dask.array.core.Array): |
| 111 | + dloc = input_data.design_loc.compute() |
| 112 | + else: |
| 113 | + dloc = input_data.design_loc |
| 114 | + one_hot = len(np.unique(dloc)) == 2 and \ |
| 115 | + np.abs(np.min(dloc) - 0.) == 0. and \ |
| 116 | + np.abs(np.max(dloc) - 1.) == 0. |
| 117 | + init_a = "standard" if not one_hot else "closed_form" |
| 118 | + |
| 119 | + if init_a.lower() == "closed_form": |
| 120 | + groupwise_means, init_a, rmsd_a = closedform_nb_glm_logmu( |
| 121 | + x=input_data.x, |
| 122 | + design_loc=input_data.design_loc, |
| 123 | + constraints_loc=input_data.constraints_loc, |
| 124 | + size_factors=input_data.size_factors, |
| 125 | + link_fn=lambda mu: np.log(mu+np.nextafter(0, 1, dtype=mu.dtype)) |
| 126 | + ) |
| 127 | + |
| 128 | + # train mu, if the closed-form solution is inaccurate |
| 129 | + train_loc = not (np.all(np.abs(rmsd_a) < 1e-20) or rmsd_a.size == 0) |
| 130 | + |
| 131 | + if input_data.size_factors is not None: |
| 132 | + if np.any(input_data.size_factors != 1): |
| 133 | + train_loc = True |
| 134 | + elif init_a.lower() == "standard": |
| 135 | + overall_means = np.mean(input_data.x, axis=0) # directly calculate the mean |
| 136 | + init_a = np.zeros([input_data.num_loc_params, input_data.num_features]) |
| 137 | + init_a[0, :] = np.log(overall_means) |
| 138 | + train_loc = True |
| 139 | + elif init_a.lower() == "all_zero": |
| 140 | + init_a = np.zeros([input_data.num_loc_params, input_data.num_features]) |
| 141 | + train_loc = True |
| 142 | + else: |
| 143 | + raise ValueError("init_a string %s not recognized" % init_a) |
| 144 | + |
| 145 | + if isinstance(init_b, str): |
| 146 | + if init_b.lower() == "auto": |
| 147 | + init_b = "standard" |
| 148 | + |
| 149 | + if init_b.lower() == "standard": |
| 150 | + groupwise_scales, init_b_intercept, rmsd_b = closedform_nb_glm_logphi( |
| 151 | + x=input_data.x, |
| 152 | + design_scale=input_data.design_scale[:, [0]], |
| 153 | + constraints=input_data.constraints_scale[[0], :][:, [0]], |
| 154 | + size_factors=input_data.size_factors, |
| 155 | + groupwise_means=None, |
| 156 | + link_fn=lambda r: np.log(r+np.nextafter(0, 1, dtype=r.dtype)) |
| 157 | + ) |
| 158 | + init_b = np.zeros([input_data.num_scale_params, input_data.num_features]) |
| 159 | + init_b[0, :] = init_b_intercept |
| 160 | + elif init_b.lower() == "closed_form": |
| 161 | + dmats_unequal = False |
| 162 | + if input_data.design_loc.shape[1] == input_data.design_scale.shape[1]: |
| 163 | + if np.any(input_data.design_loc != input_data.design_scale): |
| 164 | + dmats_unequal = True |
| 165 | + |
| 166 | + inits_unequal = False |
| 167 | + if init_a_str is not None: |
| 168 | + if init_a_str != init_b: |
| 169 | + inits_unequal = True |
| 170 | + |
| 171 | + if inits_unequal or dmats_unequal: |
| 172 | + raise ValueError("cannot use closed_form init for scale model " + |
| 173 | + "if scale model differs from loc model") |
| 174 | + |
| 175 | + groupwise_scales, init_b, rmsd_b = closedform_nb_glm_logphi( |
| 176 | + x=input_data.x, |
| 177 | + design_scale=input_data.design_scale, |
| 178 | + constraints=input_data.constraints_scale, |
| 179 | + size_factors=input_data.size_factors, |
| 180 | + groupwise_means=groupwise_means, |
| 181 | + link_fn=lambda r: np.log(r) |
| 182 | + ) |
| 183 | + elif init_b.lower() == "all_zero": |
| 184 | + init_b = np.zeros([input_data.num_scale_params, input_data.x.shape[1]]) |
| 185 | + else: |
| 186 | + raise ValueError("init_b string %s not recognized" % init_b) |
| 187 | + else: |
| 188 | + # Locations model: |
| 189 | + if isinstance(init_a, str) and (init_a.lower() == "auto" or init_a.lower() == "init_model"): |
| 190 | + my_loc_names = set(input_data.loc_names) |
| 191 | + my_loc_names = my_loc_names.intersection(set(init_model.input_data.loc_names)) |
| 192 | + |
| 193 | + init_loc = np.zeros([input_data.num_loc_params, input_data.num_features]) |
| 194 | + for parm in my_loc_names: |
| 195 | + init_idx = np.where(init_model.input_data.loc_names == parm)[0] |
| 196 | + my_idx = np.where(input_data.loc_names == parm)[0] |
| 197 | + init_loc[my_idx] = init_model.a_var[init_idx] |
| 198 | + |
| 199 | + init_a = init_loc |
| 200 | + logging.getLogger("batchglm").debug("Using initialization based on input model for mean") |
| 201 | + |
| 202 | + # Scale model: |
| 203 | + if isinstance(init_b, str) and (init_b.lower() == "auto" or init_b.lower() == "init_model"): |
| 204 | + my_scale_names = set(input_data.scale_names) |
| 205 | + my_scale_names = my_scale_names.intersection(init_model.input_data.scale_names) |
| 206 | + |
| 207 | + init_scale = np.zeros([input_data.num_scale_params, input_data.num_features]) |
| 208 | + for parm in my_scale_names: |
| 209 | + init_idx = np.where(init_model.input_data.scale_names == parm)[0] |
| 210 | + my_idx = np.where(input_data.scale_names == parm)[0] |
| 211 | + init_scale[my_idx] = init_model.b_var[init_idx] |
| 212 | + |
| 213 | + init_b = init_scale |
| 214 | + logging.getLogger("batchglm").debug("Using initialization based on input model for dispersion") |
| 215 | + |
| 216 | + return init_a, init_b, train_loc, train_scale |
| 217 | + |
0 commit comments