def fit_fisk(data, mode=1): prob = np.sum(data == 1.0) / float(data.shape[0]) num_one = np.sum(data == 1.0) trunc_data = data[data != 1.0] fisk_params = [np.nan, 0.0, np.nan] if len(trunc_data) > 0: if mode == 0: fisk_params = fisk.fit(trunc_data, floc=0.0) else: fisk_params = fisk.fit(trunc_data) return [prob, fisk_params]
def fit_fisk(signal, tag): mu, loc, std = fisk.fit(signal["mean"].values) confidence_interval = fisk.interval(CONFIDENCE, mu, loc=loc, scale=std) if PLOTTING: # Plot the histogram. plt.subplots() plt.hist(signal["mean"].values, bins=25, density=True, alpha=0.6, color="g") # Plot the PDF. xmin, xmax = plt.xlim() x = np.linspace(xmin, xmax, 100) p = fisk.pdf(x, mu, loc, std) plt.plot(x, p, "k", linewidth=1) title = "Fit results fisk: a=%2f loc = %.2f, std = %.2f" % (mu, loc, std) plt.title(title) plt.axvline(x=confidence_interval[0]) plt.axvline(x=confidence_interval[1]) # Plot the confidence interval plt.savefig(f"analysis/images/{slugify(tag)}_fit_fisk_histogram.png", format="png") return { "distribution": "fisk", "params": [{ "a": mu, "loc": loc, "std": std }], "confidence": [confidence_interval], }
def run_Parametric(story_id, data): print "[" + str(story_id) + "]Fitting Fisk" fisk_params = fisk.fit(data, floc=0) fisk_nll = fisk.nnlf(fisk_params, data) fisk_rvs = fisk.rvs(*fisk_params, size=data.shape[0]) ks_fisk = ks_2samp(data, fisk_rvs) bic_fisk = compute_BIC(data, len(fisk_params), fisk_nll) print "[" + str(story_id) + "]Fitting IG" ig_params = invgauss.fit(data, floc=0) ig_nll = invgauss.nnlf(ig_params, data) ig_rvs = invgauss.rvs(*ig_params, size=data.shape[0]) ks_ig = ks_2samp(data, ig_rvs) bic_ig = compute_BIC(data, len(ig_params), ig_nll) print "[" + str(story_id) + "]Fitting LN" ln_params = lognorm.fit(data, floc=0) ln_nll = lognorm.nnlf(ln_params, data) ln_rvs = lognorm.rvs(*ln_params, size=data.shape[0]) ks_ln = ks_2samp(data, ln_rvs) bic_ln = compute_BIC(data, len(ln_params), ln_nll) print "[" + str(story_id) + "]Fitting Weibull" weib_params = weibull_min.fit(data, floc=0) weib_nll = weibull_min.nnlf(weib_params, data) weib_rvs = weibull_min.rvs(*weib_params, size=data.shape[0]) ks_weib = ks_2samp(data, weib_rvs) bic_weib = compute_BIC(data, len(weib_params), weib_nll) print "[" + str(story_id) + "]Fitting Gamma" gamma_params = gamma.fit(data, floc=0) gamma_nll = gamma.nnlf(gamma_params, data) gamma_rvs = gamma.rvs(*gamma_params, size=data.shape[0]) ks_gamma = ks_2samp(data, gamma_rvs) bic_gamma = compute_BIC(data, len(gamma_params), gamma_nll) return [ fisk_nll, ig_nll, ln_nll, weib_nll, gamma_nll, ks_fisk, ks_ig, ks_ln, ks_weib, ks_gamma, bic_fisk, bic_ig, bic_ln, bic_weib, bic_gamma, fisk_params, ig_params, ln_params, weib_params, gamma_params ]
def fit_fisk_loop(data): loop_false = (data > 0) loop_true = (data < 0) data_false = data[loop_false] data_true = data[loop_true] loop_prob = np.sum(loop_false) / float(data.shape[0]) fisk_trunc_fit = np.array([np.nan, 0.0, np.nan]) nll_false = 0 nll_true = 0 if np.sum(loop_false) > 0: trunc_data = data_false[data_false < 1.0] prior = (len(data_false) - len(trunc_data)) / float(len(data_false)) if trunc_data.shape[0] > 0: distribution = TruncatedFisk_Prior rv_fisk_false = distribution(trunc_data) res_fisk_false = rv_fisk_false.fit() fisk_trunc_fit = np.array( [prior, res_fisk_false.params[0], res_fisk_false.params[1]]) nll_false = np.sum(rv_fisk_false.nloglikeobs( res_fisk_false.params)) if np.sum(loop_true) > 0: fisk_trueloop_fit = fisk.fit(np.abs(data_true), floc=0.0) nll_true = -np.sum( np.log( lognorm.pdf(np.abs(data_true), fisk_trueloop_fit[0], fisk_trueloop_fit[1], fisk_trueloop_fit[2]) + 1e-200)) else: fisk_trueloop_fit = np.array([np.nan, 0.0, np.nan]) nll = nll_false + nll_true return [loop_prob, fisk_trunc_fit, fisk_trueloop_fit, nll]