|
| 1 | +# Compare ESS/sec using binomial model |
| 2 | +# and simulated datasets based on smaller, larger number of observations. |
| 3 | +import os |
| 4 | +import numpy as np |
| 5 | +import pandas as pd |
| 6 | +from typing import Any, Dict, List, Tuple |
| 7 | +from cmdstanpy import CmdStanModel, set_cmdstan_path |
| 8 | + |
| 9 | + |
| 10 | +import logging |
| 11 | +cmdstanpy_logger = logging.getLogger("cmdstanpy") |
| 12 | +cmdstanpy_logger.setLevel(logging.FATAL) |
| 13 | + |
| 14 | +import warnings |
| 15 | +warnings.filterwarnings('ignore') |
| 16 | + |
| 17 | +set_cmdstan_path(os.path.join('/Users', 'mitzi', 'github', 'stan-dev', 'cmdstan')) |
| 18 | + |
| 19 | +from utils import simulate_data |
| 20 | + |
| 21 | +# Fit model and dataset for N iterations. |
| 22 | +# For each run, save wall clock time and effective samples / second (N_Eff/sec) |
| 23 | +# Return np.ndarray of size (N,2) with timing information. |
| 24 | +def time_fits(N: int, model: CmdStanModel, data: dict) -> np.ndarray: |
| 25 | + fit_times = np.ndarray(shape=(N, 2), dtype=float) |
| 26 | + for i in range(N): |
| 27 | + print('Run', i) |
| 28 | + fit = model.sample(data=data, parallel_chains=4, |
| 29 | + show_progress=False, show_console=False, refresh=10_000) |
| 30 | + fit_summary = fit.summary() |
| 31 | + total_time = 0 |
| 32 | + times = fit.time |
| 33 | + for j in range(len(times)): |
| 34 | + total_time += times[j]['total'] |
| 35 | + |
| 36 | + fit_times[i, 0] = total_time |
| 37 | + fit_times[i, 1] = fit_summary.loc['lp__', 'ESS_bulk/s'] |
| 38 | + return fit_times |
| 39 | + |
| 40 | + |
| 41 | +# Given a list of label, time pairs, populate dataframe |
| 42 | +# of means of time and std dev wall clock time, and N_Eff/sec |
| 43 | +def summarize_times(data_pairs: List[Tuple[str, np.ndarray]]) -> pd.DataFrame: |
| 44 | + result_data = [] |
| 45 | + for label, array in data_pairs: |
| 46 | + result_data.append({ |
| 47 | + 'label': label, |
| 48 | + 'mean': np.mean(array, axis=0)[0], |
| 49 | + 'std dev': np.std(array, axis=0)[0], |
| 50 | + 'ESS_bulk/s': np.mean(array, axis=0)[1] |
| 51 | + }) |
| 52 | + df = pd.DataFrame(result_data) |
| 53 | + return df.set_index('label').round(2) |
| 54 | + |
| 55 | + |
| 56 | +# Create datasets - fix sizes, and seed |
| 57 | +N_eth = 3 |
| 58 | +N_edu = 5 |
| 59 | +N_age = 9 |
| 60 | +baseline = -3.5 |
| 61 | +sens = 0.75 |
| 62 | +spec = 0.9995 |
| 63 | +data_small = simulate_data(N_eth, N_edu, N_age, baseline, sens, spec, 17, seed=45678) |
| 64 | +data_large = simulate_data(N_eth, N_edu, N_age, baseline, sens, spec, 200, seed=45678) |
| 65 | + |
| 66 | +# sum to zero vector |
| 67 | + |
| 68 | +binomial_ozs_mod = CmdStanModel(stan_file=os.path.join('stan', 'binomial_4preds_ozs.stan')) |
| 69 | +times_ozs_large = time_fits(100, binomial_ozs_mod, data_large) |
| 70 | +times_ozs_small = time_fits(100, binomial_ozs_mod, data_small) |
| 71 | + |
| 72 | +# hard sum-to-zero constraint |
| 73 | + |
| 74 | +binomial_hard_mod = CmdStanModel(stan_file=os.path.join('stan', 'binomial_4preds_hard.stan')) |
| 75 | +times_hard_small = time_fits(100, binomial_hard_mod, data_small) |
| 76 | +times_hard_large = time_fits(100, binomial_hard_mod, data_large) |
| 77 | + |
| 78 | +# soft sum-to-zero constraint |
| 79 | + |
| 80 | +binomial_soft_mod = CmdStanModel(stan_file=os.path.join('stan', 'binomial_4preds_soft.stan')) |
| 81 | +times_soft_small = time_fits(100, binomial_soft_mod, data_small) |
| 82 | +times_soft_large = time_fits(100, binomial_soft_mod, data_large) |
| 83 | + |
| 84 | + |
| 85 | +df_small = summarize_times([('ozs small', times_ozs_small), |
| 86 | + ('hard small', times_hard_small), |
| 87 | + ('soft small', times_soft_small)]) |
| 88 | +df_small.to_json("binomial_runtimes_small.json") |
| 89 | + |
| 90 | +df_large = summarize_times([('ozs large', times_ozs_large), |
| 91 | + ('hard large', times_hard_large), |
| 92 | + ('soft large', times_soft_large)]) |
| 93 | +df_large.to_json("binomial_runtimes_large.json") |
0 commit comments