oasislmf.pytools.gul.random

This file contains the utilities for generating random numbers in gulpy.

Attributes

Functions

generate_hash(group_id, event_id[, base_seed])

Generate hash for a given group_id, event_id pair for the vulnerability pdf.

generate_hash_hazard(hazard_group_id, event_id[, ...])

Generate hash for a given hazard_group_id, event_id pair for the hazard pdf.

get_random_generator(random_generator)

Get the random generator function.

generate_correlated_hash_vector(...[, base_seed])

Generate hashes for all peril correlation groups for a given event_id.

compute_norm_inv_cdf_lookup(cdf_min, cdf_max, N)

compute_norm_cdf_lookup(x_min, x_max, N)

get_corr_rval(x_unif, y_unif, rho, x_min, ...)

Calculate the correlated random values with precomputed inv_factor and norm_factor.

random_MersenneTwister(seeds, n[, skip_seeds])

Generate random numbers using the default Mersenne Twister algorithm.

random_LatinHypercube(seeds, n[, skip_seeds])

Generate random numbers using the Latin Hypercube algorithm.

random_LatinHypercube_Philox7(seeds, n[, skip_seeds])

Latin Hypercube on Philox4x32-7 (random_generator=2).

Module Contents

oasislmf.pytools.gul.random.logger[source]
oasislmf.pytools.gul.random.GROUP_ID_HASH_CODE[source]
oasislmf.pytools.gul.random.EVENT_ID_HASH_CODE[source]
oasislmf.pytools.gul.random.HASH_MOD_CODE[source]
oasislmf.pytools.gul.random.HAZARD_GROUP_ID_HASH_CODE[source]
oasislmf.pytools.gul.random.HAZARD_EVENT_ID_HASH_CODE[source]
oasislmf.pytools.gul.random.HAZARD_HASH_MOD_CODE[source]
oasislmf.pytools.gul.random.PHILOX_M0[source]
oasislmf.pytools.gul.random.PHILOX_M1[source]
oasislmf.pytools.gul.random.PHILOX_W0[source]
oasislmf.pytools.gul.random.PHILOX_W1[source]
oasislmf.pytools.gul.random.PHILOX_U32_MASK[source]
oasislmf.pytools.gul.random.PHILOX_SHIFT32[source]
oasislmf.pytools.gul.random.PHILOX_INV32[source]
oasislmf.pytools.gul.random.PHILOX_STREAM_JITTER[source]
oasislmf.pytools.gul.random.PHILOX_STREAM_SHUFFLE[source]
oasislmf.pytools.gul.random.x_min = 1e-16[source]
oasislmf.pytools.gul.random.x_max = 0.9999999999999999[source]
oasislmf.pytools.gul.random.norm_inv_N = 1000000[source]
oasislmf.pytools.gul.random.cdf_min = -20[source]
oasislmf.pytools.gul.random.cdf_max = 20.0[source]
oasislmf.pytools.gul.random.inv_factor = 999999.0000000002[source]
oasislmf.pytools.gul.random.norm_factor = 24999.975[source]
oasislmf.pytools.gul.random.epsilon = 0.025[source]
oasislmf.pytools.gul.random.generate_hash(group_id, event_id, base_seed=0)[source]

Generate hash for a given group_id, event_id pair for the vulnerability pdf.

Parameters:
  • group_id (int) – group id.

  • event_id (int]) – event id.

  • base_seed (int, optional) – base random seed. Defaults to 0.

Returns:

hash

Return type:

int64

oasislmf.pytools.gul.random.generate_hash_hazard(hazard_group_id, event_id, base_seed=0)[source]

Generate hash for a given hazard_group_id, event_id pair for the hazard pdf.

Parameters:
  • hazard_group_id (int) – group id.

  • event_id (int]) – event id.

  • base_seed (int, optional) – base random seed. Defaults to 0.

Returns:

hash

Return type:

int64

oasislmf.pytools.gul.random.get_random_generator(random_generator)[source]

Get the random generator function.

Parameters:

random_generator (int) – random generator function id.

Returns:

The random generator function.

oasislmf.pytools.gul.random.EVENT_ID_HASH_CODE[source]
oasislmf.pytools.gul.random.PERIL_CORRELATION_GROUP_HASH[source]
oasislmf.pytools.gul.random.HASH_MOD_CODE[source]
oasislmf.pytools.gul.random.generate_correlated_hash_vector(unique_peril_correlation_groups, event_id, correlated_hashes, base_seed=0)[source]

Generate hashes for all peril correlation groups for a given event_id.

Parameters:
  • unique_peril_correlation_groups (List[int]) – list of the unique peril correlation groups.

  • event_id (int) – event id.

  • correlated_hashes – empty buffer for the output (size of max group id not the number of group id)

  • base_seed (int, optional) – base random seed. Defaults to 0.

oasislmf.pytools.gul.random.compute_norm_inv_cdf_lookup(cdf_min, cdf_max, N)[source]
oasislmf.pytools.gul.random.compute_norm_cdf_lookup(x_min, x_max, N)[source]
oasislmf.pytools.gul.random.get_corr_rval(x_unif, y_unif, rho, x_min, norm_inv_cdf, inv_factor, cdf_min, norm_cdf, norm_factor, Nsamples, z_unif)[source]

Calculate the correlated random values with precomputed inv_factor and norm_factor.

inv_factor = (N - 1) / (x_max - x_min) norm_factor = (N - 1) / (cdf_max - cdf_min) Uses fast lookup for the middle values and interpolation for the tail values.

Previously get_corr_rval_float used by GulMC, now shared by both GULs

oasislmf.pytools.gul.random.random_MersenneTwister(seeds, n, skip_seeds=0)[source]

Generate random numbers using the default Mersenne Twister algorithm.

Parameters:
  • seeds (List[int64]) – List of seeds.

  • n (int) – number of random samples to generate for each seed.

  • skip_seeds (int) – number of seeds to skip starting from the beginning of the seeds array. For skipped seeds no random numbers are generated and the output rndms will contain zeros at their corresponding row. Default is 0, i.e. no seeds are skipped.

Returns:

2-d array of shape (number of seeds, n)

containing the random values generated for each seed.

rndms_idx (Dict[int64, int]): mapping between seed and the

row in rndms that stores the corresponding random values.

Return type:

rndms (array[float])

oasislmf.pytools.gul.random.random_LatinHypercube(seeds, n, skip_seeds=0)[source]

Generate random numbers using the Latin Hypercube algorithm.

Parameters:
  • seeds (List[int64]) – List of seeds.

  • n (int) – number of random samples to generate for each seed.

  • skip_seeds (int) – number of seeds to skip starting from the beginning of the seeds array. For skipped seeds no random numbers are generated and the output rndms will contain zeros at their corresponding row. Default is 0, i.e. no seeds are skipped.

Returns:

2-d array of shape (number of seeds, n)

containing the random values generated for each seed.

rndms_idx (Dict[int64, int]): mapping between seed and the

row in rndms that stores the corresponding random values.

Return type:

rndms (array[float])

Notes

Implementation follows scipy.stats.qmc.LatinHypercube v1.8.0. Following scipy notation, here we assume centered=False all the times: instead of taking samples=0.5*np.ones(n), here we always draw uniform random samples in order to initialise samples.

oasislmf.pytools.gul.random.random_LatinHypercube_Philox7(seeds, n, skip_seeds=0)[source]

Latin Hypercube on Philox4x32-7 (random_generator=2).

See the module comment above random_LatinHypercube_Philox7 for the algorithm.

Parameters:
  • seeds (array[int]) – per-row seeds (a hash of group_id/event_id).

  • n (int) – number of samples to generate for each seed.

  • skip_seeds (int) – number of leading rows to skip (left as zeros); correlation arrays pass 1.

Returns:

2-d array of shape (len(seeds), n) of LH samples in (0, 1].

Return type:

rndms (array[float64])