Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import random
  3. import math
  4. import scipy.stats as sc
  5. MO = 0
  6. PV = 0.1
  7.  
  8. def gen(n):
  9. arr = [random.triangular(a,b) for i in range(2**n)]
  10. return arr
  11.  
  12. def cnt(arr,x,y):
  13. k = 0
  14. for i in range(len(arr)):
  15. if x <= arr[i] < y:
  16. k+=1
  17. return k
  18.  
  19. def FR(x):
  20. fr = (x - a)/(b - a)
  21. return fr
  22.  
  23. def cm(arr,k):
  24. cmk = 0
  25. for i in range(len(arr)):
  26. cmk += (arr[i] - MO)**k
  27. cmk/= len(arr)
  28. return cmk
  29.  
  30. def hist(arr,f,a,b):
  31. n = 100
  32. step = (b-a)/n
  33. x = a
  34. y = a+step
  35. k = 1
  36. harr = []
  37. while k<n:
  38. harr.append(cnt(arr,x,y)/(len(arr)*step))
  39. k+=1
  40. x=y
  41. y=x+step
  42. harr.append(cnt(arr, x, b) / (len(arr) * step))
  43. x = a
  44. y = a + step
  45. plt.figure(f)
  46. for i in range(len(harr)):
  47. plt.plot([x,x,y,y],[0,harr[i],harr[i],0],color = 'blue')
  48. x = y
  49. y = x + step
  50.  
  51. def ing(arr,f):
  52. step = 1/len(arr)
  53. y = a + step
  54. n = (b-a) * len(arr)
  55. iarr = [0]
  56. x = [0]
  57. for i in range(n):
  58. iarr.append(cnt(arr,a,y)/len(arr))
  59. x.append(y)
  60. y+=step
  61. plt.figure(f)
  62. plt.plot(x,iarr)
  63. fx = [i for i in range(11)]
  64. fy = [FR(i) for i in range(11)]
  65. plt.plot(fx,fy)
  66.  
  67. def ka(arr):
  68. k = cm(arr,3)/(cm(arr,2)**(3/2))
  69. return k
  70.  
  71. def ke(arr):
  72. k = cm(arr,4)/(cm(arr,2)**(4/2))
  73. return k
  74.  
  75. def kplot(arr,f):
  76. plt.figure(f)
  77. plt.plot(ka(arr),ke(arr),'ro')
  78. plt.plot(0, 1.8, 'bs')
  79. plt.axis([-0.2,4,10,1])
  80.  
  81. def eh(arr):
  82. n = 1 + math.log2(len(arr))
  83. step = (b-a)/n
  84. x = a
  85. y = a+step
  86. k = 1
  87. harr = []
  88. xh = []
  89. while k<n:
  90. harr.append(cnt(arr,x,y)/(len(arr)*step))
  91. k+=1
  92. xh.append((x+y)/2)
  93. x=y
  94. y=x+step
  95. harr.append(cnt(arr, x, b) / (len(arr) * step))
  96. xh.append((x+y)/2)
  97. e = 0
  98. trg = sc.triang(scale=40, c=0.5, loc=-20)
  99. for i in range(len(harr)):
  100. e += (1 - harr[i]/trg.pdf(xh[i]))**2
  101. e = e*0.025
  102. return e
  103.  
  104. def ec(arr):
  105. step = 1/len(arr)
  106. y = a + step
  107. n = (b-a) * len(arr)
  108. iarr = [0]
  109. x = [0]
  110. for i in range(n):
  111. iarr.append(cnt(arr,a,y)/len(arr))
  112. x.append(y)
  113. y+=step
  114. k = 0
  115. i = 0
  116. while 1:
  117. if iarr[k] == 1:
  118. i = k
  119. break
  120. k+=1
  121. e = iarr[i] - FR(x[i])
  122. return(e)
  123.  
  124. """""
  125. arr = gen(9)
  126. hist(arr,1)
  127. ing(arr,2)
  128. kplot(arr,3)
  129. print(ec(arr))
  130. plt.figure(4)
  131. e1 = []
  132. x = []
  133. for i in range(10):
  134. e1.append(ec(gen(i+1)))
  135. x.append(2**(i+1))
  136. plt.xscale('log',basex = 2)
  137. plt.plot(x,e1)
  138. plt.figure(5)
  139. e2 = []
  140. x2 = []
  141. for i in range(10):
  142. e2.append(eh(gen(i+1)))
  143. x2.append(2**(i+1))
  144. plt.xscale('log',basex = 2)
  145. plt.plot(x2,e2)
  146. plt.show()
  147. """""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement