wbrigg

original_hist_fit

Mar 25th, 2015
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import seaborn as sns
  4. from scipy.stats.kde import gaussian_kde
  5. import math
  6. import scipy
  7.  
  8. def survivalFunction():
  9.  
  10.     data = np.random.normal(7,1,100) #Random data
  11.  
  12.     p = sns.kdeplot(data, shade=False, lw = 3)
  13.     x,y = p.get_lines()[0].get_data()
  14.     cdf = scipy.integrate.cumtrapz(y, x, initial=0)
  15.  
  16.    
  17.     plt.hist(data, 50, normed = 1, facecolor='b',alpha = 0.3)
  18.  
  19.     interp = scipy.interpolate.interp1d(x, y, kind="cubic")
  20.  
  21.     def ret_func(x):
  22.         try:
  23.           return interp(x)
  24.         except ValueError:
  25.           return 0
  26.  
  27.     return ret_func
  28.    
  29.  
  30. def surpriseFunction(mu,variance):
  31.  
  32.     hStates = np.linspace(0,20,100)
  33.     sigma = math.sqrt(variance)
  34.  
  35.     plt.plot(hStates, scipy.stats.norm.pdf(hStates, mu, sigma))
  36.    
  37.    
  38.     def ret_func(x):
  39.       return scipy.stats.norm.pdf(x, mu, sigma)
  40.  
  41.     return ret_func
  42.    
  43.  
  44.  
  45.  
  46. random_dist = survivalFunction()
  47.  
  48. def gaussian_fit(inp):
  49.   mu, sigma = inp
  50.  
  51.   def f(x):
  52.     return scipy.stats.norm.pdf(x, mu, sigma) * random_dist(x)
  53.      
  54.   return -scipy.integrate.romberg(f, 0, 20)
  55.  
  56.  
  57. mean, variance = scipy.optimize.fmin(gaussian_fit, [5,1])
  58. print mean, variance
  59.  
  60. surpriseFunction(mean, variance)
Advertisement
Add Comment
Please, Sign In to add comment