-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulation.py
More file actions
102 lines (85 loc) · 4.11 KB
/
Simulation.py
File metadata and controls
102 lines (85 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from HurricaneSimulator import HurricaneSimulator
import logging
from Utils import valid_positive_integer
logger = logging.getLogger(__name__)
class Simulation:
"""
A model to simulate economic loss from hurricanes in Florida and Gulf States.
Parameters:
florida_landfall_rate: float
The rate at which hurricanes hit Florida per year.
florida_mean: float
The mean of the economic loss in Florida when modelled as log-normal, in billions of dollars.
florida_stddev: float
The standard deviation of the economic loss in Florida when modelled as log-normal, in billions of dollars.
gulf_landfall_rate: float
The rate at which hurricanes hit Gulf States per year.
gulf_mean: float
The mean of the economic loss in Gulf States when modelled as log-normal, in billions of dollars.
gulf_stddev: float
The standard deviation of the economic loss in Gulf States when modelled as log-normal, in billions of dollars.
Attributes:
florida: HurricaneSimulator
Contains landfall rate, mean and standard deviation for Florida and enables simulation.
gulf: HurricaneSimulator
Contains landfall rate, mean and standard deviation for Gulf States and enables simulation.
Raises:
TypeError: If variables incorrectly typed
ValueError: If florida_landfall_rate is not positive.
ValueError: If florida_stddev is negative.
ValueError: If gulf_landfall_rate is not positive.
ValueError: If gulf_stddev is not positive.
"""
def __init__(
self,
florida_landfall_rate: float,
florida_mean: float,
florida_stddev: float,
gulf_landfall_rate: float,
gulf_mean: float,
gulf_stddev: float
) -> None:
self.florida_simulator = HurricaneSimulator(florida_landfall_rate, florida_mean, florida_stddev)
self.gulf_simulator = HurricaneSimulator(gulf_landfall_rate, gulf_mean, gulf_stddev)
logger.info("Simulation initialised.")
def simulate(self, years: int) -> float:
"""
Simulates the average economic loss over a given number of years using a Monte Carlo algorithm.
Parameters:
years: int
The number of years to simulate over.
Returns:
float
The expected annual loss in billions of dollars.
"""
valid_positive_integer(years)
total_loss = 0
for year in range(years):
simulation_loss = 0
num_florida_cases = self.florida_simulator.get_hurricanes()
for _ in range(num_florida_cases):
simulation_loss += self.florida_simulator.simulate()
num_gulf_cases = self.gulf_simulator.get_hurricanes()
for _ in range(num_gulf_cases):
simulation_loss += self.gulf_simulator.simulate()
total_loss += simulation_loss
if (year % 100 == 99):
logger.debug(f"Average after {year} years: {total_loss / year}")
return (total_loss / years)
def simulate_fast(self, years: int) -> float:
"""
Simulates the average economic loss over a given number of years using the CLT and Poisson addition.
Parameters:
years: int
The number of years to simulate over.
Returns:
float
The expected annual loss in billions of dollars.
"""
florida_loss = self.florida_simulator.simulate_fast(years)
logger.info(f"Florida simulation completed. Florida loss: {florida_loss:.2f} billion.")
gulf_loss = self.gulf_simulator.simulate_fast(years)
logger.info(f"Gulf simulation completed. Gulf loss: {gulf_loss:.2f} billion.")
average = (florida_loss + gulf_loss) / years
logger.info(f"Simulation completed. Average loss: {average:.2f} billion.")
return (average)