Guest User

Untitled

a guest
Jan 18th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import math
  5.  
  6.  
  7.  
  8. def normal_gaussian(x, mu, h):
  9. prob = 0
  10. xmu = (x - mu)
  11. prob = np.exp(-0.5 * np.power(xmu,2) / (h * h)) / (np.sqrt(2 * math.pi) * h)
  12. #prob=((xmu<=h/2)*(xmu>=-1*h/2))
  13. return prob
  14.  
  15.  
  16. def calculate_h_optimal(data):
  17. std = np.sqrt(np.var(data))
  18. m = np.shape(data)[0]
  19. n = 1
  20. dem = (2 * n + 1) * m
  21.  
  22. h = std * np.power(4 / dem,1 / (n + 4))
  23. return h
  24.  
  25.  
  26. data = pd.read_csv("temp.txt", sep=',',header=None)
  27. data = data.values
  28.  
  29.  
  30. fig = plt.figure()
  31. ax = fig.add_subplot('111')
  32.  
  33. h_opt = calculate_h_optimal(data)
  34. min_x = np.min(data)
  35. max_x = np.max(data)
  36. number_of_axis_points = np.shape(data)[0]
  37.  
  38. x = np.linspace(min_x, max_x, number_of_axis_points)
  39.  
  40. y = np.zeros(number_of_axis_points)
  41.  
  42. for x_value in data:
  43. y = y + normal_gaussian(x,x_value,h_opt)
  44.  
  45. y = y / number_of_axis_points
  46.  
  47.  
  48. plt.fill_between(x, y, color='b', alpha=0.2)
  49. plt.plot(x, y, color='b', alpha=0.6)
  50.  
  51. plt.show()
Add Comment
Please, Sign In to add comment