Equality Constraints#
[1]:
import hopsy
import numpy as np
import matplotlib.pyplot as plt
Basic example without qquality constraints#
[2]:
# create 3D rectangle
A = np.array(
[
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
[-1.0, 0.0, 0.0],
[0.0, -1.0, 0.0],
[0.0, 0.0, -1.0],
]
)
b = np.array([1.0, 2.0, 3.0, 1.0, 2.0, 3.0])
rectangle = hopsy.Problem(A=A, b=b)
[3]:
mc = hopsy.MarkovChain(rectangle)
rng = hopsy.RandomNumberGenerator(42)
_, samples = hopsy.sample(mc, rng, n_samples=20000, thinning=10)
[4]:
print(samples)
[[[-0.43426088 0.42225266 0.10534782]
[-0.06525985 0.06436831 -2.46131662]
[-0.05369373 1.80750632 0.80462979]
...
[ 0.21379724 -1.72031362 -2.44581382]
[ 0.98883212 -0.25319745 2.33836859]
[-0.71434968 -0.06717306 1.55590868]]]
[5]:
plt.figure()
plt.title("first dimension")
plt.hist(samples[0, :, 0])
plt.figure()
plt.title("second dimension")
plt.hist(samples[0, :, 1])
plt.figure()
plt.title("third dimension")
plt.hist(samples[0, :, 2])
plt.show()



Now constraint rectangle to 2D by fixing third dimension at 2.5#
[6]:
A_eq = np.array(
[
[0.0, 0.0, 1.0]
]
)
b_eq = np.array([2.5])
constrained_rectangle = hopsy.add_equality_constraints(rectangle, A_eq=A_eq, b_eq=b_eq)
[7]:
mc = hopsy.MarkovChain(constrained_rectangle)
rng = hopsy.RandomNumberGenerator(43)
_, constrained_samples = hopsy.sample(mc, rng, n_samples=20000, thinning=10)
[8]:
plt.figure()
plt.title("first dimension constrained")
plt.hist(constrained_samples[0, :, 0])
plt.figure()
plt.title("second dimension constrained")
plt.hist(constrained_samples[0, :, 1])
plt.figure()
plt.title("third dimension constrained (=2.5)")
plt.hist(constrained_samples[0, :, 2])
plt.show()



[ ]: