Ticket #3356: sage-3356-part4.patch

File sage-3356-part4.patch, 5.4 KB (added by was, 14 years ago)
  • sage/finance/markov_multifractal.py

    # HG changeset patch
    # User William Stein <wstein@gmail.com>
    # Date 1212602085 25200
    # Node ID cc0fb6d144c0998a515e2ce452c890abca1799d7
    # Parent  a64e6d49cfac5001b3dfd7d9fb2ceab324f4db86
    trac #3346 -- finance -- fix a few small issues
    
    diff -r a64e6d49cfac -r cc0fb6d144c0 sage/finance/markov_multifractal.py
    a b class MarkovSwitchingMultifractal: 
    2626            kbar   -- positive integer
    2727            m0     -- float with 0 <= m0 <= 2
    2828            sigma  -- positive float
    29             gamma_kbar -- float with 0 < gamma_kbar < 1
     29            gamma_kbar -- float with 0 <= gamma_kbar < 1
    3030            b      -- float > 1
    3131
    3232        EXAMPLES:
    class MarkovSwitchingMultifractal: 
    4242        self.__b = float(b)
    4343        assert self.__b > 1, "b must be bigger than 1"
    4444        self.__gamma_kbar = float(gamma_kbar)
    45         assert self.__gamma_kbar > 0 and self.__gamma_kbar < 1, \
     45        assert self.__gamma_kbar >= 0 and self.__gamma_kbar < 1, \
    4646               "gamma_kbar must be between 0 and 1"
    4747        self.__kbar = int(kbar)
    4848        assert self.__kbar > 0, "kbar must be positive"
    class MarkovSwitchingMultifractal: 
    193193            Markov switching multifractal model with m0 = 1.278, sigma = 0.262, b = 2.11, and gamma_10 = 0.644
    194194        """
    195195        return markov_multifractal_cython.simulations(n, k,
    196                    self.__m0, self.__sigma, self.__b,
     196                   self.__m0, self.__sigma,
    197197                   self.__kbar, self.gamma())
    198198       
    199199
  • sage/finance/markov_multifractal_cython.pyx

    diff -r a64e6d49cfac -r cc0fb6d144c0 sage/finance/markov_multifractal_cython.pyx
    a b from time_series cimport TimeSeries 
    1212from time_series cimport TimeSeries
    1313
    1414def simulations(Py_ssize_t n, Py_ssize_t k,
    15                double m0, double sigma, double b,
     15               double m0, double sigma,
    1616               int kbar, gamma):
    1717    cdef double m1 = 2 - m0
    1818    cdef Py_ssize_t i, j, a, c
    def simulations(Py_ssize_t n, Py_ssize_t 
    2121    cdef TimeSeries gamma_vals = TimeSeries(gamma)
    2222    cdef randstate rstate = current_randstate()
    2323
    24     sigma = sigma / 100.0  # model's sigma is a percent
     24    sigma = sigma / 100  # model's sigma is a percent
    2525
    2626    # output list of simulations
    2727    S = []
    def simulations(Py_ssize_t n, Py_ssize_t 
    4141        for a from 0 <= a < n:
    4242            # Compute next step in the simulation
    4343            t._values[a] = sigma * eps._values[a] * sqrt(markov_state_vector.prod())
    44 
     44           
    4545            # Now update the volatility state vector
    4646            for c from 0 <= c < kbar:
    4747                if rstate.c_rand_double() <= gamma_vals._values[c]:
  • sage/finance/time_series.pyx

    diff -r a64e6d49cfac -r cc0fb6d144c0 sage/finance/time_series.pyx
    a b cdef class TimeSeries: 
    10651065        """
    10661066        return sqrt(self.variance(bias=bias))
    10671067
     1068    def range_statistic(self):
     1069        sums = self.sums()
     1070        return (sums.max() - sums.min())/self.standard_deviation()
     1071
     1072    def hurst_exponent(self):
     1073        cdef double r = self.range_statistic()
     1074        if r == 0:
     1075            return 0
     1076        return log(r)/log(self._length)
     1077
    10681078    def min(self, bint index=False):
    10691079        """
    10701080        Return the smallest value in this time series. If this series
    cdef class TimeSeries: 
    12431253        for i from 0 <= i < bins:
    12441254            cnts[i] = 0
    12451255
     1256        cdef Py_ssize_t j
    12461257        for i from 0 <= i < self._length:
    1247             cnts[<Py_ssize_t>floor((self._values[i] - mn)/step)] += 1
     1258            j = int((self._values[i] - mn)/step)
     1259            if j >= bins:
     1260                j = bins-1
     1261            cnts[j] += 1
    12481262
    12491263        b = 1.0/(self._length * step)
    12501264        if normalize:
    cdef class TimeSeries: 
    12761290        s = 0
    12771291        for i, (x0,x1) in enumerate(intervals):
    12781292            s += polygon([(x0,0), (x0,counts[i]), (x1,counts[i]), (x1,0)], **kwds)
     1293        if len(intervals) > 0:
     1294            s.xmin(intervals[0][0])
     1295            s.xmax(intervals[-1][1])
     1296            s.ymin(0)
     1297            s.ymax(max(counts))
    12791298        return s
    12801299
    12811300    def numpy(self):
    cdef class TimeSeries: 
    13121331        is platform independent.
    13131332        """
    13141333        if distribution == 'uniform':
    1315             self._randomize_uniform(loc, scale)
     1334            self._randomize_uniform(loc, loc + scale)
    13161335        elif distribution == 'normal':
    13171336            self._randomize_normal(loc, scale)
    13181337        elif distribution == 'semicircle':
    cdef class TimeSeries: 
    13461365        cdef Py_ssize_t k
    13471366        for k from 0 <= k < self._length:
    13481367            while 1:
    1349                 x1 = 2.0 * rstate.c_rand_double() - 1.0
    1350                 x2 = 2.0 * rstate.c_rand_double() - 1.0
    1351                 w = x1 * x1 + x2 * x2
    1352                 if w < 1.0: break
    1353             w = sqrt( (-2.0 * log( w ) ) / w )
     1368                x1 = 2*rstate.c_rand_double() - 1
     1369                x2 = 2*rstate.c_rand_double() - 1
     1370                w = x1*x1 + x2*x2
     1371                if w < 1: break
     1372            w = sqrt( (-2*log(w))/w )
    13541373            y1 = x1 * w
    13551374            y2 = x2 * w
    13561375            self._values[k] = m + y1*s