Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168728
Image: ubuntu2004
# Requirements from numpy import linspace
# Constant in interval function generator CI = lambda min, max, value : lambda x : (lambda : 0, lambda : value)[x >= min and x <= max]() # Constant in interval with definite integration for all reals equal to one function CI1 = lambda min, max: CI(min, max, 1 / (max - min)) # Kernel density estimation KDE = lambda data, kernel, bandwidth : lambda x : sum(map(lambda value : kernel((x - value) / bandwidth), data)) / (len(data) * bandwidth) # Gaussian function generator G = lambda a, b, c : (lambda x : a * exp(-(x-b)**2 / (2 * c**2))) # Normal distribution function generator N = lambda avg, stdev : G(1/sqrt(2 * pi * stdev**2), avg, stdev) # Standard normal distribution function SN = N(0, 1)
# Inherited values num = 5 Plot_hmin = 0 Plot_hmax = 10 Plot_vmin = 0 Plot_palpha = 0.25
# (Length 1) # PDF L1_min = 1 L1_max = 2 L1_num = num * (L1_max - L1_min) L1_PDF = CI1(L1_min, L1_max) L1_PDFx = linspace(L1_min, L1_max, L1_num) #L1_PDFp = map(lambda x : (x, L1_PDF(x)), L1_PDFx) # Stats L1_PDFx_avg = mean(L1_PDFx) L1_PDFx_stdev = std(L1_PDFx) L1_low = L1_min L1_high = L1_max # Print stats print("Average: " + str(L1_PDFx_avg)) print("Standard deviation: " + str(L1_PDFx_stdev)) print("Low: " + str(L1_low)) print("High: " + str(L1_high)) # Displaying the plot L1_Plot_hmin = Plot_hmin L1_Plot_hmax = Plot_hmax L1_Plot_vmin = Plot_vmin L1_Plot = plot(L1_PDF, (L1_Plot_hmin, L1_Plot_hmax), color = "blue") L1_Plot += line([(L1_low, 0), (L1_low, L1_PDF(L1_low))], color = "red") L1_Plot += line([(L1_high, 0), (L1_high, L1_PDF(L1_high))], color = "red") L1_Plot += point(map(lambda x : (x, 0), L1_PDFx), color = "blue", alpha = Plot_palpha) L1_Plot.show(xmin = L1_Plot_hmin, xmax = L1_Plot_hmax, ymin = L1_Plot_vmin)
Average: 1.5 Standard deviation: 0.395284707521 Low: 1 High: 2
# (Length 2) # PDF L2_min = 2 L2_max = 4 L2_num = num * (L2_max - L2_min) L2_PDF = CI1(L2_min, L2_max) L2_PDFx = linspace(L2_min, L2_max, L2_num) #L2_PDFp = map(lambda x : (x, L2_PDF(x)), L2_PDFx) # Stats L2_PDFx_avg = mean(L2_PDFx) L2_PDFx_stdev = std(L2_PDFx) L2_low = L2_min L2_high = L2_max # Print stats print("Average: " + str(L2_PDFx_avg)) print("Standard deviation: " + str(L2_PDFx_stdev)) print("Low: " + str(L2_low)) print("High: " + str(L2_high)) # Displaying the plot L2_Plot_hmin = Plot_hmin L2_Plot_hmax = Plot_hmax L2_Plot_vmin = Plot_vmin L2_plot = plot(L2_PDF, (L2_Plot_hmin, L2_Plot_hmax)) L2_plot += line([(L2_low, 0), (L2_low, L2_PDF(L2_low))], color = "red") L2_plot += line([(L2_high, 0), (L2_high, L2_PDF(L2_high))], color = "red") L2_plot += point(map(lambda x : (x, 0), L2_PDFx), alpha = Plot_palpha) L2_plot.show(xmin = L2_Plot_hmin, xmax = L2_Plot_hmax, ymin = L2_Plot_vmin)
Average: 3.0 Standard deviation: 0.672811189799 Low: 2 High: 4
# (Sum Length) = (Length 1) * (Length 2) L3_f = lambda L1, L2 : L1 * L2 # PDF L3_num = L1_num * L2_num L3_PDFx = [] for L1_val in L1_PDFx: for L2_val in L2_PDFx: L3_PDFx.append(L3_f(L1_val, L2_val)) L3_PDF = KDE(L3_PDFx, SN, 0.5) #L3_PDFp = map(lambda x : (x, 0.5), L3_PDFx) # Stats L3_PDFx_avg = mean(L3_PDFx) L3_PDFx_stdev = std(L3_PDFx) L3_low = min([L3_f(L1_min, L2_min),L3_f(L1_max, L2_max)]) L3_high = max([L3_f(L1_min, L2_min), L3_f(L1_max, L2_max)]) # Print stats print("Average: " + str(L3_PDFx_avg)) print("Standard deviation: " + str(L3_PDFx_stdev)) print("Low: " + str(L3_low)) print("High: " + str(L3_high)) # Displaying the plot L3_Plot_hmin = Plot_hmin L3_Plot_hmax = Plot_hmax L3_Plot_vmin = Plot_vmin L3_plot = plot(L3_PDF, (L3_Plot_hmin, L3_Plot_hmax)) #L3_plot = plot(L3_PDF, (L3_Plot_hmin, L3_Plot_hmax), plot_points = 20) L3_plot += line([(L3_low, 0), (L3_low, L3_PDF(L3_low))], color = "red") L3_plot += line([(L3_high, 0), (L3_high, L3_PDF(L3_high))], color = "red") L3_plot += point(map(lambda x : (x, 0), L3_PDFx), alpha = Plot_palpha) L3_plot.show(xmin = L3_Plot_hmin, xmax = L3_Plot_hmax, ymin = L3_Plot_vmin)
Average: 4.5 Standard deviation: 1.4612660825 Low: 2 High: 8