Пример #1
0
    def __init__(self, mean=None, vol=None):
        if mean is None and vol is None:
            mean = 0.
            vol = 1.

        self.__mean, self.__vol = None, None

        if mean is not None:
            self.__mean = npu.tondim2(mean, ndim1tocol=True, copy=True)
            processdim = npu.nrow(self.__mean)
        if vol is not None:
            self.__vol = npu.tondim2(vol, ndim1tocol=True, copy=True)
            processdim = npu.nrow(self.__vol)

        if self.__mean is None: self.__mean = npu.colof(processdim, 0.)
        if self.__vol is None: self.__vol = np.eye(processdim)

        npc.checkcol(self.__mean)
        npc.checknrow(self.__mean, processdim)
        npc.checknrow(self.__vol, processdim)

        noisedim = npu.ncol(self.__vol)
        self.__cov = np.dot(self.__vol, self.__vol.T)

        npu.makeimmutable(self.__mean)
        npu.makeimmutable(self.__vol)
        npu.makeimmutable(self.__cov)

        super(WienerProcess, self).__init__(processdim=processdim,
                                            noisedim=noisedim,
                                            drift=lambda t, x: self.__mean,
                                            diffusion=lambda t, x: self.__vol)
Пример #2
0
 def __next__(self):
     if self.__time is None:
         self.__time = next(self.__times)
     else:
         newtime = next(self.__times)
         timedelta = newtime - self.__time
         if isinstance(timedelta, dt.timedelta):
             timedelta = timedelta.total_seconds(
             ) / self.__timeunit.total_seconds()
         npu.colof(self.__process.noisedim, 0.)
         variatedelta = np.sqrt(timedelta) * npu.tondim2(
             next(self.__variates), ndim1tocol=True, copy=False)
         drift = npu.tondim2(self.__process.drift(self.__time,
                                                  self.__value),
                             ndim1tocol=True,
                             copy=False)
         diffusion = npu.tondim2(self.__process.diffusion(
             self.__time, self.__value),
                                 ndim1tocol=True,
                                 copy=False)
         self.__value += drift * timedelta + diffusion.dot(variatedelta)
         self.__time = newtime
     v = np.copy(self.__value)
     if self.__flatten: v = v.flatten()
     return self.__time, v
Пример #3
0
 def propagate(self, time, variate, time0, value0, state0=None):
     if time == time0:
         return npu.tondim2(value0, ndim1tocol=True, copy=True)
     value0 = npu.tondim2(value0, ndim1tocol=True, copy=False)
     variate = npu.tondim2(variate, ndim1tocol=True, copy=False)
     timedelta = time - time0
     return value0 + self.__mean * timedelta + np.dot(
         self.__vol,
         np.sqrt(timedelta) * variate)
Пример #4
0
    def __init__(self, transition=None, mean=None, vol=None):
        if transition is None and mean is None and vol is None:
            transition = 1.
            mean = 0.
            vol = 1.

        self.__transition, self.__mean, self.__vol = None, None, None

        if transition is not None:
            self.__transition = npu.tondim2(transition,
                                            ndim1tocol=True,
                                            copy=True)
            processdim = npu.nrow(self.__transition)
        if mean is not None:
            self.__mean = npu.tondim2(mean, ndim1tocol=True, copy=True)
            processdim = npu.nrow(self.__mean)
        if vol is not None:
            self.__vol = npu.tondim2(vol, ndim1tocol=True, copy=True)
            processdim = npu.nrow(self.__vol)

        if self.__transition is None: self.__transition = np.eye(processdim)
        if self.__mean is None: self.__mean = npu.colof(processdim, 0.)
        if self.__vol is None: self.__vol = np.eye(processdim)

        npc.checksquare(self.__transition)
        npc.checknrow(self.__transition, processdim)
        npc.checkcol(self.__mean)
        npc.checknrow(self.__mean, processdim)
        npc.checknrow(self.__vol, processdim)

        noisedim = npu.ncol(self.__vol)

        self.__transitionx2 = npu.kronsum(self.__transition, self.__transition)
        self.__transitionx2inverse = np.linalg.inv(self.__transitionx2)
        self.__cov = np.dot(self.__vol, self.__vol.T)
        self.__covvec = npu.vec(self.__cov)

        self.__cachedmeanreversionfactor = None
        self.__cachedmeanreversionfactortimedelta = None
        self.__cachedmeanreversionfactorsquared = None
        self.__cachedmeanreversionfactorsquaredtimedelta = None

        npu.makeimmutable(self.__transition)
        npu.makeimmutable(self.__transitionx2)
        npu.makeimmutable(self.__transitionx2inverse)
        npu.makeimmutable(self.__mean)
        npu.makeimmutable(self.__vol)
        npu.makeimmutable(self.__cov)
        npu.makeimmutable(self.__covvec)

        super(OrnsteinUhlenbeckProcess, self).__init__(
            processdim=processdim,
            noisedim=noisedim,
            drift=lambda t, x: -np.dot(self.__transition, x - self.__mean),
            diffusion=lambda t, x: self.__vol)
Пример #5
0
 def propagate(self, time, variate, time0, value0, state0=None):
     if time == time0:
         return npu.tondim2(value0, ndim1tocol=True, copy=True)
     value0 = npu.tondim2(value0, ndim1tocol=True, copy=False)
     variate = npu.tondim2(variate, ndim1tocol=True, copy=False)
     timedelta = time - time0
     mrf = self.meanreversionfactor(timedelta)
     eyeminusmrf = np.eye(self.processdim) - mrf
     m = np.dot(mrf, value0) + np.dot(eyeminusmrf, self.__mean)
     c = self.noisecovariance(timedelta)
     return m + np.dot(np.linalg.cholesky(c), variate)
Пример #6
0
 def propagate(self, time, variate, time0, value0, state0=None):
     if self.noisedim != self.processdim:
         raise NotImplementedError(
             'Cannot utilise the propagatedistr of the Markov process in propagate if noisedim != processdim; provide a custom implementation'
         )
     if time == time0:
         return npu.tondim2(value0, ndim1tocol=True, copy=True)
     value0 = npu.tondim2(value0, ndim1tocol=True, copy=False)
     variate = npu.tondim2(variate, ndim1tocol=True, copy=False)
     distr = self.propagatedistr(
         time, time0, distrs.NormalDistr.creatediracdelta(value0))
     return distr.mean + np.dot(np.linalg.cholesky(distr.cov), variate)
Пример #7
0
 def __init__(self, mean=None, cov=None, vol=None, dim=None, copy=True):
     if mean is None and vol is None and cov is None:
         self.__dim = 1 if dim is None else dim
         mean = npu.colof(self.__dim, 0.)
         cov = np.eye(self.__dim)
         vol = np.eye(self.__dim)
         
     self.__dim, self.__mean, self.__vol, self.__cov = None, None, None, None
     
     # TODO We don't currently check whether cov and vol are consistent, i.e. that cov = np.dot(vol, vol.T) -- should we?
     
     if mean is not None:
         self.__mean = npu.tondim2(mean, ndim1tocol=True, copy=copy)
         self.__dim = npu.nrow(self.__mean)
     if cov is not None:
         self.__cov = npu.tondim2(cov, ndim1tocol=True, copy=copy)
         self.__dim = npu.nrow(self.__cov)
     if vol is not None:
         self.__vol = npu.tondim2(vol, ndim1tocol=True, copy=copy)
         self.__dim = npu.nrow(self.__vol)
         
     if self.__mean is None: self.__mean = npu.colof(self.__dim, 0.)
     if self.__cov is None and self.__vol is None:
         self.__cov = np.eye(self.__dim)
         self.__vol = np.eye(self.__dim)
         
     npc.checkcol(self.__mean)
     npc.checknrow(self.__mean, self.__dim)
     if self.__cov is not None:
         npc.checknrow(self.__cov, self.__dim)
         npc.checksquare(self.__cov)
     if self.__vol is not None:
         npc.checknrow(self.__vol, self.__dim)
         
     npu.makeimmutable(self.__mean)
     if self.__cov is not None: npu.makeimmutable(self.__cov)
     if self.__vol is not None: npu.makeimmutable(self.__vol)
     
     super(NormalDistr, self).__init__()
Пример #8
0
 def __init__(self,
              process,
              initialvalue=None,
              times=None,
              variates=None,
              timeunit=dt.timedelta(days=1),
              flatten=False):
     checks.checkinstance(process, proc.ItoProcess)
     self.__process = process
     self.__value = npu.tondim2(
         initialvalue, ndim1tocol=True,
         copy=True) if initialvalue is not None else npu.colof(
             process.processdim, 0.)
     self.__times = times if times is not None else xtimes(0., None, 1.)
     self.__variates = variates if variates is not None else rnd.multivatiate_normals(
         ndim=process.noisedim)
     self.__time = None
     self.__timeunit = timeunit
     self.__flatten = flatten
Пример #9
0
def multivariate_normal(mean=None,
                        cov=None,
                        size=None,
                        ndim=None,
                        randomstate=None):
    global __rs
    if ndim is None:
        if mean is not None: ndim = np.size(mean)
        elif cov is not None: ndim = npu.nrow(cov)
        else: ndim = 1
    if ndim is not None:
        if mean is None: mean = npu.ndim1of(ndim, 0.)
        if cov is None: cov = np.eye(ndim, ndim)
    mean = npu.tondim1(mean)
    cov = npu.tondim2(cov)
    npc.checksize(mean, ndim)
    npc.checknrow(cov, ndim)
    npc.checksquare(cov)
    if randomstate is None: randomstate = __rs()
    return randomstate.multivariate_normal(mean, cov, size)
Пример #10
0
 def makevolfromcov(cov):
     cov = npu.tondim2(cov, ndim1tocol=True, copy=False)
     return np.linalg.cholesky(cov)
Пример #11
0
    def testtondim2(self):
        for v in [
                429., [429.], [[429.]],
                np.array(429.),
                np.array([429.]),
                np.array([[429.]])
        ]:
            r = npu.tondim2(v)
            self.assertIsInstance(r, np.ndarray)
            self.assertEqual(np.shape(r), (1, 1))
            npt.assert_almost_equal(r, np.array([[429.]]))

        for v in [[429., 5.], [[429., 5.]],
                  np.array([429., 5.]),
                  np.array([[429., 5.]])]:
            r = npu.tondim2(v)
            self.assertIsInstance(r, np.ndarray)
            self.assertEqual(np.shape(r), (1, 2))
            npt.assert_almost_equal(r, np.array([[429., 5.]]))

        for v in [[[429.], [5.]], np.array([[429.], [5.]])]:
            r = npu.tondim2(v)
            self.assertIsInstance(r, np.ndarray)
            self.assertEqual(np.shape(r), (2, 1))
            npt.assert_almost_equal(r, np.array([[429.], [5.]]))

        for v in [[429., 5., 2., 14.], [[429., 5., 2., 14.]],
                  np.array([429., 5., 2., 14.]),
                  np.array([[429., 5., 2., 14.]])]:
            r = npu.tondim2(v)
            self.assertIsInstance(r, np.ndarray)
            self.assertEqual(np.shape(r), (1, 4))
            npt.assert_almost_equal(r, np.array([[429., 5., 2., 14.]]))

        for v in [[[429., 5.], [2., 14.]], np.array([[429., 5.], [2., 14.]])]:
            r = npu.tondim2(v)
            self.assertIsInstance(r, np.ndarray)
            self.assertEqual(np.shape(r), (2, 2))
            npt.assert_almost_equal(r, np.array([[429., 5.], [2., 14.]]))

        for v in [[[429.], [5.], [2.], [14.]],
                  np.array([[429.], [5.], [2.], [14.]])]:
            r = npu.tondim2(v)
            self.assertIsInstance(r, np.ndarray)
            self.assertEqual(np.shape(r), (4, 1))
            npt.assert_almost_equal(r, np.array([[429.], [5.], [2.], [14.]]))

        npt.assert_equal(npu.tondim2(None), np.array([[None]]))

        a = np.array([[429., 5.], [2., 14.]])
        b = npu.tondim2(a, copy=False)
        b[1, 1] = 42.
        npt.assert_almost_equal(b, np.array([[429., 5.], [2., 42.]]))
        npt.assert_almost_equal(a, np.array([[429., 5.], [2., 42.]]))

        a = [[429., 5.], [2., 14.]]
        b = npu.tondim2(a, copy=False)
        b[1, 1] = 42.
        npt.assert_almost_equal(b, np.array([[429., 5.], [2., 42.]]))
        npt.assert_almost_equal(a, np.array([[429., 5.], [2., 14.]]))

        a = np.array([[429., 5.], [2., 14.]])
        b = npu.tondim2(a, copy=True)
        b[1, 1] = 42.
        npt.assert_almost_equal(b, np.array([[429., 5.], [2., 42.]]))
        npt.assert_almost_equal(a, np.array([[429., 5.], [2., 14.]]))