ó
áp7]c        
   @  sÙ  d  Z  d d l m Z d d l Z d d l m Z m Z d d l j	 Z
 d e f d „  ƒ  YZ d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ e d k rÕd Z d Z g  Z d e k re ƒ  Z e j d d e ƒZ e r­e
 j ƒ  e
 j e d j ƒ Z e
 j e d j  d ƒ d d ƒZ e
 j! d ƒ n  d  „  Z" e j# e" d! d" d e ƒZ$ e r e
 j ƒ  e
 j e$ d j ƒ Z e
 j e$ d d d ƒZ e
 j! d# ƒ n  e j% j& e$ d e j' d$ e$ d d% ƒ e j( ƒ Z) e* e) ƒ e d& d' d( d) d* d+ ƒ Z+ e+ j, d! d, d d, ƒ Z- e r;e
 j ƒ  e
 j e- j ƒ Z e
 j e- j  d ƒ d d ƒZ e
 j! d- ƒ e
 j ƒ  e
 j e j. e- ƒ j ƒ Z e
 j e j. e- j  d ƒ ƒ d d ƒZ e
 j! d. ƒ n  e d& d d( d/ d* d ƒ Z/ e/ j, d! d, d d, ƒ Z0 e r¾e
 j ƒ  e
 j e0 j ƒ Z e
 j e0 j  d ƒ d d ƒZ e
 j! d0 ƒ n  e d& d d( d d1 d+ d* d2 ƒ Z1 e1 j, ƒ  Z2 e1 j3 d d e j4 j5 d3 dF ƒ ƒ Z6 e1 j3 d e j7 d d5 d5 d2 ƒ d ƒ e1 j8 d d5 ƒ e* e1 j8 d d5 d6 d2 d d5 ƒj  d ƒ ƒ e1 j8 d d, d6 d2 d d, ƒZ9 e rÞe
 j ƒ  e
 j e9 j ƒ Z e
 j e9 j  d ƒ d d ƒZ e
 j! d7 ƒ n  e d& d d( d d8 d+ d* d2 ƒ Z: e: j8 d d9 d6 d2 d d, ƒZ; e* e; j  d ƒ ƒ e* e j. e; j  d ƒ ƒ ƒ d Z e r¢e
 j ƒ  e
 j e; j ƒ Z e
 j e; j  d ƒ d d ƒZ e
 j! d: ƒ n  e* e: j< e; d d d … f d; d2 ƒƒ e: j8 d d" d6 d2 d d4 ƒZ= e* d< ƒ x1 e> d4 ƒ D]# Z? e* e: j< e= e? d; d2 ƒƒ q We ƒ  Z@ e@ jA d d+ d= d d" d6 d' d* d2 ƒ\ ZB ZC ZD e re
 j ƒ  e
 j eB j ƒ Z e
 j eB j  d ƒ d d ƒZ e
 j! d> ƒ e
 j ƒ  e
 j eD d? d@ dA ƒe
 j eB jE d ƒ d@ dB ƒe
 j! dC ƒ e
 jF ƒ  qn  e d d g e j4 j5 e j4 j5 g ƒ ZG eG jA d! dD d dE ƒ ZH e* eH d jI d ƒ jI d ƒ ƒ e* eH d jI ƒ  ƒ e* eH d j  d ƒ j  d ƒ ƒ e* eH d j  ƒ  ƒ e* eH d jJ ƒ e* eH d jI ƒ  ƒ n  d S(G   s6  getting started with diffusions, continuous time stochastic processes

Author: josef-pktd
License: BSD


References
----------

An Algorithmic Introduction to Numerical Simulation of Stochastic Differential
Equations
Author(s): Desmond J. Higham
Source: SIAM Review, Vol. 43, No. 3 (Sep., 2001), pp. 525-546
Published by: Society for Industrial and Applied Mathematics
Stable URL: http://www.jstor.org/stable/3649798

http://www.sitmo.com/  especially the formula collection


Notes
-----

OU process: use same trick for ARMA with constant (non-zero mean) and drift
some of the processes have easy multivariate extensions

*Open Issues*

include xzero in returned sample or not? currently not

*TODOS*

* Milstein from Higham paper, for which processes does it apply
* Maximum Likelihood estimation
* more statistical properties (useful for tests)
* helper functions for display and MonteCarlo summaries (also for testing/checking)
* more processes for the menagerie (e.g. from empirical papers)
* characteristic functions
* transformations, non-linear e.g. log
* special estimators, e.g. Ait Sahalia, empirical characteristic functions
* fft examples
* check naming of methods, "simulate", "sample", "simexact", ... ?



stochastic volatility models: estimation unclear

finance applications ? option pricing, interest rate models


iÿÿÿÿ(   t   print_functionN(   t   statst   signalt	   Diffusionc           B  sA   e  Z d  Z d „  Z d d d d d „ Z d d d d d „ Z RS(   s:   Wiener Process, Brownian Motion with mu=0 and sigma=1
    c         C  s   d  S(   N(    (   t   self(    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyt   __init__=   s    id   i   c         C  sp   | d | } t  j | d | ƒ } t  j | ƒ t  j j d | | f ƒ } t  j | d ƒ } | |  _ | | f S(   s*   generate sample of Wiener Process
        g      ð?i   t   size(   t   npt   linspacet   sqrtt   randomt   normalt   cumsumt   dW(   R   t   nobst   Tt   dtt   nreplt   tR   t   W(    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyt	   simulateW@   s    (	c   
   	   C  sU   |  j  d | d | d | d | ƒ \ } } | | | ƒ } | j d ƒ }	 | |	 | f S(   sm   get expectation of a function of a Wiener Process by simulation

        initially test example from
        R   R   R   R   i    (   R   t   mean(
   R   t   funcR   R   R   R   R   R   t   Ut   Umean(    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyt   expectedsimJ   s    *N(   t   __name__t
   __module__t   __doc__R   t   NoneR   R   (    (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR   :   s   	
t   AffineDiffusionc           B  sG   e  Z d  Z d „  Z d d d d d „ Z d d d d d d d „ Z RS(   sò   

    differential equation:

    :math::
    dx_t = f(t,x)dt + \sigma(t,x)dW_t

    integral:

    :math::
    x_T = x_0 + \int_{0}^{T}f(t,S)dt + \int_0^T  \sigma(t,S)dW_t

    TODO: check definition, affine, what about jump diffusion?

    c         C  s   d  S(   N(    (   R   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR   e   s    id   i   c   
   	   C  sr   |  j  d | d | d | d | ƒ \ } } |  j ƒ  |  j ƒ  | } t j | d ƒ } | j d ƒ }	 | |	 | f S(   NR   R   R   R   i   i    (   R   t   _driftt   _sigR   R   R   (
   R   R   R   R   R   R   R   t   dxt   xt   xmean(    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyt   simh   s
    *i   c      	   C  sk  | | } | d k r" |  j } n  | d k r? | d | } n  |  j d | d | d | d | ƒ \ } } |  j }	 t j | d | ƒ } | | }
 | | } t j | | f ƒ } | } | | d d … d f <x˜ t j d | ƒ D]„ } t j |	 d d … t j | | d d | | ƒ f d ƒ } | |  j	 d	 | ƒ |  j
 d	 | ƒ | } | | d d … | f <qß W| S(
   s7  

        from Higham 2001

        TODO: reverse parameterization to start with final nobs and DT
        TODO: check if I can skip the loop using my way from exactprocess
              problem might be Winc (reshape into 3d and sum)
        TODO: (later) check memory efficiency for large simulations
        g      ð?R   R   R   R   i   Ni    R"   (   R   t   xzeroR   R   R   R   t   zerost   aranget   sumR   R    (   R   R%   R   R   R   R   t   TratioR   R   R   t   Dtt   Lt   Xemt   Xtempt   jt   Winc(    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyt   simEMq   s$    
*	

>*N(   R   R   R   R   R   R$   R0   (    (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR   T   s   		t   ExactDiffusionc           B  s/   e  Z d  Z d „  Z d d d „ Z d „  Z RS(   st   Diffusion that has an exact integral representation

    this is currently mainly for geometric, log processes

    c         C  s   d  S(   N(    (   R   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR   §   s    g      ð?i   c   	      C  sˆ   t  j | | | | ƒ } t  j |  j | ƒ } t  j j d | | f ƒ } |  j | ƒ |  j | ƒ | } t j	 d g d | g | ƒ S(   s`   ddt : discrete delta t



        should be the same as an AR(1)
        not tested yet
        R   g      ð?(
   R   R   t   expt   lambdR
   R   t   _exactconstt	   _exactstdR   t   lfilter(	   R   R%   R   t   ddtR   R   t   expddtt   normrvst   inc(    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyt   exactprocessª   s
     c         C  sS   t  j |  j | ƒ } | | |  j | ƒ } |  j | ƒ } t j d | d | ƒ S(   Nt   loct   scale(   R   R2   R3   R4   R5   R   t   norm(   R   R%   R   t   expntt   meantt   stdt(    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyt	   exactdist»   s    (   R   R   R   R   R;   RB   (    (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR1       s   	t   ArithmeticBrownianc           B  sD   e  Z d  Z d „  Z d „  Z d „  Z d d d d „ Z d „  Z RS(	   s2   
    :math::
    dx_t &= \mu dt + \sigma dW_t
    c         C  s   | |  _  | |  _ | |  _ d  S(   N(   R%   t   mut   sigma(   R   R%   RD   RE   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR   Ç   s    		c         O  s   |  j  S(   N(   RD   (   R   t   argst   kwds(    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR   Ì   s    c         O  s   |  j  S(   N(   RE   (   R   RF   RG   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR    Î   s    g      ð?i   c         C  s   | d k r |  j } n  t j | | | | ƒ } t j j d | | f ƒ } |  j |  j t j | ƒ | } | t j	 | d ƒ S(   s7   ddt : discrete delta t

        not tested yet
        R   i   N(
   R   R%   R   R   R
   R   R   t   _sigmaR	   R   (   R   R   R%   R7   R   R   R9   R:   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR;   Ð   s    !c         C  sP   t  j |  j | ƒ } |  j | } |  j t  j | ƒ } t j d | d | ƒ S(   NR<   R=   (   R   R2   R3   R   RH   R	   R   R>   (   R   R%   R   R?   R@   RA   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyRB   Ý   s    N(	   R   R   R   R   R   R    R   R;   RB   (    (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyRC   Á   s   			t   GeometricBrownianc           B  s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   sý   Geometric Brownian Motion

    :math::
    dx_t &= \mu x_t dt + \sigma x_t dW_t

    $x_t $ stochastic process of Geometric Brownian motion,
    $\mu $ is the drift,
    $\sigma $ is the Volatility,
    $W$ is the Wiener process (Brownian motion).

    c         C  s   | |  _  | |  _ | |  _ d  S(   N(   R%   RD   RE   (   R   R%   RD   RE   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR   ð   s    		c         O  s   | d } |  j  | S(   NR"   (   RD   (   R   RF   RG   R"   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR   õ   s    
c         O  s   | d } |  j  | S(   NR"   (   RE   (   R   RF   RG   R"   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR    ø   s    
(   R   R   R   R   R   R    (    (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyRI   ä   s   		t	   OUprocessc           B  sS   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d d d „ Z d „  Z d	 „  Z	 RS(
   s¢   Ornstein-Uhlenbeck

    :math::
      dx_t&=\lambda(\mu - x_t)dt+\sigma dW_t

    mean reverting process



    TODO: move exact higher up in class hierarchy
    c         C  s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   R%   R3   RD   RE   (   R   R%   RD   R3   RE   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR   	  s    			c         O  s   | d } |  j  |  j | S(   NR"   (   R3   RD   (   R   RF   RG   R"   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR     s    
c         O  s   | d } |  j  | S(   NR"   (   RE   (   R   RF   RG   R"   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR      s    
c         C  sY   t  j |  j | ƒ } | | |  j d | |  j t  j d | | d |  j ƒ | S(   Ni   g       @(   R   R2   R3   RD   RE   R	   (   R   R%   R   R9   R?   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyt   exact  s    g      ð?i   c         C  sË   t  j | | | | ƒ } t  j |  j | ƒ } t  j |  j | ƒ } t  j j d | | f ƒ } d d l m }	 |  j d | |  j	 t  j
 d | | d |  j ƒ | }
 |	 j d g d | g |
 ƒ S(   s¢   ddt : discrete delta t

        should be the same as an AR(1)
        not tested yet
        # after writing this I saw the same use of lfilter in sitmo
        R   iÿÿÿÿ(   R   i   g       @g      ð?(   R   R   R2   R3   R
   R   t   scipyR   RD   RE   R	   R6   (   R   R%   R   R7   R   R   R?   R8   R9   R   R:   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR;     s    .c         C  s   t  j |  j | ƒ } | | |  j d | } |  j t  j d | | d |  j ƒ } d d l m } | j d | d | ƒ S(   Ni   g       @iÿÿÿÿ(   R   R<   R=   (	   R   R2   R3   RD   RE   R	   RL   R   R>   (   R   R%   R   R?   R@   RA   R   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyRB   2  s
    )c         C  s×   t  | ƒ d } t j t j | ƒ | d  f ƒ } t j j | | d d d ƒ\ } } } } | \ }	 }
 | | d } t j |
 ƒ | } t j | d t j |
 ƒ d |
 d | ƒ } |	 d |
 } | | | f S(   sN   assumes data is 1d, univariate time series
        formula from sitmo
        i   iÿÿÿÿt   rcondg       @i   (   t   lenR   t   column_stackt   onest   linalgt   lstsqt   logR	   (   R   t   dataR   R   t   exogt   parestt   rest   rankt   singt   constt   slopet   errvarR3   RE   RD   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyt   fitls;  s    "+1(
   R   R   R   R   R   R    RK   R;   RB   R]   (    (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyRJ   ý   s   							t   SchwartzOnec           B  sJ   e  Z d  Z d „  Z d „  Z d „  Z d d d „ Z d „  Z d „  Z RS(	   sÏ   the Schwartz type 1 stochastic process

    :math::
    dx_t = \kappa (\mu - \ln x_t) x_t dt + \sigma x_tdW \

    The Schwartz type 1 process is a log of the Ornstein-Uhlenbeck stochastic
    process.

    c         C  s1   | |  _  | |  _ | |  _ | |  _ | |  _ d  S(   N(   R%   RD   t   kappaR3   RE   (   R   R%   RD   R_   RE   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR   V  s
    				c         C  s%   d | |  j  |  j d d |  j S(   Ni   i   g       @(   RD   RE   R_   (   R   R?   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR4   ]  s    c         C  s'   |  j  t j d | | d |  j ƒ S(   Ni   g       @(   RE   R   R	   R_   (   R   R?   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR5   `  s    g      ð?i   c         C  sF   t  j | ƒ } t |  j |  ƒ j | | d | d | ƒ} t  j | ƒ S(   s/   uses exact solution for log of process
        R7   R   (   R   RS   t   supert	   __class__R;   R2   (   R   R%   R   R7   R   t   lnxzerot   lnx(    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR;   c  s    *c         C  s\   t  j |  j | ƒ } t  j | ƒ | |  j | ƒ } |  j | ƒ } t j d | d | ƒ S(   NR<   R=   (   R   R2   R3   RS   R4   R5   R   t   lognorm(   R   R%   R   R?   R@   RA   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyRB   j  s     c         C  sF  t  | ƒ d } t j t j | ƒ t j | d  ƒ f ƒ } t j j | t j | d ƒ d d ƒ\ } } } } | \ }	 }
 | | d } t j |
 ƒ | } t j | | d t j d | | ƒ ƒ } |	 d t j | | ƒ | d d | } t j	 | ƒ d k r| d } n  t j	 | ƒ d	 k r9| d } n  | | | f S(
   sN   assumes data is 1d, univariate time series
        formula from sitmo
        i   iÿÿÿÿRM   g       @iþÿÿÿi   i    (   i   (   i   (
   RN   R   RO   RP   RS   RQ   RR   R	   R2   t   shape(   R   RT   R   R   RU   RV   RW   RX   RY   RZ   R[   R\   R_   RE   RD   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR]   q  s    +4,,  (	   R   R   R   R   R4   R5   R;   RB   R]   (    (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR^   K  s   					t   BrownianBridgec           B  s#   e  Z d  „  Z d d d d „ Z RS(   c         C  s   d  S(   N(    (   R   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR   †  s    i   g      ð?c         C  s‡  | d } | d | } t  j | | | | ƒ } t  j | | | ƒ } | | d | | g }	 d | | | }
 | | | | } | t  j | d | | ƒ } | t  j | | | | | | ƒ } t  j | | f ƒ } | | d  d  … d f <| t  j j d | | f ƒ } xe t d | ƒ D]T } | d  d  … | d f |
 | | | | d  d  … | f | d  d  … | f <q"W| | | f S(   Ni   g      ð?i    R   (   R   R   R	   R&   R
   R   t   range(   R   t   x0t   x1R   R   R7   RE   R   R   t   wmt   wmit   wm1t   sut   sR"   t   rvst   i(    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyt   simulate‰  s    
'R(   R   R   R   Rq   (    (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyRf   …  s   	t   CompoundPoissonc           B  s,   e  Z d  Z e j j d „ Z d d „ Z RS(   sC   nobs iid compound poisson distributions, not a process in time
    c         C  sU   t  | ƒ t  | ƒ k r' t d ƒ ‚ n  t  | ƒ |  _ | |  _ t j | ƒ |  _ d  S(   Ns9   lambd and randfn need to have the same number of elements(   RN   t
   ValueErrort   nobjt   randfnR   t   asarrayR3   (   R   R3   Ru   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyR      s
    	i   c         C  sP  |  j  } t j | | | f ƒ } t j j |  j d  d  d  d  … f d | | | f ƒ} xÛ t | ƒ D]Í } |  j | } | d  d  … d  d  … | f } | d | | t j	 | ƒ f ƒ }	 t
 d |	 j ƒ  |	 j ƒ |	 j d ƒ t j | ƒ d  d  … d  f t j | ƒ | d f }
 |
 | d  d  … d  d  … | f <qe Wd | | d k <| | f S(   NR   s	   rvs.sum()iÿÿÿÿi   i    (   Rt   R   R&   R
   t   poissonR3   R   Rg   Ru   t   maxt   printR(   Re   R   R'   (   R   R   R   Rt   R"   t   Nt   iot   randfnct   ncRo   t   xio(    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyRq   ¨  s    	7!B#(   R   R   R   R   R
   R   R   Rq   (    (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyRr     s   t   __main__i   iè  t   allR   i    t	   linewidthi   s)   Standard Brownian Motion (Wiener Process)c         C  s   t  j |  d | ƒ S(   Ng      à?(   R   R2   (   R   R   (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyt   <lambda>æ  t    R   iô  s   Brownian Motion - expi	   g       @R%   g      ð?RD   g{®Gáz„?RE   g      à?id   s   Geometric Brownians$   Geometric Brownian - log-transformedgš™™™™™©?s   Arithmetic BrownianR3   gš™™™™™¹?R   i   i
   R7   s   Ornstein-UhlenbeckR_   i2   s   Schwartz OneR   s    true: mu=1, kappa=0.5, sigma=0.1ic   s   Brownian Bridget   rt   labelt   theoreticalt	   simulateds   Brownian Bridge - Variancei N  i   (   i   i
   (K   R   t
   __future__R    t   numpyR   RL   R   R   t   matplotlib.pyplott   pyplott   pltt   objectR   R   R1   RC   RI   RJ   R^   Rf   Rr   R   t   doplotR   t   examplest   wR   t   wst   figuret   plotR   t   tmpR   t   titleR   R   t   usRQ   R>   R2   t   inft   averrRy   t   gbR0   t   gbsRS   t   abt   abst   out   ousRK   R
   R   t   oueR   R;   t   ouest   sot   sosR]   t   sos2Rg   Rp   t   bbRq   t   bbsR   Rj   t   stdt   legendt   cpt   cpsR(   R   (    (    (    s@   lib/python2.7/site-packages/statsmodels/sandbox/tsa/diffusion.pyt   <module>2   sÆ   L!#N:7	
"	
5


'
!$&+
!
)
!	0

'  