Ticket #3356: sage-3356-part8.patch

File sage-3356-part8.patch, 6.8 KB (added by was, 14 years ago)
  • sage/finance/all.py

    # HG changeset patch
    # User William Stein <wstein@gmail.com>
    # Date 1214880200 25200
    # Node ID 7498109ebf28d8cb41f8c646662fc8308c1ce0ca
    # Parent  bf8722261873c874f4ebf2117cd696d1ddbe54c4
    #3356 -- more doctests, etc.
    
    diff -r bf8722261873 -r 7498109ebf28 sage/finance/all.py
    a b from markov_multifractal import MarkovSw 
    44
    55from time_series import TimeSeries, linear_filter
    66
    7 from fractal import *
     7from fractal import (stationary_gaussian_simulation,
     8                     fractional_gaussian_noise_simulation,
     9                     fractional_brownian_motion_simulation,
     10                     multifractal_cascade_random_walk_simulation)
  • sage/finance/fractal.pyx

    diff -r bf8722261873 -r 7498109ebf28 sage/finance/fractal.pyx
    a b  
    1 """
     1r"""
    22Multifractal Random Walk
    33
    4 This module implements Mandelbrot's the multifractal random walk model
    5 of asset returns as developed by Bacry, Kozhemyak, and Muzy.
    6 See
     4This module implements the fractal approach to understanding financial
     5markets that was pionered by Mandelbrot.  In particular, it implements
     6the multifractal random walk model of asset returns as developed by
     7Bacry, Kozhemyak, and Muzy, 2006, 'Continuous cascade models for asset
     8returns' and many other papers by Bacry et al. See
     9   \url{http://www.cmap.polytechnique.fr/~bacry/ftpPapers.html}
    710
     11See also Mandelbrot's 'The Misbehavior of Markets' for a motivated
     12introduction to the general idea of using a self-similar approach to
     13modeling asset returns.
     14
     15One of the main goals of this implementation is that everything by
     16highly optimized and ready for real world high performance simulation
     17work.
     18
     19AUTHOR:
     20     -- William Stein (2008)
    821"""
    922
    1023from sage.rings.all import RDF, CDF, Integer
    def fractional_brownian_motion_simulatio 
    196209    """
    197210    return fractional_gaussian_noise_simulation(H,sigma2,N,n)
    198211   
    199 def multifractal_cascade_random_walk_simulation(double T, double lambda2,
    200                                                    double ell, N, n=1):
    201     """
    202     Return a simulation of a multifractal random walk using the
    203     log-normal cascade model of Bacry-Kozhemyak-Muzy 2008.  This
    204     walk can be interpreted as the sequence of logarithms of a
    205     price series.
     212def multifractal_cascade_random_walk_simulation(double T,
     213                                                double lambda2,
     214                                                double ell,
     215                                                double sigma2,
     216                                                N,
     217                                                n=1):
     218    r"""
     219    Return a list of n simulations of a multifractal random walk using
     220    the log-normal cascade model of Bacry-Kozhemyak-Muzy 2008.  This
     221    walk can be interpreted as the sequence of logarithms of a price
     222    series.
    206223
    207224    INPUT:
    208225        T       -- positive real; the integral scale
    209226        lambda2 -- positive real; the intermittency coefficient
    210227        ell     -- a small number -- time step size.
    211         N       -- number of steps in simulation
    212         n       -- the number of simulations
     228        sigma2  -- variance of the Gaussian white noise eps[n]
     229        N       -- number of steps in each simulation
     230        n       -- the number of separate simulations to run
    213231       
    214232    OUTPUT:
    215233        list of time series
    216234
    217235    EXAMPLES:
    218236        sage: set_random_seed(0)
    219         sage: a = finance.multifractal_cascade_random_walk_simulation(3770,0.02,0.01,10,3)
     237        sage: a = finance.multifractal_cascade_random_walk_simulation(3770,0.02,0.01,0.01,10,3)
    220238        sage: a
    221239        [[-0.0096, 0.0025, 0.0066, 0.0016, 0.0078, 0.0051, 0.0047, -0.0013, 0.0003, -0.0043],
    222240         [0.0003, 0.0035, 0.0257, 0.0358, 0.0377, 0.0563, 0.0661, 0.0746, 0.0749, 0.0689],
    def multifractal_cascade_random_walk_sim 
    225243    The corresponding price series:
    226244        sage: a[0].exp()
    227245        [0.9905, 1.0025, 1.0067, 1.0016, 1.0078, 1.0051, 1.0047, 0.9987, 1.0003, 0.9957]
     246
     247    MORE DETAILS: The random walk has $n$th step $\eps_n
     248    e^{\omega_n}$, where $\eps_n$ is gaussian white noise of variance
     249    $\sigma^2$ and $\omega_n$ is renormalized gaussian magnitude,
     250    which is given by a stationary gaussian simultation associated to
     251    a certain autocovariance sequence.  See Bacry, Kozhemyak, Muzy,
     252    2006, 'Continuous cascade models for asset returns' for details.
     253
    228254    """
    229255    if ell <= 0:
    230256        raise ValueError, "ell must be positive"
    def multifractal_cascade_random_walk_sim 
    266292    cdef TimeSeries eps, steps, om
    267293    for om in omega:
    268294        # First compute N Gaussian white noise steps
    269         eps = TimeSeries(N).randomize('normal', 0, ell)
     295        eps = TimeSeries(N).randomize('normal', 0, sigma2)
    270296
    271297        # Compute the steps of the multifractal random walk.
    272298        steps = TimeSeries(N)
  • sage/finance/time_series.pyx

    diff -r bf8722261873 -r 7498109ebf28 sage/finance/time_series.pyx
    a b def linear_filter(acvs): 
    18721872    We create 100 simultations ofa multifractal random walk.  This
    18731873    models the logarithms of a stock price sequence.
    18741874        sage: set_random_seed(0)
    1875         sage: y = finance.multifractal_cascade_random_walk_simulation(3700,0.02,0.01,1000,100)
     1875        sage: y = finance.multifractal_cascade_random_walk_simulation(3700,0.02,0.01,0.01,1000,100)
    18761876
    18771877    For each walk below we replace the walk by the walk but where each
    18781878    step size is replaced by its absolute value -- this is what we
    def linear_filter(acvs): 
    18991899        0.99593284089454...
    19001900
    19011901    Now we make up an 'out of sample' sequence:
    1902         sage: y2 = finance.multifractal_cascade_random_walk_simulation(3700,0.02,0.01,1000,1)[0].diffs().abs().sums()
     1902        sage: y2 = finance.multifractal_cascade_random_walk_simulation(3700,0.02,0.01,0.01,1000,1)[0].diffs().abs().sums()
    19031903        sage: y2
    19041904        [0.0013, 0.0059, 0.0066, 0.0068, 0.0184 ... 6.8004, 6.8009, 6.8063, 6.8090, 6.8339]
    19051905
    def linear_filter(acvs): 
    19271927    How does it compare overall?  To find out we do 100 simulations
    19281928    and for each we compute the percent that our model beats naively
    19291929    using the autocovariances of the sample:
    1930         sage: y_out = finance.multifractal_cascade_random_walk_simulation(3700,0.02,0.01,1000,100)
     1930        sage: y_out = finance.multifractal_cascade_random_walk_simulation(3700,0.02,0.01,0.01,1000,100)
    19311931        sage: s1 = []; s2 = []
    19321932        sage: for v in y_out:
    19331933        ...       s1.append(sum([(v[:-i].linear_forecast(F)-v[-i])^2 for i in range(1,20)]))