Create Samples of Variables

After we’ve run a fit on a database, then we can make posterior samples of the variables.

Sample Script

cascade_at.executor.sample.simulate(path, n_sim)[source]

Simulate from a database, within a database.

Parameters
  • path (Union[str, Path]) – A path to the database object to create simulations in.

  • n_sim (int) – Number of simulations to create.

class cascade_at.executor.sample.FitSample(fit_type, **kwargs)[source]

Bases: cascade_at.dismod.api.multithreading._DismodThread

Fit Sample for a database in parallel. Copies the sample table and fits for just one sample index. Will use the __call__ method from _DismodThread.

Parameters
  • main_db – Path to the main database to sample from.

  • index_file_pattern – File pattern to create the index databases with different samples.

  • fit_type (str) – The type of fit to run, one of “fixed” or “both”.

cascade_at.executor.sample.sample_simulate_pool(main_db, index_file_pattern, fit_type, n_sim, n_pool)[source]

Fit the samples in a database in parallel by making copies of the database, fitting them separately, and then combining them back together in the sample table of main_db.

Parameters
  • main_db (Union[str, Path]) – Path to the main database that will be spawned.

  • index_file_pattern (str) – File pattern for the new databases that will have index equal to the simulation number.

  • fit_type (str) – The type of fit to run, one of “fixed” or “both”.

  • n_sim (int) – Number of simulations that will be fit.

  • n_pool (int) – Number of pools for the multiprocessing.

cascade_at.executor.sample.sample_simulate_sequence(path, n_sim, fit_type)[source]

Fit the samples in a database in sequence.

Parameters
  • path (Union[str, Path]) – A path to the database object to create simulations in.

  • n_sim (int) – Number of simulations to create.

  • fit_type (str) – Type of fit – fixed or both

cascade_at.executor.sample.sample(model_version_id, parent_location_id, sex_id, n_sim, n_pool, fit_type, asymptotic=False)[source]

Creates variable samples from a dismod database that has already had a fit run on it. Does so optionally in parallel. Defaults to doing stochastic samples (this is like the parametric bootstrap). If you want asymptotic samples, it will try to do that but if it fails, it will do stochastic samples instead.

Parameters
  • model_version_id (int) – The model version ID

  • parent_location_id (int) – The parent location ID specifying location of database

  • sex_id (int) – The sex ID specifying location of database

  • n_sim (int) – The number of simulations to do

  • n_pool (int) – The number of multiprocessing pools to create. If 1, then will not run with pools but just run all simulations together in one dmdismod command.

  • fit_type (str) – The type of fit that was performed on this database, one of fixed or both.

  • asymptotic (bool) – Whether or not to do asymptotic samples or fit-refit

Return type

None

Sample Cascade Operation

class cascade_at.cascade.cascade_operations.Sample(model_version_id, parent_location_id, sex_id, n_sim, fit_type, asymptotic, n_pool=1, **kwargs)[source]

Bases: cascade_at.cascade.cascade_operations._CascadeOperation

Create posterior samples from a dismod database that has already had a fit run on it. This may be done in parallel with a multiprocessing pool. The samples can either be asymptotic (sampling from a multivariate normal distribution) or stochastic simulations. If you choose to sample asymptotic, and it fails (it may fail because of issues with the constraints), then it will automatically do a sample simulate.

Parameters
  • model_version_id (int) – The model version ID

  • parent_location_id (int) – The parent location ID

  • sex_id (int) – The reference sex ID for the database

  • n_sim (int) – The number of posterior samples to create

  • fit_type (str) – The original fit type for this database. Should be either ‘fixed’ or ‘both’ (could also be ‘random’ but we don’t use that).

  • asymptotic (bool) – Whether or not to do asymptotic samples or simulation-based samples.

  • n_pool (int) – The number of threads to create in a multiprocessing pool. If this is 1, then it will not do multiprocessing.

  • kwargs