Пример #1
0
 def coupled_model(forcingtotal, doeclim_out, temperatures, model,
                   parameters):
     if self.aerosol_scaling.value:
         forcingtotal = forcing_total.forcing_total(
             forcing=forcing,
             alpha_doeclim=parameters[7],
             l_project=False,
             begyear=mod_time[0],
             endyear=np.max(mod_time))
     if self.climate_sensitivity.value and self.ocean_vertical_diffusivity.value:
         doeclim_out = doeclimF.doeclimF(forcingtotal,
                                         self.mod_time,
                                         S=parameters[5],
                                         kappa=parameters[6])
     #temperatures = np.array((doeclim_out.loc[(doeclim_out["time"]>=1880) & (doeclim_out["time"]<=2008), "temp"]).tolist())
     temperatures = temperatures + parameters[8]
     model = gmsl_model.gmsl_model(parameters, temperatures, deltat)
Пример #2
0
    def chain(self, deltat, N=10000):
        theta = np.array(self.values)
        print('Initial estimate for parameters -', theta)
        forcingtotal = forcing_total.forcing_total(
            forcing=self.forcing,
            alpha_doeclim=self.values[7],
            l_project=False,
            begyear=self.mod_time[0],
            endyear=np.max(self.mod_time))
        doeclim_out = doeclimF.doeclimF(forcingtotal,
                                        self.mod_time,
                                        S=self.values[5],
                                        kappa=self.values[6])
        temp_out = np.array(
            (doeclim_out.loc[(doeclim_out["time"] >= 1880) &
                             (doeclim_out["time"] <= 2008), "temp"]).tolist())
        ocheat = np.array((doeclim_out.loc[(doeclim_out["time"] >= 1880) &
                                           (doeclim_out["time"] <= 2008),
                                           'ocheat.mixed']).tolist())
        ocheat = ocheat[self.offset[0]:self.offset[0] + 44]
        temp_out += self.T_0
        gmsl_out = gmsl_model.gmsl_model(theta, temp_out, 1)

        lp = self.logp(theta, deltat, temp_out, gmsl_out, ocheat)
        theta_best = theta
        lp_max = lp
        theta_new = [0.] * len(theta)
        accepts = 0
        mcmc_chains = np.zeros((N, (len(theta))))
        step = np.diag(self.stepsizes)
        sd = 2.38**2 / len(theta)
        #step = 0.5 * step

        #Check if converged. If not keep running.
        print(N)
        for i in (range(N)):
            #print(i)
            if i > 500: step = self.update_cov(mcmc_chains[:i], sd, len(theta))
            theta_new = list(np.random.multivariate_normal(theta, step))
            doeclim_out = doeclimF.doeclimF(forcingtotal,
                                            self.mod_time,
                                            S=theta[5],
                                            kappa=theta[6])
            temp_out = np.array(
                (doeclim_out.loc[(doeclim_out["time"] >= 1880) &
                                 (doeclim_out["time"] <= 2008),
                                 "temp"]).tolist())
            temp_out += theta[8]
            ocheat = np.array((doeclim_out.loc[(doeclim_out["time"] >= 1880) &
                                               (doeclim_out["time"] <= 2008),
                                               'ocheat.mixed']).tolist())
            gmsl_out = gmsl_model.gmsl_model(theta, temp_out, 1)
            ocheat = ocheat[self.offset[0]:self.offset[0] + 44]
            lp_new = self.logp(theta_new, deltat, temp_out, gmsl_out, ocheat)
            if np.isinf(lp_new):
                mcmc_chains[i, :] = theta
                continue
            #print(lp, lp_new, 'difference')
            lq = np.abs(lp_new - lp)

            lr = np.log(np.random.uniform(1, 2))
            #print(lr, lq)
            if (lr < lq):
                theta = theta_new
                lp = lp_new
                accepts += 1
                #print('NEW VALUE')
                if lp > lp_max:
                    theta_best = theta
                    lp_max = lp
            mcmc_chains[i, :] = theta

        return mcmc_chains, accepts / N * 100
Пример #3
0
def chain(parameters,
          temperatures,
          deltat,
          sealevel,
          sealevel_sigma,
          pamnames,
          N=10000):
    if 'aerosol_scaling' in pamnames:
        forcingtotal = forcing_total.forcing_total(
            forcing=forcing,
            alpha_doeclim=aerosol_scaling,
            l_project=False,
            begyear=mod_time[0],
            endyear=np.max(mod_time))
    if 'climate_sensitivity' in pamnames and 'ocean_vertical_diffusivity' in pamnames:
        doeclim_out = doeclimF.doeclimF(forcingtotal,
                                        mod_time,
                                        S=climate_sensitivity,
                                        kappa=ocean_vertical_diffusivity)
        temperatures = np.array(
            (doeclim_out.loc[(doeclim_out["time"] >= 1880) &
                             (doeclim_out["time"] <= 2008), "temp"]).tolist())
    temperatures = temperatures + T_0
    theta = parameters
    print('Initial estimate for parameters -', theta)

    model = gmsl_model.gmsl_model(theta, temperatures,
                                  deltat) if 'alpha' in pamnames else []

    lp = logp(theta,
              sealevel,
              deltat,
              temperatures,
              model,
              pamnames,
              sigma=sealevel_sigma)
    theta_best = theta
    lp_max = lp
    theta_new = [0.] * len(parameters)
    accepts = 0
    mcmc_chains = np.array([np.zeros(len(parameters))] * N)
    stepsizes = {
        'alpha': .01,
        'Teq': .05,
        'S0': 5,
        'rho': .001,
        'sigma_ar': .1,
        'climate_sensitivity': 0.16,
        'ocean_vertical_diffusivity': 0.17,
        'aerosol_scaling': 0.025,
        'T_0': 0.03,
        'sigma_T': 5e-4,
        'rho_T': 0.007
    }
    step = []
    count = 0
    for i in pamnames:
        temp = [0] * len(pamnames)
        temp[count] = stepsizes[i]
        count += 1
        step.append(temp)
    step = np.array(step)
    #step = np.array([[.01,0,0,0,0,0,0,0,0], [0,.05,0,0,0,0,0,0,0], [0,0,5,0,0,0,0,0,0], [0,0,0,0.001,0,0,0,0,0],[0,0,0,0,0.1,0,0,0,0],
    #				 [0,0,0,0,0,0.16,0,0,0], [0,0,0,0,0,0,0.17,0,0], [0,0,0,0,0,0,0,0.025,0],[0,0,0,0,0,0,0,0,0.03]])
    sd = 2.38**2 / len(theta)
    #Check if converged. If not keep running.
    print(N)
    print(theta)
    for i in tqdm(range(N)):
        if i > 500: step = update_cov(mcmc_chains[:i], sd, len(parameters))
        theta_new = list(np.random.multivariate_normal(theta, step))
        if len(pamnames) > 10:
            coupled_model(forcingtotal, doeclim_out, temperatures, model,
                          parameters, mod_time, T_0, pamnames)
        model = gmsl_model.gmsl_model(theta, temperatures,
                                      deltat) if 'alpha' in pamnames else []

        #Separate above 3 lines + T_0 into a new file.
        lp_new = logp(theta_new,
                      sealevel,
                      deltat,
                      temperatures,
                      model,
                      pamnames,
                      sigma=sealevel_sigma)
        lq = lp_new - lp

        lr = np.math.log(np.random.uniform(0, 1))
        #print(lr, lq)
        if (lr < lq):
            theta = theta_new
            lp = lp_new
            accepts += 1
            if lp > lp_max:
                theta_best = theta
                lp_max = lp
        mcmc_chains[i] = theta

    return mcmc_chains, accepts / N * 100
Пример #4
0
    def chain(self, deltat, N=10000):
        if self.aerosol_scaling.value:
            forcingtotal = forcing_total.forcing_total(
                forcing=forcing,
                alpha_doeclim=self.aerosol_scaling,
                l_project=False,
                begyear=mod_time[0],
                endyear=np.max(mod_time))
        if self.climate_sensitivity.value and self.ocean_vertical_diffusivity.value:
            doeclim_out = doeclimF.doeclimF(forcingtotal,
                                            self.mod_time,
                                            S=self.climate_sensitivity.value,
                                            kappa=ocean_vertical_diffusivity)
        temperatures = self.temperatures + self.T_0.value
        parameters = self.values.values()
        parameters = list(filter(lambda a: a != 0, parameters))
        self.num = len(parameters)
        theta = parameters
        print('Initial estimate for parameters -', theta)

        model = gmsl_model.gmsl_model(theta, temperatures,
                                      deltat) if self.alpha.value else []

        lp = self.logp(theta, deltat, temperatures, model)
        theta_best = theta
        lp_max = lp
        theta_new = [0.] * len(parameters)
        accepts = 0
        mcmc_chains = np.array([np.zeros(len(parameters))] * N)
        step = []
        count = 0
        temp = self.stepsizes.values()
        for i in range(len(parameters)):
            temp = [0] * (len(parameters))
            temp[count] = temp[i]
            count += 1
            step.append(temp)
        step = np.array(step)
        sd = 2.38**2 / len(theta)
        #Check if converged. If not keep running.
        print(N)
        print(theta)
        for i in tqdm(range(N)):
            if i > 500:
                step = self.update_cov(mcmc_chains[:i], sd, len(parameters))
            theta_new = list(np.random.multivariate_normal(theta, step))
            if len(parameters) > 10:
                self.coupled_model(forcingtotal, doeclim_out, temperatures,
                                   model, parameters)
            model = gmsl_model.gmsl_model(theta, temperatures,
                                          deltat) if self.alpha.value else []

            lp_new = self.logp(theta_new, deltat, temperatures, model)
            lq = lp_new - lp

            lr = np.log(np.random.uniform(0, 1))
            #print(lr, lq)
            if (lr < lq):
                theta = theta_new
                lp = lp_new
                accepts += 1
                if lp > lp_max:
                    theta_best = theta
                    lp_max = lp
            mcmc_chains[i] = theta

        return mcmc_chains, accepts / N * 100
Пример #5
0
    def chain(self, deltat, N=10000):
        theta = np.array(self.values)
        print('Initial estimate for parameters -', theta)
        forcingtotal = forcing_total.forcing_total(
            forcing=self.forcing,
            alpha_doeclim=self.values[7],
            l_project=False,
            begyear=self.mod_time[0],
            endyear=np.max(self.mod_time))
        doeclim_out = doeclimF.doeclimF(forcingtotal,
                                        self.mod_time,
                                        S=self.values[5],
                                        kappa=self.values[6])
        temp_out = np.array(
            (doeclim_out.loc[(doeclim_out["time"] >= 1880) &
                             (doeclim_out["time"] <= 2008), "temp"]).tolist())
        ocheat = np.array((doeclim_out.loc[(doeclim_out["time"] >= 1880) &
                                           (doeclim_out["time"] <= 2008),
                                           'ocheat.mixed']).tolist())
        ocheat = ocheat[self.offset[0]:self.offset[0] + 44]
        temp_out += self.T_0
        gmsl_out = gmsl_model.gmsl_model(theta, temp_out, 1)
        lp = self.logp(theta, deltat, self.dfTemperature, gmsl_out, ocheat)
        theta_best = theta
        lp_max = lp
        theta_new = [0.] * len(theta)
        accepts = 0
        mcmc_chains = np.zeros((N, (len(theta))))
        step = np.diag(self.stepsizes)
        sd = 2.38**2 / len(theta)
        #step = 0.5 * step

        #Check if converged. If not keep running.
        print(N)
        for i in (range(N)):
            if i > 1 and not i % 1000:
                pd.DataFrame(mcmc_chains).to_csv('array_uncorr.csv')
            #print(i)
            if i > 500: step = self.update_cov(mcmc_chains[:i], sd, len(theta))
            if not i % 200: print(accepts)
            theta_new = list(np.random.multivariate_normal(theta, step))
            theta_new[5:] = [3.1, 3.5, 1.1, -0.06, 0.1, 0.55, 3, 0.5]
            #temp_out, heatflux_mixed_out, heatflux_interior_out, gmsl_out = \
            #doeclim_gmsl(asc = theta[7], t2co_in = theta[5], kappa_in=theta[6], alphasl_in = theta[0], Teq = theta[1], SL0 = theta[2], forcing=self.forcing)
            #temp_out += theta[8]
            #ocheat = self.flux_to_heat(heatflux_mixed_out, 	heatflux_interior_out)
            #ocheat = ocheat[self.offset[0]:self.offset[0]+ 44]
            doeclim_out = doeclimF.doeclimF(forcingtotal,
                                            self.mod_time,
                                            S=theta[5],
                                            kappa=theta[6])
            temp_out = np.array(
                (doeclim_out.loc[(doeclim_out["time"] >= 1880) &
                                 (doeclim_out["time"] <= 2008),
                                 "temp"]).tolist())
            temp_out += theta[8]
            ocheat = np.array((doeclim_out.loc[(doeclim_out["time"] >= 1880) &
                                               (doeclim_out["time"] <= 2008),
                                               'ocheat.mixed']).tolist())
            gmsl_out = gmsl_model.gmsl_model(theta, self.dfTemperature, 1)
            ocheat = ocheat[self.offset[0]:self.offset[0] + 44]
            lp_new = self.logp(theta_new, deltat, temp_out, gmsl_out, ocheat)
            if np.isinf(lp_new):
                mcmc_chains[i, :] = theta
                continue
            #print(lp, lp_new, 'difference')
            lq = lp_new - lp

            lr = np.log(np.random.uniform(0, 1))
            #print(lr, lq)
            if (lr < lq):
                theta = theta_new
                lp = lp_new
                accepts += 1
                #print('NEW VALUE')
                if lp > lp_max:
                    theta_best = theta
                    lp_max = lp
            mcmc_chains[i, :] = theta

        return mcmc_chains, accepts / N * 100