def __next__(self): if self._time is None: self._time = next(self.__times) else: newtime = next(self.__times) time_delta = newtime - self._time if isinstance(time_delta, dt.timedelta): time_delta = time_delta.total_seconds( ) / self._time_unit.total_seconds() npu.col_of(self.__process.noise_dim, 0.) variate_delta = np.sqrt(time_delta) * npu.to_ndim_2( next(self.__variates), ndim_1_to_col=True, copy=False) drift = npu.to_ndim_2(self.__process.drift(self._time, self.__value), ndim_1_to_col=True, copy=False) diffusion = npu.to_ndim_2(self.__process.diffusion( self._time, self.__value), ndim_1_to_col=True, copy=False) self.__value += drift * time_delta + diffusion.dot(variate_delta) self._time = newtime v = np.copy(self.__value) if self.__flatten: v = v.flatten() return self._time, v
def __init__(self, initial_value=None, final_value=None, initial_time=0., final_time=1., vol=None, time_unit=dt.timedelta(days=1)): process_dim = 1 self.__initial_value = None self.__final_value = None if initial_value is not None: self.__initial_value = npu.to_ndim_2(initial_value, ndim_1_to_col=True, copy=True) process_dim = npu.nrow(self.__initial_value) if final_value is not None: self.__final_value = npu.to_ndim_2(final_value, ndim_1_to_col=True, copy=True) process_dim = npu.nrow(self.__final_value) if self.__initial_value is None: self.__initial_value = npu.col_of(process_dim, 0.) if self.__final_value is None: self.__final_value = npu.col_of(process_dim, 0.) self.__vol = None if vol is not None: self.__vol = npu.to_ndim_2(vol, ndim_1_to_col=True, copy=True) process_dim = npu.nrow(self.__vol) if self.__vol is None: self.__vol = np.eye(process_dim) self.__initial_time = initial_time self.__final_time = final_time npc.check_col(self.__initial_value) npc.check_col(self.__final_value) npc.check_nrow(self.__initial_value, process_dim) npc.check_nrow(self.__final_value, process_dim) noise_dim = npu.ncol(self.__vol) self.__cov = stats.vol_to_cov(self.__vol) npu.make_immutable(self.__initial_value) npu.make_immutable(self.__final_value) npu.make_immutable(self.__vol) npu.make_immutable(self.__cov) self._to_string_helper_BrownianBridge = None self._str_BrownianBridge = None super(BrownianBridge, self).__init__(process_dim=process_dim, noise_dim=noise_dim, drift=lambda t, x: (self.__final_value - x) / (self.__final_time - t), diffusion=lambda t, x: self.__vol, time_unit=time_unit)
def __init__(self, mean=None, cov=None, vol=None, dim=None, copy=True, do_not_init=False): if not do_not_init: if mean is not None and dim is not None and np.size(mean) == 1: mean = npu.col_of(dim, npu.to_scalar(mean)) if mean is None and vol is None and cov is None: self._dim = 1 if dim is None else dim mean = npu.col_of(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.to_ndim_2(mean, ndim_1_to_col=True, copy=copy) self._dim = npu.nrow(self._mean) if cov is not None: self._cov = npu.to_ndim_2(cov, ndim_1_to_col=True, copy=copy) self._dim = npu.nrow(self._cov) if vol is not None: self._vol = npu.to_ndim_2(vol, ndim_1_to_col=True, copy=copy) self._dim = npu.nrow(self._vol) if self._mean is None: self._mean = npu.col_of(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.check_col(self._mean) npc.check_nrow(self._mean, self._dim) if self._cov is not None: npc.check_nrow(self._cov, self._dim) npc.check_square(self._cov) if self._vol is not None: npc.check_nrow(self._vol, self._dim) npu.make_immutable(self._mean) if self._cov is not None: npu.make_immutable(self._cov) if self._vol is not None: npu.make_immutable(self._vol) self._to_string_helper_WideSenseDistr = None self._str_WideSenseDistr = None super().__init__()
def __init__(self, mean_of_log=None, cov_of_log=None, vol_of_log=None, dim=None, copy=True): if mean_of_log is not None and dim is not None and np.size(mean_of_log) == 1: mean_of_log = npu.col_of(dim, npu.to_scalar(mean_of_log)) if mean_of_log is None and vol_of_log is None and cov_of_log is None: self._dim = 1 if dim is None else dim mean_of_log = npu.col_of(self._dim, 0.) cov_of_log = np.eye(self._dim) vol_of_log = np.eye(self._dim) self._dim, self._mean_of_log, self._vol_of_log, self._cov_of_log = None, None, None, None # TODO We don't currently check whether cov_of_log and vol_of_log are consistent, i.e. that cov_of_log = np.dot(vol_of_log, vol_of_log.T) -- should we? if mean_of_log is not None: self._mean_of_log = npu.to_ndim_2(mean_of_log, ndim_1_to_col=True, copy=copy) self._dim = npu.nrow(self._mean_of_log) if cov_of_log is not None: self._cov_of_log = npu.to_ndim_2(cov_of_log, ndim_1_to_col=True, copy=copy) self._dim = npu.nrow(self._cov_of_log) if vol_of_log is not None: self._vol_of_log = npu.to_ndim_2(vol_of_log, ndim_1_to_col=True, copy=copy) self._dim = npu.nrow(self._vol_of_log) if self._mean_of_log is None: self._mean_of_log = npu.col_of(self._dim, 0.) if self._cov_of_log is None and self._vol_of_log is None: self._cov_of_log = np.eye(self._dim) self._vol_of_log = np.eye(self._dim) npc.check_col(self._mean_of_log) npc.check_nrow(self._mean_of_log, self._dim) if self._cov_of_log is not None: npc.check_nrow(self._cov_of_log, self._dim) npc.check_square(self._cov_of_log) if self._vol_of_log is not None: npc.check_nrow(self._vol_of_log, self._dim) if self._cov_of_log is None: self._cov_of_log = stats.vol_to_cov(self._vol_of_log) if self._vol_of_log is None: self._vol_of_log = stats.cov_to_vol(self._cov_of_log) npu.make_immutable(self._mean_of_log) npu.make_immutable(self._cov_of_log) npu.make_immutable(self._vol_of_log) mean = np.exp(self._mean_of_log + .5 * npu.col(*[self._cov_of_log[i,i] for i in range(self._dim)])) cov = np.array([[np.exp(self._mean_of_log[i,0] + self._mean_of_log[j,0] + .5 * (self._cov_of_log[i,i] + self._cov_of_log[j,j])) * (np.exp(self._cov_of_log[i,j]) - 1.) for j in range(self._dim)] for i in range(self._dim)]) vol = stats.cov_to_vol(cov) self._to_string_helper_LogNormalDistr = None self._str_LogNormalDistr = None super().__init__(mean, cov, vol, self._dim, copy)
def test_wide_sense_distr(self): std_wide_sense_1d = distrs.WideSenseDistr(dim=1) npt.assert_almost_equal(std_wide_sense_1d.mean, 0.) npt.assert_almost_equal(std_wide_sense_1d.cov, 1.) npt.assert_almost_equal(std_wide_sense_1d.vol, 1.) with self.assertRaises(NotImplementedError): std_wide_sense_1d.sample() std_wide_sense_2d = distrs.WideSenseDistr(dim=2) npt.assert_almost_equal(std_wide_sense_2d.mean, npu.col_of(2, 0.)) npt.assert_almost_equal(std_wide_sense_2d.cov, np.eye(2)) npt.assert_almost_equal(std_wide_sense_2d.vol, np.eye(2)) with self.assertRaises(NotImplementedError): std_wide_sense_2d.sample() sd1=3.; sd2=4.; cor=-.5 wide_sense_2d = distrs.WideSenseDistr(mean=[1., 2.], cov=stats.make_cov_2d(sd1=sd1, sd2=sd2, cor=cor)) npt.assert_almost_equal(wide_sense_2d.mean, npu.col(1., 2.)) npt.assert_almost_equal(wide_sense_2d.cov, [[sd1*sd1, cor*sd1*sd2], [cor*sd1*sd2, sd2*sd2]]) npt.assert_almost_equal(wide_sense_2d.vol, [[sd1, 0.], [cor*sd2, np.sqrt(1.-cor*cor)*sd2]]) with self.assertRaises(NotImplementedError): wide_sense_2d.sample() wide_sense_2d = distrs.WideSenseDistr(mean=[1., 2.], vol=stats.make_vol_2d(sd1=sd1, sd2=sd2, cor=cor)) npt.assert_almost_equal(wide_sense_2d.mean, npu.col(1., 2.)) npt.assert_almost_equal(wide_sense_2d.cov, [[sd1*sd1, cor*sd1*sd2], [cor*sd1*sd2, sd2*sd2]]) npt.assert_almost_equal(wide_sense_2d.vol, [[sd1, 0.], [cor*sd2, np.sqrt(1.-cor*cor)*sd2]]) with self.assertRaises(NotImplementedError): wide_sense_2d.sample()
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.to_ndim_2(mean, ndim_1_to_col=True, copy=True) process_dim = npu.nrow(self._mean) if vol is not None: self._vol = npu.to_ndim_2(vol, ndim_1_to_col=True, copy=True) process_dim = npu.nrow(self._vol) if self._mean is None: self._mean = npu.col_of(process_dim, 0.) if self._vol is None: self._vol = np.eye(process_dim) npc.check_col(self._mean) npc.check_nrow(self._mean, process_dim) npc.check_nrow(self._vol, process_dim) noise_dim = npu.ncol(self._vol) self._cov = np.dot(self._vol, self._vol.T) npu.make_immutable(self._mean) npu.make_immutable(self._vol) npu.make_immutable(self._cov) self._to_string_helper_WienerProcess = None self._str_WienerProcess = None super(WienerProcess, self).__init__(process_dim=process_dim, noise_dim=noise_dim, drift=lambda t, x: self._mean, diffusion=lambda t, x: self._vol)
def __init__(self, pct_drift=None, pct_vol=None, time_unit=dt.timedelta(days=1)): if pct_drift is None and pct_vol is None: pct_drift = 0.; pct_vol = 1. self._pct_drift, self._pct_vol = None, None if pct_drift is not None: self._pct_drift = npu.to_ndim_2(pct_drift, ndim_1_to_col=True, copy=True) process_dim = npu.nrow(self._pct_drift) if pct_vol is not None: self._pct_vol = npu.to_ndim_2(pct_vol, ndim_1_to_col=True, copy=True) process_dim = npu.nrow(self._pct_vol) if self._pct_drift is None: self._pct_drift = npu.col_of(process_dim, 0.) if self._pct_vol is None: self._pct_vol = np.eye(process_dim) npc.check_col(self._pct_drift) npc.check_nrow(self._pct_drift, process_dim) npc.check_nrow(self._pct_vol, process_dim) noise_dim = npu.ncol(self._pct_vol) self._pct_cov = stats.vol_to_cov(self._pct_vol) npu.make_immutable(self._pct_drift) npu.make_immutable(self._pct_vol) npu.make_immutable(self._pct_cov) self._to_string_helper_GeometricBrownianMotion = None self._str_GeometricBrownianMotion = None super(GeometricBrownianMotion, self).__init__(process_dim=process_dim, noise_dim=noise_dim, drift=lambda t, x: self._pct_drift * x, diffusion=lambda t, x: x * self._pct_vol, time_unit=time_unit)
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.to_ndim_2(transition, ndim_1_to_col=True, copy=True) process_dim = npu.nrow(self._transition) if mean is not None: self._mean = npu.to_ndim_2(mean, ndim_1_to_col=True, copy=True) process_dim = npu.nrow(self._mean) if vol is not None: self._vol = npu.to_ndim_2(vol, ndim_1_to_col=True, copy=True) process_dim = npu.nrow(self._vol) if self._transition is None: self._transition = np.eye(process_dim) if self._mean is None: self._mean = npu.col_of(process_dim, 0.) if self._vol is None: self._vol = np.eye(process_dim) npc.check_square(self._transition) npc.check_nrow(self._transition, process_dim) npc.check_col(self._mean) npc.check_nrow(self._mean, process_dim) npc.check_nrow(self._vol, process_dim) noise_dim = npu.ncol(self._vol) self._transition_x_2 = npu.kron_sum(self._transition, self._transition) self._transition_x_2_inverse = np.linalg.inv(self._transition_x_2) self._cov = np.dot(self._vol, self._vol.T) self._cov_vec = npu.vec(self._cov) self._cached_mean_reversion_factor = None self._cached_mean_reversion_factor_time_delta = None self._cached_mean_reversion_factor_squared = None self._cached_mean_reversion_factor_squared_time_delta = None npu.make_immutable(self._transition) npu.make_immutable(self._transition_x_2) npu.make_immutable(self._transition_x_2_inverse) npu.make_immutable(self._mean) npu.make_immutable(self._vol) npu.make_immutable(self._cov) npu.make_immutable(self._cov_vec) self._to_string_helper_OrnsteinUhlenbeckProcess = None self._str_OrnsteinUhlenbeckProcess = None super(OrnsteinUhlenbeckProcess, self).__init__( process_dim=process_dim, noise_dim=noise_dim, drift=lambda t, x: -np.dot(self._transition, x - self._mean), diffusion=lambda t, x: self._vol)
def __init__(self, mean=None, dim=None, copy=True): if mean is not None and dim is not None and np.size(mean) == 1: mean = npu.col_of(dim, npu.to_scalar(mean)) if mean is None: dim = 1 if dim is None else dim mean = npu.col_of(dim, 0.) self._mean = npu.to_ndim_2(mean, ndim_1_to_col=True, copy=copy) if dim is None: dim = npu.nrow(self._mean) self._dim = dim npc.check_col(self._mean) npc.check_nrow(self._mean, self._dim) npu.make_immutable(self._mean) self._zero_cov = None self._to_string_helper_DiracDeltaDistr = None self._str_DiracDeltaDistr = None
def __init__(self, process, initial_value=None, times=None, variates=None, time_unit=dt.timedelta(days=1), flatten=False): checks.check_instance(process, proc.ItoProcess) self.__process = process self.__value = npu.to_ndim_2( initial_value, ndim_1_to_col=True, copy=True) if initial_value is not None else npu.col_of( process.process_dim, 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.noise_dim) self._time = None self._time_unit = time_unit self.__flatten = flatten
def test_multivariate_normal_distr(self): std_normal_1d = distrs.NormalDistr(dim=1) npt.assert_almost_equal(std_normal_1d.mean, 0.) npt.assert_almost_equal(std_normal_1d.cov, 1.) npt.assert_almost_equal(std_normal_1d.vol, 1.) std_normal_1d = distrs.NormalDistr(dim=2) npt.assert_almost_equal(std_normal_1d.mean, npu.col_of(2, 0.)) npt.assert_almost_equal(std_normal_1d.cov, np.eye(2)) npt.assert_almost_equal(std_normal_1d.vol, np.eye(2)) sd1 = 3. sd2 = 4. cor = -.5 normal_2d = distrs.NormalDistr(mean=[1., 2.], cov=distrs.NormalDistr.make_cov_2d( sd1=sd1, sd2=sd2, cor=cor)) npt.assert_almost_equal(normal_2d.mean, npu.col(1., 2.)) npt.assert_almost_equal( normal_2d.cov, [[sd1 * sd1, cor * sd1 * sd2], [cor * sd1 * sd2, sd2 * sd2]]) npt.assert_almost_equal( normal_2d.vol, [[sd1, 0.], [cor * sd2, np.sqrt(1. - cor * cor) * sd2]]) normal_2d = distrs.NormalDistr(mean=[1., 2.], vol=distrs.NormalDistr.make_vol_2d( sd1=sd1, sd2=sd2, cor=cor)) npt.assert_almost_equal(normal_2d.mean, npu.col(1., 2.)) npt.assert_almost_equal( normal_2d.cov, [[sd1 * sd1, cor * sd1 * sd2], [cor * sd1 * sd2, sd2 * sd2]]) npt.assert_almost_equal( normal_2d.vol, [[sd1, 0.], [cor * sd2, np.sqrt(1. - cor * cor) * sd2]])
def test_normal_distr(self): rnd.random_state(np.random.RandomState(seed=42), force=True) std_normal_1d = distrs.NormalDistr(dim=1) npt.assert_almost_equal(std_normal_1d.mean, 0.) npt.assert_almost_equal(std_normal_1d.cov, 1.) npt.assert_almost_equal(std_normal_1d.vol, 1.) sample = std_normal_1d.sample() self.assertEqual(np.shape(sample), (1, 1)) npt.assert_almost_equal(sample, [[ 0.49671415]]) sample = std_normal_1d.sample(size=10) self.assertEqual(np.shape(sample), (10, 1)) npt.assert_almost_equal(sample, [ [-0.1382643 ], [ 0.64768854], [ 1.52302986], [-0.23415337], [-0.23413696], [ 1.57921282], [ 0.76743473], [-0.46947439], [ 0.54256004], [-0.46341769]]) std_normal_2d = distrs.NormalDistr(dim=2) npt.assert_almost_equal(std_normal_2d.mean, npu.col_of(2, 0.)) npt.assert_almost_equal(std_normal_2d.cov, np.eye(2)) npt.assert_almost_equal(std_normal_2d.vol, np.eye(2)) sample = std_normal_2d.sample(size=10) self.assertEqual(np.shape(sample), (10, 2)) npt.assert_almost_equal(sample, [ [-0.46572975, 0.24196227], [-1.91328024, -1.72491783], [-0.56228753, -1.01283112], [ 0.31424733, -0.90802408], [-1.4123037 , 1.46564877], [-0.2257763 , 0.0675282 ], [-1.42474819, -0.54438272], [ 0.11092259, -1.15099358], [ 0.37569802, -0.60063869], [-0.29169375, -0.60170661]]) sd1=3.; sd2=4.; cor=-.5 normal_2d = distrs.NormalDistr(mean=[1., 2.], cov=stats.make_cov_2d(sd1=sd1, sd2=sd2, cor=cor)) npt.assert_almost_equal(normal_2d.mean, npu.col(1., 2.)) npt.assert_almost_equal(normal_2d.cov, [[sd1*sd1, cor*sd1*sd2], [cor*sd1*sd2, sd2*sd2]]) npt.assert_almost_equal(normal_2d.vol, [[sd1, 0.], [cor*sd2, np.sqrt(1.-cor*cor)*sd2]]) sample = normal_2d.sample(size=10) self.assertEqual(np.shape(sample), (10, 2)) npt.assert_almost_equal(sample, [ [-3.09581812, 9.06710684], [ 5.00400357, -1.07912958], [ 4.10821238, -2.42324481], [ 2.58989516, -7.05256838], [ 2.07671635, 3.61955714], [ 0.38728403, 2.5195548 ], [-1.36010204, -0.88681309], [ 1.63968707, -1.29329703], [-0.61960168, 6.44566548], [ 5.53451941, -4.36131646]]) normal_2d = distrs.NormalDistr(mean=[1., 2.], vol=stats.make_vol_2d(sd1=sd1, sd2=sd2, cor=cor)) npt.assert_almost_equal(normal_2d.mean, npu.col(1., 2.)) npt.assert_almost_equal(normal_2d.cov, [[sd1*sd1, cor*sd1*sd2], [cor*sd1*sd2, sd2*sd2]]) npt.assert_almost_equal(normal_2d.vol, [[sd1, 0.], [cor*sd2, np.sqrt(1.-cor*cor)*sd2]]) sample = normal_2d.sample(size=10) self.assertEqual(np.shape(sample), (10, 2)) npt.assert_almost_equal(sample, [ [ 0.4624506 , -0.26705979], [ 1.76344545, 5.54913479], [-2.76038957, 4.57609973], [ 2.35608833, 1.20642031], [-2.1218454 , 5.16796697], [-0.85307657, -0.00850715], [ 5.28771297, -1.62048489], [-2.12592264, 7.1016208 ], [-0.46508111, 6.26189296], [ 3.15543223, -0.04269231]])
def test_dirac_delta_distr(self): std_dirac_delta_1d = distrs.DiracDeltaDistr(dim=1) npt.assert_almost_equal(std_dirac_delta_1d.mean, 0.) npt.assert_almost_equal(std_dirac_delta_1d.cov, 0.) npt.assert_almost_equal(std_dirac_delta_1d.vol, 0.) sample = std_dirac_delta_1d.sample() self.assertEqual(np.shape(sample), (1, 1)) npt.assert_almost_equal(sample, [[ 0. ]]) sample = std_dirac_delta_1d.sample(size=10) self.assertEqual(np.shape(sample), (10, 1)) npt.assert_almost_equal(sample, [ [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.]]) std_dirac_delta_2d = distrs.DiracDeltaDistr(dim=2) npt.assert_almost_equal(std_dirac_delta_2d.mean, npu.col_of(2, 0.)) npt.assert_almost_equal(std_dirac_delta_2d.cov, np.zeros((2, 2))) npt.assert_almost_equal(std_dirac_delta_2d.vol, np.zeros((2, 2))) sample = std_dirac_delta_2d.sample(size=10) self.assertEqual(np.shape(sample), (10, 2)) npt.assert_almost_equal(sample, [ [ 0., 0.], [ 0., 0.], [ 0., 0.], [ 0., 0.], [ 0., 0.], [ 0., 0.], [ 0., 0.], [ 0., 0.], [ 0., 0.], [ 0., 0.]]) dirac_delta_2d = distrs.DiracDeltaDistr(mean=[1., 2.], dim=2) npt.assert_almost_equal(dirac_delta_2d.mean, [[1.], [2.]]) npt.assert_almost_equal(dirac_delta_2d.cov, np.zeros((2, 2))) npt.assert_almost_equal(dirac_delta_2d.vol, np.zeros((2, 2))) sample = dirac_delta_2d.sample(size=10) self.assertEqual(np.shape(sample), (10, 2)) npt.assert_almost_equal(sample, [ [ 1., 2.], [ 1., 2.], [ 1., 2.], [ 1., 2.], [ 1., 2.], [ 1., 2.], [ 1., 2.], [ 1., 2.], [ 1., 2.], [ 1., 2.]])
def test_col_of(self): col = npu.col_of(5, 429.) npt.assert_almost_equal(col, np.array([[429.], [429.], [429.], [429.], [429.]]))