Advertisement
jarekmor

HA_bayesian_rule

Oct 7th, 2021
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | None | 0 0
  1. def update_probability(prior, prob_true, prob_false=0.):
  2.     """Update probability using Bayes' rule."""
  3.     numerator = prob_true * prior
  4.     denominator = numerator + prob_false * (1 - prior)
  5.     probability = numerator / denominator
  6.     return probability
  7.  
  8. prior = 0.1
  9. list_posteriors = []
  10. threshold = 0.9
  11.  
  12. #weather_home_cloudy
  13. prob_true_1 = 0.6
  14. prob_false_1 = 0.2
  15.  
  16. sensor_cloudy = 'cloudy'
  17. try:
  18.     if sensor_cloudy == 'cloudy':
  19.         posterior = update_probability(prior, prob_true_1, prob_false_1)
  20.         prior = posterior
  21.         print('post_1: ', round(posterior,2), round(prior,2))
  22. except:
  23.     prior = prior
  24.  
  25. #weather_home_snowy
  26. prob_true_2 = 0.6
  27. prob_false_2 = 0.1
  28.  
  29. sensor_snowy = 'not_snowy'
  30. try:
  31.     if sensor_snowy == 'snowy':
  32.         posterior = update_probability(prior, prob_true_2, prob_false_2)
  33.         prior = posterior
  34.         print('post_2: ', round(posterior,2), round(prior,2))
  35. except:
  36.     prior = prior
  37.  
  38. #weather_home_rainy
  39. prob_true_3 = 0.9
  40. prob_false_3 = 0.1
  41.  
  42. sensor_rainy = 'not_rainy'
  43. try:
  44.     if sensor_rainy == 'rainy':
  45.         posterior = update_probability(prior, prob_true_3, prob_false_3)
  46.         prior = posterior
  47.         print('post_3: ', round(posterior,2), round(prior,2))
  48. except:
  49.     prior = prior
  50.  
  51. #sun.sun_above_horizon
  52. prob_true_4 = 0.5
  53. prob_false_4 = 0.5
  54.  
  55. sensor_sun_state = 'below'
  56. try:
  57.     if sensor_sun_state == 'above':
  58.         posterior = update_probability(prior, prob_true_4, prob_false_4)
  59.         prior = posterior
  60.         print('post_4: ', round(posterior,2), round(prior,2))
  61. except:
  62.     prior = prior    
  63.  
  64. #numeric_state_pressure
  65. prob_true_5 = 0.65
  66. prob_false_5 = 0.1
  67.  
  68. sensor_pressure = 1032
  69. try:
  70.     if sensor_pressure <= 1000:
  71.         posterior = update_probability(prior, prob_true_5)
  72.         prior = posterior
  73.         print('post_5: ', round(posterior,2), round(prior,2))
  74. except:
  75.     prior = prior
  76.  
  77. #numeric_state_humidity
  78. prob_true_6 = 0.5
  79. prob_false_6 = 0.1
  80.  
  81. sensor_humid = 75
  82. try:
  83.     if sensor_humid >= 70:
  84.         posterior = update_probability(prior, prob_true_6, prob_false_6)
  85.         prior = posterior
  86.         print('post_6: ', round(posterior,2), round(prior,2))
  87. except:
  88.     prior = prior
  89.  
  90.  
  91. print('posterior: ', round(posterior,2), round(prior,2))
  92.  
  93. if posterior >= threshold:
  94.     binary_sensor = True
  95.     print('Będzie deszcz')
  96. else:
  97.     binary_sensor = False
  98.     print('Nie będzie deszczu')
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement