# bw2calc.monte_carlo#

## Module Contents#

### Classes#

 ComparativeMonteCarlo First draft approach at comparative LCA DirectSolvingMixin Base class to use iterative techniques instead of LU factorization in Monte Carlo. DirectSolvingMonteCarloLCA Monte Carlo uncertainty analysis with separate random number generators (RNGs) for each set of parameters. IterativeMonteCarlo Base class to use iterative techniques instead of LU factorization in Monte Carlo. MonteCarloLCA Monte Carlo uncertainty analysis with separate random number generators (RNGs) for each set of parameters. MultiMonteCarlo This is a class for the efficient calculation of many demand vectors from ParallelMonteCarlo Split a Monte Carlo calculation into parallel jobs

### Functions#

 multi_worker(args) Calculate a single Monte Carlo iteration for many demands. pool_adapter(pool) single_worker(args)
class bw2calc.monte_carlo.ComparativeMonteCarlo(demands, *args, **kwargs)[source]#

First draft approach at comparative LCA

Create a new LCA calculation.

Parameters
• demand (*) – The demand or functional unit. Needs to be a dictionary to indicate amounts, e.g. {("my database", "my process"): 2.5}.

• method (*) – LCIA Method tuple, e.g. ("My", "great", "LCIA", "method"). Can be omitted if only interested in calculating the life cycle inventory.

Returns

A new LCA object

class bw2calc.monte_carlo.DirectSolvingMixin(demand, method=None, iter_solver=cgs, seed=None, *args, **kwargs)[source]#

Base class to use iterative techniques instead of LU factorization in Monte Carlo.

Create a new LCA calculation.

Parameters
• demand (*) – The demand or functional unit. Needs to be a dictionary to indicate amounts, e.g. {("my database", "my process"): 2.5}.

• method (*) – LCIA Method tuple, e.g. ("My", "great", "LCIA", "method"). Can be omitted if only interested in calculating the life cycle inventory.

Returns

A new LCA object

solve_linear_system()[source]#

Master solution function for linear system $$Ax=B$$.

To most numerical analysts, matrix inversion is a sin.

—Nicolas Higham, Accuracy and Stability of Numerical Algorithms, Society for Industrial and Applied Mathematics, Philadelphia, PA, USA, 2002, p. 260.

We use UMFpack, which is a very fast solver for sparse matrices.

If the technosphere matrix has already been factorized, then the decomposed technosphere (self.solver) is reused. Otherwise the calculation is redone completely.

class bw2calc.monte_carlo.DirectSolvingMonteCarloLCA(demand, method=None, iter_solver=cgs, seed=None, *args, **kwargs)[source]#

Monte Carlo uncertainty analysis with separate random number generators (RNGs) for each set of parameters.

Create a new LCA calculation.

Parameters
• demand (*) – The demand or functional unit. Needs to be a dictionary to indicate amounts, e.g. {("my database", "my process"): 2.5}.

• method (*) – LCIA Method tuple, e.g. ("My", "great", "LCIA", "method"). Can be omitted if only interested in calculating the life cycle inventory.

Returns

A new LCA object

class bw2calc.monte_carlo.IterativeMonteCarlo(demand, method=None, iter_solver=cgs, seed=None, *args, **kwargs)[source]#

Base class to use iterative techniques instead of LU factorization in Monte Carlo.

Create a new LCA calculation.

Parameters
• demand (*) – The demand or functional unit. Needs to be a dictionary to indicate amounts, e.g. {("my database", "my process"): 2.5}.

• method (*) – LCIA Method tuple, e.g. ("My", "great", "LCIA", "method"). Can be omitted if only interested in calculating the life cycle inventory.

Returns

A new LCA object

solve_linear_system()[source]#

Master solution function for linear system $$Ax=B$$.

To most numerical analysts, matrix inversion is a sin.

—Nicolas Higham, Accuracy and Stability of Numerical Algorithms, Society for Industrial and Applied Mathematics, Philadelphia, PA, USA, 2002, p. 260.

We use UMFpack, which is a very fast solver for sparse matrices.

If the technosphere matrix has already been factorized, then the decomposed technosphere (self.solver) is reused. Otherwise the calculation is redone completely.

class bw2calc.monte_carlo.MonteCarloLCA(demand, method=None, iter_solver=cgs, seed=None, *args, **kwargs)[source]#

Monte Carlo uncertainty analysis with separate random number generators (RNGs) for each set of parameters.

Create a new LCA calculation.

Parameters
• demand (*) – The demand or functional unit. Needs to be a dictionary to indicate amounts, e.g. {("my database", "my process"): 2.5}.

• method (*) – LCIA Method tuple, e.g. ("My", "great", "LCIA", "method"). Can be omitted if only interested in calculating the life cycle inventory.

Returns

A new LCA object

class bw2calc.monte_carlo.MultiMonteCarlo(demands, method, iterations, cpus=None)[source]#

Bases: object

This is a class for the efficient calculation of many demand vectors from each Monte Carlo iteration.

Parameters
• dictionaries (* args is a list of demand) –

• method (* method is a LCIA) –

• desired (* iterations is the number of Monte Carlo iterations) –

• the (* cpus is) –

The input list can have complex demands, so [{('foo', 'bar'): 1, ('foo', 'baz'): 1}, {('foo', 'another'): 1}] is OK.

Call .calculate() to generate results.

calculate(worker=multi_worker)[source]#

Calculate Monte Carlo results for many demand vectors.

Returns a list of results with the format:

[(demand dictionary, [lca scores])]


There is no guarantee that the results are returned in the same order as the demand input variable.

merge_results(objs)[source]#

Merge the results from each multi_worker worker.

[('a', [0,1]), ('a', [2,3])] becomes [('a', [0,1,2,3)].

class bw2calc.monte_carlo.ParallelMonteCarlo(demand, method, iterations=1000, chunk_size=None, cpus=None, log_config=None)[source]#

Bases: object

Split a Monte Carlo calculation into parallel jobs

calculate(worker=single_worker)[source]#
bw2calc.monte_carlo.direct_solving_worker(args)[source]#
bw2calc.monte_carlo.multi_worker(args)[source]#

Calculate a single Monte Carlo iteration for many demands.

args are in order:
• project: Name of project

• demands: List of demand dictionaries

• method: LCIA method

Returns a list of results: [(demand dictionary, result)]