Defining multiple scenarios

We can define multiple scenarios in a single TOML file. For example, shown below is a single TOML file that defines all of the scenarios presented in this Getting Started guide. The settings that are shared by all three scenarios are defined as per usual, and the scenario-specific settings are defined inside each scenario’s table (indicated by the highlighted lines).

An example of defining multiple scenarios in a single file, where common settings are shared between all scenarios, and scenario-specific settings are defined inside each scenario table.
 1# NOTE: Save this file as 'lorenz63_all.toml'
 2
 3[components]
 4model = "pypfilt.examples.lorenz.Lorenz63"
 5time = "pypfilt.Scalar"
 6sampler = "pypfilt.sampler.LatinHypercube"
 7summary = "pypfilt.summary.HDF5"
 8
 9[time]
10start = 0.0
11until = 25.0
12steps_per_unit = 10
13summaries_per_unit = 10
14
15[prior]
16sigma = { name = "constant", args.value = 10 }
17rho = { name = "constant", args.value = 28 }
18beta = { name = "constant", args.value = 2.66667 }
19
20[observations.x]
21model = "pypfilt.examples.lorenz.ObsLorenz63"
22
23[observations.y]
24model = "pypfilt.examples.lorenz.ObsLorenz63"
25
26[observations.z]
27model = "pypfilt.examples.lorenz.ObsLorenz63"
28
29[filter]
30particles = 500
31prng_seed = 2001
32history_window = -1
33resample.threshold = 0.25
34regularisation.enabled = true
35
36[scenario.simulate]
37prior.x = { name = "constant", args.value = 1 }
38prior.y = { name = "constant", args.value = 1 }
39prior.z = { name = "constant", args.value = 1 }
40
41[scenario.forecast]
42prior.x = { name = "uniform", args.loc = -5, args.scale = 10 }
43prior.y = { name = "uniform", args.loc = -5, args.scale = 10 }
44prior.z = { name = "uniform", args.loc = -5, args.scale = 10 }
45observations.x.file = "lorenz63-x.ssv"
46observations.y.file = "lorenz63-y.ssv"
47observations.z.file = "lorenz63-z.ssv"
48summary.tables.forecasts.component = "pypfilt.summary.PredictiveCIs"
49summary.tables.forecasts.credible_intervals = [50, 60, 70, 80, 90, 95]
50summary.tables.sim_z.component = "pypfilt.summary.SimulatedObs"
51summary.tables.sim_z.observation_unit = "z"
52
53[scenario.forecast_regularised]
54prior.x = { name = "uniform", args.loc = -5, args.scale = 10 }
55prior.y = { name = "uniform", args.loc = -5, args.scale = 10 }
56prior.z = { name = "uniform", args.loc = -5, args.scale = 10 }
57observations.x.file = "lorenz63-x.ssv"
58observations.y.file = "lorenz63-y.ssv"
59observations.z.file = "lorenz63-z.ssv"
60summary.tables.forecasts.component = "pypfilt.summary.PredictiveCIs"
61summary.tables.forecasts.credible_intervals = [50, 60, 70, 80, 90, 95]
62summary.tables.sim_z.component = "pypfilt.summary.SimulatedObs"
63summary.tables.sim_z.observation_unit = "z"
64filter.regularisation.enabled = true
65filter.regularisation.bounds.x = { min = -50, max = 50 }
66filter.regularisation.bounds.y = { min = -50, max = 50 }
67filter.regularisation.bounds.z = {}

Note

Call save_lorenz63_scenario_files() to save this scenario file (and the others used in this tutorial) in the working directory.

We can load all of these scenarios with pypfilt.load_instances() and use them to simulate observations and generate forecasts:

def run_all_lorenz63_scenarios():
    """Run all of the Lorenz-63 scenarios."""
    scenario_file = 'lorenz63_all.toml'

    # Collect the scenario instances in a dictionary.
    instances = {
        instance.scenario_id: instance
        for instance in pypfilt.load_instances(scenario_file)
    }
    assert len(instances) == 3

    # Simulate observations from the 'simulate' scenario.
    obs_tables = pypfilt.simulate_from_model(instances['simulate'])

    # Run forecasts from t = 20.
    forecast_time = 20
    forecast_results = pypfilt.forecast(
        instances['forecast'].build_context(), [forecast_time], filename=None
    )
    regularised_results = pypfilt.forecast(
        instances['forecast_regularised'].build_context(),
        [forecast_time],
        filename=None,
    )

    return (obs_tables, forecast_results, regularised_results)