def main(a): #arg = raw_input("Zipf & Pareto belongs to power law. Please select: 1: Pareto\'s Law, 2: Zipf\'s law") ) fig, ax = plt.subplots(1, 1) mean, var, skew, kurt = powerlaw.stats(a, moments='mvsk') x = power_law_dist(a) ax.plot(x, powerlaw.pdf(x, a), 'r-', lw=5, alpha=0.6, label='powerlaw pdf') rv = powerlaw(a) ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf') r = powerlaw.rvs(a, size=1000) ax.legend(loc='best', frameon=False) #plt.plt(x,y) plt.show()
def matrix_inverse_unfolding(signal, detector_response_matrix): """ Unfold the signal using simple matrix unfolding :param signal: The signal from the detector, in either total energy per chamber or energy per particle per chamber :param detector_response_matrix: The detector Response Matrix, normalized by column :return: The unfolded signal, sigma in the unfolding, the x error estimation, y error, and the unf - pdf / sigma_x """ if signal.ndim == 2: sum_signal_per_chamber = np.sum(signal, axis=1) y_vector = np.histogram(sum_signal_per_chamber, bins=detector_response_matrix.shape[0]) else: y_vector = [signal, 0] x_pdf_space = np.linspace(powerlaw.ppf(0.01, 0.70), powerlaw.ppf(1.0, 0.70), detector_response_matrix.shape[0]) x_vector = powerlaw.pdf(x_pdf_space, 0.70) # Get the inverse of the detector response matrix inv_detector_response_matrix = np.linalg.inv(detector_response_matrix) x_vector_unf = np.dot(y_vector[0], inv_detector_response_matrix) # Error propagation V_y = np.diag(y_vector[0]) V_x_est = np.dot(inv_detector_response_matrix, np.dot(V_y, inv_detector_response_matrix.T)) sigma_x_unf = np.sqrt(np.diag(V_x_est)) # print('x_unf \t\t= %s' % str(np.round(x_vector_unf, 2))) # print('simga_x_unf \t\t= %s' % str(np.round(sigma_x_unf, 2))) # print('(unf - pdf) / sigma_x \t= %s ' % str(np.round((x_vector_unf - x_vector) / sigma_x_unf, 2))) unf_pdf_sigma = (x_vector_unf - x_vector) / sigma_x_unf return x_vector_unf, sigma_x_unf, V_x_est, V_y, unf_pdf_sigma
from scipy.stats import powerlaw import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1) # Calculate a few first moments: a = 1.66 mean, var, skew, kurt = powerlaw.stats(a, moments='mvsk') # Display the probability density function (``pdf``): x = np.linspace(powerlaw.ppf(0.01, a), powerlaw.ppf(0.99, a), 100) ax.plot(x, powerlaw.pdf(x, a), 'r-', lw=5, alpha=0.6, label='powerlaw 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 = powerlaw(a) ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf') # Check accuracy of ``cdf`` and ``ppf``: vals = powerlaw.ppf([0.001, 0.5, 0.999], a) np.allclose([0.001, 0.5, 0.999], powerlaw.cdf(vals, a)) # True # Generate random numbers:
def integrand(x): pp = powerlaw.pdf(x, a=k, loc=a, scale=L) v = pp * (powerlaw.logpdf(x, a=k, loc=a, scale=L) - mix_logpdf(x, k, a, L)) return v
plt.scatter(ba_c2.keys(),ba_c2.values(),c='b',marker='x') plt.xlim((1e-4,1e-1)) plt.ylim((.9,1e4)) plt.xlabel('Connections (normalized)') plt.ylabel('Frequency') plt.show() from scipy.stats import powerlaw import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1) a = 1.66 mean, var, skew, kurt = powerlaw.stats(a, moments='mvsk') x = np.linspace(powerlaw.ppf(0.01, a), powerlaw.ppf(0.99, a), 100) ax.plot(x, powerlaw.pdf(x, a),'r-', lw=5, alpha=0.6, label='powerlaw pdf') # Dec 15, 2015 Ch4 import math math.pi import random random.choice(['a','b','c','d']) S = 'Spam' S[:-1] B = bytearray(b'spam') B.extend(b'eggs') B.decode()
plt.ylabel("Total length") plt.title("Trajectory of Search for Min Total Length, Average Min = %.1f" % np.mean(mins)) #------------------------ Power Law Networks ---------------------------------- o_dist2 = data[1][1] f_dist2 = data[1][2] #original and final length original_len = sum(o_dist2) / realizations final_len = sum(f_dist2) / realizations #fitting r = powerlaw.fit(o_dist2) rx = np.linspace(0, max(o_dist2), 100) rp = powerlaw.pdf(rx, *r) l1_fit = 1 / r[1] fig3, (ax1, ax2) = plt.subplots(2) n2, bins2, patches2 = ax1.hist(o_dist2, bins=100, density=True, alpha=0) ax1.set(xlabel="log(Edge Length)", ylabel="log(counts)") ax1.set_title('Original Length Distribution length = %.2f' % (sum(o_dist2) / realizations)) ax1.set_yscale('log') ax1.set_xscale('log') ax1.scatter(bins2[:-1] + 0.5 * (bins2[1:] - bins2[:-1]), n2, marker='x', c='red', s=40, alpha=1)
# plot distn of r for diff D, also do same for uniform ball x = np.linspace(0, 2.0, 1000) plt.figure() for dim, color in zip(D, colors): pdf = chi.pdf(np.sqrt(dim) * x, df=dim) plt.plot(x, pdf / np.max(pdf), color, label='$D=%d$' % dim) plt.xlabel('$r$ (rescaled)') plt.ylabel('$\chi$ pdf (rescaled)') plt.legend() plt.title('distributions on radius in gaussians') plt.savefig('fig0' + ext, dpi=300) plt.figure() for dim, color in zip(D, colors): pdf = powerlaw.pdf(np.sqrt(dim) * x, a=dim, loc=0, scale=np.sqrt(dim)) plt.plot(x, pdf / np.max(pdf), color, label='$D=%d$' % dim) plt.xlabel('$r$ (rescaled)') plt.ylabel('power law pdf (rescaled)') plt.legend() plt.title('distributions on radius in uniform ball') plt.savefig('fig1' + ext, dpi=300) # warp 2D gauss to look like diff D n = 500 plt.figure() for dim, color in zip(D, colors): R = np.sqrt(np.random.chisquare(df=dim, size=n)) R = R / np.median(R)