A simple parameter space consisting of two continuous parameters¶
Copyright (c) 2021 - for information on the respective copyright owner see the NOTICE file and/or the repository https://github.com/boschresearch/parameterspace
SPDX-License-Identifier: Apache-2.0
import numpy as np
import matplotlib.pyplot as plt
import parameterspace
Here, we actually instantiate the parameters and add them to the space
f1_prior = parameterspace.priors.Beta(a=2, b=1)
f2_prior = parameterspace.priors.TruncatedNormal(mean=0.8, std=.3)
f1 = parameterspace.ContinuousParameter(name='f_1', bounds=[0., 1.], prior=f1_prior)
f2 = parameterspace.ContinuousParameter(name='f_2', bounds=[-5., 5.], prior=f2_prior)
s = parameterspace.ParameterSpace()
s.add(f1)
s.add(f2)
print(s)
The space can also produce samples, but (for now) only one sample at a time!
The method to_numerical
converts a configuration (which is a dictionary) into a numpy array.
Let's draw some samples and plot the loglikelihood for this 2d space. Note that the ranges are [0,1] for both dimensions, because we plot everything in the transformed domain. We only do this here for simplicity, because working with the true values would require logic with dictionaries rather than numpy arrays, which are simpler here.
samples = np.array([s.to_numerical(s.sample()) for i in range(256)])
X,Y = np.meshgrid(np.linspace(0,1, 32, endpoint=True), np.linspace(0, 1, 32, endpoint=True))
ll =np.array([s.log_likelihood_numerical([x,y]) for x,y in zip(X.flatten(), Y.flatten())]).reshape(X.shape)
fig, ax = plt.subplots()
cs = ax.contourf(X,Y, ll)
ax.scatter(samples[:,0], samples[:,1])
fig.colorbar(cs)
plt.show()