xmd79

MathStatisticsKernelFunctions

Oct 8th, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.83 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © RicardoSantos
  3.  
  4. //@version=5
  5.  
  6. // @description TODO: add library description here
  7. library(title='MathStatisticsKernelFunctions')
  8.  
  9. // reference:
  10. // list: https://en.wikipedia.org/wiki/Kernel_%28statistics%29
  11. //
  12.  
  13.  
  14. // y+
  15. // | ---------------------------------
  16. // | | | |
  17. // | | | |
  18. // | | | |
  19. // -x -----o---------------o---------------o------ +x
  20. // -bandwidth origin (0) +bandwidth
  21.  
  22. // @function Uniform kernel.
  23. // @param distance float, distance to kernel origin.
  24. // @param bandwidth float, default=1.0, bandwidth limiter to weight the kernel.
  25. // @returns float.
  26. export uniform (float distance, float bandwidth=1.0) => //{
  27. if math.abs(distance) > bandwidth
  28. 0.0
  29. else
  30. 0.5
  31. //{ usage
  32. // b = -1 + (bar_index%100) * 0.02, plot(uniform(b, 0.9), color=color.red), plot(b, color=color.silver)
  33. //{ remarks:
  34. //}}}
  35.  
  36. // @function Triangular kernel.
  37. // @param distance float, distance to kernel origin.
  38. // @param bandwidth float, default=1.0, bandwidth limiter to weight the kernel.
  39. // @returns float.
  40. export triangular (float distance, float bandwidth=1.0) => //{
  41. if math.abs(distance) > bandwidth
  42. 0.0
  43. else
  44. 1.0 - math.abs(distance / bandwidth)
  45. //{ usage
  46. // b = -1.0 + (bar_index%100) * 0.02, plot(triangular(b, 0.75)), plot(b, color=color.red)
  47. //{ remarks:
  48. //}}}
  49.  
  50. // @function Epanechnikov kernel.
  51. // @param distance float, distance to kernel origin.
  52. // @param bandwidth float, default=1.0, bandwidth limiter to weight the kernel.
  53. // @returns float.
  54. export epanechnikov (float distance, float bandwidth=1.0) => //{
  55. if math.abs(distance) > bandwidth
  56. 0.0
  57. else
  58. 0.25 * (1.0 - math.pow(distance/bandwidth, 2.0))
  59. //{ usage
  60. // b = -1 + (bar_index%100) * 0.02, plot(epanechnikov(b, 0.9)), plot(b, color=color.red)
  61. //{ remarks:
  62. //}}}
  63.  
  64. // @function Quartic kernel.
  65. // @param distance float, distance to kernel origin.
  66. // @param bandwidth float, default=1.0, bandwidth limiter to weight the kernel.
  67. // @returns float.
  68. export quartic (float distance, float bandwidth=1.0) => //{
  69. if math.abs(distance) > bandwidth
  70. 0.0
  71. else
  72. 0.9375 * math.pow(1.0 - math.pow(distance/bandwidth, 2.0), 2.0)
  73. //{ usage
  74. // b = -1 + (bar_index%100) * 0.02, , plot(b, color=color.red)
  75. //{ remarks:
  76. //}}}
  77.  
  78. // @function Triweight kernel.
  79. // @param distance float, distance to kernel origin.
  80. // @param bandwidth float, default=1.0, bandwidth limiter to weight the kernel.
  81. // @returns float.
  82. export triweight (float distance, float bandwidth=1.0) => //{
  83. if math.abs(distance) > bandwidth
  84. 0.0
  85. else
  86. (35.0 / 32.0) * math.pow(1.0 - math.pow(distance/bandwidth, 2.0), 3.0)
  87. //{ usage
  88. // b = -1 + (bar_index%100) * 0.02, plot(triweight(b, 0.9)), plot(b, color=color.red)
  89. //{ remarks:
  90. //}}}
  91.  
  92. // @function Tricubic kernel.
  93. // @param distance float, distance to kernel origin.
  94. // @param bandwidth float, default=1.0, bandwidth limiter to weight the kernel.
  95. // @returns float.
  96. export tricubic (float distance, float bandwidth=1.0) => //{
  97. if math.abs(distance) > bandwidth
  98. 0.0
  99. else
  100. (70.0 / 81.0) * math.pow(1.0 - math.pow(math.abs(distance)/bandwidth, 3.0), 3.0)
  101. //{ usage
  102. // b = -1 + (bar_index%100) * 0.02, plot(tricubic(b, 0.9)), plot(b, color=color.red)
  103. //{ remarks:
  104. //}}}
  105.  
  106. // @function Gaussian kernel.
  107. // @param distance float, distance to kernel origin.
  108. // @param bandwidth float, default=1.0, bandwidth limiter to weight the kernel.
  109. // @returns float.
  110. export gaussian (float distance, float bandwidth=1.0) => //{
  111. 1.0 / math.sqrt(2.0 * math.pi) * math.pow(math.e, -0.5 * math.pow(distance / bandwidth, 2.0))
  112. //{ usage
  113. // b = -math.pi + (bar_index%100) * (math.pi*0.02), plot(b, color=color.red), plot(gaussian(b, 3.0)), plot(gaussian(b, 2.0), color=color.aqua), plot(gaussian(b, 1.0), color=color.yellow)
  114. //{ remarks:
  115. //}}}
  116.  
  117. // @function Cosine kernel.
  118. // @param distance float, distance to kernel origin.
  119. // @param bandwidth float, default=1.0, bandwidth limiter to weight the kernel.
  120. // @returns float.
  121. export cosine (float distance, float bandwidth=1.0) => //{
  122. if math.abs(distance) > bandwidth
  123. 0.0
  124. else
  125. (math.pi / 4.0) * math.cos(math.pi / 2.0 * (distance / bandwidth))
  126. //{ usage
  127. // b = -1 + (bar_index%100) * 0.02, plot(cosine(b, 0.5)), plot(b, color=color.red)
  128. //{ remarks:
  129. //}}}
  130.  
  131. // @function logistic kernel.
  132. // @param distance float, distance to kernel origin.
  133. // @param bandwidth float, default=1.0, bandwidth limiter to weight the kernel.
  134. // @returns float.
  135. export logistic (float distance, float bandwidth=1.0) => //{
  136. _d = distance / bandwidth
  137. 1.0 / (math.pow(math.e, _d) + 2.0 + math.pow(math.e, -_d))
  138. //{ usage
  139. // b = -1 + (bar_index%100) * 0.02, plot(logistic(b, 0.1)), plot(b, color=color.red)
  140. //{ remarks:
  141. //}}}
  142.  
  143. // @function Sigmoid kernel.
  144. // @param distance float, distance to kernel origin.
  145. // @param bandwidth float, default=1.0, bandwidth limiter to weight the kernel.
  146. // @returns float.
  147. export sigmoid (float distance, float bandwidth=1.0) => //{
  148. _d = distance / bandwidth
  149. 2.0 / math.pi * (1.0 / (math.pow(math.e, _d) + math.pow(math.e, -_d)))
  150. //{ usage
  151. // b = -1 + (bar_index%100) * 0.02, plot(sigmoid(b, 0.1)), plot(b, color=color.red)
  152. //{ remarks:
  153. //}}}
  154.  
  155. // @function Kernel selection method.
  156. // @param kernel string, kernel to select. (options="uniform", "triangle", "epanechnikov", "quartic", "triweight", "tricubic", "gaussian", "cosine", "logistic", "sigmoid")
  157. // @param distance float, distance to kernel origin.
  158. // @param bandwidth float, default=1.0, bandwidth limiter to weight the kernel.
  159. // @returns float.
  160. export select (string kernel, float distance, float bandwidth=1.0) => //{
  161. switch (kernel)
  162. 'uniform' => uniform(distance, bandwidth)
  163. 'triangle' => triangular(distance, bandwidth)
  164. 'epanechnikov' => epanechnikov(distance, bandwidth)
  165. 'quartic' => quartic(distance, bandwidth)
  166. 'triweight' => triweight(distance, bandwidth)
  167. 'tricubic' => tricubic(distance, bandwidth)
  168. 'gaussian' => gaussian(distance, bandwidth)
  169. 'cosine' => cosine(distance, bandwidth)
  170. 'logistic' => logistic(distance, bandwidth)
  171. 'sigmoid' => sigmoid(distance, bandwidth)
  172. => runtime.error('MathStatisticsKernel -> select(): "kernel" invalid kernel.'), float(na)
  173. //{ usage
  174. // b = -1 + (bar_index%100) * 0.02, plot(select('uniform', b, 0.1)), plot(b, color=color.red)
  175. //{ remarks:
  176. //}}}
  177.  
  178.  
  179.  
  180. string choice = input.string(defval='uniform', options=["uniform", "triangle", "epanechnikov", "quartic", "triweight", "gaussian", "cosine", "logistic", "sigmoid"])
  181. float bandwidth = input.float(0.5)
  182.  
  183. test = -1.0 + (bar_index % 100) * 0.02
  184.  
  185. plot(series=test, title='cycle', color=color.silver)
  186. plot(series=uniform(test, bandwidth), title='uniform', color=color.red)
  187. plot(series=triangular(test, bandwidth), title='triangle', color=color.lime)
  188. plot(series=epanechnikov(test, bandwidth), title='epanechnikov', color=color.blue)
  189. plot(series=quartic(test, bandwidth), title='quartic', color=color.aqua)
  190. plot(series=triweight(test, bandwidth), title='triweight', color=color.purple)
  191. plot(series=tricubic(test, bandwidth), title='tricubic', color=color.fuchsia)
  192. plot(series=gaussian(test, bandwidth), title='gaussian', color=color.orange)
  193. plot(series=cosine(test, bandwidth), title='cosine', color=color.yellow)
  194. plot(series=logistic(test, bandwidth), title='logistic', color=color.olive)
  195. plot(series=sigmoid(test, bandwidth), title='sigmoid', color=color.maroon)
  196.  
  197. plot(series=select(choice, test, bandwidth), title='choice', color=color.gray)
Advertisement
Add Comment
Please, Sign In to add comment