-
Notifications
You must be signed in to change notification settings - Fork 46
Automatic threshold for peaks-over-threshold #268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
2ccc286
Merge pull request #267 from MHKiT-Software/develop
ssolson 4477580
automatic Hs threshold
cmichelenstrofer dc56001
Revert "automatic Hs threshold"
cmichelenstrofer 6462315
automatic Hs threshold
cmichelenstrofer c3efa6d
simplify & include MATLAB example for debugging
cmichelenstrofer 8f5dedd
fix independent storms
cmichelenstrofer 89bbcba
Update mhkit/loads/extreme.py
cmichelenstrofer a9f94e3
Update mhkit/loads/extreme.py
cmichelenstrofer 24bc89a
Update mhkit/loads/extreme.py
cmichelenstrofer 3835f7a
Update mhkit/loads/extreme.py
cmichelenstrofer fa27382
cleanup
cmichelenstrofer 0954816
Merge branch 'DLC' of github.com:cmichelenstrofer/MHKiT-Python into DLC
cmichelenstrofer c0d858d
Update mhkit/tests/loads/test_loads.py
akeeste d0a4ddd
Update mhkit/tests/loads/test_loads.py
akeeste 80a431e
Update extreme.py
cmichelenstrofer a9c58b1
break out nested function, consolidate scipy imports
akeeste File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
function [ excess,peak_ind,lambda,avg_sz,tau ] = findPOT( Hs,srate,windsz,thresh ) | ||
%finds independent peaks over a specified threshold value for UNCENSORED Hs data. | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%When using all time records in a year: | ||
% Input Hs should be an nx1 vector of time series data of siginificant wave | ||
% heights. | ||
%When using a subset of time records in a year (i.e. seasonal data, using | ||
% Oct to Apr only, etc.: | ||
% Input Hs should be an nx1 or 1xn cell array of time series data of significant wave | ||
% heights, where each cell is a vector of time series data from a single season/a single specified | ||
% time period. For example, if computing Hs return periods from a | ||
% hindcast ranging from 2000 to 2010 using Oct through Apr data, Hs{1} is | ||
% the time series from Oct 2000 to Apr 2001, Hs{2} is the time series | ||
% from Oct 2001 to Apr 2002, etc | ||
% | ||
%Other Input: | ||
% srate - The sampling rate, in hours, of the data | ||
% windsz - The window size, in hours, to be used to ensure independent | ||
% peaks | ||
% thresh - The threshold, in meters, to be used for determining peaks | ||
% | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
%if full set is given, convert to cell array and compute # years in series | ||
if(~iscell(Hs)) | ||
ny = size(Hs,1)*srate/(24*365.25); %number of years in time series | ||
Hs = {Hs}; | ||
n = 1; | ||
else | ||
%if cell array is given, the number of years/seasons represented by | ||
%series is number of cells in array. | ||
ny = numel(Hs); | ||
n = ny; | ||
end | ||
|
||
%convert window size from 'hours between peaks' to 'observations between peaks' | ||
window = windsz/srate; | ||
excess = []; | ||
peak_ind = []; | ||
tau = 0; | ||
%loop through each season (only one when full dataset is used, jan - dec) | ||
for i = 1:n | ||
%convert to vector for easier use | ||
Hs_curr = Hs{i}; | ||
nv = max(size(Hs_curr)); | ||
|
||
%find all points above the threshold | ||
ind = find(Hs_curr > thresh); | ||
|
||
%avg time between storms | ||
temp = diff(ind); | ||
tau = tau + mean(temp(find(temp>windsz/2))); | ||
|
||
nex = max(size(ind)); | ||
if(size(ind,1) > 0) | ||
%identify start and end points of all groups of consecutive points above threshold | ||
stpt = ind(1); | ||
endpt = []; | ||
for i = 2:nex - 1 | ||
if(ind(i + 1) - ind(i) > 1) | ||
endpt = [endpt;ind(i)]; | ||
stpt = [stpt;ind(i + 1)]; | ||
end | ||
end | ||
endpt = [endpt;ind(end)]; | ||
avg_sz = mean(endpt - stpt); | ||
%set number clusters | ||
nclust = size(endpt,1); | ||
|
||
%find maxima for each cluster | ||
peaks = NaN(nclust,2); | ||
for i = 1:nclust | ||
s = stpt(i); | ||
e = endpt(i); | ||
[peaks(i,1) tempi] = max(Hs_curr(s:e)); | ||
clustrang = s:e; | ||
peaks(i,2) = clustrang(tempi); | ||
end | ||
|
||
%for independence, check that new peak is more than window from old | ||
%peak. if time between is less than window, use only larger of two. | ||
peak_dist = diff(peaks(:,2)); | ||
if(min(size(find(peak_dist<window))) > 0) | ||
newp = peaks(1,:); | ||
for i = 2:nclust | ||
if(peak_dist(i-1) > window) | ||
newp = [newp;peaks(i,:)]; | ||
else | ||
if(newp(end,1) > peaks(i,1)) | ||
if(i < nclust) | ||
peak_dist(i) = peaks(i+1,2) - newp(end,2); | ||
end | ||
else | ||
newp(end,:) = peaks(i,:); | ||
end | ||
end | ||
end | ||
|
||
peaks = newp; | ||
end | ||
|
||
%compute excesses and index of peaks to return | ||
excess = [excess;peaks(:,1) - thresh]; | ||
peak_ind = [peak_ind;peaks(:,2)]; | ||
end | ||
end | ||
|
||
%compute average number of clusters per year/season/time frame | ||
lambda = max(size(excess))/ny; | ||
tau = tau/ny; | ||
end | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
function [pct, best_thresh] = threshold(Hs,samp_rate) | ||
|
||
nlags = floor(14*24/samp_rate); | ||
[acf,lag] = xcorr(Hs - mean(Hs), nlags, 'coeff'); | ||
positive_lag = lag((nlags+1):end); | ||
positive_acf = acf((nlags+1):end) % CM | ||
below_thresh = find(acf((nlags+1):end) < 0.5); | ||
|
||
%if it doesn't drop below 0.5 in first 14 days (nlags) then the user should | ||
%double check their input OR should be fitting this manually to choose the | ||
%window. | ||
if isempty(below_thresh) | ||
fprintf(strcat('ERROR: The acf does not drop below 0.5 in first 14 days, check inputs', ... | ||
' and retry function, or fit manually.\n')) | ||
HsR = NaN; | ||
flags = true; | ||
POT_info = {}; | ||
return | ||
end | ||
|
||
%set window size (in hours) to the time where acf dropped below 0.5 | ||
windsize = samp_rate*positive_lag(min(below_thresh)); | ||
|
||
%set flag if the window is wider than 4 days | ||
if windsize > 24*4 - 1 | ||
fprintf('WARNING: The acf window is over 4 days. Check final fit.\n') | ||
flags.large_window_size = true; | ||
else | ||
flags.large_window_size = false; | ||
end | ||
|
||
%save the window size in info structure | ||
POT_info.window_size = windsize; | ||
|
||
%clear nlags acf lag positive_lag below_thresh | ||
|
||
distribution = 'GeneralizedPareto'; | ||
npar = 3; | ||
|
||
%initialize the first set of thresholds to be tested as the percentiles | ||
%ranging from 99 to 99.5 percentile | ||
pct_step = 0.1; | ||
thresh_pct = [99:pct_step:99.5]; | ||
thresh_test = prctile(Hs,thresh_pct); | ||
keep_going = true; | ||
current_best_thresh = -100; | ||
flags.test_lambda_below_1 = false; | ||
|
||
%loop through until the maximum R is found or the number of peaks per | ||
%year chosen drops to ~1 per year (annual maxima method) | ||
while(keep_going) | ||
thresh_corr = NaN(size(thresh_test)); | ||
for i = 1:length(thresh_test) | ||
%find peaks over the test threshold | ||
thresh = thresh_test(i); | ||
|
||
[ excess,peak_ind,lambda,avg_sz ] = findPOT( Hs,samp_rate,windsize,thresh ); | ||
|
||
%fit distribution | ||
|
||
POTdist = fitdist(excess,distribution); | ||
%get qq plot | ||
QQ_plt = qqplot(excess,POTdist); | ||
X_data = get(QQ_plt,'Xdata'); | ||
Y_data = get(QQ_plt,'Ydata'); | ||
%find correlation | ||
thresh_corr(i) = corr(X_data{1}',Y_data{1}'); | ||
|
||
%if lambda falls below one, the sample set is now less than one | ||
%peak per year and should not be used, (might as well use AM) | ||
if(lambda < 1) | ||
thresh_corr(i) = 0; | ||
flags.test_lambda_below_1 = true; | ||
end | ||
|
||
end | ||
%find threshold with maximum correlation | ||
[~,max_i] = max(thresh_corr); | ||
|
||
%if the threshold hasn't changed more than 0.05m OR the number of | ||
%samples per year has dropped below 2 (~one per year), then quit | ||
if(abs(current_best_thresh - thresh_test(max_i)) < 0.05 || lambda < 2) | ||
best_thresh = thresh_test(max_i); | ||
pct = thresh_pct(max_i); | ||
keep_going = false; | ||
if(lambda < 2) | ||
flags.loop_stopped_by_lambda = true; | ||
else | ||
flags.loop_stopped_by_lambda = false; | ||
end | ||
%otherwise, find a finer range around the new maximum and loop again | ||
else | ||
current_best_thresh = thresh_test(max_i); | ||
pct_step = pct_step/10; | ||
if(max_i == length(thresh_test)) | ||
thresh_pct = thresh_pct(max_i-1):pct_step:thresh_pct(max_i)+5*pct_step; | ||
elseif(max_i == 1) | ||
thresh_pct = thresh_pct(max_i)-9*pct_step:pct_step:thresh_pct(max_i+1); | ||
else | ||
thresh_pct = thresh_pct(max_i-1):pct_step:thresh_pct(max_i+1); | ||
end | ||
thresh_test = prctile(Hs,thresh_pct); | ||
end | ||
end | ||
end |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.