Updates
parent
eca5368f75
commit
2c90a05f80
File diff suppressed because it is too large
Load Diff
@ -1,195 +1,238 @@
|
|||||||
%% CHAPTER5 contains the commands used in Chapter 5 of the tutorial
|
## CHAPTER5 contains the commands used in Chapter 5 of the tutorial
|
||||||
%
|
#
|
||||||
% CALL: Chapter5
|
# CALL: Chapter5
|
||||||
%
|
#
|
||||||
% Some of the commands are edited for fast computation.
|
# Some of the commands are edited for fast computation.
|
||||||
% Each set of commands is followed by a 'pause' command.
|
# Each set of commands is followed by a 'pause' command.
|
||||||
%
|
#
|
||||||
|
|
||||||
% Tested on Matlab 5.3
|
# Tested on Matlab 5.3
|
||||||
% History
|
# History
|
||||||
% Added Return values by GL August 2008
|
# Added Return values by GL August 2008
|
||||||
% Revised pab sept2005
|
# Revised pab sept2005
|
||||||
% Added sections -> easier to evaluate using cellmode evaluation.
|
# Added sections -> easier to evaluate using cellmode evaluation.
|
||||||
% Created by GL July 13, 2000
|
# Created by GL July 13, 2000
|
||||||
% from commands used in Chapter 5
|
# from commands used in Chapter 5
|
||||||
%
|
#
|
||||||
|
|
||||||
%% Chapter 5 Extreme value analysis
|
## Chapter 5 Extreme value analysis
|
||||||
|
|
||||||
%% Section 5.1 Weibull and Gumbel papers
|
## Section 5.1 Weibull and Gumbel papers
|
||||||
|
from __future__ import division
|
||||||
|
import numpy as np
|
||||||
|
import scipy.interpolate as si
|
||||||
|
from wafo.plotbackend import plotbackend as plt
|
||||||
|
import wafo.data as wd
|
||||||
|
import wafo.objects as wo
|
||||||
|
import wafo.stats as ws
|
||||||
|
import wafo.kdetools as wk
|
||||||
pstate = 'off'
|
pstate = 'off'
|
||||||
|
|
||||||
% Significant wave-height data on Weibull paper,
|
# Significant wave-height data on Weibull paper,
|
||||||
clf
|
|
||||||
Hs = load('atlantic.dat');
|
fig = plt.figure()
|
||||||
wei = plotweib(Hs)
|
ax = fig.add_subplot(111)
|
||||||
wafostamp([],'(ER)')
|
Hs = wd.atlantic()
|
||||||
disp('Block = 1'),pause(pstate)
|
wei = ws.weibull_min.fit(Hs)
|
||||||
|
tmp = ws.probplot(Hs, wei, ws.weibull_min, plot=ax)
|
||||||
%%
|
plt.show()
|
||||||
% Significant wave-height data on Gumbel paper,
|
#wafostamp([],'(ER)')
|
||||||
clf
|
#disp('Block = 1'),pause(pstate)
|
||||||
gum=plotgumb(Hs)
|
|
||||||
wafostamp([],'(ER)')
|
##
|
||||||
disp('Block = 2'),pause(pstate)
|
# Significant wave-height data on Gumbel paper,
|
||||||
|
plt.clf()
|
||||||
%%
|
ax = fig.add_subplot(111)
|
||||||
% Significant wave-height data on Normal probability paper,
|
gum = ws.gumbel_r.fit(Hs)
|
||||||
plotnorm(log(Hs),1,0);
|
tmp1 = ws.probplot(Hs, gum, ws.gumbel_r, plot=ax)
|
||||||
wafostamp([],'(ER)')
|
#wafostamp([],'(ER)')
|
||||||
disp('Block = 3'),pause(pstate)
|
plt.show()
|
||||||
|
#disp('Block = 2'),pause(pstate)
|
||||||
%%
|
|
||||||
% Return values in the Gumbel distribution
|
##
|
||||||
clf
|
# Significant wave-height data on Normal probability paper,
|
||||||
T=1:100000;
|
plt.clf()
|
||||||
sT=gum(2) - gum(1)*log(-log(1-1./T));
|
ax = fig.add_subplot(111)
|
||||||
semilogx(T,sT), hold
|
phat = ws.norm.fit2(np.log(Hs))
|
||||||
N=1:length(Hs); Nmax=max(N);
|
phat.plotresq()
|
||||||
plot(Nmax./N,sort(Hs,'descend'),'.')
|
#tmp2 = ws.probplot(np.log(Hs), phat, ws.norm, plot=ax)
|
||||||
title('Return values in the Gumbel model')
|
|
||||||
xlabel('Return period')
|
#wafostamp([],'(ER)')
|
||||||
ylabel('Return value')
|
plt.show()
|
||||||
wafostamp([],'(ER)')
|
#disp('Block = 3'),pause(pstate)
|
||||||
disp('Block = 4'),pause(pstate)
|
|
||||||
|
##
|
||||||
%% Section 5.2 Generalized Pareto and Extreme Value distributions
|
# Return values in the Gumbel distribution
|
||||||
%% Section 5.2.1 Generalized Extreme Value distribution
|
plt.clf()
|
||||||
|
T = np.r_[1:100000]
|
||||||
% Empirical distribution of significant wave-height with estimated
|
sT = gum[0] - gum[1] * np.log(-np.log1p(-1./T))
|
||||||
% Generalized Extreme Value distribution,
|
plt.semilogx(T, sT)
|
||||||
gev=fitgev(Hs,'plotflag',2)
|
plt.hold(True)
|
||||||
wafostamp([],'(ER)')
|
# ws.edf(Hs).plot()
|
||||||
disp('Block = 5a'),pause(pstate)
|
Nmax = len(Hs)
|
||||||
|
N = np.r_[1:Nmax+1]
|
||||||
clf
|
|
||||||
x = linspace(0,14,200);
|
plt.plot(Nmax/N, sorted(Hs, reverse=True), '.')
|
||||||
plotkde(Hs,[x;pdfgev(x,gev)]')
|
plt.title('Return values in the Gumbel model')
|
||||||
disp('Block = 5b'),pause(pstate)
|
plt.xlabel('Return period')
|
||||||
|
plt.ylabel('Return value')
|
||||||
% Analysis of yura87 wave data.
|
#wafostamp([],'(ER)')
|
||||||
% Wave data interpolated (spline) and organized in 5-minute intervals
|
plt.show()
|
||||||
% Normalized to mean 0 and std = 1 to get stationary conditions.
|
#disp('Block = 4'),pause(pstate)
|
||||||
% maximum level over each 5-minute interval analysed by GEV
|
|
||||||
xn = load('yura87.dat');
|
## Section 5.2 Generalized Pareto and Extreme Value distributions
|
||||||
XI = 0:0.25:length(xn);
|
## Section 5.2.1 Generalized Extreme Value distribution
|
||||||
N = length(XI); N = N-mod(N,4*60*5);
|
|
||||||
YI = interp1(xn(:,1),xn(:,2),XI(1:N),'spline');
|
# Empirical distribution of significant wave-height with estimated
|
||||||
YI = reshape(YI,4*60*5,N/(4*60*5)); % Each column holds 5 minutes of
|
# Generalized Extreme Value distribution,
|
||||||
% interpolated data.
|
gev = ws.genextreme.fit2(Hs)
|
||||||
Y5 = (YI-ones(1200,1)*mean(YI))./(ones(1200,1)*std(YI));
|
gev.plotfitsummary()
|
||||||
Y5M = max(Y5);
|
# wafostamp([],'(ER)')
|
||||||
Y5gev = fitgev(Y5M,'method','mps','plotflag',2)
|
# disp('Block = 5a'),pause(pstate)
|
||||||
wafostamp([],'(ER)')
|
|
||||||
disp('Block = 6'),pause(pstate)
|
plt.clf()
|
||||||
|
x = np.linspace(0,14,200)
|
||||||
%% Section 5.2.2 Generalized Pareto distribution
|
kde = wk.TKDE(Hs, L2=0.5)(x, output='plot')
|
||||||
|
kde.plot()
|
||||||
% Exceedances of significant wave-height data over level 3,
|
plt.hold(True)
|
||||||
gpd3 = fitgenpar(Hs(Hs>3)-3,'plotflag',1);
|
plt.plot(x, gev.pdf(x),'--')
|
||||||
wafostamp([],'(ER)')
|
# disp('Block = 5b'),pause(pstate)
|
||||||
|
|
||||||
%%
|
# Analysis of yura87 wave data.
|
||||||
figure
|
# Wave data interpolated (spline) and organized in 5-minute intervals
|
||||||
% Exceedances of significant wave-height data over level 7,
|
# Normalized to mean 0 and std = 1 to get stationary conditions.
|
||||||
gpd7 = fitgenpar(Hs(Hs>7),'fixpar',[nan,nan,7],'plotflag',1);
|
# maximum level over each 5-minute interval analysed by GEV
|
||||||
wafostamp([],'(ER)')
|
xn = wd.yura87()
|
||||||
disp('Block = 6'),pause(pstate)
|
XI = np.r_[1:len(xn):0.25] - .99
|
||||||
|
N = len(XI)
|
||||||
%%
|
N = N - np.mod(N, 4*60*5)
|
||||||
%Simulates 100 values from the GEV distribution with parameters (0.3, 1, 2), then estimates the
|
|
||||||
%parameters using two different methods and plots the estimated distribution functions together
|
YI = si.interp1d(xn[:, 0],xn[:, 1], kind='linear')(XI)
|
||||||
%with the empirical distribution.
|
YI = YI.reshape(4*60*5, N/(4*60*5)) # Each column holds 5 minutes of
|
||||||
Rgev = rndgev(0.3,1,2,1,100);
|
# interpolated data.
|
||||||
gp = fitgev(Rgev,'method','pwm');
|
Y5 = (YI - YI.mean(axis=0)) / YI.std(axis=0)
|
||||||
gm = fitgev(Rgev,'method','ml','start',gp.params,'plotflag',0);
|
Y5M = Y5.maximum(axis=0)
|
||||||
|
Y5gev = ws.genextreme.fit2(Y5M,method='mps')
|
||||||
x=sort(Rgev);
|
Y5gev.plotfitsummary()
|
||||||
plotedf(Rgev,gp,{'-','r-'});
|
#wafostamp([],'(ER)')
|
||||||
hold on
|
#disp('Block = 6'),pause(pstate)
|
||||||
plot(x,cdfgev(x,gm),'--')
|
|
||||||
hold off
|
## Section 5.2.2 Generalized Pareto distribution
|
||||||
wafostamp([],'(ER)')
|
|
||||||
disp('Block =7'),pause(pstate)
|
# Exceedances of significant wave-height data over level 3,
|
||||||
|
gpd3 = ws.genpareto.fit2(Hs[Hs>3]-3, floc=0)
|
||||||
%%
|
gpd3.plotfitsummary()
|
||||||
% ;
|
#wafostamp([],'(ER)')
|
||||||
Rgpd = rndgenpar(0.4,1,0,1,100);
|
|
||||||
plotedf(Rgpd);
|
##
|
||||||
hold on
|
plt.figure()
|
||||||
gp = fitgenpar(Rgpd,'method','pkd','plotflag',0);
|
# Exceedances of significant wave-height data over level 7,
|
||||||
x=sort(Rgpd);
|
gpd7 = ws.genpareto.fit2(Hs(Hs>7), floc=7)
|
||||||
plot(x,cdfgenpar(x,gp))
|
gpd7.plotfitsummary()
|
||||||
% gm = fitgenpar(Rgpd,'method','mom','plotflag',0);
|
# wafostamp([],'(ER)')
|
||||||
% plot(x,cdfgenpar(x,gm),'g--')
|
# disp('Block = 6'),pause(pstate)
|
||||||
gw = fitgenpar(Rgpd,'method','pwm','plotflag',0);
|
|
||||||
plot(x,cdfgenpar(x,gw),'g:')
|
##
|
||||||
gml = fitgenpar(Rgpd,'method','ml','plotflag',0);
|
#Simulates 100 values from the GEV distribution with parameters (0.3, 1, 2),
|
||||||
plot(x,cdfgenpar(x,gml),'--')
|
# then estimates the parameters using two different methods and plots the
|
||||||
gmps = fitgenpar(Rgpd,'method','mps','plotflag',0);
|
# estimated distribution functions together with the empirical distribution.
|
||||||
plot(x,cdfgenpar(x,gmps),'r-.')
|
Rgev = ws.genextreme.rvs(0.3,1,2,size=100)
|
||||||
hold off
|
gp = ws.genextreme.fit2(Rgev, method='mps');
|
||||||
wafostamp([],'(ER)')
|
gm = ws.genextreme.fit2(Rgev, *gp.par.tolist(), method='ml')
|
||||||
disp('Block = 8'),pause(pstate)
|
gm.plotfitsummary()
|
||||||
|
|
||||||
%%
|
gp.plotecdf()
|
||||||
% Return values for the GEV distribution
|
plt.hold(True)
|
||||||
T = logspace(1,5,10);
|
plt.plot(x, gm.cdf(x), '--')
|
||||||
[sT, sTlo, sTup] = invgev(1./T,Y5gev,'lowertail',false,'proflog',true);
|
plt.hold(False)
|
||||||
|
#wafostamp([],'(ER)')
|
||||||
%T = 2:100000;
|
#disp('Block =7'),pause(pstate)
|
||||||
%k=Y5gev.params(1); mu=Y5gev.params(3); sigma=Y5gev.params(2);
|
|
||||||
%sT1 = invgev(1./T,Y5gev,'lowertail',false);
|
##
|
||||||
%sT=mu + sigma/k*(1-(-log(1-1./T)).^k);
|
# ;
|
||||||
clf
|
Rgpd = ws.genpareto.rvs(0.4,0, 1,size=100)
|
||||||
semilogx(T,sT,T,sTlo,'r',T,sTup,'r'), hold
|
gp = ws.genpareto.fit2(Rgpd, method='mps')
|
||||||
N=1:length(Y5M); Nmax=max(N);
|
gml = ws.genpareto.fit2(Rgpd, method='ml')
|
||||||
plot(Nmax./N,sort(Y5M,'descend'),'.')
|
|
||||||
title('Return values in the GEV model')
|
gp.plotecdf()
|
||||||
xlabel('Return priod')
|
x = sorted(Rgpd)
|
||||||
ylabel('Return value')
|
plt.hold(True)
|
||||||
grid on
|
plt.plot(x, gml.cdf(x))
|
||||||
disp('Block = 9'),pause(pstate)
|
# gm = fitgenpar(Rgpd,'method','mom','plotflag',0);
|
||||||
|
# plot(x,cdfgenpar(x,gm),'g--')
|
||||||
%% Section 5.3 POT-analysis
|
#gw = fitgenpar(Rgpd,'method','pwm','plotflag',0);
|
||||||
|
#plot(x,cdfgenpar(x,gw),'g:')
|
||||||
% Estimated expected exceedance over level u as function of u.
|
#gml = fitgenpar(Rgpd,'method','ml','plotflag',0);
|
||||||
clf
|
#plot(x,cdfgenpar(x,gml),'--')
|
||||||
plotreslife(Hs,'umin',2,'umax',10,'Nu',200);
|
#gmps = fitgenpar(Rgpd,'method','mps','plotflag',0);
|
||||||
wafostamp([],'(ER)')
|
#plot(x,cdfgenpar(x,gmps),'r-.')
|
||||||
disp('Block = 10'),pause(pstate)
|
plt.hold(False)
|
||||||
|
#wafostamp([],'(ER)')
|
||||||
%%
|
#disp('Block = 8'),pause(pstate)
|
||||||
% Estimated distribution functions of monthly maxima
|
|
||||||
%with the POT method (solid),
|
##
|
||||||
% fitting a GEV (dashed) and the empirical distribution.
|
# Return values for the GEV distribution
|
||||||
|
T = np.logspace(1, 5, 10);
|
||||||
% POT- method
|
#[sT, sTlo, sTup] = invgev(1./T,Y5gev,'lowertail',false,'proflog',true);
|
||||||
gpd7 = fitgenpar(Hs(Hs>7)-7,'method','pwm','plotflag',0);
|
|
||||||
khat = gpd7.params(1);
|
#T = 2:100000;
|
||||||
sigmahat = gpd7.params(2);
|
#k=Y5gev.params(1); mu=Y5gev.params(3); sigma=Y5gev.params(2);
|
||||||
muhat = length(Hs(Hs>7))/(7*3*2);
|
#sT1 = invgev(1./T,Y5gev,'lowertail',false);
|
||||||
bhat = sigmahat/muhat^khat;
|
#sT=mu + sigma/k*(1-(-log(1-1./T)).^k);
|
||||||
ahat = 7-(bhat-sigmahat)/khat;
|
plt.clf()
|
||||||
x = linspace(5,15,200);
|
#plt.semilogx(T,sT,T,sTlo,'r',T,sTup,'r')
|
||||||
plot(x,cdfgev(x,khat,bhat,ahat))
|
#plt.hold(True)
|
||||||
disp('Block = 11'),pause(pstate)
|
#N = np.r_[1:len(Y5M)]
|
||||||
|
#Nmax = max(N);
|
||||||
%%
|
#plot(Nmax./N, sorted(Y5M,reverse=True), '.')
|
||||||
% Since we have data to compute the monthly maxima mm over
|
#plt.title('Return values in the GEV model')
|
||||||
%42 months we can also try to fit a
|
#plt.xlabel('Return priod')
|
||||||
% GEV distribution directly:
|
#plt.ylabel('Return value')
|
||||||
mm = zeros(1,41);
|
#plt.grid(True)
|
||||||
for i=1:41
|
#disp('Block = 9'),pause(pstate)
|
||||||
mm(i)=max(Hs(((i-1)*14+1):i*14));
|
|
||||||
end
|
## Section 5.3 POT-analysis
|
||||||
|
|
||||||
gev=fitgev(mm);
|
# Estimated expected exceedance over level u as function of u.
|
||||||
hold on
|
plt.clf()
|
||||||
plotedf(mm)
|
|
||||||
plot(x,cdfgev(x,gev),'--')
|
mrl = ws.reslife(Hs,'umin',2,'umax',10,'Nu',200);
|
||||||
hold off
|
mrl.plot()
|
||||||
wafostamp([],'(ER)')
|
#wafostamp([],'(ER)')
|
||||||
disp('Block = 12, Last block'),pause(pstate)
|
#disp('Block = 10'),pause(pstate)
|
||||||
|
|
||||||
|
##
|
||||||
|
# Estimated distribution functions of monthly maxima
|
||||||
|
#with the POT method (solid),
|
||||||
|
# fitting a GEV (dashed) and the empirical distribution.
|
||||||
|
|
||||||
|
# POT- method
|
||||||
|
gpd7 = ws.genpareto.fit2(Hs(Hs>7)-7, method='mps', floc=0)
|
||||||
|
khat, loc, sigmahat = gpd7.par
|
||||||
|
|
||||||
|
muhat = len(Hs[Hs>7])/(7*3*2)
|
||||||
|
bhat = sigmahat/muhat**khat
|
||||||
|
ahat = 7-(bhat-sigmahat)/khat
|
||||||
|
x = np.linspace(5,15,200);
|
||||||
|
plt.plot(x,ws.genextreme.cdf(x, khat,bhat,ahat))
|
||||||
|
# disp('Block = 11'),pause(pstate)
|
||||||
|
|
||||||
|
##
|
||||||
|
# Since we have data to compute the monthly maxima mm over
|
||||||
|
#42 months we can also try to fit a
|
||||||
|
# GEV distribution directly:
|
||||||
|
mm = np.zeros((1,41))
|
||||||
|
for i in range(41):
|
||||||
|
mm[i] = max(Hs[((i-1)*14+1):i*14])
|
||||||
|
|
||||||
|
|
||||||
|
gev = ws.genextreme.fit2(mm)
|
||||||
|
|
||||||
|
|
||||||
|
plt.hold(True)
|
||||||
|
gev.plotecdf()
|
||||||
|
|
||||||
|
plt.hold(False)
|
||||||
|
#wafostamp([],'(ER)')
|
||||||
|
#disp('Block = 12, Last block'),pause(pstate)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue