from numpy import full, ones
from optimix import Function, Scalar
[docs]class OffsetMean(Function):
r"""
Offset mean function, ΞΈβ
π.
It represents a mean vector π¦ = ΞΈβ
π of size n. The offset is given by the parameter
ΞΈ.
Example
-------
.. doctest::
>>> from glimix_core.mean import OffsetMean
>>>
>>> mean = OffsetMean(3)
>>> mean.offset = 2.0
>>> print(mean.value())
[2. 2. 2.]
>>> print(mean.gradient())
{'offset': array([1., 1., 1.])}
>>> mean.name = "π¦"
>>> print(mean)
OffsetMean(): π¦
offset: 2.0
"""
[docs] def __init__(self, n):
"""
Constructor.
Parameters
----------
n : int
Size of the π array.
"""
self._offset = Scalar(0.0)
self._offset.bounds = (-200.0, +200)
self._n = n
Function.__init__(self, "OffsetMean", offset=self._offset)
def fix_offset(self):
"""
Prevent ΞΈ update during optimization.
"""
self._fix("offset")
def unfix_offset(self):
"""
Enable ΞΈ update during optimization.
"""
self._unfix("offset")
def value(self):
"""
Offset mean.
Returns
-------
π¦ : (n,) ndarray
ΞΈβ
π.
"""
return full(self._n, self._offset.value)
def gradient(self):
"""
Gradient of the offset function.
Returns
-------
offset : (n,) ndarray
Vector π.
"""
return dict(offset=ones(self._n))
@property
def offset(self):
"""
Offset parameter.
"""
return self._offset.value
@offset.setter
def offset(self, v):
self._offset.value = v
def __str__(self):
tname = type(self).__name__
msg = "{}()".format(tname)
if self.name is not None:
msg += ": {}".format(self.name)
msg += "\n"
msg += " offset: {}".format(self.offset)
return msg