Skip to content

Commit

Permalink
update homework 4
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuwq0 authored Nov 18, 2024
1 parent 1c4bc9a commit 7388f9a
Showing 1 changed file with 185 additions and 17 deletions.
202 changes: 185 additions & 17 deletions docs/exercises/homework04.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1505,17 +1505,15 @@
"\n",
"$$ log_{10}N(M) = A - b \\cdot M $$\n",
"\n",
"where $N(M\n",
"where $N(M)$ is the number of earthquakes, $M$ is the earthquake magnitude.\n",
"\n",
"The second is Omori's Law which describes the distribution of aftershocks following a primary earthquake or mainshock. It can be used to estimate the rate of aftershock production over time after the earthquake.\n",
"\n",
"$$ N(t) = \\frac{k}{(c + t)^p} $$\n",
"\n",
"<!-- $$ log_{10}N(t) = log_{10}k - p \\cdot log_{10}(c + t) $$ -->\n",
"\n",
"where $N(t)$ is the number of aftershocks at time $t$, $k$ is a constant, $c$ is the time at which the aftershock production rate peaks, and $p$ is the decay rate.\n",
"\n",
")$ is the number of earthquakes with magnitude greater than or equal to $M$, $A$ is the intercept, and $b$ is the slope.\n",
"where $N(t)$ is the number of aftershocks after $t$ days, $k$ is a constant, $c$ is the time at which the aftershock production rate peaks, and $p$ is the decay rate.\n",
"\n",
"\n",
"<!-- - Load a Bay Area seismic catalog of January 01,1989 to December 31, 1995.\n",
Expand Down Expand Up @@ -1637,9 +1635,12 @@
"## Make a figure\n",
"plt.figure(figsize=(10, 6))\n",
"\n",
"## Plot the histogram of earthquake frequency vs magnitude. Use `log=True` to plot the histogram on a log scale.\n",
"## Plot the histogram of earthquake frequency vs magnitude. \n",
"plt.hist(\n",
"\n",
"## Set the y-axis in log scale. Hint: use plt.yscale(\n",
"plt.\n",
"\n",
"## Add x-axis label and y-axis label\n",
"plt.xlabel('\n",
"plt.ylabel('\n",
Expand Down Expand Up @@ -1674,10 +1675,12 @@
"metadata": {},
"outputs": [],
"source": [
"## We can use the np.histogram function to get the frequency of each magnitude bin. Using bins=40.\n",
"## We can use the np.histogram function to get the frequency of each magnitude bin. Hint: define magnitude_bins between M0 and M6 with a bin size of M0.1\n",
"magnitude_bins = np.arange(0, 6, 0.1)\n",
"frequency, _ = np.histogram(catalog['Magnitude'], bins=magnitude_bins)\n",
"magnitude_bins = (magnitude_bins[:-1] + magnitude_bins[1:]) / 2 ## convert the bin edges to the bin centers\n",
"\n",
"## Redefine the magnitude_bins to be the bin centers, because the histogram function is using the bin edges.\n",
"magnitude_bins = (magnitude_bins[:-1] + magnitude_bins[1:]) / 2\n",
"\n",
"## Define the input and output variables. Hint: use `np.log10(frequency)` to take the logarithm of the frequency\n",
"X = \n",
Expand All @@ -1687,7 +1690,7 @@
"X = X[~np.isinf(y)]\n",
"y = y[~np.isinf(y)]\n",
"\n",
"## Reshape the input and output variables to be vertical arrays and replace inf with 0\n",
"## Reshape the input and output variables to be vertical arrays. Hint: use the `reshape(-1, 1)` method\n",
"X = X.reshape(-1, 1)\n",
"y = y.reshape(-1, 1)\n",
"\n",
Expand All @@ -1710,7 +1713,7 @@
"plt.ylabel('Frequency')\n",
"plt.legend()\n",
"plt.grid()\n",
"plt.show()\n"
"plt.show()"
]
},
{
Expand Down Expand Up @@ -1740,10 +1743,12 @@
"metadata": {},
"outputs": [],
"source": [
"## We can use the np.histogram function to get the frequency of each magnitude bin. Using bins=40.\n",
"## We can use the np.histogram function to get the frequency of each magnitude bin. Hint: use magnitude_bins between M0 and M6 with a bin size of M0.1\n",
"magnitude_bins = np.arange(0, 6, 0.1)\n",
"frequency, _ = np.histogram(catalog['Magnitude'], bins=magnitude_bins)\n",
"magnitude_bins = (magnitude_bins[:-1] + magnitude_bins[1:]) / 2 ## convert the bin edges to the bin centers\n",
"\n",
"## Redefine the magnitude_bins to be the bin centers, because the histogram function returns the bin edges.\n",
"magnitude_bins = (magnitude_bins[:-1] + magnitude_bins[1:]) / 2 \n",
"\n",
"## Define the input and output variables. Hint: use `np.log10(frequency)` to take the logarithm of the frequency\n",
"X = \n",
Expand All @@ -1757,7 +1762,7 @@
"X = X[~np.isinf(y)]\n",
"y = y[~np.isinf(y)]\n",
"\n",
"## Reshape the input and output variables to be vertical arrays and replace inf with 0\n",
"## Reshape the input and output variables to be vertical arrays. Hint: use `reshape(-1, 1)`\n",
"X = X.reshape(-1, 1)\n",
"y = y.reshape(-1, 1)\n",
"\n",
Expand All @@ -1781,7 +1786,7 @@
"plt.ylabel('Frequency')\n",
"plt.legend()\n",
"plt.grid()\n",
"plt.show()\n"
"plt.show()"
]
},
{
Expand Down Expand Up @@ -1830,6 +1835,18 @@
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Question: What does the b-value tell us about the earthquake catalog? If we have one earthquake of magnitude 6, how many earthquakes of magnitude 5 would we expect to have in the catalog? How about magnitude 4, 3, 2, and 1?\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -1913,7 +1930,7 @@
"source": [
"- Plot earthquake frequency vs time\n",
"\n",
"You can use `plt.hist()` to plot the histogram of `days_since_earthquake`. Use `log=True` to plot the histogram on a log scale.\n"
"You can use `plt.hist()` to plot the histogram of `days_since_earthquake`.\n"
]
},
{
Expand All @@ -1925,7 +1942,10 @@
"## Make a figure\n",
"plt.figure(figsize=(10, 6))\n",
"\n",
"## Plot the histogram of earthquake frequency vs time. Use `log=True` to plot the histogram on a log scale.\n",
"## Define bins from 0 to 90 days with a bin size of 5 days.\n",
"days_bins = np.arange(0, 91, 5)\n",
"\n",
"## Plot the histogram of earthquake frequency vs time. Hint: add bins=days_bins\n",
"plt.hist(\n",
"\n",
"## Add x-axis label and y-axis label\n",
Expand All @@ -1939,9 +1959,121 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Question: How would you describe the distribution of number of aftershocks with time after the main quake?\n",
"Question: Question: How would you describe the distribution of number of aftershocks with time after the main quake? What is the peak time of aftershock production? How does the aftershock rate decay with time?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's see if we can do some quantitative analysis of the aftershock rate decay with time.\n",
"\n",
"The challenge is that the Omori's law is not a linear relationship, but a power law relationship. \n",
"\n",
"$$ N(t) = \\frac{k}{(c + t)^p} $$ \n",
"\n",
"Let's first convert the equation to a linear form.\n",
"\n",
"$$ log_{10}N(t) = log_{10}k - p \\cdot log_{10}(c + t) $$\n",
"\n",
"For simplicity let's assume `c=0` and fit the data to the linear model. \n",
"\n",
"$$ log_{10}N(t) = log_{10}k - p \\cdot log_{10}(t) $$\n",
"\n",
"By how much is the rate of aftershock occurrence reduced after 7 days? After 30 days? Note that the y-axis is on a log scale."
"So the y value is `log10(N(t))` and the x value is `log10(t)`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## Calcualte frequency of earthquakes using np.histogram. Using using days_bins between 0 and 90 days for every 5 days.\n",
"days_bins = np.arange(0, 90, 5)\n",
"frequency, bins = np.histogram(LP_catalog['days_since_earthquake'], bins = days_bins)\n",
"\n",
"## Calculate the days on bin centers becuase the \"bins\" variable is defined as the bin edges\n",
"days_bins = (bins[:-1] + bins[1:]) / 2\n",
"\n",
"## Define the input and output variables. Hint: use `np.log10(frequency)` to take the logarithm of the frequency and `np.log10(days_bins)` to take the logarithm of the days_bins\n",
"X = \n",
"y = \n",
"\n",
"## filter out the inf values\n",
"X = X[~np.isinf(y)]\n",
"y = y[~np.isinf(y)]\n",
"\n",
"## Reshape the input and output variables to be vertical arrays\n",
"X = X.reshape(-1, 1)\n",
"y = y.reshape(-1, 1)\n",
"\n",
"## Fit a linear model using LinearRegression() from sklearn\n",
"model = \n",
"model.fit(\n",
"\n",
"## Get prediction from the model\n",
"y_pred = model.predict(X)\n",
"\n",
"## Evaluate the model using r2_score\n",
"R_squared = \n",
"\n",
"print(f'The model R-squared is {R_squared:.2f}')\n",
"\n",
"## Plot the prediction\n",
"plt.figure(figsize=(10, 6))\n",
"plt.scatter(X, y, label='Data')\n",
"plt.plot(X, y_pred, color='red', label='Linear Regression')\n",
"plt.xlabel('log10(Days after the earthquake)')\n",
"plt.ylabel('log10(Frequency)')\n",
"plt.legend()\n",
"plt.grid()\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can extract the slope and intercept of the linear model."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## Get the slope and intercept\n",
"slope = \n",
"intercept = \n",
"\n",
"print(f'The slope is {slope:.2f} and the intercept is {intercept:.2f}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Question: How is the fit of the linear model to the aftershock data? Can you estimate the p-value of the Omori's Law?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Question: What does the decay rate $p$ tell us about the rate of aftershock production over time? If we have 1000 aftershocks in the first 5 days, how many aftershocks every 5 day peroid would we expect after 100 days?"
]
},
{
Expand Down Expand Up @@ -1984,6 +2116,42 @@
"Map of Bay Area faults. \n",
"Source: https://pubs.er.usgs.gov/publication/fs20163020\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Question: Why is the San Andreas Fault considered a major hazard for the San Francisco Bay Area? Discuss its role as a tectonic plate boundary."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Question: Examine the map of Bay Area faults. Identify at least two other faults besides the San Andreas Fault and discuss their potential contributions to seismic hazards in the region."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Question: Refer to the USGS website and summarize key findings about the earthquake hazard associated with the San Andreas Fault. How can these findings be used in urban planning and infrastructure development?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit 7388f9a

Please sign in to comment.