Monte_Carlo_Analysis#

Classes:

Monte_Carlo_Analysis(input_file)

Monte Carlo analysis of a techno-economic model.

Functions:

calculate_distance(data, parameters, selection)

Distance of datapoints to reference is calculated using the specified metric.

coordinate_position(x_reference, x_values, ...)

Determine correct position for base case label in plot_colored_scatter().

divide_into_batches(array, batch_size)

Divide provided array into batches of size batch_size for parallel processing

extend_limits(limits_original, extension)

Extend limits_original in both directions by muliplying with extensions

normalize_parameter(parameter, base, limit)

Linear of log normalization of parameter (float or array) based on base and limit values.

select_non_reference_value(reference, values)

Select value from values which is not the reference one.

class pyH2A.Analysis.Monte_Carlo_Analysis.Monte_Carlo_Analysis(input_file)[source]#

Monte Carlo analysis of a techno-economic model.

Parameters
Monte_Carlo_Analysis > Samples > Valueint

Number of samples for Monte Carlo analysis.

Monte_Carlo_Analysis > Target Price Range ($) > Valuestr

Target price range for H2 specfied in the following format: lower value; higher value (e.g. “1.5: 1.54”).

Monte_Carlo_Analysis > Output File > Valuestr, optional

Path to location where output file containing Monte Carlo analysis results should be saved.

Monte_Carlo_Analysis > Input File > Valuestr, optional

Path to location of file containing Monte Carlo analysis results that should be read.

Parameters - Monte_Carlo_Analysis > […] > Namestr

Display name for parameter, e.g. used for axis labels.

Parameters - Monte_Carlo_Analysis > […] > Typestr

Type of parameter values. If Type is ‘value’, provided values are used as is. If Type is ‘factor’, provided values are multiplied with base value of parameter in input file.

Parameters - Monte_Carlo_Analysis > […] > Valuesstr

Value range for parameter in Monte Carlo analysis. Specified in following format: upper limit; lower limit (order is irrelevant). Instead of actual values, ‘Base’ or ‘Reference’ can be used to retrieve base value of parameter in input file as one of the values. E.g. ‘Base; 20%’.

Parameters - Monte_Carlo_Analysis > […] > File Indexint, optional

If Monte Carlo results are read from a provided input file, File Index for each parameter can be specified. File Index refers to the column position of each parameter in the read input file. This mapping allows for changing the display name and position of a parameter in the Parameters - Monte_Carlo_Analysis analysis table and still ensure correct mapping of the read results.

Notes

Parameters - Monte_Carlo_Analysis contains parameters which are to be varied in Monte Carlo Analysis. First column specifies path to parameter in input file (top key > middle key > bottom key format, e.g. Catalyst > Cost per kg ($) > Value). Order of parameters can be changed, which for example affects the mapping onto different axis in plot_colored_scatter (first parameter is on x axis, second on y axis, etc.).

Methods:

check_parameter_integrity(values)

Checking that parameters in self.results are within ranges specified in self.parameters['Values'].

determine_principal_components()

Converting parameters to list sorted by input index.

development_distance([metric, ...])

Calculation of development distance for models within target price range.

full_distance_cost_relationship([metric, ...])

Calculation of development distance for all datapoints from Monte Carlo Analysis and calculation of Savitzky-Golay filter.

generate_parameter_string_table([...])

String of parameter table is generated, used in self.render_parameter_table.

perform_full_monte_carlo()

Monte Carlo analysis is performed based on random parameter variations in self.values.

perform_h2_cost_calculation(values)

H2 cost calculation for provided parameter values is performed.

perform_monte_carlo_multiprocessing(values)

Monte Carlo analysis is performed with multiprocessing parallelization across all available CPUs.

plot_colored_scatter([limit_extension, ...])

Plotting colored scatter plot showing all models within target price range.

plot_colored_scatter_3D([limit_extension, ...])

3D colored scatter plot of models within target price range.

plot_complete_histogram([bins, xlim_low, ...])

Complete histogram of price distribution from Monte Carlo analysis

plot_distance_cost_relationship([ax, ylim, ...])

Plotting relationship of development distance and H2 cost.

plot_distance_histogram([ax, bins, ...])

Plotting development distances as histogram.

plot_target_parameters_by_distance([ax, ...])

process_parameters()

Monte Carlo Analysis parameters are read from 'Monte Carlo Analysis - Parameters' in self.inp and processed.

read_results(file_name)

Reads Monte Carlo simulation results from file_name.

render_parameter_table(ax[, xpos, ypos, ...])

Rendering table of parameters which are varied during Monte Carlo analysis.

save_results(file_name)

Results of Monte Carlo simulation are saved in file_name and a formatted header is added.

target_price_2D_region([grid_points])

Determining largest region spanned by first two parameters within which target prices can be achieved.

target_price_components()

Monte Carlo simulation results are sorted by H2 cost and the entries of self.results with a H2 cost within the specified target price range are stored in self.target_price_data.

check_parameter_integrity(values)[source]#

Checking that parameters in self.results are within ranges specified in self.parameters[‘Values’].

determine_principal_components()[source]#

Converting parameters to list sorted by input index.

development_distance(metric='cityblock', log_normalize=False, sum_distance=False)[source]#

Calculation of development distance for models within target price range.

Parameters
metric: str, optional

Metric used for distance calculation, defaults to cityblock.

Returns
self.distances: ndarrray

Array containing distances for models within target price range.

Notes

The euclidean or cityblock distance in n-dimensional space of each Monte Carlo simulation datapoint within the target price range to the reference point is calculated and stored in self.distances. Parameter ranges and the reference parameters are scaled to be within a n-dimensional unit cube. Distances are normalized by the number of dimensions, so that the maximum distance is always 1.

full_distance_cost_relationship(metric='cityblock', reduction_factor=25, poly_order=4, log_normalize=False, sum_distance=False)[source]#

Calculation of development distance for all datapoints from Monte Carlo Analysis and calculation of Savitzky-Golay filter.

Parameters
metricstr, optional

Distance metric used for calculate_distance()

reduction_factorint, optional

Determines window size for Savitzky-Golay filter.

poly_orderint, optional

Order of polynomial for Savitzky-Golay filter

Returns
self.results_distances_sortedndarray

Sorted array of distances for all datapoints from Monte Carlo Analysis.

self.distances_cost_savgolndarray

Savitzky-Golay filter results.

generate_parameter_string_table(base_string='Base', limit_string='Limit', format_cutoff=6)[source]#

String of parameter table is generated, used in self.render_parameter_table.

Parameters
base_stringstr, optional

String used to label base column.

limit_stringstr, optinal

String used to label limit column.

format_cutoffint

Length of number string at which it is converted to scientific/millified representation.

perform_full_monte_carlo()[source]#

Monte Carlo analysis is performed based on random parameter variations in self.values.

perform_h2_cost_calculation(values)[source]#

H2 cost calculation for provided parameter values is performed.

Parameters
valuesndarray

Array containing parameter variations.

Returns
h2_costndarray

1D array of H2 cost values for each set of parameters.

Notes

Performs H2 cost calulation by modifying a copy of self.inp based on the provided values and self.parameters. The modified copy of self.inp is then passed to Discounted_Cash_Flow(). A parameter value can be either a value replacing the existing one in self.inp (Type = value) or it can be a factor hich will be multiplied by the existing value.

perform_monte_carlo_multiprocessing(values, return_full_array=True)[source]#

Monte Carlo analysis is performed with multiprocessing parallelization across all available CPUs.

Parameters
valuesndarray

2D array containing parameters variations which are to be evaluated.

return_full_arraybool, optional

If return_full_array is True, the full 2D array containing parameter variations and H2 cost is returned. Otherwise, a 1D array containing only H2 cost values is returned.

Returns
full_arrayndarray

2D array containing parameter variations and H2 cost values.

h2_costndarray

1D array containing H2 costvalues.

plot_colored_scatter(limit_extension=0.03, title_string='Target cost range: ', base_string='Base', image_kwargs={}, plot_kwargs={}, **kwargs)[source]#

Plotting colored scatter plot showing all models within target price range.

Parameters
limit_extension: float, optional

Amount of limit extension of axes as fraction of axis range.

title_stringstr, optional

String for title.

base_stringstr, optional

String to label base case datapoint.

image_kwargs: dict, optional

Dictionary containing optional keyword arguments for insert_image()

plot_kwargs: dict, optional

Dictionary containing optional keyword arguments for Figure_Lean(), has priority over **kwargs.

**kwargs:

Additional kwargs passed to Figure_Lean()

Returns
figurematplotlib.fig or None

matplotlib.fig is returned.

Notes

x, y and color axis are determined by determine_principal_components(), with pc[0] being the x axis, pc[1] the y axis and pc[2] the color axis. Order can be changed by changing order of parameters in input file.

plot_colored_scatter_3D(limit_extension=0.03, title_string='Target cost range: ', **kwargs)[source]#

3D colored scatter plot of models within target price range.

Parameters
limit_extension: float, optional

Amount of limit extension of axes as fraction of axis range.

title_string: str, optional

Title string.

Returns
fig: matplotlib.figure

Figure object.

plot_complete_histogram(bins=None, xlim_low=None, xlim_high=None, xlabel_string='Levelized $H_{2}$ Cost / \\$/kg', ylabel_string='Normalized Frequency', image_kwargs={}, plot_kwargs={}, **kwargs)[source]#

Complete histogram of price distribution from Monte Carlo analysis

Parameters
binsint, optional

Number of bins for histogram. If None, bins is calculated from the size of self.results.

xlim_lowfloat or None, optional

Lower x axis limit.

xlim_highfloat or None, optional

Higher x axis limit.

xlabel_stringstr, optional

String for x axis label.

ylabel_stringstr, optional

String for y axis label.

image_kwargs: dict, optional

Dictionary containing optional keyword arguments for insert_image()

plot_kwargs: dict, optional

Dictionary containing optional keyword arguments for Figure_Lean(), has priority over **kwargs.

**kwargs:

Additional kwargs passed to Figure_Lean()

Returns
figurematplotlib.fig or None

matplotlib.fig is returned.

plot_distance_cost_relationship(ax=None, ylim=None, xlim=None, figure_lean=True, parameter_table=True, legend_loc='upper left', log_scale=False, xlabel_string='Development distance', ylabel_string='Levelized $H_{2}$ cost / \\$/kg', linewidth=1.5, markersize=0.2, marker_alpha=0.2, table_kwargs={}, image_kwargs={}, plot_kwargs={}, **kwargs)[source]#

Plotting relationship of development distance and H2 cost.

Parameters
axmatplotlib.axes, optional

Axes object in which plot is drawn. Default is None, creating new plot.

ylimarray, optional

Ordered limit values for y axis. Default is None.

xlimarray, optional

Ordered limit values for x axis. Default is None.

figure_leanbool, optional

If figure_lean is True, matplotlib.fig object is returned.

parameter_tablebool, optional

If parameter_table is True, the parameter table is shown in the plot.

legend_locstr, optional

Controls location of legend in plot. Defaults to ‘upper left’.

log_scalebool, optional

If log_scale is True, the y axis will use a log scale.

xlabel_stringstr, optional

String for x axis label.

ylabel_stringstr, optional

String for y axis label.

linewidthfloat, optional

Line width for smoothed trendline.

markersizefloat, optional

Size of markers in scatter plot.

marker_alphafloat, optional

Transparency of markers in scatter plot (0: maximum transpareny, 1: no transparency).

table_kwargsdict, optional

Dictionary containing optional keyword arguments for render_parameter_table()

image_kwargs: dict, optional

Dictionary containing optional keyword arguments for insert_image()

plot_kwargs: dict, optional

Dictionary containing optional keyword arguments for Figure_Lean(), has priority over **kwargs.

**kwargs:

Additional kwargs passed to Figure_Lean()

Returns
figurematplotlib.fig or None

matplotlib.fig is returned if figure_lean is True.

plot_distance_histogram(ax=None, bins=25, figure_lean=True, xlabel=False, title=True, xlabel_string='Development distance', ylabel_string='Frequency', title_string='Target cost range:', show_parameter_table=True, show_mu=True, mu_x=0.2, mu_y=0.5, table_kwargs={}, image_kwargs={}, plot_kwargs={}, **kwargs)[source]#

Plotting development distances as histogram.

Parameters
axmatplotlib.axes, optional

Axes object in which plot is drawn. Default is None, creating new plot.

binsint, optional

Number of bins for histogram.

figure_leanbool, optional

If figure_lean is True, matplotlib.fig object is returned.

xlabelbool, optional

Flag to control if x axis label is displayed or not.

titlebool, optional

Flag to control if title is displayed or not.

title_stringstr, optional

String for title.

xlabel_stringstr, optional

String for x axis label.

ylabel_stringstr, optional

String for y axis label.

show_parameter_tablebool, optional

Flag to control if parameter table is shown.

show_mubool, optional

Flag to control if mu and sigma values of normal distribution are shown.

mu_xfloat, optional

x axis coordinate of shown mu and sigma values in axis coordinates.

mu_yfloat, optional

y axis coordinate of shown mu and sigma values in axis coordinates.

table_kwargsdict, optional

Dictionary containing optional keyword arguments for render_parameter_table()

image_kwargs: dict, optional

Dictionary containing optional keyword arguments for insert_image()

plot_kwargs: dict, optional

Dictionary containing optional keyword arguments for Figure_Lean(), has priority over **kwargs.

**kwargs:

Additional kwargs passed to Figure_Lean()

Returns
figurematplotlib.fig or None

matplotlib.fig is returned if figure_lean is True.

plot_target_parameters_by_distance(ax=None, figure_lean=True, table_kwargs={}, image_kwargs={}, plot_kwargs={}, **kwargs)[source]#
process_parameters()[source]#

Monte Carlo Analysis parameters are read from ‘Monte Carlo Analysis - Parameters’ in self.inp and processed.

Notes

The ranges for each parameter are defined in Values column as ‘;’ seperated entries. Entries can either be a number, a path, or a special_value such as Base or Reference. If such a special_value is specified, the base value of that parameter is retrieved from self.inp. Parameter information is stored in self.parameters attribute. Based on the ranges for each parameter, random values (uniform distribution) are generated and stored in the self.values attribute. The target price range is read from self.inp file and stored in self.target_price_range attribute.

read_results(file_name)[source]#

Reads Monte Carlo simulation results from file_name.

Parameters
file_namestr

Path to file containing Monte Carlo simulation results.

Returns
self.resultsndarray

Array containing parameters and H2 cost for each model.

self.parametersdict

Dictionary containing information on varied parameters.

self.target_price_rangendarray

Selected target price range from self.inp.

Notes

Assumes formatting created by self.save_results()`function. Header must contain name of parameters, path to parameters in input file, type of parameter and value range. The header is processed to retrieve these atrribtues and stores them in `self.parameters. Reference values for each parameter and target price range are read from self.inp The order of parameters is also read from self.inp and stored in self.parameters as Input Index. If the name of a parameter has been changed in self.inp and is different from the parameter stored in file_name, it is checked whether a File Index is specified, which allows for mapping of renamed parameter to parameter stored in File Index. If File Index is specified, the existing parameter at this position in File Index is renamed to the specified name.

render_parameter_table(ax, xpos=1.05, ypos=0.0, height=1.0, colWidths=[0.55, 0.25, 0.07, 0.25], left_pad=0.01, edge_padding=0.02, fontsize=12, base_string='Base', limit_string='Limit', format_cutoff=7)[source]#

Rendering table of parameters which are varied during Monte Carlo analysis.

Parameters
axmatplotlib.axes

Axes object in which parameter table is displayed.

xposfloat, optional

x axis position of left edge of table in axis fraction coordinates.

yposfloat, optional

y axis position of lower edge of table in axis fraction coordinates.

heightfloat, optional

Height of table in axis fraction coordinates (e.g. height = 0.5 meaning that the table has half the height of the plot).

colWidthslist, optional

List of length 4 with widths for each column from left to right in axis fraction coordinates.

left_padfloat, optional

Padding of the table on the left site.

edge_padding: float, optional

Padding of edges that are drawn.

fontsizefloat, optional

fontsize for table.

base_stringstr, optional

String used to label base column.

limit_stringstr, optional

String used to label limit column.

Notes

Table is rendered in provided matplotlib.axes object.

save_results(file_name)[source]#

Results of Monte Carlo simulation are saved in file_name and a formatted header is added. Contains name, parameter path, type and values range from self.parameters.

target_price_2D_region(grid_points=15)[source]#

Determining largest region spanned by first two parameters within which target prices can be achieved.

Parameters
grid_pointsint, optional

Number of grid points to determine density of grid evaluation.

Returns
self.target_price_2D_regiondict

Dict of ndarrays with information of target price 2D region.

Notes

Model is evaluated on grid spanned by first two parameters (density of grid is controlled by grid_points), other parameters are set to limit (non-reference) values. Output is a dictionary (self.target_price_2D_region), which can be used to overlay target price region onto scatter ploting using plt.contourf

target_price_components()[source]#

Monte Carlo simulation results are sorted by H2 cost and the entries of self.results with a H2 cost within the specified target price range are stored in self.target_price_data.

pyH2A.Analysis.Monte_Carlo_Analysis.calculate_distance(data, parameters, selection, metric='cityblock', log_normalize=False, sum_distance=False)[source]#

Distance of datapoints to reference is calculated using the specified metric.

Parameters
datandarray

2D array of datapoints containing parameter values for each model.

parametersdict

Dictionary specifying ranges for each parameter.

selectionlist

List of parameters names used for distance calculation.

metricstr, optional

Metric used for distance calculation (e.g. ‘cityblock’ or ‘euclidean’). Default is ‘cityblock’.

log_normalizebool, optional

Flag to control if log normalization is used instead of linear normalization.

sum_distancebool, optional

Flag to control if distance is calculated by simply summing individual normalized values (equal to cityblock distance but without using absolute values, hence distance can be negative).

Returns
distances: ndarray

Array containing distance for each model.

Notes

Parameter ranges and the reference parameters are scaled to be within a n-dimensional unit cube. Distances are normalized by the number of dimensions, so that the maximum distance is always 1.

pyH2A.Analysis.Monte_Carlo_Analysis.coordinate_position(x_reference, x_values, y_reference, y_values)[source]#

Determine correct position for base case label in plot_colored_scatter().

pyH2A.Analysis.Monte_Carlo_Analysis.divide_into_batches(array, batch_size)[source]#

Divide provided array into batches of size batch_size for parallel processing

pyH2A.Analysis.Monte_Carlo_Analysis.extend_limits(limits_original, extension)[source]#

Extend limits_original in both directions by muliplying with extensions

pyH2A.Analysis.Monte_Carlo_Analysis.normalize_parameter(parameter, base, limit, log_normalize=False)[source]#

Linear of log normalization of parameter (float or array) based on base and limit values.

pyH2A.Analysis.Monte_Carlo_Analysis.select_non_reference_value(reference, values)[source]#

Select value from values which is not the reference one.