Advertisement
AmCholadawan

levyrandomstep_2D

Dec 18th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1. import random
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from datetime import datetime
  5. from scipy.stats import levy
  6.  
  7. random.seed(datetime.now())
  8.  
  9. """pick an initial random direction with uniform distribution"""
  10. directions = ['+1', '-1']
  11. direction_idx = random.randint(0, 1)
  12. direction = directions[direction_idx]
  13. print('initial random direction: '+direction+'\n')
  14.  
  15. # custom parameters
  16. loc = 0
  17. scale = 1
  18. switch_threshold = 4
  19. levy_max_extension = 1000
  20. #
  21.  
  22. plot_resolution = 50000
  23. x = np.linspace(0, levy_max_extension, plot_resolution)
  24. y = levy.pdf(x, loc, scale)
  25. plt.axvspan(switch_threshold, levy_max_extension, facecolor='b', alpha=0.05)
  26.  
  27. # plot normal distribution (loc, scale)
  28. plt.plot(x, 1/(scale * np.sqrt(2 * np.pi)) *
  29.     np.exp( - (x - switch_threshold)**2 / (2 * scale**2) ),
  30.     linewidth=2, color='g', alpha=0.4)
  31.  
  32. # plot levy distribution
  33. plt.plot(x, y, 'b-', lw=2, alpha=0.6)
  34.  
  35. # plot threshold
  36. plt.axvline(x=switch_threshold, color='red')
  37.  
  38. # rescale axis
  39. plt.axis([0,10,0,0.5])
  40.  
  41. for i in range(50):
  42.     """direction inversion using a random pick from levy distribution"""
  43.     r = levy.rvs(loc, scale)
  44.     n = np.random.normal(switch_threshold, scale)
  45.    
  46.     plt.plot(r, 0.5, 'b+')
  47.     plt.plot(n, 0.0, 'g+')
  48.    
  49.     # use > to invert direction on tail
  50.     # use < to invert direction on head
  51.     if r > switch_threshold:
  52.         direction_idx = 1 if direction_idx==0 else 0
  53.         print(str(r)+'\tinverted direction => '+directions[direction_idx])
  54.     else:
  55.         print(str(r)+'\t'+directions[direction_idx])
  56.    
  57.     print('\tnormal:', n, '\n')
  58.  
  59. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement