Esempio n. 1
0
def PrintSignificance():
    # Print out totals.
    print("TOTALS:")
    print("N_obs = ", sum(obs))
    print("N_ss = ", sum(ss))
    print("N_brn =", sum(brn_prompt + brn_delayed))
    print("N_cevns = ", sum(cevns))

    an = Analyzer(4, "multinest/cenns10_stat/cenns10_stat")
    bf = an.get_best_fit()['parameters']

    an_null = Analyzer(
        3, "multinest/cenns10_stat_no_cevns/cenns10_stat_no_cevns")
    bf_null = an_null.get_best_fit()['parameters']
    bf = [
        an.get_stats()['marginals'][0]['median'],
        an.get_stats()['marginals'][1]['median'],
        an.get_stats()['marginals'][2]['median'],
        an.get_stats()['marginals'][3]['median']
    ]
    bf_null = [
        an_null.get_stats()['marginals'][0]['median'],
        an_null.get_stats()['marginals'][1]['median'],
        an_null.get_stats()['marginals'][2]['median']
    ]
    # Save best-fit (MLE) parameters from MultiNest (in <out>stats.dat)
    # Truncated gaussian
    bf_norm = [
        0.128203949389575733E+01, -0.757751720547599188E-02,
        0.928830540200280969E-01, -0.681121212215910043E+00
    ]
    bf_norm_null = [
        -0.799580130637969101E-02, 0.253213583049654078E+00,
        -0.514351228113789194E+00
    ]
    # Unconstrained Gaussian
    bf_truncnorm = [
        0.168960153287222759E+01, -0.312937517992761469E-01,
        0.780942325684447630E-01, -0.970385882467374672E+00
    ]
    bf_truncnorm_null = [
        -0.147905160635425168E-01, 0.245574237324468231E+00,
        -0.460897530294036739E+00
    ]

    # Get ratio test
    print("Significance (stat):")
    stat_q = sqrt(abs(2*(-poisson(obs, events_gen_stat(bf)) \
                        + poisson(obs, events_gen_stat_null(bf_null)))))
    print(stat_q)

    print("Best-fit norms:")
    events_gen_stat(bf, report_stats=True)
def PrintSignificance():
    an = Analyzer(9, "multinest/cenns10_syst/cenns10_syst")
    bf = an.get_best_fit()['parameters']

    an_null = Analyzer(6, "multinest/cenns10_syst_no_cevns/cenns10_syst_no_cevns")
    bf_null = an_null.get_best_fit()['parameters']

    # Get ratio test
    print("Significance (stat):")
    stat_q = sqrt(abs(2*(-poisson(obs, events_gen(bf)) \
                        + poisson(obs, events_gen_null(bf_null)))))
    print(stat_q)

    print("Best-fit norms:")
    events_gen(bf, report_stats=True)
Esempio n. 3
0
    def __init__(self, fits, silent=False):
        '''
        Loads the fits and creates an array of analyzers
        to compare with

        '''

        analyzers = []
        modelnames = []
        parameters = []

        # Loop over the loaded chains and fit files
        for f in fits:

            self._LoadData(
                f)  #method should attach self.modelnames and self.parameters

            anal = Analyzer(n_params=self.n_params,
                            outputfiles_basename=self.basename)
            analyzers.append(anal)  #Gather the analyzers
            modelnames.append(self.modName)  #Gather the model names
            parameters.append(
                self.parameters)  #Gather the parameter names of the models

        self.fits = fits
        self.analyzers = analyzers
        self.modelnames = modelnames
        self.parameters = parameters

        self.silent = silent

        #Call the private functions
        self._Evidence()  #Compute logZ for each model
        self._SortModels()  # Sort the models based on logZ
        self._EvidenceMatrix()  # Calculate the bayes factors
        self._PrintResults()
Esempio n. 4
0
def check_full_solver(finesse_folder):

    data_filename = path.join(finesse_folder, 'finesse_input.h5')

    data = file_io.h5_2_dict(data_filename)

    ix0 = data['fit_ix']['0']
    #ix1 = data['fit_ix']['1']

    r0 = data['r'][ix0]
    sig0 = data['sig'][ix0]
    sig0_sd = data['sig_sd'][ix0]

    # r1 = data['r'][ix1]
    # sig1 = data['sig'][ix1]
    # sig1_sd = data['sig_sd'][ix1]

    n_params = 4
    analyzer = Analyzer(n_params,
                        outputfiles_basename=path.join(finesse_folder,
                                                       "full_"))
    post = analyzer.get_equal_weighted_posterior()

    post = post[:,
                0:-1]  # Don't include the last column, I forget what it means

    npost = post.shape[0]
    nsamples = 100
    samples = np.random.choice(npost, size=nsamples)

    sampled_post = post[samples, :]
    output0 = np.zeros((nsamples, len(r0)))
    #output1 = np.zeros((nsamples, len(r1)))

    with concurrent.futures.ProcessPoolExecutor() as executor:
        futures_map = dict()
        for idx, cube in enumerate(sampled_post):
            # fut = executor.submit(model_wrapper, r0, r1, cube)
            fut = executor.submit(model_wrapper, r0, cube)
            futures_map[fut] = idx

        for future in concurrent.futures.as_completed(futures_map):
            idx = futures_map[future]

            #val0, val1 = future.result()
            val0 = future.result()
            output0[idx, :] = val0
            #output1[idx, :] = val1

    # calculate simple statistics for the fit
    output0_mean = np.mean(output0, axis=0)
    output0_low = np.min(output0, axis=0)
    output0_high = np.max(output0, axis=0)

    # output1_mean = np.mean(output1, axis=0)
    # output1_low = np.min(output1, axis=0)
    # output1_high = np.amax(output1, axis=0)

    # Plotting fit results
    fig, ax = plt.subplots(figsize=(12, 9))
    ax.fill_between(r0, output0_low, output0_high, color='C0', alpha=0.5)
    ax.plot(r0, output0_mean, color='C0', label='fit')

    #ax.fill_between(r1, output1_low, output1_high, color='C0', alpha=0.5)
    #ax.plot(r1, output1_mean, color='C0')

    ax.errorbar(r0, sig0, yerr=sig0_sd, color='C1', label='data')
    #ax.errorbar(r1, sig1, yerr=sig1_sd, color='C1')
    ax.legend(fontsize=16)
    ax.set_xlabel('R (px)', fontsize=16)
    ax.set_ylabel('Counts', fontsize=16)
    ax.tick_params(labelsize=16)
    fig.tight_layout()
    plt.show()

    labels = ["L", "d", "F", "A"]  #, "B"]#, "Ti"]
    fac = [0.004, 1.0, 1.0, 1.0, 1.0]  #, 1.0]
    dbins = (post[:, 1].max() - post[:, 1].min()) / (468e-6 / 10.0)
    print(post[:, 1].max(), post[:, 1].min())
    dbins = int(dbins)
    print(dbins)
    #dbins = 50
    #bins = ['auto', 'auto', 'auto', 'auto', 'auto', 'auto']
    bins = ['auto', dbins, 'auto', 'auto', 'auto']  #, 'auto']
    for idx, label in enumerate(labels):
        print(label, ": ", np.nanmean(post[:, idx] * fac[idx]))
        fig, ax = plt.subplots()
        plotting.my_hist(ax, post[:, idx] * fac[idx], bins=bins[idx])
        ax.set_xlabel(label)
        plt.show(block=False)
    plt.show(block=True)
Esempio n. 5
0
class FitView(object):
    '''
    FitView examines the output of mnfit and produces plots

    '''

    def __init__(self,data,silent=False,journal="plain",stats="best"):
        '''
        The type of data depends on the subclass.
        The generic _LoadData function is called and should set
        the xlabel

        ______________________________________________________
        arguments:
        data: a file

        '''

        
        self.xlabel = "x"

        # This will be called to format the data
        self._LoadData(data)

        self.anal  = Analyzer(n_params=self.n_params,outputfiles_basename=self.basename)
        self.stats = stats

        if self.stats == "best":
            self.bestFit = array(self.anal.get_best_fit()["parameters"])
        elif self.stats == "mean":
            self.bestFit = array(self.anal.get_stats()['modes'][0]['mean'])

        self.loglike = self.anal.get_best_fit()["log_likelihood"]

        #Calculate teh effective # of free parameters from Spiegelhalter (2002)
        posterior = self.anal.get_equal_weighted_posterior()[:,-1]
        posteriorMean = mean(posterior)

        self.effNparams = -2.*(posteriorMean - self.loglike)

        
        if silent: #Don't print anything out
            return
        self._StatResults()
        
        self.journal = journal

        self.elinewidth = .8
        self.capsize= 3
        self.linewidth = 1.8
        


        
        if self.journal == "mnras":
            figW = 240

        if self.journal == "apj":

            figW = 245.6


        if self.journal != "plain":

            fig_width_pt = figW  # Get this from LaTeX using \showthe\columnwidth
            inches_per_pt = 1.0/72.27               # Convert pt to inch
            golden_mean = (sqrt(5)-1.0)/2.0         # Aesthetic ratio
            fig_width = fig_width_pt*inches_per_pt  # width in inches
            fig_height = fig_width*golden_mean      # height in inches
            fig_size =  [fig_width,fig_height]
            params = {'backend': 'ps',\
                      'axes.labelsize': 10,\
                      'text.fontsize': 10,\
                      'legend.fontsize': 10,\
                      'xtick.labelsize': 8,\
                      'ytick.labelsize': 8,\
                      'figure.figsize': fig_size,\
                      'lines.markersize': 1,\
                      'legend.numpoints': 1,\
                      'legend.fontsize': 6,\
                      'legend.markerscale': 1.0,\
                      'font.family': 'serif',\
                      'ps.useafm' : True,\
                      'pdf.use14corefonts' : True,\
                      'text.usetex' : True,\
                      'pdf.use14corefonts' : True,\
                       }
            plt.rcParams.update(params)
            self.elinewidth = .3
            self.capsize=1
            self.linewidth = .7


    def _StatResults(self):
        '''
        Prints the statistical results of the fit
        as well as other information via the self._CustomInfo()
        set in inherited classes

        '''

        s = self.anal.get_stats() 

        print
        print "-"*30+" ANALYIS "+"-"*30
        print

        self._CustomInfo()
        print
        
        #print "Global Evidence:\n\t%.3e +- %.3e" % ( s['nested sampling global log-evidence'],\
        #                                             s['nested sampling global log-evidence error'] )
        print "Effective number of free parameters:\n\t%.2f"%self.effNparams
        print
        print "LogLikelihood of Best Fit:\n\t%.2f"%self.loglike
        print
        print "-"*69
        

    def _CustomInfo(self):

        pass



    def _LoadData(self,data):

        pass


    def ViewChain(self):


        pass


    def ViewParam(self,param,fignum=1000):


        i = self.parameters.index(param)
        
        marg = self.anal.get_stats()["marginals"]

        p = probPlot.PlotMarginalModes(self.anal)
        fig = plt.figure(fignum,figsize=(5*self.n_params,5*self.n_params))


        ax = fig.add_subplot(self.n_params,self.n_params, i+1, axisbg="#FCF4F4")

        p.plot_marginal(i, with_ellipses=True , with_points = False, grid_points=50)

        marg1 = marg[i]["1sigma"]
        h=.005
        ax.hlines(h,marg1[0],marg1[1],color="#FF0040",linewidth=1.2)
        ax.plot(self.bestFit[i],h,"o",color="#FF0040")
        
        ax.set_ylabel("Probability")
        ax.set_xlabel(param)
        return ax
    
    def ViewMarginals(self,fignum=900):
        '''
        Plot the marginal distributions of the parameters
        along with the best-fit and 1-sigma errors

        returns:
        ax: matplotlib ax instance

        '''
        marg = self.anal.get_stats()["marginals"]

        p = probPlot.PlotMarginalModes(self.anal)
        fig = plt.figure(fignum,figsize=(5*self.n_params,5*self.n_params))

        for i in range(self.n_params):
            ax = fig.add_subplot(self.n_params,self.n_params, i+1, axisbg="#FCF4F4")

            p.plot_marginal(i, with_ellipses=True , with_points = False, grid_points=50)

            marg1 = marg[i]["1sigma"]
            h=.005
            ax.hlines(h,marg1[0],marg1[1],color="#FF0040",linewidth=1.2)
            ax.plot(self.bestFit[i],h,"o",color="#FF0040")
            if i == 0:
                ax.set_ylabel("Probability")
            ax.set_xlabel(self.parameters[i])


            for j in range(i):

                ax = fig.add_subplot(self.n_params,self.n_params, self.n_params*(j+1)+i+1)
                p.plot_conditional(i, j, with_ellipses = False, with_points = False, grid_points=30)

                marg1 = marg[i]["1sigma"]
                marg2 = marg[j]["1sigma"]

                ax.hlines(self.bestFit[j],marg1[0],marg1[1],color="#FF0040",linewidth=1.2)
                ax.vlines(self.bestFit[i],marg2[0],marg2[1],color="#FF0040",linewidth=1.2)

                ax.plot(self.bestFit[i],self.bestFit[j],"o",color="#FF0040")

                if j==i-1:
                    ax.set_xlabel(self.parameters[i])
                    ax.set_ylabel(self.parameters[j])


        return ax


    def ViewFit(self):
        '''
        Plot the best fit and contours 

        ______________________________________________________
        returns:
        ax: matplotlib ax instance

        '''
        fig = plt.figure(120)
        ax = fig.add_subplot(111)

        yData = []


        for params in self.anal.get_equal_weighted_posterior()[::100,:-1]:

            tmp = []
            
            for x in self.dataRange:

                tmp.append(self.model(x, *params))
            yData.append(tmp)

        



        
        #Plot the spread in data
            
        for y in yData:

            ax.plot(self.dataRange,y,"#04B404",alpha=.2) ## modify later

        bfModel = []


        # Plot the best fit
        for x in self.dataRange:

            bfModel.append(self.model(x, *self.bestFit))
        
            
        ax.plot(self.dataRange,bfModel,"#642EFE") #modify later

        ax.set_xlabel(self.xlabel)


        return ax
        
        

        
        pass


    def GetTeXTable(self,tableName="params.tex"):



        

        

        marg = self.anal.get_stats()["marginals"]

        tmp = []
        for params,val,err in zip(self.parameters,self.bestFit,marg):

            
            

            err = err["1sigma"]
            
            print "\t%s:\t%.2f\t+%.2f -%.2f"%(params,val,err[1]-val,val-err[0])
            tmp.append(['%s'%params,'%.2f'%val,'%.2f'%(err[1]-val),'%.2f'%(val-err[0])])

        data = {}

        for t in tmp:
            data[t[0]]=["$%s^{+%s}_{-%s}$"%(t[1],t[2],t[3])]

        ascii.write(data,output=tableName,Writer=ascii.Latex,latexdict={'preamble': r'\begin{center}','tablefoot': r'\end{center}','tabletype': 'table*','header_end': '\\hline \\hline \\hspace{3mm}','caption':self.modName})
        


        
        



    
    def Propagate(self,function,params,direct=True):
        '''
        Propagates the chain into a derived function

        arguments:
         *function: function head to be calculated
         *params:   list of params that are needed

        returns:
         *f  evaluated function
        
        '''

        ewp = self.anal.get_equal_weighted_posterior()

        
        selectedParams = ewp[:,self.GetParamIndex(params)]

        f = []
        if direct:
            for p in selectedParams:

                f.append(function(*p))
        else:
            for p in selectedParams:

                f.append(function(p))

                
        return array(f)

    




    def GetParamIndex (self, params):

        tmp = map(lambda test: array(self.parameters) == test, params)

        tt = tmp[0]
        if len(tmp)>1:
            for test in tmp[1:]:

                tt = logical_or(tt,test)

        return tt
Esempio n. 6
0
    def __init__(self,data,silent=False,journal="plain",stats="best"):
        '''
        The type of data depends on the subclass.
        The generic _LoadData function is called and should set
        the xlabel

        ______________________________________________________
        arguments:
        data: a file

        '''

        
        self.xlabel = "x"

        # This will be called to format the data
        self._LoadData(data)

        self.anal  = Analyzer(n_params=self.n_params,outputfiles_basename=self.basename)
        self.stats = stats

        if self.stats == "best":
            self.bestFit = array(self.anal.get_best_fit()["parameters"])
        elif self.stats == "mean":
            self.bestFit = array(self.anal.get_stats()['modes'][0]['mean'])

        self.loglike = self.anal.get_best_fit()["log_likelihood"]

        #Calculate teh effective # of free parameters from Spiegelhalter (2002)
        posterior = self.anal.get_equal_weighted_posterior()[:,-1]
        posteriorMean = mean(posterior)

        self.effNparams = -2.*(posteriorMean - self.loglike)

        
        if silent: #Don't print anything out
            return
        self._StatResults()
        
        self.journal = journal

        self.elinewidth = .8
        self.capsize= 3
        self.linewidth = 1.8
        


        
        if self.journal == "mnras":
            figW = 240

        if self.journal == "apj":

            figW = 245.6


        if self.journal != "plain":

            fig_width_pt = figW  # Get this from LaTeX using \showthe\columnwidth
            inches_per_pt = 1.0/72.27               # Convert pt to inch
            golden_mean = (sqrt(5)-1.0)/2.0         # Aesthetic ratio
            fig_width = fig_width_pt*inches_per_pt  # width in inches
            fig_height = fig_width*golden_mean      # height in inches
            fig_size =  [fig_width,fig_height]
            params = {'backend': 'ps',\
                      'axes.labelsize': 10,\
                      'text.fontsize': 10,\
                      'legend.fontsize': 10,\
                      'xtick.labelsize': 8,\
                      'ytick.labelsize': 8,\
                      'figure.figsize': fig_size,\
                      'lines.markersize': 1,\
                      'legend.numpoints': 1,\
                      'legend.fontsize': 6,\
                      'legend.markerscale': 1.0,\
                      'font.family': 'serif',\
                      'ps.useafm' : True,\
                      'pdf.use14corefonts' : True,\
                      'text.usetex' : True,\
                      'pdf.use14corefonts' : True,\
                       }
            plt.rcParams.update(params)
            self.elinewidth = .3
            self.capsize=1
            self.linewidth = .7