Example #1
0
def run_model(nu_params):
    # Get band definition
    nu_min, nu_max = nu_params
    print "nu_min = %d GHz, nu_max = %d GHz" % (nu_min, nu_max)
    nu = bands_log(nu_min, nu_max, nbands)
    label = str(nu_min) + '_' + str(nu_max)
    
    # Name of sample file
    fname_samples = "output/samples_%s.%s_nb%d_seed%d_%s.dat" \
                  % (name_in, name_fit, nbands, SEED, label)
    
    # Simulate data and run MCMC fit
    D_vec, Ninv = fitting.generate_data(nu, fsigma_T, fsigma_P, 
                                        components=mods_in, 
                                        noise_file=NOISE_FILE)
                                        
    gls_cmb, cmb_chisq, cmb_noise \
            = fitting.model_test(nu, D_vec, Ninv, mods_fit, 
                                 burn=200, steps=800,
                                 cmb_amp_in=cmb_model.amps(),
                                 sample_file=fname_samples)
    
    # FIXME: CMB noise is actually the variance!
    #cmb_true = np.array([cmb_model.amp_I, cmb_model.amp_Q, cmb_model.amp_U])
    print "chisq =", cmb_chisq, gls_cmb.flatten()
    #print "Frac. err. (I,Q,U) =", np.sqrt(cmb_noise.flatten()) / cmb_true
    #print "Bias =", (gls_cmb.flatten() - cmb_true) / np.sqrt(cmb_noise.flatten())
    
    # Append summary statistics to file
    f = open(filename, 'a')
    f.write(9*('%0.6e ') % (nu_min, nu_max, cmb_chisq, 
                            gls_cmb[0], gls_cmb[1], gls_cmb[2], 
                            cmb_noise[0]/cmb_model.amp_I,
                            cmb_noise[1]/cmb_model.amp_Q, 
                            cmb_noise[2]/cmb_model.amp_U) )
    f.write('\n')
    f.close()
Example #2
0
def run_model(nu_params):
    # Get band definition
    nu_min, nu_max = nu_params
    print "nu_min = %d GHz, nu_max = %d GHz" % (nu_min, nu_max)
    nu = bands_log(nu_min, nu_max, nbands)
    label = str(nu_min) + '_' + str(nu_max)
    
    # Make copies of models
    my_mods_in = mods_in
    my_mods_fit = mods_fit
    
    # Name of sample file
    #fname_samples = "output/%s_samples_%s.%s_nb%d_seed%d_%s.dat" \
    #              % (PREFIX, name_in, name_fit, nbands, SEED, label)
    fname_samples = None
    
    # Simulate data and run MCMC fit
    D_vec, Ninv = fitting.generate_data(nu, fsigma_T, fsigma_P, 
                                        components=my_mods_in, 
                                        noise_file=NOISE_FILE)
                                        
    pnames, samples, logp, ini = model_test(nu, D_vec, Ninv, my_mods_fit, 
                                            burn=NBURN, steps=NSTEPS, 
                                            nwalkers=NWALKERS,
                                            cmb_amp_in=cmb_model.amps(),
                                            sample_file=fname_samples)
    # Calculate best-fit chisq.
    chisq = -2.*logp
    dof = D_vec.size - len(pnames)
    
    # Get best-fit (max. prob.) parameter values
    maxl_idx = np.argmax(logp)
    bf_params = samples[:, maxl_idx]
    
    
    # Reshape sample array into (Nparams, Nwalkers, Nsamples)
    samples = samples.reshape((samples.shape[0], 
                               NWALKERS, 
                               samples.shape[1]/NWALKERS))
    
    # Loop over different burn-in cuts to produce summary stats
    for n, cut in enumerate(cut_range):
        
        # Set output filename
        fname = filename + "_cut%d.dat" % cut
        
        # Output mean and bias
        summary_data = [nu_min, nu_max, np.min(chisq), maxl_idx]
        header = "nu_min nu_max chi2_min maxlike_idx "
        
        # Loop over parameter names
        for i in range(len(pnames)):
            
            # Mean, std. dev., and fractional shift from true value 
            # (for mean, median, and max. likelihood param values)
            _mean = np.mean(samples[i,:,cut:])
            _std = np.std(samples[i,:,cut:])
            _fracbias = (np.mean(samples[i,:,cut:]) - ini[i]) \
                      / np.std(samples[i,:,cut:])
            _med_fracbias = (np.median(samples[i,:,cut:]) - ini[i]) \
                          / np.std(samples[i,:,cut:])
            _ml_fracbias = (bf_params[i] - ini[i]) / np.std(samples[i,:,cut:])
            
            stats = [_mean, _std, _fracbias, _med_fracbias, _ml_fracbias]
            
            # Keep summary stats, to be written to file
            summary_data += stats
            header += "mean_%s std_%s Delta_%s MedDelta_%s MLDelta_%s " \
                    % (pnames[i], pnames[i], pnames[i], pnames[i], pnames[i])
            
            # Only output summary stats once
            if n == 0:
                print "%14s: %+3.3e +/- %3.3e [Delta = %+3.3f, MLDelta = %+3.3f]" \
                      % (pnames[i], stats[0], stats[1], stats[2], stats[3])
        
        # If file is empty, set flag to write header when saving output
        has_header = False if os.stat(fname).st_size == 0 else True
        
        # Append summary statistics to file
        f = open(fname, 'a')
        if has_header:
            np.savetxt(f, np.atleast_2d(summary_data))
        else:
            np.savetxt(f, np.atleast_2d(summary_data), header=header[:-1])
        f.close()
Example #3
0
    def run_model(nu_params, Nside, i_px ):

        ## Number of maps
        n_maps = 0
        for ii in range( len( amps_fit ) ):
                n_maps += len( amps_fit[ ii ] )
        for jj in range( len( params_fit ) ):
                n_maps += len( params_fit[ jj ] )

        # Get band definition
        nu_min, nu_max = nu_params
        #print "nu_min = %d GHz, nu_max = %d GHz" % (nu_min, nu_max)
        nu = bands_log(nu_min, nu_max, n_bands)
        label = str(nu_min) + '_' + str(nu_max)

        # Make copies of models
        my_mods_in = mods_in
        my_mods_fit = mods_fit

        fname_samples = None

        # Simulate data and run MCMC fit
        D_vec, Ninv = fitting.generate_data(nu, fsigma_T, fsigma_P,
                                                components=my_mods_in,
                                                noise_file=NOISE_FILE, idx_px = i_px)

        pnames, samples, logp, ini = model_test(nu, D_vec, Ninv, my_mods_fit,
                                                burn=NBURN, steps=NSTEPS,
                                                nwalkers=NWALKERS,
                                                cmb_amp_in=cmb_model.amps(),
                                                sample_file=fname_samples, 
						i_px = i_px, 
						nthreads = nthreads )
        # Calculate best-fit chisq.
        chisq = -2.*logp
        dof = D_vec.size - len(pnames)

        # Reshape sample array into (Nparams, Nwalkers, Nsamples)
        samples = samples.reshape((samples.shape[0],
                                   NWALKERS,
                                   samples.shape[1]/NWALKERS))

        # Array to store the results (NB: this is the structure for the final FITS file to be stored with healpy)
        data_fits = np.full( ( len( pnames ) * 3 ), hp.UNSEEN, dtype = 'float32' )

        # Loop over different burn-in cuts to produce summary stats
        ## Right now, since it's only printing out some diagnostic, it's set for one case only
        ## Before: for cut in cut_range:
        for cut in [ cut_range[ -1 ] ]:

                # Output mean and bias
                summary_str = "%4.4e %4.4e %4.4e " % (nu_min, nu_max, np.min(chisq))
                summary_data = [nu_min, nu_max, np.min(chisq)]
                header = "nu_min nu_max chi2_min "
                # Loop over parameter names
                for i in range(len(pnames)):

                        # Mean, std. dev., and fractional shift from true value
                        _mean = np.mean(samples[i,:,cut:])
                        _std = np.std(samples[i,:,cut:])
                        _fracbias = (np.mean(samples[i,:,cut:]) - ini[i]) \
                                          / np.std(samples[i,:,cut:])
                        stats = [_mean, _std, _fracbias]
                        data_fits[ 3 * i ] = ini[ i ] 
                        data_fits[ 3 * i + 1 ] = _mean 
                        data_fits[ 3 * i + 2 ] = _std

                        # Keep summary stats, to be written to file
                        summary_data += stats
                        header += "mean_%s std_%s Delta_%s " \
                                        % (pnames[i], pnames[i], pnames[i])
                        # Printing out only the first pixel
                        if i_px == 0:
                            print "%14s: %+3.3e %+3.3e +/- %3.3e [Delta = %+3.3f]" \
                            % (pnames[i], ini[ i ], stats[ 0 ], stats[ 1 ], stats[ 2 ] )
        return data_fits
Example #4
0
def run_model(nu_params, Nside, Npix):

    ## Number of maps
    n_maps = 0
    for ii in range(len(amps_fit)):
        n_maps += len(amps_fit[ii])
    for jj in range(len(params_fit)):
        n_maps += len(params_fit[jj])

    ## For each amplitude & parameter, we store 3 results: in/out/err
    n_maps *= 3
    if os.path.isfile(filename + '.fits') == 1:
        # Check whether there are already some results (if the fit was forced to be re-started, the values are hp.UNSEEN)
        data_ini = hp.read_map(filename + '.fits',
                               range(n_maps),
                               verbose=False)
        # if any is hp.UNSEEN, run the fit, otherwise return
        s_tmp = 0
        for i in range(n_maps):
            s_tmp += np.abs(data_ini[i][Npix])
        if s_tmp < np.abs(hp.UNSEEN):
            print '(run_joint_mcmc_allsky_serial) Pixel %i already analyzed. Skipping it.' % (
                Npix)
            return
    # creare the output file if it does not exist
    if os.path.isfile(filename + '.fits') != 1:
        data_start = np.full((n_maps, 12 * Nside * Nside), hp.UNSEEN)
        hp.write_map(filename + '.fits', data_start, dtype='float32')

    # Get band definition
    nu_min, nu_max = nu_params
    print "nu_min = %d GHz, nu_max = %d GHz" % (nu_min, nu_max)
    nu = bands_log(nu_min, nu_max, nbands)
    label = str(nu_min) + '_' + str(nu_max)

    # Make copies of models
    my_mods_in = mods_in
    my_mods_fit = mods_fit

    # Name of sample file
    #fname_samples = "output_serial/%s_samples_%s.%s_nb%d_seed%d_%s.dat" \
    #              % (PREFIX, name_in, name_fit, nbands, SEED, label)
    fname_samples = None

    # Simulate data and run MCMC fit
    D_vec, Ninv = fitting.generate_data(nu,
                                        fsigma_T,
                                        fsigma_P,
                                        components=my_mods_in,
                                        noise_file=NOISE_FILE)

    pnames, samples, logp, ini = model_test(nu,
                                            D_vec,
                                            Ninv,
                                            my_mods_fit,
                                            burn=NBURN,
                                            steps=NSTEPS,
                                            nwalkers=NWALKERS,
                                            cmb_amp_in=cmb_model.amps(),
                                            sample_file=fname_samples)
    # Calculate best-fit chisq.
    chisq = -2. * logp
    dof = D_vec.size - len(pnames)

    # Reshape sample array into (Nparams, Nwalkers, Nsamples)
    samples = samples.reshape(
        (samples.shape[0], NWALKERS, samples.shape[1] / NWALKERS))

    # Loop over different burn-in cuts to produce summary stats
    for cut in cut_range:

        # Set output filename
        fname = filename + "_cut%d.dat" % cut

        # Output mean and bias
        summary_str = "%4.4e %4.4e %4.4e " % (nu_min, nu_max, np.min(chisq))
        summary_data = [nu_min, nu_max, np.min(chisq)]
        header = "nu_min nu_max chi2_min "
        data_fits = np.full((len(pnames), 3), hp.UNSEEN, dtype='float32')
        # Loop over parameter names
        for i in range(len(pnames)):

            # Mean, std. dev., and fractional shift from true value
            _mean = np.mean(samples[i, :, cut:])
            _std = np.std(samples[i, :, cut:])
            _fracbias = (np.mean(samples[i,:,cut:]) - ini[i]) \
                      / np.std(samples[i,:,cut:])
            stats = [_mean, _std, _fracbias]
            data_fits[i] = [ini[i], _mean, _std]

            # Keep summary stats, to be written to file
            summary_data += stats
            header += "mean_%s std_%s Delta_%s " \
                    % (pnames[i], pnames[i], pnames[i])

            print "%14s: %+3.3e %+3.3e +/- %3.3e [Delta = %+3.3f]" \
                  % (pnames[i], ini[ i ], stats[ 0 ], stats[ 1 ], stats[ 2 ] )

        # Insert the results
        data_maps = hp.read_map(filename + '.fits',
                                range(len(pnames) * 3),
                                verbose=False)
        for i_tmp in range(len(pnames)):
            for i_tmp_2 in range(3):
                data_maps[i_tmp * 3 +
                          i_tmp_2][Npix] = data_fits[i_tmp][i_tmp_2]
        hp.write_map(filename + '.fits', data_maps, dtype='float32')
params_pMBB_narrow = [pMBB_narrow.amp_I, pMBB_narrow.amp_Q, pMBB_narrow.amp_U, cmb.amp_I, cmb.amp_Q, cmb.amp_U,
                  sync.amp_I, sync.amp_Q, sync.amp_U, pMBB_narrow.dust_beta, pMBB_narrow.dust_T,
                  pMBB_narrow.sigma_beta, pMBB_narrow.sigma_temp, sync.sync_beta]

initial_vals_sMBB = (amp_I, amp_Q, amp_U, cmb.amp_I, cmb.amp_Q, cmb.amp_U,
                    sync.amp_I, sync.amp_Q, sync.amp_U, mean_beta, mean_temp,
                    sync.sync_beta)
initial_vals_pMBB_broad = (amp_I, amp_Q, amp_U, cmb.amp_I, cmb.amp_Q, cmb.amp_U,
                        sync.amp_I, sync.amp_Q, sync.amp_U, mean_beta, mean_temp,
                        sigma_beta, sigma_temp, sync.sync_beta)
initial_vals_pMBB_narrow = (amp_I, amp_Q, amp_U, cmb.amp_I, cmb.amp_U, cmb.amp_Q,
                        sync.amp_I, sync.amp_Q, sync.amp_U,mean_beta, mean_temp,
                        .1 * sigma_beta, .1 * sigma_temp, sync.sync_beta)
parent_model = 'mbb'

D_vec_sMBB, Ninv = fitting.generate_data(nu_pico, fsigma_T, fsigma_P, [sMBB, cmb, sync],
                                        noise_file="data/noise_pico.dat" )
D_vec_pMBB_broad, Ninv = fitting.generate_data(nu_pico, fsigma_T, fsigma_P, [pMBB_broad, cmb, sync],
                                           noise_file="data/noise_pico.dat")
D_vec_pMBB_narrow, Ninv = fitting.generate_data(nu_pico, fsigma_T, fsigma_P, [pMBB_narrow, cmb, sync],
                                           noise_file="data/noise_pico.dat")

data_spec_sMBB = (nu_pico, D_vec_sMBB, Ninv, beam_mat)
data_spec_pMBB_broad = (nu_pico, D_vec_pMBB_broad, Ninv, beam_mat)
data_spec_pMBB_narrow = (nu_pico, D_vec_pMBB_narrow, Ninv, beam_mat)

p_spec_sMBB = (pnames_sMBB, initial_vals_sMBB, parent_model)
p_spec_pMBB_broad = (pnames_pMBB_broad, initial_vals_pMBB_broad, parent_model)
p_spec_pMBB_narrow = (pnames_pMBB_narrow, initial_vals_pMBB_narrow, parent_model)

print "running emcee"