Exemplo n.º 1
0
def main():
    """
        Function to initialize instance of the sampler, specify the forward model simulation and begin sampling.
        """
    if fm_sim == "snana":
        sampler = abc.ABC_class(nparam, npart, dataLC, tlevels, niter, prior,
                                **prop)
    elif fm_sim == "sncosmo":
        sampler = abc.ABC_class(nparam, npart, data, tlevels, niter, prior,
                                **prop)
    model_sim = run_simulation

    sampler.sample(model_sim)
Exemplo n.º 2
0
def main():
    mean = [0.037, 0.57]
    data = np.random.multivariate_normal(mean, cov, 1000)
    model_sim = simulation
    sampler = astroabc.ABC_class(2, 100, data, [0.7, 0.02], 20, prior, **prop)

    sampler.sample(model_sim)  # Here we run the ABC algoritm
def main():
  
    sampler = astroabc.ABC_class(57,300,data,[0.02,0.008],50,priors,**prop)
    model_sim=simu_LMC
    
    
    sampler.sample(model_sim)
 def sampler(self,
             num_particles: int = 100,
             tol_up: float = 0.5,
             tol_low: float = 0.002,
             itr: int = 20):
     self.sampler = astroabc.ABC_class(self.num_params, num_particles, self.X,
                                       [tol_up, tol_low], itr, self.priors, **self.prop)
def main():
    param=[50]
    data=simu_LMC(param)
    sampler = astroabc.ABC_class(1,10,data,[1000,1.],20,priors,**prop)
    model_sim=simu_LMC
    
    
    sampler.sample(model_sim)
def main():
    param = [50, 34.7, 0.688, 122.5, 202.7]
    data = simu_LMC(param)
    sampler = astroabc.ABC_class(5, 40, data, [1000, 0.001], 40, priors,
                                 **prop)
    model_sim = simu_LMC

    sampler.sample(model_sim)
Exemplo n.º 7
0
    parser.add_argument("-o",
                        "--output_path",
                        type=str,
                        help="a path to an output directory, optional",
                        required=True)
    parser.add_argument("-i",
                        "--real_data_file",
                        type=str,
                        help="input directory with .seq files",
                        required=True)
    args = parser.parse_args()
    if not vars(args):
        parser.print_help()
        parser.exit(1)

    #REAL_DATA = pd.read_csv('/sternadi/home/volume2/noam/cheater_model/37B_realdata_until_p23_for_fit.csv')
    REAL_DATA = pd.read_csv(args.real_data_file)
    MAX_PASSAGE = REAL_DATA.passage.max()
    prop = {
        'dfunc': calculate_particle_diff,
        'outfile': args.output_path,
        'tol_type': 'exp',
        'verbose': 1,
        'pert_kernel':
        2,  # multivariate perturbation based on local covariance
        'mp': True
    }
    sampler = astroabc.ABC_class(11, 10000, REAL_DATA, [0.3, 0.07], 15, PRIORS,
                                 **prop)
    sampler.sample(simulation)
Exemplo n.º 8
0
# For $\Omega_{m}$ we use a normal distribution with mean $0.3$ and standard deviation $0.5$.
# For $w_{0}$ we use a normal distribution with mean $-1.0$ and standard deviation $0.5$.

# priorname  = ["normal","normal"]
# hyperp = [[0.3,0.5], [-1.0,0.5]]
# prior = zip(priorname,hyperp)

priorname = ["normal", "normal"]  #, "normal", "normal", "normal"]
hyperp = [
    [param1[1], (param1[3] - param1[2]) / 4.],
    [param3[1], (param3[3] - param3[2]) / 4.]
]  #, [param3[1], (param3[3] - param3[2])/4.], [param4[1], (param4[3] - param4[2])/4.], [param5[1], (param5[3] - param5[2])/4.]]

prior = zip(priorname, hyperp)

sampler = astroabc.ABC_class(nparam, npart, data, tlevels, niter, prior,
                             **prop)
sampler.sample(ABCsimulation)

######### PLOTTING ROUTINE ################

###########################################################################
samples_plot = sampler.chain[:, :, :].reshape((-1, nparam))

print(sampler.outfile)
####### FINAL PARAMETER ESTIMATES #######################################

outfile = sampler.outfile
samples_plot = np.loadtxt(outfile, skiprows=1)
samples_plot = samples_plot[:, :-2]

import pygtc
Exemplo n.º 9
0
def main_test():
    import plotly.plotly as py
    import plotly.graph_objs as go

    #make the fake data with diagonal covariance
    means = np.array([0.037579, 0.573537])
    cov = np.array([0.01, 0.005, 0.005, 0.1])
    COV = [[0.05, 0.025], [0.025, 0.5]]
    #    data = astroabc.Model("normal",100).make_mock((means,cov))
    data = astroabc.Model("normal", 100).make_mock(([1, 1], cov))
    case = 3
    """ Implemented """
    if case == 1:
        priors = [('uniform', [-0.1, 0.2]), ('uniform', [0.1, 0.9])]
        Nparas = 2
    """Issue 5: var A; not working """
    if case == 2:

        multivariate = ('multigauss', [means[0], means[1], COV])
        priors = [multivariate]
        Nparas = 1
    """Issue 5: var B; working wekk """
    if case == 3:
        from scipy.stats import multivariate_normal

        prior = np.random.multivariate_normal(means, COV,
                                              200)  #cov.reshape((2,2))
        np.savetxt('output_1.txt', prior, fmt='%+0.4e')
        priors = [('nonstandard', ["output_", 1, 0]),
                  ('nonstandard', ["output_", 1,
                                   1])]  #('uniform', [value_1,value_2]),
        Nparas = 2
        """For the non-standard option the format expected is from a single pdf for one parameter written as text column output. 
           Let's say you have the output from an MCMC run, each column in the output file would then correspond to the marginalized 1D pdf for each of 
           the parameters. E.g. for parameter A whose pdf is in column 0 in "output_1.txt" use:
                          
                          NFILES = 1;
                          FILENAME = "output_"    
                          
                          and prior setting priors = [('nonstandard', [1])] 
        """

    prop = {
        'dfunc': dist_metric,
        'outfile': "gaussian_exampleB.txt",
        'verbose': 1,
        'adapt_t': True,
        'variance_method': 4,
        'k_near': 10,
        'mp': True,
        'num_proc': 20
    }

    walkers = 160
    steps = 5
    sampler = astroabc.ABC_class(Nparas, walkers, data, [0.5, 0.002], steps,
                                 priors, **prop)

    if case == 2:
        sampler.sample(model_sim_mod)
    else:
        sampler.sample(model_sim)

    A = np.loadtxt('gaussian_exampleB.txt', skiprows=1)
    plot_chain2D(prior, A, walkers, steps, extent=(-0.5, 0.5, -1.0, 2.0))

    return 0
Exemplo n.º 10
0
def main_MUSIC_2(newrun=True):
    """ Runs astroABC on MUSIC-2 vs NVSS"""
    folder = '/data/ClusterBuster-Output/'

    start = time.time()

    if newrun:
        walkers = 100  #100       # number of particles/walkers
        niter = 5  #4           # number of iterations
        tlevels = [3.0, 0.9]  # maximum,minimum tolerance
        Ndim = 3
        mu_lgeff, mu_lgB0 = -5.0, -0.5  #-6.0,-0.5 with less sky coverage
        means = [mu_lgeff, mu_lgB0]
        COV = [[0.8, -0.5], [-0.5, 0.8]]

        #        """ SuperMegaTest """
        #        mu_lgeff, mu_lgB0  = -5.0 , -0.5  #-6.0,-0.5 with less sky coverage
        #        COV      = [[0.001, -0.0], [-0.0, 0.001]]
        #        walkers  = 16
        #        niter    = 1
        """ Caseof a non-standart-prior """

        prior = np.random.multivariate_normal(means, COV,
                                              20000)  #cov.reshape((2,2))
        np.savetxt('output_1.txt', prior, fmt='%+0.4e')
        priors = [('nonstandard', ["output_", 1, 0]),
                  ('nonstandard', ["output_", 1, 1]),
                  ("uniform", [0, 1.0])]  #('uniform', [value_1,value_2]),
        """  This is the pre-existing electron part Gelszinnis_Model """
        #        N_pre    = 4
        #        Ndim    += N_pre
        #        priors  += [("normal",[-4,2]),("uniform",[0,1.0]),("normal",[-4,2]),("uniform",[0.3,2.0])] #lgp0, p_sigma, lgsigmoid_0, sigmoid_width)
        outfile = folder + "abc_pmc_output_run_05_" + str(Ndim) + "param.txt"
        """  This is the pre-existing electron part Hoeft_Model 
        
        I had to made a decicion about some values, so I tried to fix ...
        B0,kappa,eff
        t0,t1    = 0.1,5 --> varying   ---> get these values as logarithmic values? better not
        Also fix t0 as 10% of t1, because if not, you could get an odd correlation ...
        
        ratio    = 0.05 --> varying
        nu1,nu2  = 1e-6,1e-2
        
        
        Metric: Add mach number and maybe the disance of the mean of the othe rcorrelations?
           ---> Somehow like the entropy of the correlation matrix ? ...
           ---> Prewhiten The data (zero, mean, standard etc. for NVSS)
           ---> Transform the simulated data in the saem frame and measure the distance**2 along PCA1,PCA2,PCA3 as metrices etc, ...
           --> This wont work, because data with zero scatter would be good. 
           --> Better take the 
        """

        #
        prop = {
            'tol_type': 'exp',
            "verbose": 1,
            'adapt_t': True,
            'threshold': 75,
            'variance_method': 4,
            'k_near': 10,  #'threshold':75,'pert_kernel':2,'variance_method':0,
            'dist_type': 'user',
            'dfunc': SurveyMetrics.ABC_dist_music2,
            'restart': folder + "restart_test.txt",
            'outfile': outfile,
            'mpi': False,
            'mp': True,
            'num_proc': 16,
            'from_restart': False
        }
        """Loads NVSS survey"""
        surveyname = 'NVSS'
        data = iout.unpickleObject('%s%s/pickled/SurveySample' %
                                   (folder, surveyname))

        print priors

        #    multivariate = ('multigauss', [mu_lgeff,mu_lgB0,COV])
        #    prior = [multivariate, ("uniform",[0,1.0])]

        sampler = astroabc.ABC_class(Ndim, walkers, data, tlevels, niter,
                                     priors, **prop)
        sampler.sample(MUSIC2.main_astroABC)

    #oldrun
    else:

        run = 4
        means = [-5.0, -0.5]
        COV = [[0.8, -0.5], [-0.5, 0.8]]
        """ run 7: First try with (fake) preexisting pasma
            + From here on MUSIC2_NVSS02_SSD.parset (with compression enhanced B-field) """
        if run == 7:
            tlevels = [3.0, 0.6]
            means = [-5.0, -0.5]
            walkers = 100
            niter = 6
            outfile = folder + 'abc_pmc_output_run_06_7param.txt'
            """
            standard metric (pls copy here)
            """
        """ run 6: Test only one (best) model without any covariance """
        if run == 5:
            tlevels = [3.0, 0.9]
            means = [-5.0, -0.5]
            COV = [[0.001, -0.0], [-0.0, 0.001]]
            walkers = 20
            niter = 0
            outfile = folder + 'abc_pmc_output_run_05_3param.txt'
            """
            standard metric (pls copy here)
            """
        """ run 5: From here on MUSIC2_NVSS02_SSD.parset(4 times larger, more particles with lower mach numbers)
                   From here in with compression enhanced B-field"""
        if run == 5:
            tlevels = [3.0, 0.9]
            means = [-5.0, -0.5]
            walkers = 100
            niter = 5
            outfile = folder + 'abc_pmc_output_run_05_3param.txt'
            """
            standard metric (pls copy here)
            """
        """ run 4 """
        if run == 4:
            tlevels = [3.0, 0.9]
            means = [-5.0, 0.0]
            walkers = 100
            niter = 9
            outfile = folder + 'abc_pmc_output_run_04_3param.txt'
            """
            standard metric (pls copy here)
            """
        """ run 3 """
        if run == 3:
            tlevels = [3.0, 0.6]
            means = [-5.0, 0.0]
            walkers = 300
            niter = 11
            outfile = folder + 'abc_pmc_output_run_03_3param.txt'
            """
            standard metric (pls copy here)
            did terminate at step 5/6 because no of the samples fulfiled the strict rejection criterium
            """
        """ run 2: From here in survey completeness 0.85 ?"""
        if run == 2:
            tlevels = [3.0, 0.9]
            means = [-5.0, 0.0]
            walkers = 180
            niter = 8
            outfile = folder + 'abc_pmc_output_run_02_3param.txt'
            """standard metric (pls copy here)"""
        """ run 1: survey completeness 0.25"""
        if run == 1:
            outfile = folder + 'abc_pmc_output_3param_0.25simple.txt'
            tlevels = [3.0, 1.1]  # maximum,minimum tolerance
    #        prior    = np.random.multivariate_normal(means, COV, 20000) #cov.reshape((2,2))
        A = np.loadtxt(outfile, skiprows=1)
        print len(A[:, 0:2])
        #        plot_chain2D(prior, A[:,0:2], 70, 6, extent=(-5.5,-3,-1.2,1.7), outfile=outfile+'.pdf')
        #        plot_chain2D_new(prior, A[:,0:2], 70, 6, extent=(-5.5,-3,-1.2,1.7), outfile=outfile+'.pdf')

        import corner
        figure = corner.corner(A[:, 0:4],
                               labels=[
                                   r"$\lg f_e$", r"$lg B_0$", r"$\kappa$",
                                   r"Dist$_\mathrm{metric}$"
                               ],
                               quantiles=[0.16, 0.5, 0.84],
                               show_titles=True,
                               title_kwargs={"fontsize": 12})
        plt.savefig(folder + 'corner_allruns')
        plt.figure()

    end = time.time()
    print 'Elapsed time for main_MUSIC_2(): %.2e seconds.' % (end - start)

    return 0