예제 #1
0
파일: test_sec3.4.py 프로젝트: klocey/plfit
import numpy as np
import plfit
import pylab as plt

xmins = np.logspace(-1, 1)
nel = 2000

fig1 = plt.figure(1)
fig1.clf()
ax1 = fig1.add_subplot(2, 1, 1)
ax2 = fig1.add_subplot(2, 1, 2)

for alpha in (1.5, 2.5, 3.5):
    results = []
    for xmin in xmins:
        data = plfit.plexp_inv(np.random.rand(nel), xmin, alpha)
        result = plfit.plfit(data, quiet=True, silent=True)
        results.append(result)

    fitted_xmins = [r._xmin for r in results]
    fitted_alphas = [r._alpha for r in results]
    fitted_alpha_errors = [r._alphaerr for r in results]

    ax1.loglog(xmins, fitted_xmins, "s", label="$\\alpha={0}$".format(alpha), alpha=0.5)
    ax1.loglog(xmins, xmins, "k--", alpha=0.5, zorder=-1)

    ax2.errorbar(
        xmins, fitted_alphas, yerr=fitted_alpha_errors, marker="s", label="$\\alpha={0}$".format(alpha), alpha=0.5
    )
    ax2.set_xscale("log")
예제 #2
0
#import cplfit
import plfit
import time
from numpy.random import rand, seed
from numpy import unique, sort, array, asarray, log, sum, min, max, argmin, argmax, arange
import sys
import powerlaw
from agpy import readcol

try:
    ne = int(sys.argv[1])
    seed(1)
    X = plfit.plexp_inv(rand(ne), 1, 2.5)
    X[:100] = X[100:200]
except ValueError:
    X = readcol(sys.argv[1])

if len(sys.argv) > 2:
    discrete = bool(sys.argv[2])
else:
    discrete = None

print("Cython")
t1 = time.time()
p3 = plfit.plfit(X, discrete=discrete, usefortran=False, usecy=True)
print(time.time() - t1)
print("Fortran")
t1 = time.time()
p1 = plfit.plfit(X, discrete=discrete, usefortran=True)
print(time.time() - t1)
print("Numpy")
예제 #3
0
#import cplfit
import plfit
import time
from numpy.random import rand,seed
from numpy import unique,sort,array,asarray,log,sum,min,max,argmin,argmax,arange
import sys
import powerlaw
from agpy import readcol

try:
    ne = int(sys.argv[1])
    seed(1)
    X=plfit.plexp_inv(rand(ne),1,2.5)
    X[:100] = X[100:200]
except ValueError:
    X = readcol(sys.argv[1])

if len(sys.argv)>2:
    discrete = bool(sys.argv[2])
else:
    discrete=None

print "Cython"
t1=time.time(); p3=plfit.plfit(X,discrete=discrete,usefortran=False,usecy=True); print time.time()-t1
print "Fortran"
t1=time.time(); p1=plfit.plfit(X,discrete=discrete,usefortran=True); print time.time()-t1
print "Numpy"
t1=time.time(); p3=plfit.plfit(X,discrete=discrete,usefortran=False); print time.time()-t1

print "Jeff Alcott's Powerlaw"
t5=time.time(); p5=powerlaw.Fit(X,discrete=discrete); print time.time()-t5
예제 #4
0
    if len(sys.argv) > 2:
        nel = int(sys.argv[2])
        if len(sys.argv) > 3:
            xmin = float(sys.argv[3])
        else:
            xmin = 0.5
    else:
        nel = 1000
else:
    nel = 1000
    xmin = 0.5
    ntests = 1000

a = np.zeros(ntests)
for i in range(ntests):
    X = plfit.plexp_inv(np.random.rand(nel), xmin, 2.5)
    p = plfit.plfit(X, xmin=xmin, quiet=True, silent=True)
    a[i] = p._alpha

h, b = plt.hist(a, bins=30)[:2]
bx = (b[1:] + b[:-1]) / 2.0

from agpy import gaussfitter

p, m, pe, chi2 = gaussfitter.onedgaussfit(bx,
                                          h,
                                          params=[0, ntests / 10.0, 2.5, 0.05],
                                          fixed=[1, 0, 0, 0])

fig1 = plt.figure(1)
fig1.clf()
예제 #5
0
    if len(sys.argv) > 2:
        nel = int(sys.argv[2])
        if len(sys.argv) > 3:
            xmin = float(sys.argv[3])
        else:
            xmin = 0.5
    else: 
        nel = 1000
else:
    nel = 1000
    xmin = 0.5
    ntests = 1000

a = np.zeros(ntests)
for i in range(ntests):
    X=plfit.plexp_inv(np.random.rand(nel),xmin,2.5)
    p=plfit.plfit(X,xmin=xmin,quiet=True,silent=True)
    a[i]=p._alpha

h,b = plt.hist(a,bins=30)[:2]
bx = (b[1:]+b[:-1])/2.0

from agpy import gaussfitter
p,m,pe,chi2 = gaussfitter.onedgaussfit(bx,h,params=[0,ntests/10.0,2.5,0.05],fixed=[1,0,0,0])

fig1 = plt.figure(1)
fig1.clf()
plt.plot(bx,m)

print("XMIN fixed: Alpha = 2.5 (real), %0.3f +/- %0.3f (measured)" % (p[2],p[3]))
예제 #6
0
import numpy as np
import plfit
import pylab as plt

xmins = np.logspace(-1, 1)
nel = 2000

fig1 = plt.figure(1)
fig1.clf()
ax1 = fig1.add_subplot(2, 1, 1)
ax2 = fig1.add_subplot(2, 1, 2)

for alpha in (1.5, 2.5, 3.5):
    results = []
    for xmin in xmins:
        data = plfit.plexp_inv(np.random.rand(nel), xmin, alpha)
        result = plfit.plfit(data, quiet=True, silent=True)
        results.append(result)

    fitted_xmins = [r._xmin for r in results]
    fitted_alphas = [r._alpha for r in results]
    fitted_alpha_errors = [r._alphaerr for r in results]

    ax1.loglog(xmins,
               fitted_xmins,
               's',
               label='$\\alpha={0}$'.format(alpha),
               alpha=0.5)
    ax1.loglog(xmins, xmins, 'k--', alpha=0.5, zorder=-1)

    ax2.errorbar(xmins,
예제 #7
0
if len(sys.argv) > 1:
    ntests = int(sys.argv[1])
    if len(sys.argv) > 2:
        nel = int(sys.argv[2])
        if len(sys.argv) > 3:
            xmin = float(sys.argv[3])
        else:
            xmin = 0.5
    else: 
        nel = 1000
else:
    ntests = 10000

a=zeros(ntests)
for i in range(ntests):
    X=plfit.plexp_inv(rand(nel),xmin,2.5)
    p=plfit.plfit(X,xmin=xmin,quiet=True,silent=True)
    a[i]=p._alpha

h,b = hist(a,bins=30)[:2]
bx = (b[1:]+b[:-1])/2.0

import gaussfitter
p,m,pe,chi2 = gaussfitter.onedgaussfit(bx,h,params=[0,ntests/10.0,2.5,0.05],fixed=[1,0,0,0])

plot(bx,m)

print "XMIN fixed: Alpha = 2.5 (real), %0.3f +/- %0.3f (measured)" % (p[2],p[3])


a=zeros(ntests)
예제 #8
0
from __future__ import print_function
import numpy as np
import plfit
import pylab as plt

nel = 2000
alpha = 2.5
xmin = 1.0

data_prob = np.random.rand(nel)
data = plfit.plexp_inv(data_prob, xmin, alpha)
inds = np.argsort(data)
data_prob = data_prob[inds]
data = data[inds]

xdata = np.logspace(-2,2,10000)
plt.figure(1).clf()
plt.loglog(xdata, 1-plfit.plexp_cdf(xdata, xmin=xmin, alpha=alpha), 'k', linewidth=5, alpha=0.2, zorder=-1)
plt.loglog(xdata, 1-plfit.plexp_cdf(xdata, xmin=xmin, alpha=alpha, pl_only=True), 'g', linewidth=3, alpha=1, zorder=0)
plt.loglog(xdata, 1-plfit.plexp_cdf(xdata, xmin=xmin, alpha=alpha, exp_only=True), 'b', linewidth=3, alpha=1, zorder=0)
plt.plot(xmin, 1-plfit.plexp_cdf(xmin, xmin=xmin, alpha=alpha, exp_only=True), 'kx', markersize=20, linewidth=3, alpha=1, zorder=1)
plt.ylim(1e-2,1)

plt.figure(2).clf()
plt.loglog(data, 1-plfit.plexp_cdf(data, xmin=xmin, alpha=alpha), 'k', linewidth=10, alpha=0.1, zorder=-1)
plt.loglog(data, 1-plfit.plexp_cdf(data, xmin=xmin, alpha=alpha, pl_only=True), 'g', linewidth=3, alpha=0.5, zorder=0)
plt.loglog(data, 1-plfit.plexp_cdf(data, xmin=xmin, alpha=alpha, exp_only=True), 'b', linewidth=3, alpha=0.5, zorder=0)
plt.plot(xmin, 1-plfit.plexp_cdf(xmin, xmin=xmin, alpha=alpha, exp_only=True), 'kx', markersize=20, linewidth=3, alpha=1, zorder=1)
plt.loglog(data, 1-data_prob, 'r.', zorder=2, alpha=0.1)
plt.ylim((1-data_prob).min(),1)