def test_closed_form_naive(): """ Test whether the results line up with a closed form solution for the special case, where alpha is set to zero. This function test the naive monte carlo implementation. """ # This unit test shows that the naive Monte Carlo implementation does not # line up with a closed form solution. However, we want to ensure a # successful run of the testing battery in our continous integration # workflow. On the slides, this test is presented and discussed without # the capture. with pytest.raises(AssertionError): for _ in range(10): # Generate random request. alpha, shape, _, int_options = generate_random_request() # Set options favourable. int_options['naive_mc']['num_draws'] = 1000 int_options['naive_mc']['implementation'] = 'fast' # Restrict to special case. alpha, shape = 0.0, 0.001 # Calculate closed form solution and simulate special case. closed_form = lognorm.mean(shape) simulated = eupy.get_baseline_lognormal(alpha, shape, 'naive_mc', int_options) # Test equality. np.testing.assert_almost_equal(closed_form, simulated, decimal=3)
def expect(mu, sigma, pos, cost): ev = lognorm.mean(sigma, scale=np.exp(mu)) val = ((1-pos)*-cost) + (pos * (ev - cost)) return(val)
def __init__(self, mode=0, elem=None, sample=None): if mode == 0: self.s = elem[0] self.mu = elem[1] self.sigma = elem[2] else: self.s, self.mu, self.sigma = lognorm.fit(sample) self.math_average = lognorm.mean(self.s, loc=self.mu, scale=self.sigma) self.dispersion = lognorm.var(self.s, loc=self.mu, scale=self.sigma)
def test_closed_form_quad(): """ Test whether the results line up with a closed form solution for the special case, where alpha is set to zero. This function test the quadrature implementation. """ for _ in range(10): # Generate random request. alpha, shape, _, int_options = generate_random_request() # Restrict to special case. alpha, shape = 0.0, 0.001 # Calculate closed form solution and simulate special case. closed_form = lognorm.mean(shape) simulated = eupy.get_baseline_lognormal(alpha, shape, 'quad', int_options) # Test equality. np.testing.assert_almost_equal(closed_form, simulated, decimal=3)
def plot_prob_density(mu, la, predsData, testData, xmin, xmax): from scipy.stats import lognorm fig, axes = plt.subplots(1, 1, figsize=(5, 4), sharey=True, dpi=120) font = "Times New Roman" f3 = lambda x, mu, la: (1 / x * la * (2 * math.pi)**0.5) * np.exp(-( (np.log(x) - mu)**2) / (2 * la**2)) x2 = np.linspace(0, xmax, 300) axes.plot(x2, f3(x2, mu, la)) ymin, ymax = axes.get_ylim() x_bounds = lognorm.interval(alpha=0.95, s=la, scale=np.exp(mu)) x_bounds_std = lognorm.interval(alpha=0.68, s=la, scale=np.exp(mu)) axes.axvline(x=testData.sum(), color='red', linestyle=':') ymaxes = f3(np.asarray(x_bounds), mu, la) / ymax + 0.01 axes.axvline(x=x_bounds[0], color='blue', alpha=0.3, linestyle=':') axes.axvline(x=x_bounds[1], color='blue', alpha=0.3, linestyle=':') xfill = np.linspace(x_bounds[0], x_bounds[1], 100) xfill_std = np.linspace(x_bounds_std[0], x_bounds_std[1], 100) axes.fill_between(xfill, f3(xfill, mu, la), alpha=0.1, color='blue') axes.fill_between(xfill_std, f3(xfill_std, mu, la), alpha=0.1, color='blue') #axes.fill_between(xfill,) axes.text(x=testData.sum() + 1, y=.03 * ymax, s='Actual: ' + str(int(testData.sum())), color='red') #axes.text(x=x_bounds[1]+1,y=ymax*.9,s='Upper 95%:',color='blue') #axes.text(x=x_bounds[1]+1,y=ymax*.82,s=str(round(x_bounds[1],1)),color='blue') #axes.text(x=x_bounds[0]-10,y=ymax*.9,s='Lower 95%:',color='blue') #axes.text(x=x_bounds[0]-10,y=ymax*.82,s=str(round(x_bounds[0],1)),color='blue') axes.set_xlabel('Number of days exceeding threshold', fontname=font, fontweight="heavy", fontsize=12) axes.set_ylabel('Probability density function (-)', fontname=font, fontweight="heavy", fontsize=12) axes.set_ylim(0, ymax) axes.set_xlim(0, xmax) labels = axes.get_xticklabels() + axes.get_yticklabels() [label.set_fontname(font) for label in labels] fig.show() print('**********************************') print('Expected number of days exceeding thermal comfort criteria: ' + str(round(lognorm.mean(s=la, scale=np.exp(mu)), 1)) + ' +/- ' + str(round(lognorm.std(s=la, scale=np.exp(mu)), 1))) print('Most likely number of days exceeding thermal comfort criteria: ' + str(round(np.exp(mu - la**2))) + ' +/- ' + str(round(lognorm.std(s=la, scale=np.exp(mu)), 1))) print( 'Predicted number of days exceeding thermal comfort criteria (deterministic): ' + str(int(np.sum(predsData)))) print('Actual number of days exceeding thermal comfort criteria: ' + str(int(testData.sum()))) print('**********************************') from sklearn.metrics import accuracy_score, precision_score, recall_score, roc_auc_score acc_score = accuracy_score(predsData, testData) prec_score = precision_score(predsData, testData) rec_score = recall_score(predsData, testData) roc_auc_score = roc_auc_score(predsData, testData) print("Test Accuracy score: ", acc_score) print("Test Precision score: ", prec_score) print("Test Recall score: ", rec_score) print("Test ROC AUC score: ", roc_auc_score)
def length(mask, estSize, returnAll=False, showSteps=False, pdf=None): """ Calculates the average between node ligament length and standard deviation of the passed mask. Only meaningful on the fully connected phase. Parameters ---------- mask : 2D array of bool Mask showing the area of one phase. estSize : float64 The estimated diameter. Used to set some parameters. returnAll : bool, optional If set to true, a list of all the measurements will be returned. showSteps : bool, optional If set to true, figures will be displayed that shows the results of intermediate steps when calculating the diameter. pdf : matplotlib.backends.backend_pdf.PdfPages, optional If provided, information will be output to the pdf file. Returns ------- logavg : float64 Average ligament length. (Assumes lognormal distribution.) logstd : float64 Standard deviation of ligament diameters. allMeasurements: 1D array of float64, optional A list of all measured ligament lengths. """ # Debug mode debug = False if debug: showSteps = True # Adjustable parameters. # These have been highly tested and changing these may throw off results. small_lig = 0.6 * estSize # Ligaments smaller than this are within a node. filter_out_mult = 0.4 # This * estSize is ignored in final calculation. # Return 0 if the mask is not fully connected. if not isConnected(mask): if returnAll: return 0, 0, 0 else: return 0, 0 rows, cols = mask.shape skel = skeleton(mask, estSize, removeEdge=True, times=3) if debug: display.showSkel(skel, mask) # get ligament and node labels nodes = np.copy(skel) ligaments = np.copy(skel) neighbors = convolve2d(skel, np.ones((3, 3)), mode='same') neighbors = neighbors * skel nodes[neighbors < 4] = 0 ligaments[neighbors > 3] = 0 ligaments = label(ligaments, background=0) nodes = binary_dilation(nodes, selem=disk(3)) nodes = label(nodes, background=0) # get a list of ligaments connected to each node node_to_lig = [ ] # node_to_lig[n] is an array of each ligament label that is connected to node n unodes = np.unique(nodes) for n in unodes: node_to_lig.append(np.unique(ligaments[nodes == n])) # get a list of nodes connected to each ligament lig_to_node = [ ] # lig_to_node[l] is an array of each node label that is connected to ligament l uligs = np.unique(ligaments) for l in uligs: lig_to_node.append(np.unique(nodes[ligaments == l])) # Get the length of each ligament between nodes. lengths = np.bincount(ligaments.flatten()) # Add ligaments that are within a single node to connected ligaments. small = int(round(small_lig)) too_small = [] for l in uligs: if lengths[l] <= small: too_small.append(l) #keep track of ligaments that are too small for connected in lig_to_node[l]: if connected > 0 and connected != l: # add half the small ligament to the connected ligaments. lengths[connected] += int(round(lengths[l] / 2, 0)) # Set to True to show which ligaments are considered to be within a # single node. if showSteps: ligaments_small = ligaments > 0 ligaments_small = ligaments_small.astype('uint8') ligaments_small[ligaments_small == 1] = 2 for i in too_small: ligaments_small[ligaments == i] = 1 display.showSkel(ligaments_small, mask, dialate=False, title=("Green = within node ligaments, " \ "White = between node ligaments")) # filter out background and extra small lengths lengths[0] = 0 allMeasurements = lengths[lengths > 0] lengths = lengths[lengths > estSize * filter_out_mult] if len(lengths) == 0: if returnAll: return 0, 0, 0 else: return 0, 0 # Get a lognormal fit. fit = lognorm.fit(lengths.flatten(), floc=0) pdf_fitted = lognorm.pdf(lengths.flatten(), fit[0], loc=fit[1], scale=fit[2]) # Get gaussian fit. gfit = norm.fit(lengths.flatten()) # Get average and standard deviation for lognormal fit. logaverage = lognorm.mean(fit[0], loc=fit[1], scale=fit[2]) logstd = lognorm.std(fit[0], loc=fit[1], scale=fit[2]) # Get average and standard deviation for normal fit. average = norm.mean(gfit[0], gfit[1]) std = norm.std(gfit[0], gfit[1]) numbMeasured = lengths.size if showSteps: display.showSkel(ligaments, mask, dialate=False) if showSteps: display.showHist(lengths, gauss=True, log=True, title='Ligament Lengths', xlabel='Ligament lengths [pixels]') if pdf is not None: inout.pdfSaveSkel(pdf, ligaments, mask, dialate=True) inout.pdfSaveHist(pdf, lengths, gauss=True, log=True, title='Ligament Lengths', xlabel='Ligament lengths [pixels]') if returnAll: # Change to return average, std... for mean and SD of normal PDF. return logaverage, logstd, allMeasurements else: return logaverage, logstd
def mean(self, dist): return lognorm.mean(*self._get_params(dist))
def mean(self, n, p): mu = lognorm.mean(self, n, p) return mu
return(sum(self.portfolio), sum(self.portfolio)/size) port = portfolio() # ============================================================================= # Simulating a portfolio # ============================================================================= p1_num = 44 p1_ports = [port.PoS_portfolio(0.07, size=p1_num, scale=scale_factor) for i in range(10000)] p1_ports_df = pd.DataFrame(p1_ports, columns=['Total value', 'Value per asset']) #minus ev p1_ports_df['Net return'] = p1_ports_df['Total value'] - (lognorm.mean(sigma, scale=scale_factor)*p1_num*0.07) #minus median #p1_ports_df['Net return'] = p1_ports_df['Total value'] - (lognorm.median(sigma, scale=scale_factor)*p1_num*0.07) p1_ports_df['Net return'].describe() p2_num = 21 p2_ports = [port.PoS_portfolio(0.15, size=p2_num, scale=scale_factor) for i in range(10000)] p2_ports_df = pd.DataFrame(p2_ports, columns=['Total value', 'Value per asset']) #minus ev p2_ports_df['Net return'] = p2_ports_df['Total value'] - (lognorm.mean(sigma, scale=scale_factor)*p2_num*0.15)
def mean(self): return lognorm.mean(self.shape, self.loc, self.scale)