예제 #1
0
def derivative(params, p, output, best_parameters=None, partial=False):
    F = threebody.Fitter(**params)
    if p not in F.parameters:
        raise ValueError("Parameter %s not among options: %s" %
                         (p, F.parameters))
    if best_parameters is not None:
        F.best_parameters = pickle.load(open(best_parameters, "rb"))
    print(F.goodness_of_fit(F.best_parameters))
    if partial:
        vals, names = F.compute_linear_parts()
        for (n, v) in zip(names, vals):
            F.best_parameters[n] = v
    bs = base_steps.get(p, 1e-3)
    sg = numdifftools.MaxStepGenerator(bs, use_exact_steps=True)

    def fres(v):
        bp = F.best_parameters.copy()
        print(p, bp[p], v, end="\t")
        bp[p] += v
        try:
            r = F.residuals(bp, linear_fit=False)
            print(F.goodness_of_fit(bp))
            return r
        except ValueError:
            print(np.inf)
            return np.inf * np.ones_like(F.mjds)

    nl_der = numdifftools.Derivative(fres, step=sg)(0)
    np.save(output, nl_der)
예제 #2
0
def optimize(params,
             best_filename,
             best_parameters=None,
             method="BOBYQA",
             output=None):
    F = threebody.Fitter(**params)
    if (best_parameters is not None and best_parameters != "default"):
        F.best_parameters = pickle.load(open(best_parameters, "rb"))
    M = minuit_wrap.Fitter(F.make_mfun(), output=output)
    for p in F.parameters:
        M.values[p] = F.best_parameters[p]
        M.errors[p] = F.best_errors[p]
    M.set_normalization()
    M.tol = 100
    M.strategy = 1
    M.printMode = 3
    M.best_filename = best_filename

    if method == "MINUIT":
        M.migrad()
    elif method == "Powell":
        M.scipy_minimize("Powell", options=dict(ftol=1e-8))
    elif method == "basinhopping":
        M.scipy_minimize("basinhopping",
                         minimizer_kwargs=dict(method="Powell",
                                               options=dict(ftol=1e-6)))
    elif method == "BOBYQA":
        M.nlopt_minimize()
    elif method == "simplex":
        import nlopt
        M.nlopt_minimize(optimizer=nlopt.LN_SBPLX)
    elif method == "gp":
        M.gp_minimize(n_random_starts=len(F.parameters),
                      n_calls=1000,
                      verbose=True)
    else:
        raise ValueError("Unknown method")
예제 #3
0
# Aargh! but taskset fixes it
# Not needed if using MPI
# os.system("taskset -p 0xffffffff %d" % os.getpid())

trust_nfs = True

n_steps = 100000

logger.debug("Process of rank %s running on host %s",
             os.environ['OMPI_COMM_WORLD_RANK'], os.environ["HOST"])
logger.debug("Environment: %s", os.environ)
logger.debug("creating Fitter")
fn = 'emcee_params.pickle'
fitter_params = pickle.load(open(fn, "rb"))

F = threebody.Fitter(**fitter_params)
logger.debug("Fitter created")
j = 0


def lnprob(offset):
    global j
    logger.debug("call %d" % j)
    j = j + 1
    params = F.best_parameters.copy()
    if len(offset) != len(F.parameters):
        raise ValueError("Parameter mismatch between walker and Fitter")
    for p, o in zip(F.parameters, offset):
        params[p] += o
    if False:
        with open(
예제 #4
0
#!/usr/bin/env python
import os
import cPickle as pickle

import numpy as np
import scipy.linalg

import kepler
import quad_integrate
import threebody

fp = "fake_toas_fitter.pickle"
if os.path.exists(fp):
    d = pickle.load(open(fp, "rb"))
    F = threebody.Fitter(**d)
else:
    F = threebody.Fitter()

rms = 3e-6
days = 1000
n_per_day = 100
times = np.linspace(1, days + 1, n_per_day * days + 1)

o = F.compute_orbit(F.best_parameters)
F_linear = F.compute_linear_parts(F.best_parameters)


def freq(t):
    t0 = F_linear['tzrmjd'] + (F_linear['tzrmjd_base'] - F.base_mjd)
    t_s = (t - t0) * 86400
    return (F_linear['f0'] + F_linear['f1'] * t_s)
예제 #5
0
import numpy as np
import scipy.linalg
import pymc

import minuit

import threebody
import kepler
import quad_integrate
import minuit_wrap

jobid = os.environ['PBS_JOBID']
dbdir = os.path.join('/home/aarchiba/projects/threebody/chains', jobid)
local_dbdir = tempfile.mkdtemp()

F = threebody.Fitter()

# Any distribution I put here is a prior, so let's be broad
#spread = 10000.
#offset = pymc.Normal('offset',
#    mu=np.zeros(len(F.parameters)),
#    tau=spread**(-2)*np.array([F.best_errors[p] for p in F.parameters])**(-2))


# Flat priors! This is not kosher but should be fine
@pymc.stochastic()
def offset(value=np.zeros(len(F.parameters))):
    return 0


@pymc.deterministic(trace=False)
예제 #6
0
def make_fitter(d):
    return threebody.Fitter(**d)
예제 #7
0
import numpy as np
import scipy.linalg

import emcee

import threebody
import optimizer

# For some reason numpy sets the CPU affinity so we only use one processor
# Aargh! but taskset fixes it
#os.system("taskset -p 0xffffffff %d" % os.getpid())

debug = False

F = threebody.Fitter("0337+17-scott-2013-06-06", tzrmjd_middle=True)


def chi2(offset):
    if np.shape(offset) != (len(F.parameters), ):
        raise ValueError("Received an offset of shape %s" %
                         (np.shape(offset), ))
    params = F.best_parameters.copy()
    for p, o in zip(F.parameters, offset):
        params[p] += o
    r = F.residuals(params) / F.phase_uncerts
    return np.sum(r**2)


pool = emcee.utils.MPIPool()
if not pool.is_master():