Path: blob/master/src/sage/finance/markov_multifractal.py
8815 views
"""1Markov Switching Multifractal model23REFERENCE:45*How to Forecast Long-Run Volatility: Regime Switching and6the Estimation of Multifractal Processes*, Calvet and Fisher, 2004.78AUTHOR:910- William Stein, 20081112TESTS::1314sage: msm = finance.MarkovSwitchingMultifractal(8,1.4,1.0,0.95,3)15sage: loads(dumps(msm)) == msm16True17"""18import math1920class MarkovSwitchingMultifractal:21def __init__(self, kbar, m0, sigma, gamma_kbar, b):22"""23INPUT:2425- ``kbar`` -- positive integer2627- ``m0`` -- float with ``0 <= m0 <= 2``2829- ``sigma`` -- positive float3031- ``gamma_kbar`` -- float with ``0 <= gamma_kbar < 1``3233- ``b`` -- float > 13435EXAMPLES::3637sage: msm = finance.MarkovSwitchingMultifractal(8,1.4,0.5,0.95,3); msm38Markov switching multifractal model with m0 = 1.4, sigma = 0.5, b = 3.0, and gamma_8 = 0.9539sage: yen_usd = finance.MarkovSwitchingMultifractal(10,1.448,0.461,0.998,3.76)40sage: cad_usd = finance.MarkovSwitchingMultifractal(10,1.278,0.262,0.644,2.11)41sage: dm = finance.MarkovSwitchingMultifractal(10,1.326,0.643,0.959,2.7)42"""43self.__m0 = float(m0)44assert self.__m0 >= 0 and self.__m0 <= 2, "m0 must be between 0 and 2"45self.__sigma = float(sigma)46assert self.__sigma > 0, "sigma must be positive"47self.__b = float(b)48assert self.__b > 1, "b must be bigger than 1"49self.__gamma_kbar = float(gamma_kbar)50assert self.__gamma_kbar >= 0 and self.__gamma_kbar < 1, \51"gamma_kbar must be between 0 and 1"52self.__kbar = int(kbar)53assert self.__kbar > 0, "kbar must be positive"5455def __cmp__(self, other):56"""57Compare ``self`` and ``other``.5859Comparison is done on the tuple ``(m0, sigma, b, gamma_kbar, kbar)``.6061EXAMPLES::6263sage: msm = finance.MarkovSwitchingMultifractal(8,1.4,1.0,0.95,3)64sage: msm.__cmp__(3) # random - depends on memory layout65-166sage: msm.__cmp__(msm)67068sage: cad_usd = finance.MarkovSwitchingMultifractal(10,1.278,0.262,0.644,2.11); cad_usd69Markov switching multifractal model with m0 = 1.278, sigma = 0.262, b = 2.11, and gamma_10 = 0.64470sage: msm.__cmp__(cad_usd)71172"""73if not isinstance(other, MarkovSwitchingMultifractal):74return cmp(type(self), type(other))75return cmp((self.__m0, self.__sigma, self.__b, self.__gamma_kbar, self.__kbar),76(other.__m0, other.__sigma, other.__b, other.__gamma_kbar, other.__kbar))7778def __repr__(self):79"""80Return string representation of Markov switching multifractal model.8182EXAMPLES::8384sage: msm = finance.MarkovSwitchingMultifractal(8,1.4,1,0.95,3)85sage: msm.__repr__()86'Markov switching multifractal model with m0 = 1.4, sigma = 1.0, b = 3.0, and gamma_8 = 0.95'87"""88return "Markov switching multifractal model with m0 = %s, sigma = %s, b = %s, and gamma_%s = %s"%(self.m0(), self.sigma(), self.b(), self.kbar(), self.gamma_kbar())8990def m0(self):91"""92Return parameter m0 of Markov switching multifractal model.9394EXAMPLES::9596sage: msm = finance.MarkovSwitchingMultifractal(8,1.4,1,0.95,3)97sage: msm.m0()981.499"""100return self.__m0101102def sigma(self):103"""104Return parameter sigma of Markov switching multifractal model.105106EXAMPLES::107108sage: msm = finance.MarkovSwitchingMultifractal(8,1.4,1,0.95,3)109sage: msm.sigma()1101.0111"""112return self.__sigma113114def b(self):115"""116Return parameter b of Markov switching multifractal model.117118EXAMPLES::119120sage: msm = finance.MarkovSwitchingMultifractal(8,1.4,1,0.95,3)121sage: msm.b()1223.0123"""124return self.__b125126def gamma_kbar(self):127"""128Return parameter ``gamma_kbar`` of Markov switching multifractal model.129130EXAMPLES::131132sage: msm = finance.MarkovSwitchingMultifractal(8,1.4,0.01,0.95,3)133sage: msm.gamma_kbar()1340.95135"""136return self.__gamma_kbar137138def kbar(self):139"""140Return parameter ``kbar`` of Markov switching multifractal model.141142EXAMPLES::143144sage: msm = finance.MarkovSwitchingMultifractal(8,1.4,0.01,0.95,3)145sage: msm.kbar()1468147"""148return self.__kbar149150def gamma(self):151"""152Return the vector of the kbar transitional probabilities.153154OUTPUT:155156- gamma -- a tuple of ``self.kbar()`` floats.157158EXAMPLES::159160sage: msm = finance.MarkovSwitchingMultifractal(8,1.4,1.0,0.95,3)161sage: msm.gamma()162(0.001368852970712986, 0.004100940201672509, 0.012252436441829..., 0.03630878209190..., 0.10501923017634..., 0.28312883556311..., 0.6315968501359..., 0.95000000000000...)163"""164try:165return self.__gamma166except AttributeError:167pass168169b = self.__b170gamma_kbar = self.__gamma_kbar171kbar = self.__kbar172173# We compute gamma1 from gamma_kbar by inverting the relation174# that defines the gamma_k given on page 54 of Calvet-Fisher:175gamma1 = 1 - math.exp(math.log(1-gamma_kbar)/(b**(kbar-1)))176177gamma = tuple([1 - (1 - gamma1)**(b**k) for k in range(kbar)])178self.__gamma = gamma179return gamma180181def simulation(self, n):182"""183Same as ``self.simulations``, but run only 1 time, and returns a time184series instead of a list of time series.185186INPUT:187188- ``n`` -- a positive integer.189190EXAMPLES::191192sage: msm = finance.MarkovSwitchingMultifractal(8,1.4,1.0,0.95,3)193sage: msm.simulation(5)194[0.0059, -0.0097, -0.0101, -0.0110, -0.0067]195sage: msm.simulation(3)196[0.0055, -0.0084, 0.0141]197"""198return self.simulations(n, 1)[0]199200def simulations(self, n, k=1):201"""202Return ``k`` simulations of length ``n`` using this Markov switching203multifractal model for ``n`` time steps.204205INPUT:206207- ``n`` -- positive integer; number of steps.208209- ``k`` -- positive integer (default: 1); number of simulations.210211OUTPUT:212213list -- a list of TimeSeries objects.214215EXAMPLES::216217sage: cad_usd = finance.MarkovSwitchingMultifractal(10,1.278,0.262,0.644,2.11); cad_usd218Markov switching multifractal model with m0 = 1.278, sigma = 0.262, b = 2.11, and gamma_10 = 0.644219"""220import markov_multifractal_cython221return markov_multifractal_cython.simulations(n, k,222self.__m0, self.__sigma,223self.__kbar, self.gamma())224225226227## def ml_estimation(v, kbar, M):228## """229## Compute parameters that model the time series v,230231## INPUT:232## v -- series of returns; e.g., sequence of233## differences of logs of price234## kbar -- positive integer; model parameter235## m -- finite list of the values that the multiplier236## M takes on.237238## OUTPUT:239## m0, sigma, gamma_kbar, b240## """241242243244