Skip to content

Instantly share code, notes, and snippets.

@bennyistanto
Created June 25, 2024 05:58
Show Gist options
  • Save bennyistanto/05fd4bc54b31728d07703ace60ec885d to your computer and use it in GitHub Desktop.
Save bennyistanto/05fd4bc54b31728d07703ace60ec885d to your computer and use it in GitHub Desktop.
Visual representation of flood probability model, illustrating the different scenarios

Visual representation of flood probability model

You can paste the below code into an online Python compiler like https://python-fiddle.com/ and grab the result instantly.

This script will generate a plot that illustrates:

  1. The original model (blue line).
  2. A scenario with a steeper slope (red line), showing a more rapid increase in flood risk with rainfall.
  3. A scenario with a gentler slope (green line), showing a more gradual increase in risk.
  4. A scenario with a less negative intercept (purple line), resulting in higher probabilities at lower rainfall amounts.
  5. A scenario with a more negative intercept (orange line), requiring more rainfall to reach moderate flood probabilities.

The plot also includes:

  • A horizontal dashed line at 0.5 probability for reference.
  • A vertical dotted line at 226.95 mm rainfall, corresponding to your example.

This visualization should help in understanding how changes in slope and intercept affect the relationship between rainfall and flood probability in your model.

flood_probability

import numpy as np
import matplotlib.pyplot as plt
def flood_probability(rainfall, slope, intercept):
g = slope * rainfall + intercept
return 1 / (1 + np.exp(-g))
# Set up rainfall range
rainfall = np.linspace(0, 300, 1000)
# Define different scenarios
scenarios = [
{"slope": 0.01888, "intercept": -4.0219, "label": "Original Model", "color": "blue"},
{"slope": 0.03, "intercept": -4.0219, "label": "Steeper Slope", "color": "red"},
{"slope": 0.01, "intercept": -4.0219, "label": "Gentler Slope", "color": "green"},
{"slope": 0.01888, "intercept": -3, "label": "Less Negative Intercept", "color": "purple"},
{"slope": 0.01888, "intercept": -5, "label": "More Negative Intercept", "color": "orange"}
]
# Create the plot
plt.figure(figsize=(12, 8))
for scenario in scenarios:
prob = flood_probability(rainfall, scenario["slope"], scenario["intercept"])
plt.plot(rainfall, prob, label=scenario["label"], color=scenario["color"])
plt.axhline(y=0.5, color='gray', linestyle='--', label='50% Probability')
plt.axvline(x=226.95, color='black', linestyle=':', label='Example Rainfall (226.95 mm)')
plt.xlabel('Rainfall (mm)')
plt.ylabel('Flood Probability')
plt.title('Flood Probability vs Rainfall for Different Model Parameters')
plt.legend()
plt.grid(True, which='both', linestyle=':', color='gray', alpha=0.5)
plt.ylim(0, 1)
plt.xlim(0, 300)
plt.tight_layout()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment