Exemplo n.º 1
0
    # Calculate prior
    log_prior = tf.constant(0.0, dtype=tf.float64)
    # Add the jacobian to the prior
    for tensor in var:
        if tensor.log_prior is not None:
            log_prior += tensor.log_prior
            
    log_prob = - 0.5 * chi2_tensor + log_prior
    
    model = TFModel(log_prob, var_list=var_list, feed_dict=feed_dict)
    model.setup()
    coords = model.current_vector()

    metric = hemcee.metric.DenseMetric(np.eye(len(coords)))
    step = hemcee.step_size.VariableStepSize()
    sampler = hemcee.NoUTurnSampler(model.value, model.gradient, step_size=step, metric=metric)
    
    # Burn-in
    results = sampler.run_warmup(coords, 1500, tune_metric=True)
    
    # Run the sampler
    coords_chain, logprob_chain = sampler.run_mcmc(
            results[0], 
            5000, 
            initial_log_prob=results[1], 
            var_names='', 
            plot=False, update_interval=100, 
            tune=False
            )
    
    plt.plot([coord[0] for coord in coords_chain])
Exemplo n.º 2
0
 def sampler(self):
     self._sampler = hemcee.NoUTurnSampler(self.tf_model.value,
                                           self.tf_model.gradient,
                                           step_size=self._step)
     return self._sampler
Exemplo n.º 3
0
    def __init__(self,
                 time,
                 mag,
                 nu=None,
                 rvs=None,
                 log_sigma2=None,
                 session=None,
                 max_peaks=9,
                 **kwargs):
        self.time_data = np.atleast_1d(time)
        self.mag_data = np.atleast_1d(mag)

        self.rvs = rvs or []

        # Estimate frequencies if none are supplied
        if nu is None:
            nu = estimate_frequencies(time, mag, max_peaks=max_peaks)
        self.nu_data = np.atleast_1d(nu)

        # Placeholder tensors for time and mag data
        self.time = tf.constant(self.time_data, dtype=self.T)
        self.mag = tf.constant(self.mag_data, dtype=self.T)

        self._session = session

        # Parameters
        if log_sigma2 is None:
            log_sigma2 = 0.0
        self.log_sigma2 = tf.Variable(log_sigma2,
                                      dtype=self.T,
                                      name="log_sigma2")
        self.nu = tf.Variable(self.nu_data, dtype=self.T, name="frequencies")

        self.setup_orbit_model(**kwargs)

        arg = 2.0 * np.pi * self.nu[None, :] * (self.time[:, None] - self.tau)
        D = tf.concat([
            tf.cos(arg),
            tf.sin(arg),
            tf.ones((len(self.time_data), 1), dtype=self.T)
        ],
                      axis=1)

        # Solve for the amplitudes and phases of the oscillations
        DTD = tf.matmul(D, D, transpose_a=True)
        DTy = tf.matmul(D, self.mag[:, None], transpose_a=True)
        W_hat = tf.linalg.solve(DTD, DTy)

        # Model and the chi^2 objective:
        self.model_tensor = tf.squeeze(tf.matmul(D, W_hat))
        self.chi2 = tf.reduce_sum(tf.square(self.mag - self.model_tensor))
        self.chi2 *= tf.exp(-self.log_sigma2)
        self.chi2 += len(self.time_data) * self.log_sigma2

        # Add radial velocity to objective
        for rv in self.rvs:
            self.chi2 += tf.reduce_sum(rv.chi)

        # Initialize all the variables
        self.run(tf.global_variables_initializer())

        # Minimal working feed dict for lightcurve
        self._feed_dict = {self.time: self.time_data, self.mag: self.mag_data}

        # Update feed dict with RV data
        for rv in self.rvs:
            self._feed_dict.update({
                rv.time: rv.time_data,
                rv.vel: rv.vel_data,
                rv.err: rv.err_data
            })

        # Wrap tensorflow in hemcee model
        self._tf_model = TFModel(self.ln_prob,
                                 self.params,
                                 self.feed_dict,
                                 session=self._session)

        self._step = hemcee.step_size.VariableStepSize()

        self._sampler = hemcee.NoUTurnSampler(self.tf_model.value,
                                              self.tf_model.gradient,
                                              step_size=self._step)
Exemplo n.º 4
0
model = TFModel(log_prob, var_list)
model.setup(session)

# In[10]:

# We'll use the inverse Hessian to estimate the initial scales of the problem
hess = session.run(tf.hessians(log_prob, var_list))
var = 1.0 / np.abs(np.concatenate([np.diag(np.atleast_2d(h)) for h in hess]))

# In[11]:

import hemcee

metric = hemcee.metric.DenseMetric(np.diag(var))

sampler = hemcee.NoUTurnSampler(model.value, model.gradient, metric=metric)

q, lp = sampler.run_warmup(model.current_vector(), 10000)

# In[12]:

metric.sample_p()

# In[13]:

nuts = sampler.run_mcmc(q, 10000)

# In[14]:

plt.plot(nuts[0][:, 0])