def compute_LogLikelihood(data, fisk_params, ig_params, lognorm_params, gamma_params): n_fisk_params = 2 n_ig_params = 3 n_lognorm_params = 3 n_gamma_params = 3 trunc_data = data[data != 1.0] prob = np.sum(data == 1.0) / float(data.shape[0]) ll_fisk_trunc = np.sum(-np.log(fisk.pdf(trunc_data, *fisk_params))) bic = np.log(trunc_data.shape[0]) * n_fisk_params - 2 * ll_fisk_trunc #ll_fisk_nontrunc = np.sum(-np.log(prob * np.ones())) ll_ig_trunc = np.sum(-np.log(invgauss.pdf(trunc_data, *ig_params))) #ll_ig_nontrunc = np.sum(np.log(prob)) ll_ln_trunc = np.sum(-np.log(lognorm.pdf(trunc_data, *lognorm_params))) #ll_ln_nontrunc = np.sum(np.log(prob)) ll_gamma_trunc = np.sum(-np.log(gamma.pdf(trunc_data, *gamma_params))) #ll_exp_trunc = np.sum(-np.log(expon.pdf(trunc_data, *exp_params))) #ll_exp_nontrunc = np.sum(np.log(prob)) return ll_fisk_trunc, ll_ig_trunc, ll_ln_trunc, ll_gamma_trunc
def truncfiskprior_pdf(data, c, scale): epsilon = 1e-200 term2 = (fisk.pdf(data, c, loc=0.0, scale=scale) / (fisk.cdf(1.0, c, loc=0.0, scale=scale) - fisk.cdf(0.0, c, loc=0.0, scale=scale))) * (data < 1.0) return term2 + epsilon
def LogLogisticLikelihood(Samples): p1 = [1, 1.5, 2, 3] p2 = [15, 16, 17, 18, 19, 20, 21, 22] p3 = [30, 40, 50, 60, 70, 80, 90.100] maximumLikelihood = float('-inf') #ax = plt.axes(projection='3d') for a in p1: # plt.title("Maximum Likelihood") # plt.tight_layout() for b in p2: Likelihood = [] for c in p3: Logistic = fisk.pdf(Samples, a, loc=b, scale=c) likelihood = 0 for p in Logistic: likelihood += np.log(p) Likelihood.append(likelihood[0]) if (likelihood > maximumLikelihood): RetA = a RetB = b RetC = c maximumLikelihood = likelihood #ax.plot3D(p3, p2, Likelihood,label='a='+str(a)) #plt.plot(p3,Likelihood,lw=3,alpha=2.5,label="b="+str(b)) #plt.legend() return RetA, RetB, RetC
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 truncfiskprior_pdf(data, prior, c, scale): epsilon = 1e-200 term1 = prior * (data == 1.0) term2 = (1 - prior) * (fisk.pdf(data, c, loc=0.0, scale=scale) / (fisk.cdf(1.0, c, loc=0.0, scale=scale) - fisk.cdf( 0.0, c, loc=0.0, scale=scale))) * (data < 1.0) return term1 + term2 + epsilon
def compute_LogLikelihood_ExtData(data, fisk_params, ig_params): prob = fisk_params[0] fisk_params = fisk_params[1] num_one = np.sum(data == 1.0) trunc_data = data[data != 1.0] fisk_trunc_ll = np.sum(-np.log(fisk.pdf(trunc_data, *fisk_params))) fisk_nontrunc_ll = -np.log(prob) * num_one fisk_ll = fisk_trunc_ll + fisk_nontrunc_ll ig_trunc_ll = np.sum(-np.log(invgauss.pdf(trunc_data, *fisk_params))) ig_nontrunc_ll = -np.log(prob) * num_one ig_ll = ig_trunc_ll + ig_nontrunc_ll return fisk_ll, ig_ll
from scipy.stats import fisk import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1) # Calculate a few first moments: c = 3.09 mean, var, skew, kurt = fisk.stats(c, moments='mvsk') # Display the probability density function (``pdf``): x = np.linspace(fisk.ppf(0.01, c), fisk.ppf(0.99, c), 100) ax.plot(x, fisk.pdf(x, c), 'r-', lw=5, alpha=0.6, label='fisk 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 = fisk(c) ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf') # Check accuracy of ``cdf`` and ``ppf``: vals = fisk.ppf([0.001, 0.5, 0.999], c) np.allclose([0.001, 0.5, 0.999], fisk.cdf(vals, c)) # True
# plt.xscale('log',base=2) #a,b,c=LogLogisticLikelihood(Runtimes[22:]) #logistic=fisk.pdf(Runtimes[22:],a,loc=b,scale=c) # fig=plt.figure() # plt.plot(Runtimes[22:],logistic,lw=3,alpha=2.5) # plt.xscale('log',base=2) #initialize the distributions # dist=[] # for i in range(len(gauss1)): # dist.append((logistic[i]+gauss1[i])/2) # fig = plt.figure() gauss1 = gauss(Runtimes[0:21], [1, 0.865, 0.02]) logistic = fisk.pdf(Runtimes[21:], 1, loc=7.4859, scale=0.8139) plt.subplot(2, 1, 1) plt.plot(Runtimes[0:21], gauss1, lw=3, alpha=2.5) plt.xscale('log', base=2) plt.subplot(2, 1, 2) plt.plot(Runtimes[21:], logistic, lw=3, alpha=2.5) plt.xscale('log', base=2) plt.tight_layout() plt.show() #dist=np.concatenate((gaussian,logistic)) # plt.title("the two distributions") # plt.xscale('log',base=2) # plt.tight_layout() # plt.plot(Runtimes,dist,lw=3,alpha=2.5)