Guest User

Untitled

a guest
Apr 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. import numpy as np
  2. import math
  3. import matplotlib.pyplot as plt
  4. from random import*
  5. p=.20
  6. q=np.zeros(100)
  7. #initilizing a list
  8. y=[]
  9. #value of percentage error
  10. L=.05*p
  11. #value of sigma
  12. sigma=.005
  13. l=math.pow(L,2)
  14. #define a function which take p,sigma as an argument.
  15. def function(p,l,sigma,q):
  16. r=np.zeros(100)
  17. for i in range(0,100):
  18. y.append(np.random.normal(p,3*sigma))#add random value in each iteration
  19. ub=p+3*sigma# upper limit
  20. lb=p-3*sigma #lower limit
  21. if y[i]>ub or y[i]<lb:
  22. q[i]=np.random.randint(0,2)
  23. if q[i]==1:
  24. p=0
  25. for j in range(0,i+1):
  26. p=p+y[j]
  27. p=p/(i+1)
  28. n=3.84*p*(1-p)/l
  29. sigma=math.sqrt(l/3.84)
  30. review(y,sigma,j,p,r)
  31. else:
  32. y[i]=p
  33. return(y,ub,lb,p);
  34. #function for revising the value of p
  35. def review (y,sigma,i,p,r):
  36. ub=p+3*sigma
  37. lb=p-3*sigma
  38. for j in (0,i):
  39. if y[j]>=ub or y[j]<=lb:
  40. q[j]=np.random.randint(0,2)
  41. if r[j]==1:
  42. p=0
  43. for k in range(0,j+1):
  44. p=p+y[k]
  45. p=p/(j+1)
  46. n=3.84*p*(1-p)/l
  47. sigma=math.sqrt(l/3.84)
  48. review(y,sigma,j,p,r)
  49. else:
  50. y[j]=p
  51. return (y,ub,lb,p);
  52. s=[]
  53. x=list(range(100))
  54. y,ub,lb,p=function(p,l,sigma,q)
  55. print('the final mean:',p)
  56. for i in range(0,100):
  57. s.append(y[i])
  58. plt.plot(x,s,'bo')
  59. plt.plot([0,100],[lb,lb],label="lower bound")
  60. plt.plot([0,100],[ub,ub],label="upper bound")
  61. plt.plot([0,100],[p,p],label='mean')
  62. plt.ylabel('p value')
  63. plt.xlabel('days')
  64. plt.legend()
  65. plt.show()
Add Comment
Please, Sign In to add comment