def __init__(self, y, sigma=1.0): self._y = checkArrayType(y) if isinstance(sigma, numpy.ndarray): if len(sigma.shape) > 1: if 1 in sigma.shape: sigma = sigma.flatten() if y.shape == sigma.shape: self._sigma = sigma else: raise InitializeError( "Standard deviation not of the correct size") else: if y.shape == sigma.shape: self._sigma = sigma else: raise InitializeError( "Standard deviation not of the correct size") elif sigma is None or sigma == 1.0: self._sigma = numpy.ones(self._y.shape) else: raise InitializeError("Standard deviation not of the correct type") self._sigma2 = self._sigma**2 self.loss(self._y)
def __init__(self, y, weights=None): self._y = checkArrayType(y) self._numObv = len(self._y) if weights is None: self._numVar = 0 self._w = numpy.ones(self._y.shape) else: self._w = checkArrayType(weights) if len(self._w.shape) > 1: if 1 == self._w.shape[1]: self._w = self._w.flatten() assert self._y.shape == self._w.shape, "Input weight not of the same size as y" self.loss(self._y)
def __init__(self, theta, ode, x0, t0, t, y, stateName, sigma=None, targetParam=None, targetState=None): if sigma is None: super(NormalLoss, self).__init__(theta, ode, x0, t0, t, y, stateName, sigma, targetParam, targetState) else: sigma = checkArrayType(sigma) super(NormalLoss, self).__init__(theta, ode, x0, t0, t, y, stateName, 1 / sigma, targetParam, targetState)
def __init__(self, y, sigma=1.0): self._y = checkArrayType(y) if isinstance(sigma, numpy.ndarray): if len(sigma.shape) > 1: if 1 in sigma.shape: sigma = sigma.flatten() if y.shape == sigma.shape: self._sigma = sigma else: raise InitializeError("Standard deviation not of the correct size") else: if y.shape == sigma.shape: self._sigma = sigma else: raise InitializeError("Standard deviation not of the correct size") elif sigma is None or sigma == 1.0: self._sigma = numpy.ones(self._y.shape) else: raise InitializeError("Standard deviation not of the correct type") self._sigma2 = self._sigma**2 self.loss(self._y)
def __init__(self, y): self._y = checkArrayType(y) self.loss(self._y)
def hybrid(x0, t0, t1, stateChangeMat, reactantMat, transitionFunc, transitionMeanFunc, transitionVarFunc, output_time=False, seed=None): ''' Stochastic simulation using an hybrid method that uses either the first reaction method or the :math:`\\tau`-leap depending on the size of the states and transition rates. Starting from time t0 to t1 with the starting state values of x0. Parameters ---------- x: array like state vector t0: double start time t1: double final time stateChangeMat: array like State change matrix :math:`V_{i,j}` where :math:`i,j` represent the state and transition respectively. :math:`V_{i,j}` is some non-zero integer such that transition :math:`j` happens means that state :math:`i` changes by :math:`V_{i,j}` amount transitionFunc: callable a function that takes the input argument (x,t) and returns the vector of transition rates transitionMeanFunc: callable a function that takes the input argument (x,t) and returns the expected transitions transitionVarFunc: callable a function that takes the input argument (x,t) and returns the variance of the transitions output_time: bool, optional defaults to False, if True then a tuple of two elements will be returned, else only the state vector seed: optional represent which type of seed to use. None or False uses the default seed. When seed is an integer number, it will reset the seed via numpy.random.seed. When seed=True, then a :class:`numpy.random.RandomState` object will be used for the underlying random number generating process. If seed is an object of :class:`numpy.random.RandomState` then it will be used directly Returns ------- x: array like state vector t: double time ''' x = checkArrayType(x0) t = t0 if seed: seed = np.random.RandomState() f = firstReaction while t < t1: if np.min(x) > 10: x_new, t_new, s = tauLeap(x, t, stateChangeMat, reactantMat, transitionFunc, transitionMeanFunc, transitionVarFunc, seed=seed) if s is False: x_new, t_new, s = f(x, t, stateChangeMat, transitionFunc, seed=seed) else: x_new, t_new, s = f(x, t, stateChangeMat, transitionFunc, seed=seed) if s: x, t = x_new, t_new else: break if output_time: return (x, t) else: return (x)
def sde(x0, t0, t1, drift, diffusion, stateChangeMat=None, h=None, n=500, positive=True, output_time=False, seed=None): ''' Stochastic simulation using a SDE approximation starting from time t0 to t1 with the starting state values of x0. The SDE approximation is performed using a simple Euler-Maruyama method with step size h. We assume that the input parameter drift and diffusion each gives a function that takes in two arguments :math:`(x,t)` and computes the drift and diffusion. If stateChangeMat is a :class:`numpy.ndarray` then we assume that a pre-multiplication against the drift and diffusion is required. Parameters ---------- x: array like state vector t0: double start time t1: double final time drift: callable a function that takes the input argument (x,t) and returns the vector that contains the drift diffusion: callable a function that takes the input argument (x,t) and returns the vector that contains the diffusion stateChangeMat: array like State change matrix :math:`V_{i,j}` where :math:`i,j` represent the state and transition respectively. :math:`V_{i,j}` is some non-zero integer such that transition :math:`j` happens means that state :math:`i` changes by :math:`V_{i,j}` amount h: double, optional step size h, defaults to None which then h = (t1 - t0)/n n: int, optional number of steps to take for the whole simulation, defaults to 500 positive: bool or array of bool, optional whether the states :math:`x >= 0`. If input is an array then the length should be the same as len(x) output_time: bool, optional defaults to False, if True then a tuple of two elements will be returned, else only the state vector seed: optional represent which type of seed to use. None or False uses the default seed. When seed is an integer number, it will reset the seed via numpy.random.seed. When seed=True, then a :class:`numpy.random.RandomState` object will be used for the underlying random number generating process. If seed is an object of :class:`numpy.random.RandomState` then it will be used directly Returns ------- x: array like state vector t: double time ''' if stateChangeMat is not None: assert isinstance(stateChangeMat, np.ndarray), \ "stateChangeMat should be a numpy array" p = stateChangeMat.shape[1] else: p = len(drift(x0, t0)) if hasattr(positive, '__iter__'): assert len(positive) == len(x0), \ "an array for the input positive should have same length as x" assert all(isinstance(p, bool) for p in positive), \ "elements in positive should be a bool" positive = np.array(positive) else: assert isinstance(positive, bool), "positive should be a bool" positive = np.array([positive] * len(x0)) if seed: rvs = np.random.RandomState().normal else: rvs = scipy.stats.norm.rvs if h is None: h = (t1 - t0) / n x = checkArrayType(x0) t = t0 while t < t1: mu = h * drift(x, t) sigma = diffusion(x, t) * rvs(0, np.sqrt(h), size=p) if stateChangeMat is None: x += mu + sigma else: x += stateChangeMat.dot(mu + sigma) ## We might like to put a defensive line below to stop the states ## going below zero. This applies only to models where each state ## represent a physical count x[x[positive] < 0] = 0 t += h if output_time: return (x, t) else: return (x)