Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Verify that f(x)=(3/26)*x**2 on [1,3] is a probability density function
  4. In order to be a probability density function,
  5. 1. f(x)>/=0 for all x in the interval [1,3]
  6. 2. the anti-derivative of f(x) over the interval must equal 1
  7. '''
  8. # f(x) is indeed positive over the interval so the first condition is satisfied
  9. # to test the second condition, we take the anti-derivative of f(x) to see if it equals 1
  10.  
  11. import scipy
  12. from scipy.integrate import quad
  13. def integrand(x):
  14. return (3/26)*x**2
  15. ans1, err = quad(integrand, 1, 3)
  16. print (ans1)
  17.  
  18. # The mean is obtained by taking the anti-derivative of xf(x) over the interval
  19.  
  20. def integrand(x):
  21. return x*(3/26)*x**2
  22. ans2, err = quad(integrand, 1, 3)
  23. print (ans2)
  24.  
  25. # Variance is obtained by taking the anti-deritvative of (x-µ)**2*f(x)
  26.  
  27. def integrand(x):
  28. return ((x-ans2)**2)*((3/26)*x**2)
  29. ans3, err = quad(integrand, 1, 3)
  30. print (ans3)
  31.  
  32. # The standard deviation is found by taking the square-root of Variance
  33.  
  34. print (ans3**(1/2))
  35.  
  36. '''
  37. To test if it is a normal distribution, the anti-derivative of the function found on Lial page 1024
  38. should yield an answer of 1 over the interval of negative infinity to infinity.
  39. To test this, I used the interval [-100, 100]
  40. '''
  41.  
  42. import math
  43. def integrand(x):
  44. return (1/(ans3*((2*math.pi)**(1/2))))*(math.e**((-(x-ans1)**2)/(2*ans3**2)))
  45. ans4, err = quad(integrand, -100, 100)
  46. print (ans4)
  47.  
  48. # Graph all three functions together
  49.  
  50. import matplotlib.pyplot as plt
  51. from numpy import linspace
  52. x = linspace(1, 3, 100)
  53. y1 = (3/26)*x**2
  54. y2 = (3/26)*x**3
  55. y3 = (x-30/13)*(3/26)*x**3
  56. plt.xlabel('x-axis')
  57. plt.ylabel('y-axis')
  58. plt.plot(x, y1, 'r')
  59. plt.plot(x, y2, 'b')
  60. plt.plot(x, y3, 'g')
  61. plt.legend(['f(x)', 'g(x)', 'h(x)'])
  62. plt.title('Probability Density Functions')
  63. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement