""" import copy import math import matplotlib.pyplot as plt import numpy as np import scipy.stats import statspy as sp import statspy.interval X = sp.RV("norm(x|mux=20,sigmax=5)") x = X(size=100) print 'True values: mu = 20, sigma = 5' pdf_fit = sp.PF("pdf_fit=norm(x|mu=1,sigma=1)") mu = sp.get_obj('mu') sigma = sp.get_obj('sigma') # First compute 95% CL bounds mu.value = x.mean() - x.std() sigma.value = x.std() * 0.5 mu.unc = 0 sigma.unc = 0 params, corr, quantile = statspy.interval.pllr(pdf_fit, x, cl=0.95) bounds_95cl = [] for par in params: bounds_95cl.append([par.value + par.neg_unc, par.value + par.pos_unc]) # Second compute 1-sigma positive/negative uncertainties (68.27% CL) mu.value = x.mean() - x.std() sigma.value = x.std() * 0.5
handles, labels = ax_mul.get_legend_handles_labels() ax_mul.legend(handles[::-1], labels[::-1]) # Division X4 = sp.RV("norm(x4;mux4=1.,sigmax4=0.05)") Y4 = sp.RV("norm(y4;muy4=1.,sigmay4=0.05)") Z4 = X4 / Y4 ax_div = fig.add_subplot(324) mean_Z4 = Z4.pf.mean() rms_Z4 = Z4.pf.std() z4 = np.linspace(mean_Z4 - 5 * rms_Z4, mean_Z4 + 5 * rms_Z4, 200) ax_div.plot(z4, X4.pf(z4), label='X') ax_div.plot(z4, Y4.pf(z4), label='Y') ax_div.plot(z4, Z4.pf(z4), label='X/Y') handles, labels = ax_div.get_legend_handles_labels() ax_div.legend(handles[::-1], labels[::-1]) # Rescaling mux = sp.get_obj('mux') sigmax = sp.get_obj('sigmax') Z5 = (X - mux) / sigmax ax_res = fig.add_subplot(325) z5 = np.linspace(mux.value - 7 * sigmax.value, mux.value + 3 * sigmax.value, 200) ax_res.plot(z5, X.pf(z5), label='X') ax_res.plot(z5, Z5.pf(z5), label='(X-$\mu$)/$\sigma$') handles, labels = ax_res.get_legend_handles_labels() ax_res.legend(handles[::-1], labels[::-1]) plt.show() fig.savefig('rv_operations.png', dpi=fig.dpi)
nobs = scipy.stats.poisson.rvs(nexp, size=1)[0] data = pdf_true.rvs(size=nobs) fig = plt.figure() fig.patch.set_color('w') ax = fig.add_subplot(111) # Build histogram of the data ydata, bins, patches = ax.hist(data, 30, range=[50, 200], log=True, facecolor='green', alpha=0.75, label='Data') xdata = 0.5*(bins[1:]+bins[:-1]) # Bin centers dx = bins[1:] - bins[:-1] # Bin widths # Define the background and signal PFs pdf_fit_bkg = sp.PF("pdf_fit_bkg=expon(x;offset=50,lambda=10)") sp.get_obj("lambda").label = "\\lambda" offset = sp.get_obj('offset') offset.const = True # Fix parameter value #pdf_fit_bkg.norm.value = nobs #sidebands = np.logical_or((xdata < 100), (xdata > 150)) #pdf_fit_bkg.leastsq_fit(xdata, ydata, dx=dx, cond=sidebands) pdf_fit_sig = sp.PF("pdf_fit_sig=norm(x;mu=120,sigma=20)") sp.get_obj("mu").label = "\\mu" sp.get_obj("sigma").label = "\\sigma" pdf_fit = pdf_fit_bkg + pdf_fit_sig pdf_fit.name = 'pdf_fit' pdf_fit.norm.const = False # Fit total rate to data pdf_fit.norm.label = 'Norm' pdf_fit.norm.value = nobs pdf_fit_sig.norm.label = 'frac(sig)' # Least square fit to the data (whole data range)