def fit_and_plot_weibull(wind_speeds, x_plot, line_styling, line_label, percentiles): """Fit Weibull distribution to histogram data and plot result. The used fitting method yielded better fits than weibull_min.fit(). Args: wind_speeds (list): Series of wind speeds. x_plot (list): Wind speeds for which the Weibull fit is plotted. format string (str): A format string for setting basic line properties. line_label (str): Label name of line in legend. percentiles (list): Heights above the ground at which the wind speeds are evaluated. """ # Perform curve fitting. starting_point = curve_fit_starting_points.get(line_label, None) # Actual histogram data is used to fit the Weibull distribution to. hist, bin_edges = np.histogram(wind_speeds, 100, range=(0., 35.)) bin_width = bin_edges[1] - bin_edges[0] bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2 def weibull_fit_fun(x, c, loc, scale): y = weibull_min.pdf(x, c, loc, scale) return y * len(wind_speeds) * bin_width wb_fit, _ = curve_fit(weibull_fit_fun, bin_centers, hist, p0=starting_point, bounds=((-np.inf, 0, -np.inf), (np.inf, np.inf, np.inf))) # Plot fitted curve. y_plot = weibull_min.pdf(x_plot, *wb_fit) plt.plot(x_plot, y_plot, line_styling, label=line_label) # Plot percentile markers. x_percentile_markers = np.percentile(wind_speeds, percentiles) y_percentile_markers = weibull_min.pdf(x_percentile_markers, *wb_fit) for x_p, y_p, m, p in zip(x_percentile_markers, y_percentile_markers, marker_cycle, percentiles): plt.plot(x_p, y_p, m, color=line_styling, markersize=8, markeredgewidth=2, markerfacecolor='None') return x_percentile_markers, wb_fit
def truncatedweibull_pdf(data, c, scale): epsilon = 1e-200 term2 = (weibull_min.pdf(data, c, scale=scale, loc=0.0) / (weibull_min.cdf(1.0, c, scale=scale, loc=0.0) - weibull_min.cdf(0.0, c, scale=scale, loc=0.0))) * (data < 1.0) return term2 + epsilon
def adjustar_Weibull(mid_pointa, histoa): a_ajustar_wei = lambda x,c,l,s: weibull_min.pdf(x, c, l, s) popt_wei, pcov = curve_fit(a_ajustar_wei, mid_pointa, histoa) print(popt_wei, pcov) ajustado_wey = a_ajustar_wei(mid_pointa, popt_wei[0], popt_wei[1], popt_wei[2]) savez('adjTemp.npz', parametros=popt_wei) return(ajustado_wey)
def plot_weibull(sample, c, loc, scale, ks, pVal, p, q, figname="Lily_weibull_test.png"): # compare the sample histogram and fitting result fig, ax = plt.subplots(1, 1) x = np.linspace(-1.01 * max(sample), -0.99 * min(sample), 100) ax.plot(x, weibull_min.pdf(x, c, loc, scale), 'r-', label='fitted pdf ' + p + '-bnd') ax.hist(-sample, normed=True, bins=20, histtype='stepfilled') ax.legend(loc='best', frameon=False) plt.xlabel('-Lips_' + q) plt.ylabel('pdf') plt.title( 'c = {:.2f}, loc = {:.2f}, scale = {:.2f}, ks = {:.2f}, pVal = {:.2f}'. format(c, loc, scale, ks, pVal)) plt.savefig(figname) plt.close()
def fit_weibull(data, length=10): trans_fun = lambda x: (x - 1) / 10 + 0.5 x = numpy.array(sorted(list(set([trans_fun(i) for i in numpy.arange(1, length + 1)])))) confidence_vals = [[] for i in range(len(x))] nums = numpy.array(data.groupby('user_id').apply(len).to_dict().values()) nums_groupped = defaultdict(list) for num in nums: nums_groupped[(num - 1) / 10].append(num) nums_avg = {key: numpy.mean(values) / 10.0 for (key, values) in nums_groupped.iteritems()} nums_trans = [nums_avg[(num - 1) / 10] for num in nums] fit_values = weibull_min.fit(nums_trans, floc=0) fit = weibull_min.pdf(x, *fit_values) for i, f in enumerate(fit): confidence_vals[i] = f def _aggr(r): return { 'value': r, 'confidence_interval_min': r, 'confidence_interval_max': r, } return { 'serie': map(_aggr, confidence_vals), 'params': list(fit_values), }
def loglikelihood(I,a,b,T): # calculates the inverse of the loglikelihood function for observing data I # when the distribution is truncated at time T, given shape a and scale b event1=I[:,0]; # first events event2=I[:,1]; # second events L = weibull_min.pdf(event2-event1,b,0,a)/weibull_min.cdf(T-event1,b,0,scale = a); logL=(sum(np.log(L))**(-1)); # calculate inverse loglikelihood return(logL);
def getWeibullPdf(dataset, nbins, bins): c = 1.5 shape, loc, scale = weibull_min.fit(dataset, floc=0) x = np.linspace(min(bins), max(bins), nbins) print('WEI: shape=' + str(shape) + ', loc=' + str(loc) + ", scale=" + str(scale)) pdf = weibull_min.pdf(x, shape, loc, scale) return (x, pdf)
def truncatedweibull_pdf(data, prior, c, scale): epsilon = 1e-200 term1 = prior * (data == 1.0) term2 = (1 - prior) * (weibull_min.pdf(data, c, scale=scale, loc=0.0) / (weibull_min.cdf(1.0, c, scale=scale, loc=0.0) - weibull_min.cdf(0.0, c, scale=scale, loc=0.0))) * ( data < 1.0) return term1 + term2 + epsilon
def fit_weibull(df_speed, x, weibull_params=None): from scipy.stats import weibull_min if not weibull_params: k_shape, _, lamb_scale = weibull_params = weibull_min.fit(df_speed, loc=0) y_weibull = weibull_min.pdf(x, *weibull_params) density_expected_weibull = weibull_min.cdf( x[1:], *weibull_params) - weibull_min.cdf(x[:-1], *weibull_params) y_cdf_weibull = 1 - exp(-(x / lamb_scale)**k_shape) return weibull_params, y_weibull, density_expected_weibull, y_cdf_weibull
def interpret(parameters: np.ndarray, classes: np.ndarray, interval: float): n_samples, n_components, n_classes = classes.shape assert parameters.ndim == 3 assert parameters.shape == (n_samples, Weibull.N_PARAMETERS + 1, n_components) shapes = np.expand_dims(relu(parameters[:, 0, :]), 2).repeat(n_classes, 2) scales = np.expand_dims(relu(parameters[:, 1, :]), 2).repeat(n_classes, 2) proportions = np.expand_dims(softmax(parameters[:, 2, :], axis=1), 1) components = weibull_min.pdf(classes, shapes, scale=scales) * interval m, v, s, k = weibull_min.stats(shapes[:, :, 0], scale=scales[:, :, 0], moments="mvsk") return proportions, components, (m, np.sqrt(v), s, k)
def _update_canvas(self): """ Update the figure when the user changes an input value. :return: """ # Get the parameters from the form loc = float(self.loc.text()) scale = float(self.scale.text()) shape_factor = float(self.shape_factor.text()) # Get the selected distribution from the form distribution_type = self.distribution_type.currentText() if distribution_type == 'Gaussian': x = linspace(norm.ppf(0.001, loc, scale), norm.ppf(0.999, loc, scale), 200) y = norm.pdf(x, loc, scale) elif distribution_type == 'Weibull': x = linspace(weibull_min.ppf(0.001, shape_factor), weibull_min.ppf(0.999, shape_factor), 200) y = weibull_min.pdf(x, shape_factor, loc, scale) elif distribution_type == 'Rayleigh': x = linspace(rayleigh.ppf(0.001, loc, scale), rayleigh.ppf(0.999, loc, scale), 200) y = rayleigh.pdf(x, loc, scale) elif distribution_type == 'Rice': x = linspace(rice.ppf(0.001, shape_factor), rice.ppf(0.999, shape_factor), 200) y = rice.pdf(x, shape_factor, loc, scale) elif distribution_type == 'Chi Squared': x = linspace(chi2.ppf(0.001, shape_factor), chi2.ppf(0.999, shape_factor), 200) y = chi2.pdf(x, shape_factor, loc, scale) # Clear the axes for the updated plot self.axes1.clear() # Display the results self.axes1.plot(x, y, '') # Set the plot title and labels self.axes1.set_title('Probability Density Function', size=14) self.axes1.set_xlabel('x', size=12) self.axes1.set_ylabel('Probability p(x)', size=12) # Set the tick label size self.axes1.tick_params(labelsize=12) # Turn on the grid self.axes1.grid(linestyle=':', linewidth=0.5) # Update the canvas self.my_canvas.draw()
def fit_weibull_and_ecdf(df_speed, x=None): from scipy.stats import weibull_min max_speed = df_speed.max() if x is None: x = linspace(0, max_speed, 20) # Fit Weibull k_shape, _, lamb_scale = weibull_params = weibull_min.fit(df_speed, loc=0) y_weibull = weibull_min.pdf(x, *weibull_params) y_cdf_weibull = 1 - exp(-(x / lamb_scale) ** k_shape) # Weibull cdf # Fit Ecdf y_ecdf = sm.distributions.ECDF(df_speed)(x) return x, y_weibull, y_cdf_weibull, weibull_params, y_ecdf
def moment_method( data ): mean_ = mean( data ) var_ = var( data, ddof = 1 ) print '#### moment method ####' print 'mean = ', mean_ print 'var = ', var_ params = fmin( moment_w, [2., 5.], args = ( mean_, var_ ) ) print 'Weibull shape = ', params[0] print 'Weibull scale = ', params[1] # plot the Weibull fit based on the moment method e = linspace( 0., 0.3 * ( max( data ) - min( data ) ) + max( data ), 100 ) plt.plot( e, weibull_min.pdf( e, params[0], scale = params[1], loc = 0. ), color = 'blue', linewidth = 2, label = 'moment method' )
def distrib(x, debut, largeur, hauteur, offset): """modèles de distribution (pour affichage de l'allure de la courbe [plot])""" global ladistribution if ladistribution == "alpha": return alpha.pdf(x, 0.8, debut - largeur / 10, largeur) * hauteur * largeur elif ladistribution == "gauss": return stat.norm.pdf(x, debut, largeur) * hauteur * largeur elif ladistribution == "triangle": return triang.pdf(x, 0, debut, largeur) * hauteur * largeur elif ladistribution == "weibull": return weibull_min.pdf(x, 1.1, debut + offset, largeur) * hauteur * largeur
def maximum_likelihood( data ): params = fmin( maxlike, [2., 5.], args = ( data ) ) moments = weibull_min( params[0], scale = params[1], loc = 0. ).stats( 'mv' ) print ' #### max likelihood #### ' print 'mean = ', moments[0] print 'var = ', moments[1] print 'Weibull shape = ', params[0] print 'Weibull scale = ', params[1] # plot the Weibull fit according to maximum likelihood e = linspace( 0., 0.3 * ( max( data ) - min( data ) ) + max( data ), 100 ) plt.plot( e, weibull_min.pdf( e, params[0], scale = params[1], loc = 0. ), color = 'red', linewidth = 2, label = 'max likelihood' ) plt.xlabel( 'strain' ) plt.ylabel( 'PDF' )
def weibull_ll_loop(params, data): loop_prob = params[0] complete_prob = params[1] ln_false = params[2] ln_true = params[3] false_pdf1 = np.zeros(data.shape) false_pdf2 = np.zeros(data.shape) true_pdf = np.zeros(data.shape) if ~np.isnan(ln_false[0]): false_pdf1 = loop_prob * complete_prob * (data == 1.0) false_pdf2 = loop_prob * (1 - complete_prob) * weibull_min.pdf( data, ln_false[0], ln_false[1], ln_false[2]) * (data > 0.0) * (data < 1.0) if ~np.isnan(ln_true[0]): true_pdf = (1 - loop_prob) * weibull_min.pdf( np.abs(data), ln_true[0], ln_true[1], ln_true[2]) * (data < 0.0) pdf = false_pdf1 + false_pdf2 + true_pdf + 1e-200 ll = -np.sum(np.log(pdf)) return ll
def moment_method(self, plot = True): mean_ = np.mean(self.data) var_ = np.var(self.data, ddof = 1) print '#### moment method ####' print 'mean = ', mean_ print 'var = ', var_ params = fmin(self.moment_w, [10., np.mean(self.data)], args = (mean_, var_)) print 'Weibull shape = ', params[0] print 'Weibull scale = ', params[1] print 'scale sV0 = ', params[1] * (pi * 13.e-3**2 * self.length)**(1./params[0]) if plot == True: # plot the Weibull fit based on the moment method e = np.linspace(0., 0.3 * (np.max(self.data) - np.min(self.data)) + np.max(self.data), 100) plt.plot(e, weibull_min.pdf(e, params[0], scale = params[1], loc = 0.), color = 'blue', linewidth = 2, label = 'moment method') return params[0], params[1]
def fit_weibull(df_speed, x, weibull_params=None, floc=True): from scipy.stats import weibull_min if not weibull_params: if floc: # sometimes need to set as loc=0 k_shape, _, lamb_scale = weibull_params = weibull_min.fit(df_speed, floc=0) else: k_shape, _, lamb_scale = weibull_params = weibull_min.fit(df_speed) else: k_shape, _, lamb_scale = weibull_params y_weibull = weibull_min.pdf(x, *weibull_params) density_expected_weibull = weibull_min.cdf( x[1:], *weibull_params) - weibull_min.cdf(x[:-1], *weibull_params) y_cdf_weibull = weibull_min.cdf(x, *weibull_params) return weibull_params, y_weibull, density_expected_weibull, y_cdf_weibull
def maximum_likelihood(self, plot = True): data = self.data params = fmin(self.maxlike, np.array([10., np.mean(data)])) moments = weibull_min(params[0], scale = params[1], loc = 0.).stats('mv') print ' #### max likelihood #### ' print 'mean = ', moments[0] print 'var = ', moments[1] print 'Weibull shape = ', params[0] print 'Weibull scale = ', params[1] #print 'scale sV0 = ', params[1] * (pi * 13.e-3**2 * self.length)**(1./params[0]) if plot == True: # plot the Weibull fit according to maximum likelihood e = np.linspace(0., 0.3 * (np.max(data) - np.min(data)) + np.max(data), 100) plt.plot(e, weibull_min.pdf(e, params[0], scale = params[1], loc = 0.), color = 'red', linewidth = 2, label = 'max likelihood') plt.xlabel('strain') plt.ylabel('PDF') return params[0], params[1]
def fit_distribution(data, fit_type, x_min, x_max, n_points=1000): # Initialization of the variables param, x, cdf, pdf = [-1, -1, -1, -1] if fit_type == 'exponweib': x = np.linspace(x_min, x_max, n_points) # Fit data to the theoretical distribution param = exponweib.fit(data, 1, 1, scale=02, loc=0) # param = exponweib.fit(data, fa=1, floc=0) # param = exponweib.fit(data) cdf = exponweib.cdf(x, param[0], param[1], param[2], param[3]) pdf = exponweib.pdf(x, param[0], param[1], param[2], param[3]) elif fit_type == 'lognorm': x = np.linspace(x_min, x_max, n_points) # Fit data to the theoretical distribution param = lognorm.fit(data, loc=0) cdf = lognorm.cdf(x, param[0], param[1], param[2]) pdf = lognorm.pdf(x, param[0], param[1], param[2]) elif fit_type == 'norm': x = np.linspace(x_min, x_max, n_points) # Fit data to the theoretical distribution param = norm.fit(data, loc=0) cdf = norm.cdf(x, param[0], param[1]) pdf = norm.pdf(x, param[0], param[1]) elif fit_type == 'weibull_min': x = np.linspace(x_min, x_max, n_points) # Fit data to the theoretical distribution param = weibull_min.fit(data, floc=0) cdf = weibull_min.cdf(x, param[0], param[1], param[2]) pdf = weibull_min.pdf(x, param[0], param[1], param[2]) return param, x, cdf, pdf
def calculate_pdf(distribution, linspace): if distribution[0] == 'EXP': lambda_ = distribution[1] scale_ = 1 / lambda_ return expon.pdf(linspace, scale=scale_) if distribution[0] == 'WEIBULL': scale = distribution[1] shape = distribution[2] return weibull_min.pdf(linspace, shape, loc=0, scale=scale) if distribution[0] == 'NORMAL': mu = distribution[1] sigma = distribution[2] return norm.pdf(linspace, loc=mu, scale=sigma) if distribution[0] == 'LOGNORM': mu = distribution[1] sigma = distribution[2] scale = math.exp(mu) return lognorm.pdf(linspace, sigma, loc=0, scale=scale)
def distrib(t, x, debut, scale, var, offset): """ renvoie la distribution selectionnee avec les parametres specifies t : type de distribution, 'a' pour alpha, 't' pour triangulaire, 'w' pour weibull x : array des abscisses debut : abscisse où debute la distribution scale : parametre de largeur var : parametre de hauteur offset : permet de decaler legerement le debut de la distribution pour alpha et triangulaire. Pour Weibull ce parametre au parametre de forme """ if t == 'a': return alpha.pdf( x, 0.8, debut - scale / 10 + offset, scale / 1.7) * var * scale if scale and var > 0 else [0] * len(x) elif t == 'w': return weibull_min.pdf(x, offset, debut, scale) * var elif t == 't': return triang.pdf(x, 0.1, debut + offset, scale) * scale * var
def moment_method(self, plot=True): mean_ = np.mean(self.data) var_ = np.var(self.data, ddof=1) print '#### moment method ####' print 'mean = ', mean_ print 'var = ', var_ params = fmin(self.moment_w, [10., np.mean(self.data)], args=(mean_, var_)) print 'Weibull shape = ', params[0] print 'Weibull scale = ', params[1] print 'scale sV0 = ', params[1] * (pi * 13.e-3**2 * self.length)**(1. / params[0]) if plot == True: # plot the Weibull fit based on the moment method e = np.linspace( 0., 0.3 * (np.max(self.data) - np.min(self.data)) + np.max(self.data), 100) plt.plot(e, weibull_min.pdf(e, params[0], scale=params[1], loc=0.), color='blue', linewidth=2, label='moment method') return params[0], params[1]
def maximum_likelihood(self, plot=True): data = self.data params = fmin(self.maxlike, np.array([10., np.mean(data)])) moments = weibull_min(params[0], scale=params[1], loc=0.).stats('mv') print ' #### max likelihood #### ' print 'mean = ', moments[0] print 'var = ', moments[1] print 'Weibull shape = ', params[0] print 'Weibull scale = ', params[1] #print 'scale sV0 = ', params[1] * (pi * 13.e-3**2 * self.length)**(1./params[0]) if plot == True: # plot the Weibull fit according to maximum likelihood e = np.linspace(0., 0.3 * (np.max(data) - np.min(data)) + np.max(data), 100) plt.plot(e, weibull_min.pdf(e, params[0], scale=params[1], loc=0.), color='red', linewidth=2, label='max likelihood') plt.xlabel('strain') plt.ylabel('PDF') return params[0], params[1]
def compute_probability(self, proxel, timestamp): #print('Computing for ' + str(self.state)) # We look at if the state is False, and then we use reliability since we want the transition from # state True to state False. if proxel.state is False: distribution = self.reliability_distribution else: distribution = self.maintainability_distribution #print('Before Probability: ' + str(self.probability)) if distribution[0] == 'EXP': lambda_ = distribution[1] scale_ = 1 / lambda_ proxel.probability *= (expon.pdf(timestamp, scale=scale_)/(1 - expon.cdf(timestamp, scale=scale_))) \ * self.delta_time if distribution[0] == 'WEIBULL': scale = distribution[1] shape = distribution[2] proxel.probability *= ( weibull_min.pdf(timestamp, shape, loc=0, scale=scale) / (1 - weibull_min.cdf(timestamp, shape, loc=0, scale=scale)) ) * self.delta_time if distribution[0] == 'NORMAL': mu = distribution[1] sigma = distribution[2] proxel.probability *= ( norm.pdf(timestamp, loc=mu, scale=sigma) / (1 - norm.cdf(timestamp, loc=mu, scale=sigma))) * self.delta_time if distribution[0] == 'LOGNORM': mu = distribution[1] sigma = distribution[2] scale = math.exp(mu) proxel.probability *= ( lognorm.pdf(timestamp, sigma, loc=0, scale=scale) / (1 - lognorm.cdf(timestamp, sigma, loc=0, scale=scale)) ) * self.delta_time
def fit_weibull(series, c=0, floc=0, scale=1, title=""): """ Fits a Weibull distribution, initialized with given parameters. Plots the fitted distribution against the ground-truth data. Params ------ series: Pandas series Pandas series containing the ground-truth values Returns ------- params : dictionary Contains the fitted parameters """ # Fit distribution (c, loc, scale) = weibull_min.fit(series, c, floc=floc, scale=scale) # Plot ax = plt.figure(figsize=(12,6)).gca() # the histogram of the data # Set as many bins as days bins = int(series.max() - series.min() + 1) n, bins, patches = plt.hist(series, bins, facecolor='green', alpha=1, density=True) # add a 'best fit' line y = weibull_min.pdf(bins, c, floc, scale) l = plt.plot(bins, y, 'r--', linewidth=2) plt.xlabel("Días") plt.title(title) # Only integer days ax.xaxis.set_major_locator(MaxNLocator(integer=True)) # Store parameters params = {"c":c, "scale":scale} return params
def distrib(x, debut, scale, var): """modèle de distribution""" #return alpha.pdf(x, 0.8, debut-scale/10, scale)#*var*scale #return stat.norm.pdf(x,debut,scale)#*var*scale #return alpha.pdf(x, 0.8, debut, scale)*scale*var return weibull_min.pdf(x, 1.1, debut, scale) * scale * var
def maxlike(self, v): data = self.data shape, scale = v r = np.sum(np.log(weibull_min.pdf(data, shape, scale=scale, loc=0.))) return -r
from scipy.stats import weibull_min import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1) # Calculate a few first moments: c = 1.79 mean, var, skew, kurt = weibull_min.stats(c, moments='mvsk') # Display the probability density function (``pdf``): x = np.linspace(weibull_min.ppf(0.01, c), weibull_min.ppf(0.99, c), 100) ax.plot(x, weibull_min.pdf(x, c), 'r-', lw=5, alpha=0.6, label='weibull_min pdf') # Alternatively, the distribution object can be called (as a function) # to fix the shape, location and scale parameters. This returns a "frozen" # RV object holding the given parameters fixed. # Freeze the distribution and display the frozen ``pdf``: rv = weibull_min(c) ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf') # Check accuracy of ``cdf`` and ``ppf``: vals = weibull_min.ppf([0.001, 0.5, 0.999], c)
import numpy as np from scipy.stats import weibull_min import pandas as pd dat = pd.read_csv('./amalia_directionally_averaged_speeds.txt', sep=r'\s+') power = pd.read_csv('./power.csv') ws, dirs = [], [] #speeds = [ 8., 9., 10., ] #speeds = [3., 4., 5., 6., 7.] speeds = [3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15.] #speeds = [3.] #speeds = [10., 11., 12., 13., 14., 15.] ouu_exp, ouu_det, det_det, det_exp = [], [], [], [] base_exp, base_det = [], [] spees, dees, pees = [], [], [] for ii in range(dat.shape[0]): theseprobs = weibull_min.pdf(speeds, 3, scale=dat.average_speed[ii]) theseprobs /= np.sum(theseprobs) if np.isnan(theseprobs.max()): hey for jj in range(len(speeds)): ws.append(speeds[jj]) dirs.append(dat.direction[ii]) fact = theseprobs[jj] * dat.probability[ii] subpow = power[power.ws == speeds[jj]] print(speeds[jj], int(dat.direction[ii]), fact * subpow.base_exp_power.values[0], subpow.base_det_power.values[0]) subsubpow = subpow[subpow.dir == int(dat.direction[ii])] ouu_exp.append(fact * subsubpow.ouu_exp_power.values[0]) det_exp.append(fact * subsubpow.det_exp_power.values[0]) det_det.append(fact * subsubpow.det_det_power.values[0]) ouu_det.append(fact * subsubpow.ouu_det_power.values[0]) base_exp.append(fact * subsubpow.base_exp_power.values[0]) base_det.append(fact * subsubpow.base_det_power.values[0])
def weibull_pdf_genuines(x, c, scale): return weibull_min.pdf(np.abs(x), c, scale=scale)
def maxlike(self, v): data = self.data shape, scale = v r = np.sum(np.log(weibull_min.pdf(data, shape, scale = scale, loc = 0.))) return -r
# Parameter estimates for generic data shape1, loc1, scale1 = lognorm.fit(data2, floc=0) mu1 = np.log(scale1) sigma1 = shape1 y1 = lognorm.pdf(data2, s=sigma1, scale=np.exp(mu1)) log_likelihood1 = np.sum(np.log(y1)) print("Lognorm loglikelihood = " + str(log_likelihood1)) aic1= -2 * log_likelihood1 + 2 * num_params print("Lognorm AIC = " + str(aic1)) # https://stackoverflow.com/questions/33070724/determine-weibull-parameters-from-data # Parameter estimates for generic data shape2, loc2, scale2 = weibull_min.fit(data2, floc=0) c = shape2 b = scale2 y2 = weibull_min.pdf(data2, c, scale=b) log_likelihood2 = np.sum(np.log(y2)) print("Weibull loglikelihood = " + str(log_likelihood2)) aic2= -2 * log_likelihood2 + 2 * num_params print("Weibull AIC = " + str(aic2)) # https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.invgauss.html # the argument floc=0 to ensure that it does not treat the location as a free parameter # Parameter estimates for generic data shape3, loc3, scale3 = invgauss.fit(data2, floc=0) mu3 = shape3 lambda3 = scale3 # fitting the data with the estimated parameters y3 = invgauss.pdf(data2, mu3, scale=lambda3) # calculate the log_likelihood log_likelihood3 = np.sum(np.log(y3))
kvar = [0.5, 1.0, 1.5, 5.0, 15.0] lvar = [1.0, 1.25, 1.5, 1.75, 2.0] pvar = [0.0, 0.5, 1.0, 1.5] cmap = plt.get_cmap('Set1') outpdf = PdfPages('weibull_example_plots.pdf') x = np.arange(0.0, 10.0, 0.01) lbda = 1.0 for i in range(5): k = kvar[i] c = cmap(i) w = wbl.pdf(x, k, scale=lbda) plt.plot(x, w, color=c, label='lambda = {0:0.2}, k = {1:0.1f}'.format(lbda, k), linewidth=1) plt.xlim(0.0, 3.5) plt.ylim(0.0, 2.5) plt.legend() plt.suptitle('Weibull PDF (Fixed Scale = 1.0)') outpdf.savefig() plt.close() k = 0.5 for i in range(5): lbda = lvar[i] c = cmap(i) w = wbl.pdf(x, k, scale=lbda) plt.plot(x, w, color=c, label='lambda = {0:0.3}, k = {1:0.1f}'.format(lbda, k), linewidth=1)
def weibull_pdf_impostors(x, c, scale): return weibull_min.pdf(np.abs(x), c, scale=scale)
def pdf(self, x): """ @brief Return the value of a probability density function for x. """ return weibull_min.pdf(x, self.shape, loc=self.loc, scale=self.scale)
def maxlike( v, *args ): data = args shape, scale = v r = sum( log( weibull_min.pdf( data, shape, scale = scale, loc = 0. ) ) ) return - r
def X(self, x): return weibull_min.pdf(x, self.X_shape, scale=self.X_scale, loc=self.X_loc)
def plot_weibull(name, metric, distribution, times, theoretical_distribution=None): scale = distribution[1] shape = distribution[2] theoretical_scale = None theoretical_shape = None # Checks whether there is a theoretical distribution to compare it to if theoretical_distribution is not None: theoretical_scale = theoretical_distribution[1] theoretical_shape = theoretical_distribution[2] fig, subplots = setup_fig_subplots(metric) fig.suptitle(name) linspace = np.linspace(weibull_min.ppf(0.001, shape, loc=0, scale=scale), weibull_min.ppf(0.999, shape, loc=0, scale=scale), 1000) # First plot PDF if theoretical_distribution is not None: subplots[0].plot(linspace, weibull_min.pdf(linspace, shape, loc=0, scale=scale), 'r-', lw=1, alpha=0.6, label='Reconstructed') subplots[0].plot(linspace, weibull_min.pdf(linspace, theoretical_shape, loc=0, scale=theoretical_scale), 'b-', lw=1, alpha=0.6, label='Theoretical') subplots[0].legend() else: subplots[0].plot(linspace, weibull_min.pdf(linspace, shape, loc=0, scale=scale), 'r-', lw=1, alpha=0.6) if times != EMPTY_LIST: if metric == 'Reliability': subplots[0].hist(times, bins=20, normed=True, histtype='stepfilled', alpha=0.2, label='Time to failures') if metric == 'Maintainability': subplots[0].hist(times, bins=20, normed=True, histtype='stepfilled', alpha=0.2, label='Time to repairs') subplots[0].legend() subplots[0].set_title('Weibull PDF') # Second plot CDF and/or Maintainability if theoretical_distribution is not None: subplots[1].plot(linspace, weibull_min.cdf(linspace, shape, loc=0, scale=scale), 'r-', lw=1, alpha=0.6, label='Reconstructed') subplots[1].plot(linspace, weibull_min.cdf(linspace, theoretical_shape, loc=0, scale=theoretical_scale), 'b-', lw=1, alpha=0.6, label='Theoretical') subplots[1].legend() else: subplots[1].plot(linspace, weibull_min.cdf(linspace, shape, loc=0, scale=scale), 'r-', lw=1, alpha=0.6) if metric == 'Reliability': subplots[1].set_title('Weibull CDF') if metric == 'Maintainability': subplots[1].set_title('Weibull CDF (Maintainability)') # Third plot Reliability if metric == 'Reliability': if theoretical_distribution is not None: subplots[2].plot( linspace, 1 - weibull_min.cdf(linspace, shape, loc=0, scale=scale), 'r-', lw=1, alpha=0.6, label='Reconstructed') subplots[2].plot(linspace, 1 - weibull_min.cdf(linspace, theoretical_shape, loc=0, scale=theoretical_scale), 'b-', lw=1, alpha=0.6, label='Theoretical') subplots[2].legend() else: subplots[2].plot( linspace, 1 - weibull_min.cdf(linspace, shape, loc=0, scale=scale), 'r-', lw=1, alpha=0.6) subplots[2].set_title(metric) #plt.show() plt.show(block=False)