worst package

Submodules

worst.constants module

worst.sci module

Fit GEV using scipy and compare to known upper bound.

worst.sci.bg_pdf(z, z_star, beta, gamma)

Upper bound known GEV probability density function.

Parameters:
  • z (np.ndarray) – data.

  • z_star (float) – Upper bound.

  • beta (float) – Scale parameter.

  • gamma (float) – Shape parameter.

Returns:

Probability density function.

Return type:

np.ndarray

worst.sci.evt_fig_scipy(z_star=7, beta=1, gamma=-0.2, ex_seed=42, ex_num=50, min_samp=20, max_samp=1000, samp_steps=100, seed_steps=1000, save_fig_path='/home/docs/checkouts/readthedocs.org/user_builds/worstsurge/checkouts/latest/img/evt_fig_scipy.pdf', color_true='black', color_max_known='#1b9e77', color_max_unknown='#d95f02')

Fit GEV using scipy with or without knowing an upper bound.

Plot the systematic comparison of what difference it makes for the 1 in 100 and 1 in 500 year return values when fitting a GEV distribution to data sampled from a known upper bound GEV distribution, varying the random seed and the number of samples to plot how the sampling error decreases in each case.

Parameters:
  • z_star (float, optional) – Upper bound. Defaults to 7.

  • beta (float, optional) – Scale. Defaults to 1.

  • gamma (float, optional) – Shape. Defaults to -0.2.

  • ex_seed (int, optional) – Example fit seed for (a). Defaults to 42.

  • ex_num (int, optional) – Example number. Defaults to 50.

  • min_samp (int, optional) – Minimum number of samples. Defaults to 20.

  • max_samp (int, optional) – Maximum number of samples. Defaults to 1000.

  • samp_steps (int, optional) – How many different sample sizes to choose. Defaults to 100.

  • seed_steps (int, optional) – _description_. Defaults to 1000.

  • save_fig_path (str, optional) – Defaults to os.path.join(FIGURE_PATH, “evt_fig_scipy.pdf”).

  • color_true (str, optional) – _description_. Defaults to “black”.

  • color_max_known (str, optional) – _description_. Defaults to “#1b9e77”.

  • color_max_unknown (str, optional) – _description_. Defaults to “#d95f02”.

worst.sci.fit_gev(z)
Return type:

Tuple[float, float, float]

worst.sci.fit_seeds(z_star, beta, gamma, seeds=array([0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, 410, 420, 430, 440, 450, 460, 470, 480, 490, 500, 510, 520, 530, 540, 550, 560, 570, 580, 590, 600, 610, 620, 630, 640, 650, 660, 670, 680, 690, 700, 710, 720, 730, 740, 750, 760, 770, 780, 790, 800, 810, 820, 830, 840, 850, 860, 870, 880, 890, 900, 910, 920, 930, 940, 950, 960, 970, 980, 990, 1001, 1011, 1021, 1031, 1041, 1051, 1061, 1071, 1081, 1091, 1101, 1111, 1121, 1131, 1141, 1151, 1161, 1171, 1181, 1191, 1201, 1211, 1221, 1231, 1241, 1251, 1261, 1271, 1281, 1291, 1301, 1311, 1321, 1331, 1341, 1351, 1361, 1371, 1381, 1391, 1401, 1411, 1421, 1431, 1441, 1451, 1461, 1471, 1481, 1491, 1501, 1511, 1521, 1531, 1541, 1551, 1561, 1571, 1581, 1591, 1601, 1611, 1621, 1631, 1641, 1651, 1661, 1671, 1681, 1691, 1701, 1711, 1721, 1731, 1741, 1751, 1761, 1771, 1781, 1791, 1801, 1811, 1821, 1831, 1841, 1851, 1861, 1871, 1881, 1891, 1901, 1911, 1921, 1931, 1941, 1951, 1961, 1971, 1981, 1991, 2002, 2012, 2022, 2032, 2042, 2052, 2062, 2072, 2082, 2092, 2102, 2112, 2122, 2132, 2142, 2152, 2162, 2172, 2182, 2192, 2202, 2212, 2222, 2232, 2242, 2252, 2262, 2272, 2282, 2292, 2302, 2312, 2322, 2332, 2342, 2352, 2362, 2372, 2382, 2392, 2402, 2412, 2422, 2432, 2442, 2452, 2462, 2472, 2482, 2492, 2502, 2512, 2522, 2532, 2542, 2552, 2562, 2572, 2582, 2592, 2602, 2612, 2622, 2632, 2642, 2652, 2662, 2672, 2682, 2692, 2702, 2712, 2722, 2732, 2742, 2752, 2762, 2772, 2782, 2792, 2802, 2812, 2822, 2832, 2842, 2852, 2862, 2872, 2882, 2892, 2902, 2912, 2922, 2932, 2942, 2952, 2962, 2972, 2982, 2992, 3003, 3013, 3023, 3033, 3043, 3053, 3063, 3073, 3083, 3093, 3103, 3113, 3123, 3133, 3143, 3153, 3163, 3173, 3183, 3193, 3203, 3213, 3223, 3233, 3243, 3253, 3263, 3273, 3283, 3293, 3303, 3313, 3323, 3333, 3343, 3353, 3363, 3373, 3383, 3393, 3403, 3413, 3423, 3433, 3443, 3453, 3463, 3473, 3483, 3493, 3503, 3513, 3523, 3533, 3543, 3553, 3563, 3573, 3583, 3593, 3603, 3613, 3623, 3633, 3643, 3653, 3663, 3673, 3683, 3693, 3703, 3713, 3723, 3733, 3743, 3753, 3763, 3773, 3783, 3793, 3803, 3813, 3823, 3833, 3843, 3853, 3863, 3873, 3883, 3893, 3903, 3913, 3923, 3933, 3943, 3953, 3963, 3973, 3983, 3993, 4004, 4014, 4024, 4034, 4044, 4054, 4064, 4074, 4084, 4094, 4104, 4114, 4124, 4134, 4144, 4154, 4164, 4174, 4184, 4194, 4204, 4214, 4224, 4234, 4244, 4254, 4264, 4274, 4284, 4294, 4304, 4314, 4324, 4334, 4344, 4354, 4364, 4374, 4384, 4394, 4404, 4414, 4424, 4434, 4444, 4454, 4464, 4474, 4484, 4494, 4504, 4514, 4524, 4534, 4544, 4554, 4564, 4574, 4584, 4594, 4604, 4614, 4624, 4634, 4644, 4654, 4664, 4674, 4684, 4694, 4704, 4714, 4724, 4734, 4744, 4754, 4764, 4774, 4784, 4794, 4804, 4814, 4824, 4834, 4844, 4854, 4864, 4874, 4884, 4894, 4904, 4914, 4924, 4934, 4944, 4954, 4964, 4974, 4984, 4994, 5005, 5015, 5025, 5035, 5045, 5055, 5065, 5075, 5085, 5095, 5105, 5115, 5125, 5135, 5145, 5155, 5165, 5175, 5185, 5195, 5205, 5215, 5225, 5235, 5245, 5255, 5265, 5275, 5285, 5295, 5305, 5315, 5325, 5335, 5345, 5355, 5365, 5375, 5385, 5395, 5405, 5415, 5425, 5435, 5445, 5455, 5465, 5475, 5485, 5495, 5505, 5515, 5525, 5535, 5545, 5555, 5565, 5575, 5585, 5595, 5605, 5615, 5625, 5635, 5645, 5655, 5665, 5675, 5685, 5695, 5705, 5715, 5725, 5735, 5745, 5755, 5765, 5775, 5785, 5795, 5805, 5815, 5825, 5835, 5845, 5855, 5865, 5875, 5885, 5895, 5905, 5915, 5925, 5935, 5945, 5955, 5965, 5975, 5985, 5995, 6006, 6016, 6026, 6036, 6046, 6056, 6066, 6076, 6086, 6096, 6106, 6116, 6126, 6136, 6146, 6156, 6166, 6176, 6186, 6196, 6206, 6216, 6226, 6236, 6246, 6256, 6266, 6276, 6286, 6296, 6306, 6316, 6326, 6336, 6346, 6356, 6366, 6376, 6386, 6396, 6406, 6416, 6426, 6436, 6446, 6456, 6466, 6476, 6486, 6496, 6506, 6516, 6526, 6536, 6546, 6556, 6566, 6576, 6586, 6596, 6606, 6616, 6626, 6636, 6646, 6656, 6666, 6676, 6686, 6696, 6706, 6716, 6726, 6736, 6746, 6756, 6766, 6776, 6786, 6796, 6806, 6816, 6826, 6836, 6846, 6856, 6866, 6876, 6886, 6896, 6906, 6916, 6926, 6936, 6946, 6956, 6966, 6976, 6986, 6996, 7007, 7017, 7027, 7037, 7047, 7057, 7067, 7077, 7087, 7097, 7107, 7117, 7127, 7137, 7147, 7157, 7167, 7177, 7187, 7197, 7207, 7217, 7227, 7237, 7247, 7257, 7267, 7277, 7287, 7297, 7307, 7317, 7327, 7337, 7347, 7357, 7367, 7377, 7387, 7397, 7407, 7417, 7427, 7437, 7447, 7457, 7467, 7477, 7487, 7497, 7507, 7517, 7527, 7537, 7547, 7557, 7567, 7577, 7587, 7597, 7607, 7617, 7627, 7637, 7647, 7657, 7667, 7677, 7687, 7697, 7707, 7717, 7727, 7737, 7747, 7757, 7767, 7777, 7787, 7797, 7807, 7817, 7827, 7837, 7847, 7857, 7867, 7877, 7887, 7897, 7907, 7917, 7927, 7937, 7947, 7957, 7967, 7977, 7987, 7997, 8008, 8018, 8028, 8038, 8048, 8058, 8068, 8078, 8088, 8098, 8108, 8118, 8128, 8138, 8148, 8158, 8168, 8178, 8188, 8198, 8208, 8218, 8228, 8238, 8248, 8258, 8268, 8278, 8288, 8298, 8308, 8318, 8328, 8338, 8348, 8358, 8368, 8378, 8388, 8398, 8408, 8418, 8428, 8438, 8448, 8458, 8468, 8478, 8488, 8498, 8508, 8518, 8528, 8538, 8548, 8558, 8568, 8578, 8588, 8598, 8608, 8618, 8628, 8638, 8648, 8658, 8668, 8678, 8688, 8698, 8708, 8718, 8728, 8738, 8748, 8758, 8768, 8778, 8788, 8798, 8808, 8818, 8828, 8838, 8848, 8858, 8868, 8878, 8888, 8898, 8908, 8918, 8928, 8938, 8948, 8958, 8968, 8978, 8988, 8998, 9009, 9019, 9029, 9039, 9049, 9059, 9069, 9079, 9089, 9099, 9109, 9119, 9129, 9139, 9149, 9159, 9169, 9179, 9189, 9199, 9209, 9219, 9229, 9239, 9249, 9259, 9269, 9279, 9289, 9299, 9309, 9319, 9329, 9339, 9349, 9359, 9369, 9379, 9389, 9399, 9409, 9419, 9429, 9439, 9449, 9459, 9469, 9479, 9489, 9499, 9509, 9519, 9529, 9539, 9549, 9559, 9569, 9579, 9589, 9599, 9609, 9619, 9629, 9639, 9649, 9659, 9669, 9679, 9689, 9699, 9709, 9719, 9729, 9739, 9749, 9759, 9769, 9779, 9789, 9799, 9809, 9819, 9829, 9839, 9849, 9859, 9869, 9879, 9889, 9899, 9909, 9919, 9929, 9939, 9949, 9959, 9969, 9979, 9989, 10000]), nums=array([990, 1190, 1430, 1718, 2065, 2482, 2983, 3585, 4309, 5179, 6224, 7480, 8990, 10804, 12985, 15605, 18755, 22540, 27089, 32556, -26410, -18513, -9023, 2382, 16089, 32562, -13176, 10617, -26324, 8041, -16194, -32093, 27560, -31819, -11194, 26819, 20195, -26851, 21823, -24299, -26826, 23043, 4807, -3288, 14029, 9578, 5417, 28058, -21711, 25473], dtype=int16))
Return type:

DataArray

worst.sci.gen_samples_from_gev(z_star, beta, gamma, n)
Return type:

ndarray

worst.sci.get_evt_fit_data(z_star, beta, gamma, seeds, nums, load=True)
Return type:

DataArray

worst.sci.gev_pdf(z, alpha, beta, gamma)

Generalized extreme value probability density function.

Parameters:
  • z (np.ndarray) – data.

  • alpha (float) – Location parameter.

  • beta (float) – Scale parameter.

  • gamma (float) – Shape parameter.

Returns:

Probability density function.

Return type:

np.ndarray

worst.sci.min_ll_bg(z, z_star)
Return type:

Tuple[float, float]

worst.sci.plot_ex(z_star, beta, gamma, ex_seed, ex_num, color_true, color_max_known, color_max_unknown, ax=None)
worst.sci.return_ll_beta_gamma(z, z_star)
Return type:

Callable[[float, float], float]

worst.sci.try_fit(z_star=7, beta=4, gamma=-0.1, n=40, seed=42, quantiles=[0.01, 0.005])
Return type:

None

worst.sci.try_fits(z_star=7, beta=1, gamma=-0.3, seed=100, nums=[20, 22, 25, 27, 30, 33, 35, 40, 50, 60, 75, 100, 200, 500, 1000])
Return type:

DataArray

worst.tens module

worst.utils module

Worst.utils.py

worst.utils.alpha_from_z_star_beta_gamma(z_star, beta, gamma)

Calculate the location parameter of the Weibull class GEV distribution.

Parameters:
  • z_star (float) – Upper bound of the GEV distribution.

  • beta (float) – Scale parameter of the GEV distribution (should be positive).

  • gamma (float) – Shape parameter of the GEV distribution (should be negative).

Returns:

The location parameter of the GEV distribution, alpha.

Return type:

float

worst.utils.bg_cdf(z, z_star, beta, gamma)

CDF of the Weibull class GEV distribution with a known upper bound.

Parameters:
  • z (np.ndarray) – Data points to find the CDF for.

  • z_star (float) – Upper bound of the GEV distribution.

  • beta (float) – Scale parameter of the GEV distribution.

  • gamma (float) – Shape parameter of the GEV distribution.

Returns:

The CDF of the GEV distribution for different z values.

Return type:

np.ndarray

worst.utils.plot_rp(alpha, beta, gamma, color='blue', label='', ax=None, plot_alpha=0.8)

Plot the return period vs return value curve for the given parameters.

Parameters:
  • alpha (float) – The location parameter.

  • beta (float) – The scale parameter.

  • gamma (float) – The shape parameter.

  • color (str, optional) – The color of the line. Defaults to “blue”.

  • label (str, optional) – The line label. Defaults to “”.

  • ax (Optional[matplotlib.axes.Axes], optional) – The axes to add the figure too. Defaults to None.

  • plot_alpha (float, optional) – Transparency parameter of line. Defaults to 0.8.

Return type:

None

worst.utils.plot_sample_points(data, color='black', ax=None, label='Sampled data points')

Plot the sample points on the return period vs return value curve.

Parameters:
  • data (np.ndarray) – The sample points to plot.

  • color (str, optional) – The color of the points. Defaults to “black”.

  • ax (Optional[matplotlib.axes.Axes], optional) – The axes to add the figure too. Defaults to None.

  • label (str, optional) – The label of the points. Defaults to “Sampled data points”.

Return type:

None

worst.utils.retry_wrapper(max_retries=10)

Retry wrapper. If a function fails flakeily, then wrap with this to retry it a few times.

Puts the function in a try except block and retries it a max_retries times if unsuccesful.

Parameters:

max_retries (int, optional) – Number of retries. Defaults to 10.

Returns:

Wrapper function to recall the function in case of failure.

Return type:

callable

Example::
>>> @retry_wrapper(max_retries=5)
... def my_function():
...     # Some code that will fail
...     assert False
>>> try:
...    my_function()
... except Exception as e:
...    print(e)
    Exception:
    Retrying my_function 1/5
    Exception:
    Retrying my_function 2/5
    Exception:
    Retrying my_function 3/5
    Exception:
    Retrying my_function 4/5
    Exception:
    Retrying my_function 5/5
    Max retries exceeded for function my_function
worst.utils.z_star_from_alpha_beta_gamma(alpha, beta, gamma)

Calculate the upper bound of the Weibull class GEV distribution.

Parameters:
  • alpha (float) – Location parameter of the GEV distribution.

  • beta (float) – Scale parameter of the GEV distribution (should be positive).

  • gamma (float) – Shape parameter of the GEV distribution (should be negative).

Returns:

The upper bound of the GEV distribution, z_star.

Return type:

float

worst.vary_gamma_beta module

worst.vary_noise module

worst.vary_samples_ns module

Module contents