def get_stats(name, **kwargs): ''' Takes a distribution name and paras, and returns key statistics. Note: for stat-getting the only choice is to use *scipy*. We need to be careful to ensure the parametrization matches with that used in our get_generator fn, which runs on numpy Generator methods. Thus, kwargs here follow *numpy* namings. ''' _sp = "mv" # moment specification for scipy.stats computations. if name == "lognormal": mean, var = lognorm.stats(s=kwargs["sigma"], scale=np.exp(kwargs["mean"]), moments=_sp) elif name == "normal": mean, var = norm.stats(loc=kwargs["loc"], scale=kwargs["scale"], moments=_sp) elif name == "pareto": mean, var = pareto.stats(b=kwargs["shape"], scale=kwargs["scale"], moments=_sp) else: raise ValueError("Please provide a proper distribution name.") return {"mean": mean, "var": var}
def __init__(self, shape_parameter): self.shape_parameter = shape_parameter if self.shape_parameter is not None: self.bounds = np.array([0.0, np.inf]) if self.shape_parameter > 0: mean, var, skew, kurt = lognorm.stats(s=self.shape_parameter, moments='mvsk') self.parent = lognorm(s=self.shape_parameter) self.mean = mean self.variance = var self.skewness = skew self.kurtosis = kurt self.x_range_for_pdf = np.linspace(0., 20., RECURRENCE_PDF_SAMPLES)
def main(sharp): fig, ax = plt.subplots(1, 1) mean, var, skew, kurt = lognorm.stats(s, moments='mvsk') x = logNrm_dist(s) ax.plot(x, lognorm.pdf(x, s), 'r-', lw=5, alpha=0.6, label='lognorm pdf') # freeze the dist and display the frozen pdf rv = lognorm(s) ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf') # gen radom numbers r = lognorm.rvs(s, size=1000) #ax.hist(r, histtype='stepfilled', alpha=0.2) ax.legend(loc='best', frameon=False) plt.show()
def get_population_average_estimate(param, shape): size = 100000 if shape == 'normal' or shape == 'contaminated normal': mu = 0 elif shape == 'lognormal': mu = lognorm.stats(param, moments='m') elif shape == 'contaminated chi-squared': # mu = chi2.stats(4, 0, param, moments='m') data = chi2.rvs(4, size=size) contam_inds = np.random.randint(size, size=int(param * size)) data[contam_inds] *= 10 mu = np.mean(data) elif shape == 'exponential': mu = expon.stats(0, param, moments='m') return mu
#Next, I am interested in understand how lognormal fits would approximate #the data instead of skewed normal fits, so I repeat the analysis with lognormal fits plt.figure(6) plt.title('Lognormal distributions of distance bins') plt.ylabel('Sizes (m)') plt.xlabel('Distance (m)') plt.ylim(0, 0.125) n = 1 lognorm_mean_distance = [] lognorm_var_distance = [] lognorm_skew_distance = [] lognorm_kurt_distance = [] for bins in size_nest_distances: if len(bins) > 10: s, loc, scale = lognorm.fit(bins) mean, var, skew, kurt = lognorm.stats(s, loc, scale, moments='mvsk') lognorm_mean_distance.append(mean) lognorm_var_distance.append(var) lognorm_skew_distance.append(skew) lognorm_kurt_distance.append(kurt) pdf = lognorm.pdf(x, s, loc, scale) plt.plot(x, pdf, label='{}'.format( n)) #In this plot I am plotting all distributions together n += 1 plt.legend() plt.figure(7) plt.title('Lognormal distributions of distance bins') plt.ylabel('Sizes (m)') plt.xlabel('Distance bin (m)') n = 1
# @Time : 2020/5/14 22:07 # @Author : gzzang # @File : lognorm # @Project : notebook from scipy.stats import lognorm import matplotlib.pyplot as plt import numpy as np import pdb fig, ax = plt.subplots(1, 1) s = 0.954 s = 0.5 mean, var, skew, kurt = lognorm.stats(s, moments='mvsk') x = np.linspace(lognorm.ppf(0.01, s), lognorm.ppf(0.99, s), 100) ax.plot(x, lognorm.pdf(x, s), 'r-', lw=5, alpha=0.6, label='lognorm pdf') rv = lognorm(s) ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf') vals = lognorm.ppf([0.001, 0.5, 0.999], s) print(np.allclose([0.001, 0.5, 0.999], lognorm.cdf(vals, s))) print(np.allclose(a=[1.1000000000001, 1.2], b=[1.1, 1.2])) # pdb.set_trace() r = lognorm.rvs(s, size=1000) sigma = s mu = 0
def hesap(ax, bx): z, k, m, n, p, t = 0, 0, 0, 0, 0, 0 studentGuvenAlt, aadmGuvenAlt, maadGuvenAlt, madmGuvenAlt, johnsonGuvenAlt, chenGuvenAlt = list( ), list(), list(), list(), list(), list() studentGuvenUst, aadmGuvenUst, maadGuvenUst, madmGuvenUst, johnsonGuvenUst, chenGuvenUst = list( ), list(), list(), list(), list(), list() workbook = xlwt.Workbook() sayfa = workbook.add_sheet("Sayfa1") sayfa.write(0, 1, "Student-t") sayfa.write(0, 3, "AADM-t") sayfa.write(0, 5, "MAAD-t") sayfa.write(0, 7, "MADM-t") sayfa.write(0, 9, "Johnson-t") sayfa.write(0, 11, "Chen-t") for item in range(0, 13): if item == 0: sayfa.write(1, 0, "n") elif item % 2 == 0: sayfa.write(1, item, "AW") else: sayfa.write(1, item, "CP") for i in range(5, 10): for j in range(1, 2501): x = lognorm.rvs(bx, size=i) mean, var, skew, kurt = lognorm.stats(bx, moments='mvsk') meanx = round(statistics.mean(x), 4) medianx = round(statistics.median(x), 4) stdevx = round(statistics.stdev(x), 4) aadmx = round((math.sqrt(math.pi / 2) / i) * sum(abs(x - medianx)), 4) maadx = round(statistics.median(abs(x - meanx)), 4) madmx = round(statistics.median(abs(x - medianx)), 4) toplam = 0 for k in range(0, i): toplam = toplam + ((x[k] - meanx)**3) m3 = (i / ((i - 1) * (i - 2))) * toplam studentalt = round(meanx - cell[i - 5] * stdevx / math.sqrt(i), 4) studentust = round(meanx + cell[i - 5] * stdevx / math.sqrt(i), 4) aadmalt = round(meanx - cell[i - 5] * aadmx / math.sqrt(i), 4) aadmust = round(meanx + cell[i - 5] * aadmx / math.sqrt(i), 4) maadalt = round(meanx - cell[i - 5] * maadx / math.sqrt(i), 4) maadust = round(meanx + cell[i - 5] * maadx / math.sqrt(i), 4) madmalt = round(meanx - cell[i - 5] * madmx / math.sqrt(i), 4) madmust = round(meanx + cell[i - 5] * madmx / math.sqrt(i), 4) johnsonalt = round((meanx + (m3 / (6 * i * (stdevx**2)))) - cell[i - 5] * math.sqrt(i) * stdevx, 4) johnsonust = round((meanx + (m3 / (6 * i * (stdevx**2)))) + cell[i - 5] * math.sqrt(i) * stdevx, 4) chenalt = round(meanx - (cell[i - 5] + ( ((m3 / (stdevx**3)) * (1 + 2 * (cell[i - 5]**2))) / (6 * math.sqrt(i))) + ( (((m3 / (stdevx**3))**2) * (cell[i - 5] + 2 * (cell[i - 5])**2) / 9 * i)) + math.sqrt(i) * stdevx)) chenust = round(meanx + (cell[i - 5] + ( ((m3 / (stdevx**3)) * (1 + 2 * (cell[i - 5]**2))) / (6 * math.sqrt(i))) + ( (((m3 / (stdevx**3))**2) * (cell[i - 5] + 2 * (cell[i - 5])**2) / 9 * i)) + math.sqrt(i) * stdevx)) studentGuvenAlt.append(studentalt) studentGuvenUst.append(studentust) aadmGuvenAlt.append(aadmalt) aadmGuvenUst.append(aadmust) maadGuvenAlt.append(maadalt) maadGuvenUst.append(maadust) madmGuvenAlt.append(madmalt) madmGuvenUst.append(madmust) johnsonGuvenAlt.append(johnsonalt) johnsonGuvenUst.append(johnsonust) chenGuvenAlt.append(chenalt) chenGuvenUst.append(chenust) if studentalt <= mean <= studentust: z = z + 1 if aadmalt <= mean <= aadmust: k = k + 1 if madmalt <= mean <= madmust: m = m + 1 if maadalt <= mean <= maadust: n = n + 1 if johnsonalt <= mean <= johnsonust: p = p + 1 if chenalt <= mean <= chenust: t = t + 1 sayfa.write(i - 3, 0, f"{i}") sayfa.write(i - 3, 1, f"{round(z / 2500, 4)}") sayfa.write( i - 3, 2, f"{round(statistics.mean(studentGuvenUst) - statistics.mean(studentGuvenAlt), 4)}" ) sayfa.write(i - 3, 3, f"{round(k / 2500, 4)}") sayfa.write( i - 3, 4, f"{round(statistics.mean(aadmGuvenUst) - statistics.mean(aadmGuvenAlt), 4)}" ) sayfa.write(i - 3, 5, f"{round(n / 2500, 4)}") sayfa.write( i - 3, 6, f"{round(statistics.mean(maadGuvenUst) - statistics.mean(maadGuvenAlt), 4)}" ) sayfa.write(i - 3, 7, f"{round(m / 2500, 4)}") sayfa.write( i - 3, 8, f"{round(statistics.mean(madmGuvenUst) - statistics.mean(madmGuvenAlt), 4)}" ) sayfa.write(i - 3, 9, f"{round(p / 2500, 4)}") sayfa.write( i - 3, 10, f"{round(statistics.mean(johnsonGuvenUst) - statistics.mean(johnsonGuvenAlt), 4)}" ) sayfa.write(i - 3, 11, f"{round(t / 2500, 4)}") sayfa.write( i - 3, 12, f"{round(statistics.mean(chenGuvenUst) - statistics.mean(chenGuvenAlt), 4)}" ) workbook.save(f'L({ax} {bx}).xls') # excelisim z, k, m, n, p, t = 0, 0, 0, 0, 0, 0 studentGuvenAlt, aadmGuvenAlt, maadGuvenAlt, madmGuvenAlt, johnsonGuvenAlt, chenGuvenAlt = list( ), list(), list(), list(), list(), list() studentGuvenUst, aadmGuvenUst, maadGuvenUst, madmGuvenUst, johnsonGuvenUst, chenGuvenUst = list( ), list(), list(), list(), list(), list()