Advertisement
Guest User

Untitled

a guest
Jul 8th, 2015
1,107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.52 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Spyder Editor
  4.  
  5. This is a temporary script file.
  6. """
  7. #!python
  8. """
  9. brownian() implements one dimensional Brownian motion (i.e. the Wiener process).
  10.  
  11. """
  12.  
  13. # File: brownian.py
  14.  
  15. from math import sqrt
  16. from scipy.stats import norm
  17. import numpy as np
  18.  
  19.  
  20. def brownian(x0, n, dt, delta, out=None):
  21.     """\
  22.    Generate an instance of Brownian motion (i.e. the Wiener process):
  23.  
  24.        X(t) = X(0) + N(0, delta**2 * t; 0, t)
  25.  
  26.    where N(a,b; t0, t1) is a normally distributed random variable with mean a a
  27. nd
  28.    variance b.  The parameters t0 and t1 make explicit the statistical
  29.    independence of N on different time intervals; that is, if [t0, t1) and
  30.    [t2, t3) are disjoint intervals, then N(a, b; t0, t1) and N(a, b; t2, t3)
  31.    are independent.
  32.  
  33.    Written as an iteration scheme,
  34.  
  35.        X(t + dt) = X(t) + N(0, delta**2 * dt; t, t+dt)
  36.  
  37.  
  38.    If `x0` is an array (or array-like), each value in `x0` is treated as
  39.    an initial condition, and the value returned is a numpy array with one
  40.    more dimension than `x0`.
  41.  
  42.    Arguments
  43.    ---------
  44.    x0 : float or numpy array (or something that can be converted to a numpy arr
  45. ay
  46.         using numpy.asarray(x0)).
  47.        The initial condition(s) (i.e. position(s)) of the Brownian motion.
  48.    n : int
  49.        The number of steps to take.
  50.    dt : float
  51.        The time step.
  52.    delta : float
  53.        delta determines the "speed" of the Brownian motion.  The random variabl
  54. e
  55.        of the position at time t, X(t), has a normal distribution whose mean is
  56.  
  57.        the position at time t=0 and whose variance is delta**2*t.
  58.    out : numpy array or None
  59.        If `out` is not None, it specifies the array in which to put the
  60.        result.  If `out` is None, a new numpy array is created and returned.
  61.  
  62.    Returns
  63.    -------
  64.    A numpy array of floats with shape `x0.shape + (n,)`.
  65.  
  66.    Note that the initial value `x0` is not included in the returned array.
  67.    """
  68.  
  69.     x0 = np.asarray(x0)
  70.  
  71.     # For each element of x0, generate a sample of n numbers from a
  72.     # normal distribution.
  73.     r = norm.rvs(size=x0.shape + (n,), scale=delta*sqrt(dt))
  74.  
  75.     # If `out` was not given, create an output array.
  76.     if out is None:
  77.         out = np.empty(r.shape)
  78.  
  79.     # This computes the Brownian motion by forming the cumulative sum of
  80.     # the random samples.
  81.     np.cumsum(r, axis=-1, out=out)
  82.  
  83.     # Add the initial condition.
  84.     out += np.expand_dims(x0, axis=-1)
  85.  
  86.     return out
  87.  
  88. -------------------------------------------------------------------------------------------------------------------------
  89.  
  90. # -*- coding: utf-8 -*-
  91. """
  92. Created on Wed Jul  1 02:34:50 2015
  93.  
  94. @author: Raleigh
  95. """
  96. import numpy
  97. from pylab import plot, xlabel, ylabel, title, grid, show
  98. from brownian import brownian
  99.  
  100. #This is the code that performs the main iteration of the Euler Marayuma process, from t=0 to t=10.
  101.  
  102. def main():
  103.  
  104. # The Wiener process parameter.
  105.     delta = 2
  106. # Total time.
  107. T = 10.0
  108. # Number of steps.
  109. N = 500
  110. # Time step size
  111. dt = T/N
  112. # Number of realizations to generate.
  113. m = 1000
  114. # Create an empty array to store the realizations.
  115. x = numpy.empty((m,N+1))
  116. # Initial values of x.
  117. x[:, 0] = 0
  118.  
  119. brownian(x[:,0], N, dt, delta, out=x[:,1:])
  120. t = numpy.linspace(0.0, N*dt, N+1)
  121. for k in range(m):
  122.     plot(t, x[k])
  123.     xlabel('t', fontsize=16)
  124.     ylabel('x', fontsize=16)
  125.     title('Brownian Motion of 1000 particles with x_0 = 0')
  126.     grid(True)
  127.     show()
  128. if __name__ == "__main__":
  129.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement