Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def gaussian_problem():
def nllh(x):
return - gaussian_llh(x)
objective = pypesto.Objective(fun=nllh)
problem = pypesto.Problem(objective=objective, lb=[-10], ub=[10])
return problem
def create_problem():
objective = rosen_for_sensi(2)['obj']
lb = [-3, -3, -3, -3, -3]
ub = [3, 3, 3, 3, 3]
x_fixed_indices = [1, 3]
x_fixed_vals = [1, 1]
problem = pypesto.Problem(objective=objective,
lb=lb, ub=ub,
x_fixed_indices=x_fixed_indices,
x_fixed_vals=x_fixed_vals)
return problem
def create_problem():
# define a pypesto objective
objective = pypesto.Objective(fun=so.rosen,
grad=so.rosen_der,
hess=so.rosen_hess)
# define a pypesto problem
(lb, ub) = create_bounds()
problem = pypesto.Problem(objective=objective, lb=lb, ub=ub)
return problem
def create_optimization_results(objective):
# create optimizer, pypesto problem and options
options = {
'maxiter': 200
}
optimizer = optimize.ScipyOptimizer(method='TNC', options=options)
lb = -2 * np.ones(2)
ub = 2 * np.ones(2)
problem = pypesto.Problem(objective, lb, ub)
optimize_options = optimize.OptimizeOptions(allow_failed_starts=True)
# run optimization
result = optimize.minimize(
problem=problem,
optimizer=optimizer,
n_starts=5,
startpoint_method=pypesto.startpoint.uniform,
options=optimize_options
)
return problem, result, optimizer
def test_prior():
"""Check that priors are defined for sampling."""
# define negative log posterior
posterior_fun = pypesto.Objective(fun=negative_log_posterior)
# define negative log prior
prior_fun = pypesto.Objective(fun=negative_log_prior)
# define pypesto prior object
prior_object = pypesto.NegLogPriors(objectives=[prior_fun])
# define pypesto problem using prior object
test_problem = pypesto.Problem(objective=posterior_fun,
x_priors_defs=prior_object,
lb=-10, ub=10,
x_names=['x'])
sampler = sample.AdaptiveMetropolisSampler()
result = sample.sample(test_problem, n_samples=1e4, sampler=sampler,
x0=np.array([0.]))
# get log prior values of first chain
logprior_trace = -result.sample_result.trace_neglogprior[0, :]
# check that not all entries are zero
assert (logprior_trace != 0.).any()
# get samples of first chain
filename = ".test_logging.tmp"
pypesto.logging.log_to_file(logging.DEBUG, filename)
logger = logging.getLogger('pypesto')
if os.path.exists(filename):
os.remove(filename)
fh = logging.FileHandler(filename)
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
logger.info("start test")
# problem definition
def fun(_):
raise Exception("This function cannot be called.")
objective = pypesto.Objective(fun=fun)
problem = pypesto.Problem(objective, -1, 1)
optimizer = pypesto.optimize.ScipyOptimizer()
options = {'allow_failed_starts': True}
# optimization
pypesto.optimize.minimize(problem, optimizer, 5, options=options)
# assert logging worked
assert os.path.exists(filename)
f = open(filename, 'rb')
content = str(f.read())
f.close()
# tidy up
os.remove(filename)
def create_optimal_surrogate_problem(xs, edatas, rdatas, problem):
# create paramter vector for optimal scaling bounds sorted like the category numbers with lower_bound, upper_bound
# create constraints for gaps between intervals and bound size
parameter_length = 2*len(xs)
lb = -np.inf * np.ones((parameter_length, 1))
ub = np.inf * np.ones((parameter_length, 1))
constraints = get_constraints_for_optimization(xs, rdatas, problem)
obj_surr = lambda x: obj_surrogate_data(xs, x,edatas, rdatas)
obj = pypesto.Objective(fun=obj_surr, grad=False, hess=False)
problem = pypesto.Problem(objective=obj, lb=lb, ub=ub, constraints=constraints) # TODO: Implement constraints in pyPESTO
return problem