Exemple #1
0
def run_demo(args):
    """
    @brief a Gaussian Process regression example using an input covariance 
    model. The demo divides the observations along the x-axis and computes in 
    parallel using mpi4py
    """

    np.random.seed(1)

    def f(x):
        """
        @brief the function to predict.
        """
        return x * np.sin(x)
  
    # the inpute data points
    X = np.linspace(0.1, 9.9, 500)

    # make the observations with added noise
    y = f(X).ravel()
    dy = 0.5 + 1.0 * np.random.random(y.shape)
    noise = np.random.normal(0, dy)
    y += noise
    
    # mesh the input space for evaluations of the prediction
    x = np.linspace(-2, 12, 2*len(X))

    
    # instanciate a Gaussian Process model, allowing all params to vary
    gp = GaussianProcessMPI(theta0 = [0.5, 2.0, 1.0], 
                         covfunction=args.covariance, verbose=True, 
                         fixed=[False, False, False])

    # fit to data using Maximum Likelihood Estimation of the parameters
    gp.fit(X, y, dy)

    # make the prediction on the meshed x-axis
    y_pred, sigma = gp.predict(x)
    
    if MPI.COMM_WORLD.Get_rank() == 0:
        
        # plot the function, the prediction and the 95% confidence interval based on
        # the standard deviation
        fig = pl.figure()
        
        pl.plot(x, f(x), 'r:', label=r'$f(x) = x \ \mathrm{sin}(x)$')
        pl.errorbar(X.ravel(), y, dy, label='Observations')
        pl.plot(x, y_pred, label='Prediction')
        pl.fill(np.concatenate([x, x[::-1]]),
                np.concatenate([y_pred - 1.9600 * sigma,
                               (y_pred + 1.9600 * sigma)[::-1]]),
                alpha=.2, fc='DarkGoldenRod', ec="None", label='95% confidence interval')
    
        pl.xlabel('$x$', fontsize=16)
        pl.ylabel('$f(x)$', fontsize=16)
        pl.ylim(-15, 20)
        pl.legend(loc='upper left')
    
        pl.show()
Exemple #2
0
def run_demo(args):
    """
    @brief a Gaussian Process regression example that fits several supernovae
           spectra in parallel using mpi4pys
    """
    
    myrank = comm.Get_rank()
    nprocs = comm.Get_size()
    
    # read in the relevant files in correct order
    f1 = glob("../data/SN2011fe/11feM*")
    f1.sort(reverse=True)
    f2 = glob("../data/SN2011fe/11feP*")
    f2.sort()
    files = f1+f2
    
    pl.ion()
    
    for N, f in enumerate(files):
        
        file_root = os.path.splitext(os.path.basename(f))[0]
        time = float(file_root[-3:])/10.
        if 'M' in file_root: time *= -1.
        
        # load the data from inputdata.txt
        X, Y, Yerr = np.loadtxt(f, unpack=True)
  
        n_eval = len(X)
        batch_size = args.batch_size
        resolution = args.resolution
    
        # instanciate a Gaussian Process model, allowing all params to vary
        gp = GaussianProcessMPI(theta0 = [1e-13, 2.0, 1e-13], 
                             covfunction=args.covariance, verbose=True, 
                             fixed=[False, False, False])

        xmin = np.amin(X)
        xmax = np.amax(X)
        nstar = len(X)*resolution
    
        # mesh the input space for evaluations of the prediction
        x = np.linspace(xmin, xmax, nstar)

        # fit to data using Maximum Likelihood Estimation of the parameters
        gp.fit(X, Y, Yerr, batch_size=200)

        # make the prediction on the meshed x-axis
        y_pred, sigma = gp.predict(x)
        
        # plot if you are the master
        if myrank == 0:
        
            # plot the function, the prediction and the 95% confidence interval based on
            # the standard deviation
            pl.cla()
            pl.plot(X, Y, label='Observations')
            pl.plot(x, y_pred, label='Prediction')
            pl.fill(np.concatenate([x, x[::-1]]),
                   np.concatenate([y_pred - 1.9600 * sigma,
                                  (y_pred + 1.9600 * sigma)[::-1]]),
                   alpha=0.5, fc='DarkGoldenRod', ec="None", 
                   label='95% confidence interval')
                
            pl.xlabel(r'$\lambda \ (\AA)$', fontsize=16)
            pl.ylabel('$\mathrm{Flux \ (erg/s/cm^2/\AA)}$', fontsize=16)
            pl.legend(loc='upper right')
            pl.title("SNe 2011fe %+.1f days relative to B-band max" %time)
            pl.ylim(-0.2e-12, 1.2e-12)
            pl.savefig("figures/SN11fe_mpi_%02d.png" %N)
            pl.draw()