Skip to content

Monte Carlo

Monte Carlo simulation for PyPSA-Earth.


monte_carlo

Prepares network files with monte-carlo parameter sweeps for solving process.

Relevant Settings

.. code:: yaml

monte_carlo:
options:
    add_to_snakefile: false # When set to true, enables Monte Carlo sampling
    samples: 9 # number of optimizations. Note that number of samples when using scipy has to be the square of a prime number
    sampling_strategy: "chaospy"  # "pydoe2", "chaospy", "scipy", packages that are supported
    seed: 42 # set seedling for reproducibilty
uncertainties:
    loads_t.p_set:
      type: uniform
      args: [0, 1]
    generators_t.p_max_pu.loc[:, n.generators.carrier == "onwind"]:
      type: lognormal
      args: [1.5]
    generators_t.p_max_pu.loc[:, n.generators.carrier == "solar"]:
      type: beta
      args: [0.5, 2]

.. seealso:: Documentation of the configuration file config.yaml at :ref:monte_cf

Inputs

  • networks/elec_s_10_ec_lcopt_Co2L-24H.nc

Outputs

  • networks/elec_s_10_ec_lcopt_Co2L-24H_{unc}.nc

e.g. networks/elec_s_10_ec_lcopt_Co2L-24H_m0.nc networks/elec_s_10_ec_lcopt_Co2L-24H_m1.nc ...

Description

PyPSA-Earth is deterministic which means that a set of inputs give a set of outputs. Parameter sweeps can help to explore the uncertainty of the outputs cause by parameter changes. Many are familiar with the classical "sensitivity analysis" that can be applied by varying the input of only one feature, while exploring its outputs changes. Here implemented is a "global sensitivity analysis" that can help to explore the multi-dimensional uncertainty space when more than one feature are changed at the same time.

To do so, the scripts is separated in two building blocks: One creates the experimental design, the other, modifies and outputs the network file. Building the experimental design is currently supported by the packages pyDOE2, chaospy and scipy. This should give users the freedom to explore alternative approaches. The orthogonal latin hypercube sampling is thereby found as most performant, hence, implemented here. Sampling the multi-dimensional uncertainty space is relatively easy. It only requires two things: The number of samples (defines the number of total networks to be optimized) and features (pypsa network object e.g loads_t.p_set or generators_t.p_max_pu). This results in an experimental design of the dimension (samples X features).

The experimental design lh (dimension: sample X features) is used to modify the PyPSA networks. Thereby, this script creates samples x amount of networks. The iterators comes from the wildcard {unc}, which is described in the config.yaml and created in the Snakefile as a range from 0 to (total number of) SAMPLES.

monte_carlo_sampling_pydoe2(N_FEATURES, SAMPLES, uncertainties_values, random_state, criterion=None, iteration=None, correlation_matrix=None)

Creates Latin Hypercube Sample (LHS) implementation from PyDOE2 with various options. Additionally, all "corners" are simulated.

Adapted from Disspaset: https://github.com/energy-modelling-toolkit/Dispa-SET/blob/master/scripts/build_and_run_hypercube.py Documentation on PyDOE2: https://github.com/clicumu/pyDOE2 (fixes latin_cube errors)

monte_carlo_sampling_chaospy(N_FEATURES, SAMPLES, uncertainties_values, seed, rule='latin_hypercube')

Creates Latin Hypercube Sample (LHS) implementation from chaospy.

Documentation on Chaospy: https://github.com/clicumu/pyDOE2 (fixes latin_cube errors) Documentation on Chaospy latin-hyper cube (quasi-Monte Carlo method): https://chaospy.readthedocs.io/en/master/user_guide/fundamentals/quasi_random_samples.html#Quasi-random-samples

monte_carlo_sampling_scipy(N_FEATURES, SAMPLES, uncertainties_values, seed, strength=2, optimization=None)

Creates Latin Hypercube Sample (LHS) implementation from SciPy with various options:

  • Center the point within the multi-dimensional grid, centered=True
  • Optimization scheme, optimization="random-cd"
  • Strength=1, classical LHS
  • Strength=2, performant orthogonal LHS, requires the sample to be square of a prime e.g. sq(11)=121

Options could be combined to produce an optimized centered orthogonal array based LHS. After optimization, the result would not be guaranteed to be of strength 2.

Documentation for Quasi-Monte Carlo approaches: https://docs.scipy.org/doc/scipy/reference/stats.qmc.html Documentation for Latin Hypercube: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.qmc.LatinHypercube.html#scipy.stats.qmc.LatinHypercube Orthogonal LHS is better than basic LHS: https://github.com/scipy/scipy/pull/14546/files, https://en.wikipedia.org/wiki/Latin_hypercube_sampling

report_discrepancy(latin_hypercube)

Calculates the discrepancy of a Latin hypercube sample (LHS) using the scipy.stats.qmc module. The discrepancy is a measure of how uniformly the sample points are distributed in the multi-dimensional space.

rescale_distribution(latin_hypercube, uncertainties_values)

Rescales a Latin hypercube sampling (LHS) using specified distribution parameters. More information on the distributions can be found here https://docs.scipy.org/doc/scipy/reference/stats.html

Parameters:

  • latin_hypercube (np.array): The Latin hypercube sampling to be rescaled.
  • uncertainties_values (list): List of dictionaries containing distribution information.

Each dictionary should have 'type' key specifying the distribution type and 'args' key containing parameters specific to the chosen distribution.

Returns:

  • np.array: Rescaled Latin hypercube sampling with values in the range [0, 1].

Supported Distributions:

  • "uniform": Rescaled to the specified lower and upper bounds.
  • "normal": Rescaled using the inverse of the normal distribution function with specified mean and std.
  • "lognormal": Rescaled using the inverse of the log-normal distribution function with specified mean and std.
  • "triangle": Rescaled using the inverse of the triangular distribution function with mean calculated from given parameters.
  • "beta": Rescaled using the inverse of the beta distribution function with specified shape parameters.
  • "gamma": Rescaled using the inverse of the gamma distribution function with specified shape and scale parameters.

Note:

  • The function supports rescaling for uniform, normal, lognormal, triangle, beta, and gamma distributions.
  • The rescaled samples will have values in the range [0, 1].

validate_parameters(sampling_strategy, samples, uncertainties_values)

Validates the parameters for a given probability distribution. Inputs from user through the config file needs to be validated before proceeding to perform monte-carlo simulations.

Parameters:

  • sampling_strategy: str The chosen sampling strategy from chaospy, scipy and pydoe2
  • samples: int The number of samples to generate for the simulation
  • distribution: str The name of the probability distribution.
  • distribution_params: list The parameters associated with the probability distribution.

Raises:

  • ValueError: If the parameters are invalid for the specified distribution.