Skip to content

calculators

A collection of calculation functions for hazard aggregation.

cov(mean, std)

Calculate the coeficient of variation handling zero mean by setting cov to zero.

Parameters:

Name Type Description Default
mean NDArray

array of mean values

required
std NDArray

array of standard deviation values

required

Returns:

Name Type Description
cov NDArray

array of coeficient of variation values

Source code in toshi_hazard_post/calculators.py
60
61
62
63
64
65
66
67
68
69
70
71
72
def cov(mean: 'npt.NDArray', std: 'npt.NDArray') -> 'npt.NDArray':
    """Calculate the coeficient of variation handling zero mean by setting cov to zero.

    Args:
        mean: array of mean values
        std: array of standard deviation values

    Returns:
        cov: array of coeficient of variation values
    """
    cov = std / mean
    cov[mean == 0] = 0
    return cov

prob_to_rate(prob, inv_time)

Convert probability of exceedance to rate assuming Poisson distribution.

Parameters:

Name Type Description Default
prob NDArray

probability of exceedance

required
inv_time float

time period for probability (e.g. 1.0 for annual probability)

required

Returns:

Name Type Description
rate NDArray

return rate in inv_time

Source code in toshi_hazard_post/calculators.py
15
16
17
18
19
20
21
22
23
24
25
26
@jit(nopython=True)
def prob_to_rate(prob: 'npt.NDArray', inv_time: float) -> 'npt.NDArray':
    """Convert probability of exceedance to rate assuming Poisson distribution.

    Args:
        prob: probability of exceedance
        inv_time: time period for probability (e.g. 1.0 for annual probability)

    Returns:
        rate: return rate in inv_time
    """
    return -np.log(1.0 - prob) / inv_time

rate_to_prob(rate, inv_time)

Convert rate to probabiility of exceedance assuming Poisson distribution.

Parameters:

Name Type Description Default
rate NDArray

rate over inv_time

required
inv_time float

time period of rate (e.g. 1.0 for annual rate)

required

Returns:

Name Type Description
prob NDArray

probability of exceedance in inv_time

Source code in toshi_hazard_post/calculators.py
29
30
31
32
33
34
35
36
37
38
39
40
@jit(nopython=True)
def rate_to_prob(rate: 'npt.NDArray', inv_time: float) -> 'npt.NDArray':
    """Convert rate to probabiility of exceedance assuming Poisson distribution.

    Args:
        rate: rate over inv_time
        inv_time: time period of rate (e.g. 1.0 for annual rate)

    Returns:
        prob: probability of exceedance in inv_time
    """
    return 1.0 - np.exp(-inv_time * rate)

weighted_avg_and_std(values, weights)

Calculate weighted average and standard deviation of an array.

Parameters:

Name Type Description Default
values NDArray

array of values (branch, IMTL)

required
weights NDArray

weights of values (branch, )

required

Returns:

Type Description
Tuple[NDArray, NDArray]

A tuple of (mean, std) where mean is the weighted mean and std is the standard devaition both with size (IMTL, ).

Source code in toshi_hazard_post/calculators.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def weighted_avg_and_std(values: 'npt.NDArray', weights: 'npt.NDArray') -> Tuple['npt.NDArray', 'npt.NDArray']:
    """Calculate weighted average and standard deviation of an array.

    Args:
        values: array of values (branch, IMTL)
        weights: weights of values (branch, )

    Returns:
        A tuple of (mean, std) where mean is the weighted mean and
            std is the standard devaition both with size (IMTL, ).
    """
    average = np.average(values, weights=weights, axis=0)
    # Fast and numerically precise:
    variance = np.average((values - average) ** 2, weights=weights, axis=0)
    return (average, np.sqrt(variance))

weighted_quantiles(values, weights, quantiles)

Calculate weighted quantiles of array.

Parameters:

Name Type Description Default
values NDArray

values of data

required
weights NDArray

weights of values. Same length as values

required
quantiles Union[list[float], NDArray]

quantiles to be found. Values should be in [0,1]

required

Returns:

Type Description
NDArray

weighted quantiles

Source code in toshi_hazard_post/calculators.py
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def weighted_quantiles(
    values: 'npt.NDArray', weights: 'npt.NDArray', quantiles: Union[list[float], 'npt.NDArray']
) -> 'npt.NDArray':
    """Calculate weighted quantiles of array.

    Args:
        values: values of data
        weights: weights of values. Same length as values
        quantiles: quantiles to be found. Values should be in [0,1]

    Returns:
        weighted quantiles
    """
    sorter = np.argsort(values, kind='stable')
    values = values[sorter]
    weights = weights[sorter]

    quantiles_at_values = np.cumsum(weights) - 0.5 * weights
    quantiles_at_values /= np.sum(weights)

    wq = np.interp(quantiles, quantiles_at_values, values)

    return wq