Advertisement
korenizla

variance and E

Nov 18th, 2022
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. hsy_rvalue = pd.Series([-7.40,
  2. -6.20,
  3. -5.00,
  4. -3.80,
  5. -2.60,
  6. -1.40,
  7. -0.20,
  8. 1.00,
  9. 2.20,
  10. 3.90,
  11. ])
  12. hsy_prob = pd.Series([0.20,
  13. 0.00,
  14. 0.00,
  15. 0.40,
  16. 2.39,
  17. 13.72,
  18. 46.52,
  19. 30.42,
  20. 5.37,
  21. 0.99,
  22. ])
  23.  
  24. def expected_value(data_rvalue, data_prob):
  25.     '''Данная функция находит математическое ожидание'''
  26.     E = 0
  27.     for x in range(len(data_rvalue)):
  28.         E += data_rvalue[x] * data_prob[x]
  29.     return E
  30. expected_value(hsy_rvalue, hsy_prob)
  31. # OUTPUT: 8.369000000000003
  32.  
  33. def variance(data_rvalue, data_prob):
  34.    
  35.     '''Данная функция находит дисперсию'''
  36.    
  37.     rvalue_probs = dict(zip(data_rvalue, data_prob))
  38.     expectation = expected_value(data_rvalue, data_prob) # E(X)
  39.     square_of_expectation = expectation ** 2 # (E(X))^2
  40.     expectation_of_squares = expected_value(data_rvalue**2, data_prob) # E(X^2)
  41.     variance = expectation_of_squares - square_of_expectation # E(X^2) - (E(X))^2
  42.    
  43.     return variance
  44.  
  45. variance(hsy_rvalue, hsy_prob)
  46. # OUTPUT: 63.06493899999994
  47.  
  48. def standard_dev(data_rvalue, data_prob):
  49.     v = variance(data_rvalue, data_prob)
  50.     standard_dev = math.sqrt(v)
  51.     return standard_dev
  52. standard_dev(hsy_rvalue, hsy_prob)
  53. # OUTPUT: 7.94134365205284
  54.  
  55. lisn_rvalue = [-3.53,
  56. -2.59,
  57. -1.66,
  58. -0.72,
  59. 0.22,
  60. 1.16,
  61. 2.10,
  62. 3.04,
  63. 3.97,
  64. 5.22
  65. ]
  66. lisn_prob = [1.19,
  67. 3.75,
  68. 10.87,
  69. 23.12,
  70. 32.61,
  71. 18.58,
  72. 6.92,
  73. 1.98,
  74. 0.59,
  75. 0.40
  76. ]
  77.  
  78. expected_value(lisn_rvalue, lisn_prob)
  79. # OUTPUT: 5.104700000000004
  80.  
  81. variance(lisn_rvalue, lisn_prob)
  82.  
  83. # ---------------------------------------------------------------------------
  84. # TypeError                                 Traceback (most recent call last)
  85. # /tmp/ipykernel_27/1282851349.py in <module>
  86. # ----> 1 variance(lisn_rvalue, lisn_prob)
  87.  
  88. # /tmp/ipykernel_27/325116310.py in variance(data_rvalue, data_prob)
  89. #       6     expectation = expected_value(data_rvalue, data_prob) # E(X)
  90. #       7     square_of_expectation = expectation ** 2 # (E(X))^2
  91. # ----> 8     expectation_of_squares = expected_value(data_rvalue**2, data_prob) # E(X^2)
  92. #       9     variance = expectation_of_squares - square_of_expectation # E(X^2) - (E(X))^2
  93. #      10
  94. #
  95. # TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement