mirror of
https://github.com/TeachBooks/template.git
synced 2026-11-12 22:25:52 +00:00
started stripping down risk-and-reliability to create template
This commit is contained in:
parent
6697c0f5f8
commit
75ff95fa67
127 changed files with 54699 additions and 0 deletions
12
book/code/cosinewave.py
Normal file
12
book/code/cosinewave.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
x = np.linspace(0, 2*np.pi, endpoint=True)
|
||||
y = np.cos(x)
|
||||
|
||||
plt.figure()
|
||||
plt.plot(x, y)
|
||||
plt.title('$y=\cos(x)$')
|
||||
plt.xlabel('$x$')
|
||||
plt.ylabel('$y$')
|
||||
plt.savefig('cosinewave.svg')
|
||||
38
book/code/ex_FN_curve_step_01.py
Normal file
38
book/code/ex_FN_curve_step_01.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from mpl_toolkits.axisartist.axislines import AxesZero
|
||||
|
||||
# Probability density/mass function
|
||||
|
||||
fig = plt.figure(figsize=(8,6))
|
||||
ax = fig.add_subplot(axes_class=AxesZero)
|
||||
|
||||
for direction in ["xzero", "yzero"]:
|
||||
ax.axis[direction].set_axisline_style("-|>") # add arrows
|
||||
ax.axis[direction].set_visible(True) # x and y from origin
|
||||
|
||||
for direction in ["left", "right", "bottom", "top"]:
|
||||
ax.axis[direction].set_visible(False) # hide borders
|
||||
|
||||
N_fatalities = ['0', '10', '100']
|
||||
N_fatalities_loc = [0.05, 0.30, 0.66]
|
||||
|
||||
pdf = ['0.989', '$10^{-2}$', '$10^{-3}$']
|
||||
pdf_loc = [0.99, 0.66, 0.33]
|
||||
|
||||
ax.bar(N_fatalities_loc, pdf_loc, width=0.05, color= 'lightcyan', edgecolor='k')
|
||||
|
||||
ax.set_xticks(N_fatalities_loc)
|
||||
ax.set_xticklabels(N_fatalities)
|
||||
|
||||
ax.set_yticks(pdf_loc)
|
||||
ax.set_yticklabels(pdf)
|
||||
|
||||
ax.set_title('Probability density/mass function, $f_{N}(n)$', x=0.6, y=1.0)
|
||||
ax.set_xlabel('Fatalities')
|
||||
ax.set_ylabel('Probability')
|
||||
|
||||
ax.axhline(y=0.66, xmin= 0.01, xmax= 0.40, linewidth=1, linestyle='dashed', color='k') # add dashed horizontal lines
|
||||
ax.axhline(y=0.33, xmin= 0.01, xmax= 0.90, linewidth=1, linestyle='dashed', color='k')
|
||||
|
||||
plt.savefig('ex_FN_curve_step_01_py.svg')
|
||||
38
book/code/ex_FN_curve_step_02.py
Normal file
38
book/code/ex_FN_curve_step_02.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from mpl_toolkits.axisartist.axislines import AxesZero
|
||||
|
||||
# Cumulative Distribution Function
|
||||
|
||||
fig = plt.figure(figsize=(8,6))
|
||||
ax = fig.add_subplot(axes_class=AxesZero)
|
||||
|
||||
for direction in ["xzero", "yzero"]:
|
||||
ax.axis[direction].set_axisline_style("-|>") # add arrows
|
||||
ax.axis[direction].set_visible(True) # x and y from origin
|
||||
|
||||
for direction in ["left", "right", "bottom", "top"]:
|
||||
ax.axis[direction].set_visible(False) # hide borders
|
||||
|
||||
N_fatalities = ['0', '10', '100', '']
|
||||
N_fatalities_loc = [0.03, 0.33, 0.76, 0.95]
|
||||
|
||||
pdf = ['0', '0.989', '0.999', '1']
|
||||
pdf_loc = [0.03, 0.66, 0.86, 0.99]
|
||||
|
||||
ax.step(N_fatalities_loc, pdf_loc, color= 'darkblue',linewidth=4)
|
||||
|
||||
ax.set_xticks(N_fatalities_loc)
|
||||
ax.set_xticklabels(N_fatalities)
|
||||
|
||||
ax.set_yticks(pdf_loc)
|
||||
ax.set_yticklabels(pdf)
|
||||
|
||||
ax.set_title('Cumulative distribution function, $F_{N}(n) = P(N \leq n)$', x=0.55, y=1.0)
|
||||
ax.set_xlabel('Fatalities')
|
||||
ax.set_ylabel('Probability')
|
||||
|
||||
ax.text(0.8, 0.9, r'} $10^{-3}$', fontsize=20)
|
||||
ax.text(0.4, 0.725, r'} $10^{-2}$', fontsize=30)
|
||||
|
||||
plt.savefig('ex_FN_curve_step_02_py.svg')
|
||||
35
book/code/ex_FN_curve_step_03.py
Normal file
35
book/code/ex_FN_curve_step_03.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from mpl_toolkits.axisartist.axislines import AxesZero
|
||||
|
||||
# FN curve
|
||||
|
||||
fig = plt.figure(figsize=(8,6))
|
||||
ax = fig.add_subplot(axes_class=AxesZero)
|
||||
|
||||
for direction in ["xzero", "yzero"]:
|
||||
ax.axis[direction].set_axisline_style("-|>") # add arrows
|
||||
ax.axis[direction].set_visible(True) # x and y from origin
|
||||
|
||||
for direction in ["left", "right", "bottom", "top"]:
|
||||
ax.axis[direction].set_visible(False) # hide borders
|
||||
|
||||
N_fatalities = ['0', '10', '100','100']
|
||||
N_fatalities_loc = [0.03,0.33, 0.95, 0.95]
|
||||
|
||||
pdf = ['0','$1.1 * 10^{-2}$', '$10^{-3}$','0']
|
||||
pdf_loc = [0.03,0.99, 0.66, 0.03]
|
||||
|
||||
ax.step(N_fatalities_loc, pdf_loc, color= 'darkblue',linewidth=4)
|
||||
|
||||
ax.set_xticks(N_fatalities_loc)
|
||||
ax.set_xticklabels(N_fatalities)
|
||||
|
||||
ax.set_yticks(pdf_loc)
|
||||
ax.set_yticklabels(pdf)
|
||||
|
||||
ax.set_title('FN curve, $1-F_{N}(n) = P(N > n)$', x=0.75, y=1.0)
|
||||
ax.set_xlabel('Fatalities')
|
||||
ax.set_ylabel('Probability')
|
||||
|
||||
plt.savefig('ex_FN_curve_step_03_py.svg')
|
||||
16
book/code/present_value.py
Normal file
16
book/code/present_value.py
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
t = np.linspace(0, 101)
|
||||
y_2 = 1 / (1 + 0.02)**t
|
||||
y_4 = 1 / (1 + 0.04)**t
|
||||
|
||||
fig = plt.figure()
|
||||
plt.plot(t, y_2, label='2%')
|
||||
plt.plot(t, y_4, label='4%')
|
||||
plt.grid()
|
||||
plt.legend()
|
||||
plt.xlabel('Year')
|
||||
plt.ylabel('Normalized Present Value [$-$]')
|
||||
|
||||
plt.savefig('present_value_py.svg')
|
||||
54
book/code/risk_types.py
Normal file
54
book/code/risk_types.py
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# Given Parameters
|
||||
|
||||
N_fatalities = np.linspace(1,1000,1000) # fatalities
|
||||
|
||||
P_df = 0.1; beta = 0.1
|
||||
|
||||
alpha = 2
|
||||
k = 3
|
||||
N_A = 100
|
||||
|
||||
r= 0.025
|
||||
I = 5*10**6
|
||||
B = 0.33
|
||||
|
||||
# Individual Risk
|
||||
|
||||
Pf_ind_risk = (beta*(10**-4))/P_df # per year
|
||||
|
||||
# print('Acceptable Failure Probability: ', Pf_ind_risk)
|
||||
|
||||
# Societal Risk
|
||||
|
||||
C = ((beta*100)/(k*np.sqrt(N_A)))**2 # binomial distribution
|
||||
|
||||
Pf_SR = (C/(N_fatalities**alpha))
|
||||
|
||||
# Economic Risk
|
||||
|
||||
D_damage = (5*10**7)*N_fatalities
|
||||
|
||||
Pf_ER = (I*B*r)/(D_damage)
|
||||
|
||||
# Visualize
|
||||
|
||||
my_linewidth = 4
|
||||
|
||||
plt.figure(figsize=(8,6))
|
||||
|
||||
plt.yscale('log')
|
||||
plt.xscale('log')
|
||||
plt.plot(N_fatalities, Pf_SR, linewidth=my_linewidth, label='Societal Risk')
|
||||
plt.plot(N_fatalities,Pf_ER, linewidth=my_linewidth, color='r', label='Economic Risk')
|
||||
plt.axhline(Pf_ind_risk, linewidth=my_linewidth, color='g', label='Individual Risk')
|
||||
|
||||
plt.ylabel('Probability of Exceedance, $1-F_N(n)$')
|
||||
plt.xlabel('Fatalities, N')
|
||||
plt.ylim(10**(-7),1)
|
||||
|
||||
plt.legend(loc='upper right')
|
||||
|
||||
plt.savefig('risk_types_py.svg')
|
||||
12
book/code/sinewave.py
Normal file
12
book/code/sinewave.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
x = np.linspace(0, 2*np.pi, endpoint=True)
|
||||
y = np.sin(x)
|
||||
|
||||
plt.figure()
|
||||
plt.plot(x, y)
|
||||
plt.title('$y=\sin(x)$')
|
||||
plt.xlabel('$x$')
|
||||
plt.ylabel('$y$')
|
||||
plt.savefig('sinewave.svg')
|
||||
23
book/code/test.py
Normal file
23
book/code/test.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import modulefinder
|
||||
|
||||
finder = modulefinder.ModuleFinder()
|
||||
finder.run_script('cosinewave.py')
|
||||
|
||||
moduleslist = {}
|
||||
for name, mod in finder.modules.iteritems():
|
||||
filename = mod.__file__
|
||||
if filename is None:
|
||||
continue
|
||||
if '__' in name:
|
||||
continue
|
||||
#if "python" in filename.lower():
|
||||
# continue
|
||||
moduleslist[name.split(".")[0]] = True
|
||||
#print '%s: %s' % (name, filename)
|
||||
#print ','.join(mod.globalnames.keys()[:3])
|
||||
|
||||
print('Loaded modules:')
|
||||
for name, dummy in moduleslist.iteritems():
|
||||
print(name)
|
||||
|
||||
input("Press Enter to continue...")
|
||||
Loading…
Add table
Add a link
Reference in a new issue