コード例 #1
0
ファイル: test_gp.py プロジェクト: baklanovp/pystella
    def test_fit_GP_SNRefsdal_all_lc(self):
        # k = gptools.SquaredExponentialKernel(param_bounds=[(0, 1e3), (0, 100)])
        # gp = gptools.GaussianProcess(k, mu=gptools.LinearMeanFunction())

        # add data
        dm = -29.38  # D = 7.5e6 pc
        # dm = -30.4  # D = 12.e6 pc
        image = "S1"
        bands = ['F160W', 'F105W', 'F125W']
        # bands = ('F160W','F140W','F105W', 'F125W')
        curves = snrefsdal.read_curves(snrefsdal.path_data, image)
        for bname in bands:
            lc = curves.get(bname)
            # lc.mshift = dm
            t = lc.Time
            y = lc.Mag
            yerr = lc.MagErr

            #  Gaussian process
            k = gptools.SquaredExponentialKernel(param_bounds=[(min(np.abs(y)), max(np.abs(y))),
                                                               (0, np.std(t))])
            # k = gptools.SquaredExponentialKernel(param_bounds=[(min(np.abs(y)), max(np.abs(y))),
            #                                                    (0, np.std(t))])
            gp = gptools.GaussianProcess(k)
            # gp = gptools.GaussianProcess(k, mu=gptools.LinearMeanFunction())
            gp.add_data(t, y, err_y=yerr)

            is_mcmc = True
            if is_mcmc:
                out = gp.predict(t, use_MCMC=True, full_MCMC=True, return_std=True,
                                 num_proc=0, nsamp=100,
                                 plot_posterior=True, plot_chains=False,
                                 burn=10, thin=1)

            else:
                gp.optimize_hyperparameters()
                out = gp.predict(t, use_MCMC=False)
            y_star, err_y_star = out
            # y_star, err_y_star = gp.predict(t)

            fig = plt.figure()
            ax = fig.add_axes((0.1, 0.3, 0.8, 0.65))
            ax.invert_yaxis()

            bcolor = band.bands_colors()[bname]
            ax.plot(t, y, color=bcolor, label='L bol', lw=2.5)
            ax.errorbar(t, y, yerr=yerr, fmt='o', color=bcolor, label='%s obs.')

            #
            # ax.plot(t, y_star, color='red', ls='--', lw=1.5, label='GP')
            ax.plot(t, y_star, '-', color=bcolor)
            ax.fill_between(t, y_star - 2 * err_y_star, y_star + 2 * err_y_star, color=bcolor, alpha=0.3)

        plt.show()
コード例 #2
0
    def test_fit_GP_SNRefsdal_all_lc(self):
        # k = gptools.SquaredExponentialKernel(param_bounds=[(0, 1e3), (0, 100)])
        # gp = gptools.GaussianProcess(k, mu=gptools.LinearMeanFunction())

        # add data
        dm = -29.38  # D = 7.5e6 pc
        # dm = -30.4  # D = 12.e6 pc
        image = "S1"
        bands = ['F160W', 'F105W', 'F125W']
        # bands = ('F160W','F140W','F105W', 'F125W')
        curves = snrefsdal.read_curves(snrefsdal.path_data, image)
        for bname in bands:
            lc = curves.get(bname)
            # lc.mshift = dm
            t = lc.Time
            y = lc.Mag
            yerr = lc.Err

            #  Gaussian process
            k = gptools.SquaredExponentialKernel(param_bounds=[(min(np.abs(y)), max(np.abs(y))),
                                                               (0, np.std(t))])
            # k = gptools.SquaredExponentialKernel(param_bounds=[(min(np.abs(y)), max(np.abs(y))),
            #                                                    (0, np.std(t))])
            gp = gptools.GaussianProcess(k)
            # gp = gptools.GaussianProcess(k, mu=gptools.LinearMeanFunction())
            gp.add_data(t, y, err_y=yerr)

            is_mcmc = True
            if is_mcmc:
                out = gp.predict(t, use_MCMC=True, full_MCMC=True, return_std=True,
                                 num_proc=0, nsamp=100,
                                 plot_posterior=True, plot_chains=False,
                                 burn=10, thin=1)

            else:
                gp.optimize_hyperparameters()
                out = gp.predict(t, use_MCMC=False)
            y_star, err_y_star = out
            # y_star, err_y_star = gp.predict(t)

            fig = plt.figure()
            ax = fig.add_axes((0.1, 0.3, 0.8, 0.65))
            ax.invert_yaxis()

            bcolor = band.colors()[bname]
            ax.plot(t, y, color=bcolor, label='L bol', lw=2.5)
            ax.errorbar(t, y, yerr=yerr, fmt='o', color=bcolor, label='%s obs.')

            #
            # ax.plot(t, y_star, color='red', ls='--', lw=1.5, label='GP')
            ax.plot(t, y_star, '-', color=bcolor)
            ax.fill_between(t, y_star - 2 * err_y_star, y_star + 2 * err_y_star, color=bcolor, alpha=0.3)

        plt.show()
コード例 #3
0
ファイル: test_gp.py プロジェクト: baklanovp/pystella
    def test_fit_GP_SNRefsdal(self):
        # k = gptools.SquaredExponentialKernel(param_bounds=[(0, 1e3), (0, 100)])
        # gp = gptools.GaussianProcess(k, mu=gptools.LinearMeanFunction())

        # add data
        dm = -29.38  # D = 7.5e6 pc
        # dm = -30.4  # D = 12.e6 pc
        image = "S1"
        bname = 'F160W'
        curves = snrefsdal.read_curves(snrefsdal.path_data, image)
        lc = curves.get(bname)
        # lc.mshift = dm
        t = lc.Time
        y = lc.Mag
        yerr = lc.MagErr

        #  Gaussian process
        k = gptools.SquaredExponentialKernel(param_bounds=[(0, max(np.abs(y))),
                                                           (0, np.std(t))])
        # k = gptools.SquaredExponentialKernel(param_bounds=[(min(np.abs(y)), max(np.abs(y))),
        #                                                    (0, np.std(t))])
        gp = gptools.GaussianProcess(k)
        # gp = gptools.GaussianProcess(k, mu=gptools.LinearMeanFunction())
        gp.add_data(t, y, err_y=yerr)

        gp.optimize_hyperparameters()
        y_star, err_y_star = gp.predict(t)

        fig = plt.figure()
        ax = fig.add_axes((0.1, 0.3, 0.8, 0.65))
        ax.invert_yaxis()

        ax.plot(t, y, color='blue', label='L bol', lw=2.5)
        ax.errorbar(t, y, yerr=yerr, fmt='o', color='blue', label='%s obs.')

        #
        # ax.plot(t, y_star, color='red', ls='--', lw=1.5, label='GP')
        ax.plot(t, y_star, '-', color='gray')
        ax.fill_between(t, y_star - 2 * err_y_star, y_star + 2 * err_y_star, color='gray', alpha=0.3)

        plt.show()
コード例 #4
0
    def test_fit_GP_SNRefsdal(self):
        # k = gptools.SquaredExponentialKernel(param_bounds=[(0, 1e3), (0, 100)])
        # gp = gptools.GaussianProcess(k, mu=gptools.LinearMeanFunction())

        # add data
        dm = -29.38  # D = 7.5e6 pc
        # dm = -30.4  # D = 12.e6 pc
        image = "S1"
        bname = 'F160W'
        curves = snrefsdal.read_curves(snrefsdal.path_data, image)
        lc = curves.get(bname)
        # lc.mshift = dm
        t = lc.Time
        y = lc.Mag
        yerr = lc.Err

        #  Gaussian process
        k = gptools.SquaredExponentialKernel(param_bounds=[(0, max(np.abs(y))),
                                                           (0, np.std(t))])
        # k = gptools.SquaredExponentialKernel(param_bounds=[(min(np.abs(y)), max(np.abs(y))),
        #                                                    (0, np.std(t))])
        gp = gptools.GaussianProcess(k)
        # gp = gptools.GaussianProcess(k, mu=gptools.LinearMeanFunction())
        gp.add_data(t, y, err_y=yerr)

        gp.optimize_hyperparameters()
        y_star, err_y_star = gp.predict(t)

        fig = plt.figure()
        ax = fig.add_axes((0.1, 0.3, 0.8, 0.65))
        ax.invert_yaxis()

        ax.plot(t, y, color='blue', label='L bol', lw=2.5)
        ax.errorbar(t, y, yerr=yerr, fmt='o', color='blue', label='%s obs.')

        #
        # ax.plot(t, y_star, color='red', ls='--', lw=1.5, label='GP')
        ax.plot(t, y_star, '-', color='gray')
        ax.fill_between(t, y_star - 2 * err_y_star, y_star + 2 * err_y_star, color='gray', alpha=0.3)

        plt.show()
コード例 #5
0
ファイル: test_gp.py プロジェクト: baklanovp/pystella
    def test_scikit_GP_SNRefsdal(self):
        from sklearn.gaussian_process import GaussianProcessRegressor
        from sklearn.gaussian_process.kernels import RBF, ConstantKernel as C

        # add data
        dm = -29.38  # D = 7.5e6 pc
        # dm = -30.4  # D = 12.e6 pc
        image = "S1"
        bname = 'F160W'
        curves = snrefsdal.read_curves(snrefsdal.path_data, image)
        lc = curves.get(bname)
        # lc.mshift = dm
        t = lc.Time
        y = lc.Mag
        yerr = lc.MagErr
        #
        # Instanciate a Gaussian Process model
        # kernel = C(1.0, (1e-3, 1e3)) * RBF(10, (1e-2, 1e2))
        # Instanciate a Gaussian Process model
        # kernel = C(1.0, (1e-3, 1e3)) * RBF(10, (1e-2, 1e2))
        # gp = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=9)
        gp = GaussianProcessRegressor()

        # Fit to data using Maximum Likelihood Estimation of the parameters
        X = np.atleast_2d(t).T
        gp.fit(X, y)

        # gp = GaussianProcessRegressor()

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

        # Make the prediction on the meshed x-axis (ask for MSE as well)
        # y_star, err_y_star = gp.predict(t, return_std=True)
        # Make the prediction on the meshed x-axis (ask for MSE as well)
        y_pred, sigma = gp.predict(t, return_std=True)

        # k = gptools.SquaredExponentialKernel(param_bounds=[(min(np.abs(y)), max(np.abs(y))),
        #                                                    (0, np.std(t))])
        # # k = gptools.SquaredExponentialKernel(param_bounds=[(min(np.abs(y)), max(np.abs(y))),
        # #                                                    (0, np.std(t))])
        # gp = gptools.GaussianProcess(k)
        # # gp = gptools.GaussianProcess(k, mu=gptools.LinearMeanFunction())
        # gp.add_data(t, y, err_y=yerr)
        #
        # gp.optimize_hyperparameters()
        # y_star, err_y_star = gp.predict(t)

        fig = plt.figure()
        ax = fig.add_axes((0.1, 0.3, 0.8, 0.65))
        ax.invert_yaxis()

        ax.plot(t, y, color='blue', label='L bol', lw=2.5)
        ax.errorbar(t, y, yerr=yerr, fmt='o', color='blue', label='%s obs.')

        #
        # ax.plot(t, y_star, color='red', ls='--', lw=1.5, label='GP')
        ax.plot(t, y_pred, '-', color='gray')
        # ax.fill_between(t, y_star - 2 * err_y_star, y_star + 2 * err_y_star, color='gray', alpha=0.3)
        ax.fill(np.concatenate([t, t[::-1]]),
                np.concatenate([y_pred - 1.9600 * sigma,
                                (y_pred + 1.9600 * sigma)[::-1]]),
                alpha=.5, fc='b', ec='None', label='95% confidence interval')

        plt.show()
コード例 #6
0
    def test_scikit_GP_SNRefsdal(self):
        from sklearn.gaussian_process import GaussianProcessRegressor
        from sklearn.gaussian_process.kernels import RBF, ConstantKernel as C

        # add data
        dm = -29.38  # D = 7.5e6 pc
        # dm = -30.4  # D = 12.e6 pc
        image = "S1"
        bname = 'F160W'
        curves = snrefsdal.read_curves(snrefsdal.path_data, image)
        lc = curves.get(bname)
        # lc.mshift = dm
        t = lc.Time
        y = lc.Mag
        yerr = lc.Err
        #
        # Instanciate a Gaussian Process model
        # kernel = C(1.0, (1e-3, 1e3)) * RBF(10, (1e-2, 1e2))
        # Instanciate a Gaussian Process model
        # kernel = C(1.0, (1e-3, 1e3)) * RBF(10, (1e-2, 1e2))
        # gp = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=9)
        gp = GaussianProcessRegressor()

        # Fit to data using Maximum Likelihood Estimation of the parameters
        X = np.atleast_2d(t).T
        gp.fit(X, y)

        # gp = GaussianProcessRegressor()

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

        # Make the prediction on the meshed x-axis (ask for MSE as well)
        # y_star, err_y_star = gp.predict(t, return_std=True)
        # Make the prediction on the meshed x-axis (ask for MSE as well)
        y_pred, sigma = gp.predict(t, return_std=True)

        # k = gptools.SquaredExponentialKernel(param_bounds=[(min(np.abs(y)), max(np.abs(y))),
        #                                                    (0, np.std(t))])
        # # k = gptools.SquaredExponentialKernel(param_bounds=[(min(np.abs(y)), max(np.abs(y))),
        # #                                                    (0, np.std(t))])
        # gp = gptools.GaussianProcess(k)
        # # gp = gptools.GaussianProcess(k, mu=gptools.LinearMeanFunction())
        # gp.add_data(t, y, err_y=yerr)
        #
        # gp.optimize_hyperparameters()
        # y_star, err_y_star = gp.predict(t)

        fig = plt.figure()
        ax = fig.add_axes((0.1, 0.3, 0.8, 0.65))
        ax.invert_yaxis()

        ax.plot(t, y, color='blue', label='L bol', lw=2.5)
        ax.errorbar(t, y, yerr=yerr, fmt='o', color='blue', label='%s obs.')

        #
        # ax.plot(t, y_star, color='red', ls='--', lw=1.5, label='GP')
        ax.plot(t, y_pred, '-', color='gray')
        # ax.fill_between(t, y_star - 2 * err_y_star, y_star + 2 * err_y_star, color='gray', alpha=0.3)
        ax.fill(np.concatenate([t, t[::-1]]),
                np.concatenate([y_pred - 1.9600 * sigma,
                                (y_pred + 1.9600 * sigma)[::-1]]),
                alpha=.5, fc='b', ec='None', label='95% confidence interval')

        plt.show()