| 222 | @implicit_stochastic |
| 223 | @scope.define |
| 224 | def LGMM1(weights, mus, sigmas, low=None, high=None, q=None, rng=None, size=()): |
| 225 | weights, mus, sigmas = list(map(np.asarray, (weights, mus, sigmas))) |
| 226 | n_samples = np.prod(size) |
| 227 | # n_components = len(weights) |
| 228 | if low is None and high is None: |
| 229 | active = np.argmax(rng.multinomial(1, weights, (n_samples,)), axis=1) |
| 230 | assert len(active) == n_samples |
| 231 | samples = np.exp(rng.normal(loc=mus[active], scale=sigmas[active])) |
| 232 | else: |
| 233 | # -- draw from truncated components |
| 234 | # TODO: one-sided-truncation |
| 235 | low = float(low) |
| 236 | high = float(high) |
| 237 | if low >= high: |
| 238 | raise ValueError("low >= high", (low, high)) |
| 239 | samples = [] |
| 240 | while len(samples) < n_samples: |
| 241 | active = np.argmax(rng.multinomial(1, weights)) |
| 242 | draw = rng.normal(loc=mus[active], scale=sigmas[active]) |
| 243 | if low <= draw < high: |
| 244 | samples.append(np.exp(draw)) |
| 245 | samples = np.asarray(samples) |
| 246 | |
| 247 | samples = np.reshape(np.asarray(samples), size) |
| 248 | if q is not None: |
| 249 | samples = np.round(old_div(samples, q)) * q |
| 250 | return samples |
| 251 | |
| 252 | |
| 253 | def logsum_rows(x): |