def test_HDC3d_WLN(self): dep1 = (None, None, None) dep2 = (0, None, 0) dep3 = (None, 0, 0) #define parameters shape = ConstantParam(1.471) loc = ConstantParam(0.8888) scale = ConstantParam(2.776) par1 = (shape, loc, scale) shape = None loc = FunctionParam(4, 10, 0.02, "f1") scale = FunctionParam(0.1, 0.02, -0.1, "f2") par2 = (shape, loc, scale) mu = FunctionParam(0.1, 1.5, 0.2, "f1") sigma = FunctionParam(0.1, 0.2, -0.2, "f2") #create distributions dist1 = WeibullDistribution(*par1) dist2 = LognormalDistribution(mu=mu, sigma=sigma) dist3 = NormalDistribution(*par2) distributions = [dist1, dist2, dist3] dependencies = [dep1, dep2, dep3] mul_dist = MultivariateDistribution(distributions, dependencies) del mu, sigma #del dist1, dist2, par1, par2, dep1, dep2, dependencies, distributions #calc contour n_years = 50 limits = [(0, 20), (0, 20), (0, 20)] deltas = [0.5, 0.5, 0.05] test_contour_HDC = HighestDensityContour(mul_dist, n_years, 3, limits, deltas) finaldt3 = pd.DataFrame({ 'x': test_contour_HDC.coordinates[0][0], 'y': test_contour_HDC.coordinates[0][1], 'z': test_contour_HDC.coordinates[0][2] }) matlab3 = pd.read_csv(testfiles_path + "/hdc3d_wln.csv", names=['x', 'y', 'z']) result3 = pd.read_csv(testfiles_path + "/HDC3dWLN_coordinates.csv") for m, n in [(m, n) for m in result3.index for n in result3.columns]: self.assertAlmostEqual(result3.loc[m, n], finaldt3.loc[m, n], places=8)
def test_HDC3d_WLL(self): """ Creating Contour example for 3-d HDC with Weibull, Lognormal and Lognormal distribution """ dep1 = (None, None, None) dep2 = (0, None, 0) dep3 = (0, None, 0) #define parameters shape = ConstantParam(1.471) loc = ConstantParam(0.8888) scale = ConstantParam(2.776) par1 = (shape, loc, scale) mu = FunctionParam(0.1000, 1.489, 0.1901, "f1") sigma = FunctionParam(0.0400, 0.1748, -0.2243, "f2") #del shape, loc, scale #create distributions dist1 = WeibullDistribution(*par1) dist2 = LognormalDistribution(mu=mu, sigma=sigma) dist3 = LognormalDistribution(mu=mu, sigma=sigma) distributions = [dist1, dist2, dist3] dependencies = [dep1, dep2, dep3] mul_dist = MultivariateDistribution(distributions, dependencies) #del dist1, dist2, par1, par2, dep1, dep2, dependencies, distributions #calc contour n_years = 50 limits = [(0, 20), (0, 18), (0, 18)] deltas = [1, 1, 1] test_contour_HDC = HighestDensityContour(mul_dist, n_years, 3, limits, deltas) finaldt = pd.DataFrame({ 'x': test_contour_HDC.coordinates[0][0], 'y': test_contour_HDC.coordinates[0][1], 'z': test_contour_HDC.coordinates[0][2] }) result = pd.read_csv(testfiles_path + "/HDC3dWLL_coordinates.csv") for i, j in [(i, j) for i in result.index for j in result.columns]: self.assertAlmostEqual(result.loc[i, j], finaldt.loc[i, j], places=8)
def test_HDC2d_WL(self): """ 2-d HDC with Weibull and Lognormal distribution. The used probabilistic model is described in Vanem and Bitner-Gregersen (2012), DOI: 10.1016/j.apor.2012.05.006 """ #define dependency tuple dep1 = (None, None, None) dep2 = (0, None, 0) #define parameters shape = ConstantParam(1.471) loc = ConstantParam(0.8888) scale = ConstantParam(2.776) par1 = (shape, loc, scale) mu = FunctionParam(0.1000, 1.489, 0.1901, 'power3') sigma = FunctionParam(0.0400, 0.1748, -0.2243, 'exp3') #del shape, loc, scale #create distributions dist1 = WeibullDistribution(*par1) dist2 = LognormalDistribution(mu=mu, sigma=sigma) distributions = [dist1, dist2] dependencies = [dep1, dep2] mul_dist = MultivariateDistribution(distributions, dependencies) #del dist1, dist2, par1, par2, dep1, dep2, dependencies, distributions #calc contour n_years = 50 limits = [(0, 20), (0, 18)] deltas = [0.1, 0.1] test_contour_HDC = HighestDensityContour(mul_dist, n_years, 3, limits, deltas) finaldt0 = pd.DataFrame({ 'x': test_contour_HDC.coordinates[0][0], 'y': test_contour_HDC.coordinates[0][1] }) result0 = pd.read_csv(testfiles_path + "/HDC2dWL_coordinates.csv") for g, h in [(g, h) for g in result0.index for h in result0.columns]: self.assertAlmostEqual(result0.loc[g, h], finaldt0.loc[g, h], places=8)
def test_HDC2d_WN(self): """ Creating Contour example """ #define dependency tuple dep1 = (None, None, None) dep2 = (None, 0, 0) #define parameters shape = ConstantParam(1.471) loc = ConstantParam(0.8888) scale = ConstantParam(2.776) par1 = (shape, loc, scale) shape = None loc = FunctionParam(4, 10, 0.02, "f1") scale = FunctionParam(0.1, 0.02, -0.1, "f2") par2 = (shape, loc, scale) #del shape, loc, scale #create distributions dist1 = WeibullDistribution(*par1) dist2 = NormalDistribution(*par2) distributions = [dist1, dist2] dependencies = [dep1, dep2] mul_dist = MultivariateDistribution(distributions, dependencies) #del dist1, dist2, par1, par2, dep1, dep2, dependencies, distributions #calc contour n_years = 50 limits = [(0, 20), (0, 20)] deltas = [0.05, 0.01] test_contour_HDC = HighestDensityContour(mul_dist, n_years, 3, limits, deltas) finaldt2 = pd.DataFrame({ 'x': test_contour_HDC.coordinates[0][0], 'y': test_contour_HDC.coordinates[0][1] }) result2 = pd.read_csv(testfiles_path + "/HDC2dWN_coordinates.csv") for k, l in [(k, l) for k in result2.index for l in result2.columns]: self.assertAlmostEqual(result2.loc[k, l], finaldt2.loc[k, l], places=8)
def test_omae2020_wind_wave_contour(self): """ Contour similar to the wind-wave contour in 'Global hierararchical models for wind and wave contours', dataset D. First variable = wind speed, second variable = significant wave height. """ # Define dependency tuple. dep1 = (None, None, None, None) # shape, location, scale, shape2 dep2 = (0, None, 0, None) # shape, location, scale, shape2 # Define parameters. v_shape = ConstantParam(2.42) v_loc = None v_scale = ConstantParam(10) v_shape2 = ConstantParam(0.761) par1 = (v_shape, v_loc, v_scale, v_shape2) hs_shape = FunctionParam('logistics4', 0.582, 1.90, 0.248, 8.49) hs_loc = None hs_scale = FunctionParam('alpha3', 0.394, 0.0178, 1.88, C1=0.582, C2=1.90, C3=0.248, C4=8.49) hs_shape2 = ConstantParam(5) par2 = (hs_shape, hs_loc, hs_scale, hs_shape2) # Create distributions. dist1 = ExponentiatedWeibullDistribution(*par1) dist2 = ExponentiatedWeibullDistribution(*par2) distributions = [dist1, dist2] dependencies = [dep1, dep2] mul_dist = MultivariateDistribution(distributions, dependencies) # Calculate the contour. n_years = 50 limits = [(0, 40), (0, 20)] deltas = [0.1, 0.1] test_contour_HDC = HighestDensityContour(mul_dist, n_years, 1, limits, deltas) # Compare the computed contours to the contours published in # 'Global hierarchical models for wind and wave contours', Figure 8. max_v = max(test_contour_HDC.coordinates[0][0]) self.assertAlmostEqual(max_v, 29.5, delta=0.5) # Should be about 29.5 max_hs = max(test_contour_HDC.coordinates[0][1]) self.assertAlmostEqual(max_hs, 14.5, delta=0.5) # Should be about 15
def hdc(probabilistic_model: ProbabilisticModel, return_period, state_duration, limits, deltas): """ Interface to viroconcom to compute an highest density contour (HDC). Parameters ---------- probabilistic_model : ProbabilisticModel, The probabilistic model, i.e. the joint distribution function, which should be evaluated. return_period : float, The return period of the contour in years. state_duration : float, The sea state's or more general the environmental state's duration in hours. limits : list of tuple, One 2-element tuple per dimension in mul_var_distribution, containing min and max limits for calculation ((min, max)). The smaller value is always assumed minimum. deltas : float or list of float, The grid cell size used for the calculation. If a single float is supplied it is used for all dimensions. If a list of float is supplied it has to be of the same length as there are dimensions in mul_var_dist. Returns ------- contour_coordinates : list of list of numpy.ndarray, Contains the coordinates of points on the contour. The outer list contains can hold multiple contour paths if the distribution is multimodal. The inner list contains multiple numpy arrays of the same length, one per dimension. The values of the arrays are the coordinates in the corresponding dimension. """ mul_dist = setup_mul_dist(probabilistic_model) contour = HighestDensityContour(mul_var_distribution=mul_dist, return_period=return_period, state_duration=state_duration, limits=limits, deltas=deltas, timeout=MAX_COMPUTING_TIME) contour_coordinates = contour.coordinates return contour_coordinates
def _setup(self, limits=[(0, 20), (0, 20)], deltas=[0.05, 0.05], n_years = 25, dep1=(None, None, None), dep2=(0, None, 0), par1=(ConstantParam(1.471), ConstantParam(0.8888), ConstantParam(2.776)), par2=(FunctionParam('exp3', 0.0400, 0.1748, -0.2243), None, FunctionParam('power3', 0.1, 1.489, 0.1901)) ): """ Creating a contour (same as in DOI: 10.1016/j.coastaleng.2017.03.002). """ self.limits = limits self.deltas = deltas self.n_years = n_years # Define dependency tuple. self.dep1 = dep1 self.dep2 = dep2 # Define parameters. self.par1 = par1 self.par2 = par2 # Create distributions. dist1 = WeibullDistribution(*par1) dist2 = LognormalDistribution(*par2) distributions = [dist1, dist2] dependencies = [dep1, dep2] mul_dist = MultivariateDistribution(distributions, dependencies) # Compute contour. test_contour_HDC = HighestDensityContour(mul_dist, n_years, 3, limits, deltas) return test_contour_HDC
def test_HDC4d_WLLL(self): """ Contour example for a 4-dimensinal HDC with Weibull, Lognormal, Lognormal and Lognormal distribution. """ # Define dependency tuple. dep1 = (None, None, None) dep2 = (0, None, 0) dep3 = (0, None, 0) dep4 = (0, None, 0) # Define parameters. shape = ConstantParam(2.776) loc = ConstantParam(1.471) scale = ConstantParam(0.8888) par1 = (shape, loc, scale) mu = FunctionParam('power3', 0.1000, 1.489, 0.1901) sigma = FunctionParam('exp3', 0.0400, 0.1748, -0.2243) # Create distributions. dist1 = WeibullDistribution(*par1) dist2 = LognormalDistribution(mu=mu, sigma=sigma) dist3 = LognormalDistribution(mu=mu, sigma=sigma) dist4 = LognormalDistribution(mu=mu, sigma=sigma) distributions = [dist1, dist2, dist3, dist4] dependencies = [dep1, dep2, dep3, dep4] mul_dist = MultivariateDistribution(distributions, dependencies) # Compute contour. n_years = 50 limits = [(0, 20), (0, 18), (0, 18), (0, 18)] deltas = [1, 1, 1, 1] test_contour_HDC = HighestDensityContour(mul_dist, n_years, 3, limits, deltas)
def test_HDC4d_WLLL(self): """ Creating Contour example for 4-d HDC with Weibull, Lognormal, Lognormal and Lognormal distribution """ #define dependency tuple dep1 = (None, None, None) dep2 = (0, None, 0) dep3 = (0, None, 0) dep4 = (0, None, 0) #define parameters shape = ConstantParam(2.776) loc = ConstantParam(1.471) scale = ConstantParam(0.8888) par1 = (shape, loc, scale) mu = FunctionParam(0.1000, 1.489, 0.1901, "f1") sigma = FunctionParam(0.0400, 0.1748, -0.2243, "f2") #create distributions dist1 = WeibullDistribution(*par1) dist2 = LognormalDistribution(mu=mu, sigma=sigma) dist3 = LognormalDistribution(mu=mu, sigma=sigma) dist4 = LognormalDistribution(mu=mu, sigma=sigma) distributions = [dist1, dist2, dist3, dist4] dependencies = [dep1, dep2, dep3, dep4] mul_dist = MultivariateDistribution(distributions, dependencies) #del dist1, dist2, par1, par2, dep1, dep2, dependencies, distributions #calc contour n_years = 50 limits = [(0, 20), (0, 18), (0, 18), (0, 18)] deltas = [1, 1, 1, 1] test_contour_HDC = HighestDensityContour(mul_dist, n_years, 3, limits, deltas)
def _setup(self, limits=[(0, 20), (0, 20)], deltas=[0.05, 0.05], n_years=25, dep1=(None, None, None), dep2=(0, None, 0), par1=(ConstantParam(1.471), ConstantParam(0.8888), ConstantParam(2.776)), par2=(FunctionParam(0.0400, 0.1748, -0.2243, "f2"), None, FunctionParam(0.1, 1.489, 0.1901, "f1"))): """ Creating Contour example """ self.limits = limits self.deltas = deltas self.n_years = n_years #define dependency tuple self.dep1 = dep1 self.dep2 = dep2 #define parameters self.par1 = par1 self.par2 = par2 #create distributions dist1 = WeibullDistribution(*par1) dist2 = LognormalDistribution(*par2) distributions = [dist1, dist2] dependencies = [dep1, dep2] mul_dist = MultivariateDistribution(distributions, dependencies) #calc contour test_contour_HDC = HighestDensityContour(mul_dist, n_years, 3, limits, deltas) return test_contour_HDC
def test_HDC2d_ExponentiatedWbl(self): """ 2-d HDC with exponentiated Weibull distributions. """ # Define dependency tuple. dep1 = (None, None, None, None) # shape, location, scale, shape2 dep2 = (None, None, 0, None) # shape, location, scale, shape2 # Define parameters. v_shape = ConstantParam(11) v_loc = None v_scale = ConstantParam(2.6) v_shape2 = ConstantParam(0.54) par1 = (v_shape, v_loc, v_scale, v_shape2) hs_shape = ConstantParam(1.4) hs_loc = None hs_scale = FunctionParam('power3', 0.15, 0.0033, 2.45) hs_shape2 = ConstantParam(5) par2 = (hs_shape, hs_loc, hs_scale, hs_shape2) # Create distributions. dist1 = ExponentiatedWeibullDistribution(*par1) dist2 = ExponentiatedWeibullDistribution(*par2) distributions = [dist1, dist2] dependencies = [dep1, dep2] mul_dist = MultivariateDistribution(distributions, dependencies) # Calculate the contour. n_years = 50 limits = [(0, 20), (0, 18)] deltas = [0.1, 0.1] test_contour_HDC = HighestDensityContour(mul_dist, n_years, 3, limits, deltas)
# Define a Lognormal distribution representing spectral peak period. my_sigma = FunctionParam(0.05, 0.2, -0.2, "exp3") my_mu = FunctionParam(0.1, 1.5, 0.2, "power3") dist1 = LognormalDistribution(sigma=my_sigma, mu=my_mu) dep1 = (0, None, 0) # Parameter one and three depend on dist0. # Create a multivariate distribution by bundling the two distributions. distributions = [dist0, dist1] dependencies = [dep0, dep1] mul_dist = MultivariateDistribution(distributions, dependencies) # Compute an IFORM contour with a return period of 25 years, a sea state # duration of 3 hours and 100 points along the contour. iform_contour = IFormContour(mul_dist, 25, 3, 100) # Compute a highest density contour with the same settings (25 years return # period, 3 hour sea state duration). limits = [(0, 20), (0, 20)] # The limits of the computational domain. deltas = [0.4, 0.4] # The dimensions of the grid cells. hdens_contour = HighestDensityContour(mul_dist, 25, 3, limits, deltas) # Plot the two contours. plt.scatter(hdens_contour.coordinates[0][0], hdens_contour.coordinates[0][1], label="highest density contour") plt.scatter(iform_contour.coordinates[0][0], iform_contour.coordinates[0][1], label="IFORM contour") plt.xlabel('significant wave height [m]') plt.ylabel('spectral peak period [s]') plt.legend() plt.show()
# shape, location, scale, shape2 'functions': ('logistics4', None, 'alpha3', None), # shape, location, scale, shape2 'min_datapoints_for_fit': 50, 'do_use_weights_for_dependence_function': True} # Fit the model to the dataset. fit = Fit((dataset_d_v, dataset_d_hs), (dist_description_v, dist_description_hs)) dist0 = fit.mul_var_dist.distributions[0] dist1 = fit.mul_var_dist.distributions[1] # Compute 50-yr contour. return_period = 50 ts = 1 # Sea state duration in hours. limits = [(0, 45), (0, 25)] # Limits of the computational domain. deltas = [GRID_CELL_SIZE, GRID_CELL_SIZE] # Dimensions of the grid cells. hdc = HighestDensityContour(fit.mul_var_dist, return_period, ts, limits, deltas) contour_with_all_data = sort_points_to_form_continous_line( hdc.coordinates[0], hdc.coordinates[1], do_search_for_optimal_start=True) # Create the figure for plotting the contours. fig, axs = plt.subplots(len(NR_OF_YEARS_TO_DRAW), 2, sharex=True, sharey=True, figsize=(8, 9)) for j in range(len(NR_OF_YEARS_TO_DRAW)): nr_of_datapoints_to_draw = round(NR_OF_YEARS_TO_DRAW[j] * 365.25 * 24) for i in range(NR_OF_BOOTSTRAP_SAMPLES[j]): print('j = {}/{}, random sample, contour {}/{}'.format( j, len(NR_OF_YEARS_TO_DRAW) - 1, i + 1, NR_OF_BOOTSTRAP_SAMPLES[j])) # Resample from the hindcast dataset to get the sample D_i. sample_indices = np.random.randint(dataset_d_v.size, size=nr_of_datapoints_to_draw)
# shape, location, scale, shape2 'min_datapoints_for_fit': 50, 'do_use_weights_for_dependence_function': True } # Fit the model to the dataset. fit = Fit((v_i, hs_i), (dist_description_v, dist_description_hs)) dist0 = fit.mul_var_dist.distributions[0] dist1 = fit.mul_var_dist.distributions[1] # Compute 50-yr IFORM contour. return_period = 50 ts = 1 # Sea state duration in hours. limits = [(0, 45), (0, 25)] # Limits of the computational domain. deltas = [0.05, 0.05] # Dimensions of the grid cells. hdc_contour_i = HDC(fit.mul_var_dist, return_period, ts, limits, deltas) c = sort_points_to_form_continous_line(hdc_contour_i.coordinates[0][0], hdc_contour_i.coordinates[0][1], do_search_for_optimal_start=True) hdc_contour_i.c = c if DO_COMPUTE_CONFIDENCE_INTERVAL: # Define angles based on normalization. theta_stars = np.arange(0, 360, ANGLE_STEP_FOR_CI) / 180 * np.pi t1 = max(dataset_d_v) - min(dataset_d_v) t2 = max(dataset_d_hs) - min(dataset_d_hs) thetas = thetastar_to_theta(theta_stars, t1, t2) nr_of_datapoints_on_angled_line = 10 line_tot_length = 50.0 line_length = np.linspace(0.0, line_tot_length, nr_of_datapoints_on_angled_line)
dist_hs = ExponentiatedWeibullDistribution() dist_hs.fit(hs) joint_dist = MultivariateDistribution(distributions=[dist_hs, dist_s], dependencies=[(None, None, None, None), (None, 0, None)]) # Fit the model to the data. #fit = Fit((hs, steepness), # (dist_description_hs, dist_description_s)) #joint_dist = fit.mul_var_dist trs = [1, 50, 250] fms = np.empty(shape=(3, 1)) for i, tr in enumerate(trs): HDC = HighestDensityContour(joint_dist, return_period=tr, state_duration=1, limits=[(0, 20), (0, 0.1)]) fms[i] = HDC.fm h_step = 0.05 s_step = 0.00025 h_grid, s_grid = np.mgrid[0:18:h_step, 0:0.08:s_step] f = np.empty_like(h_grid) for i in range(h_grid.shape[0]): for j in range(h_grid.shape[1]): f[i, j] = joint_dist.pdf([h_grid[i, j], s_grid[i, j]]) print('Done with calculating f') axs[2].scatter(tp, hs, c='black', s=5, alpha=0.5, rasterized=True) axs[2].spines['right'].set_visible(False) axs[2].spines['top'].set_visible(False)
my_sigma = FunctionParam('exp3', 0.040, 0.175, -0.224) my_mu = FunctionParam('power3', 0.100, 1.489, 0.190) dist1 = LognormalDistribution(sigma=my_sigma, mu=my_mu) dep1 = (0, None, 0) # Parameter one and three depend on dist0. distributions = [dist0, dist1] dependencies = [dep0, dep1] mul_dist = MultivariateDistribution(distributions, dependencies) # Compute an IFORM, an ISORM and a highest density contour. return_period = 50 # In years sea_state_duration = 6 # In hours iform_contour = IFormContour(mul_dist, return_period, sea_state_duration, 100) isorm_contour = ISormContour(mul_dist, return_period, sea_state_duration, 100) limits = [(0, 20), (0, 20)] # Limits of the computational domain deltas = [0.005, 0.005] # Dimensions of the grid cells hdens_contour = HighestDensityContour( mul_dist, return_period, sea_state_duration, limits, deltas) # Plot the three contours. plt.scatter(hdens_contour.coordinates[0][0], hdens_contour.coordinates[0][1], label='highest density contour') plt.scatter(iform_contour.coordinates[0][0], iform_contour.coordinates[0][1], label='IFORM contour') plt.scatter(isorm_contour.coordinates[0][0], isorm_contour.coordinates[0][1], label='ISORM contour') plt.xlabel('significant wave height [m]') plt.ylabel('zero-upcrossing period [s]') plt.legend() plt.show() # To evalute viroconcom, we compare the maximum values of the contour with the
unconditonal_variable_label=v_label) fig_fit.subplots_adjust(wspace=0.25, bottom=0.15) figure_folder = 'gfx/' figure_fname = figure_folder + 'FitVHs' #fig_fit.savefig(figure_fname + '.pdf', bbox_inches='tight') fig_fit.savefig(figure_fname, dpi=dpi_for_printing_figures, bbox_inches='tight') IFORMC = IFormContour(joint_dist, return_period=50, state_duration=1) limits = [(0, 45), (0, 25)] deltas = [0.05, 0.05] HDC2D = HighestDensityContour(joint_dist, return_period=50, state_duration=1, limits=limits, deltas=deltas) print('Done with calcluating the 2D HDC') v_step = 0.1 h_step = 0.1 vgrid, hgrid = np.mgrid[0:45:v_step, 0:22:h_step] f = np.empty_like(vgrid) for i in range(vgrid.shape[0]): for j in range(vgrid.shape[1]): f[i, j] = joint_dist.pdf([vgrid[i, j], hgrid[i, j]]) print('Done with calculating f') fig, ax = plt.subplots(1, 1, figsize=(5, 5), dpi=300) ax.scatter(v, hs, c='black', s=5, alpha=0.5, rasterized=True)