Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. // timeseries analysis utilities
  2.  
  3. // Data generation
  4.  
  5. sub cpu(from, to) {
  6. demo cdn metrics 'cpu' -from from -to to -every :m:
  7. -nhosts 4 -dos .5 -dos_dur :5m: -ripple .3 -cpu_alpha 0.8
  8. | filter host ~ 'sjc*'
  9. }
  10.  
  11. sub daily_cpu(from, to) {
  12. demo cdn metrics 'cpu' -from from -to to -every :10m:
  13. -nhosts 4 -dos .3 -dos_dur :15m: -cpu_alpha 0.8 -daily 4
  14. | filter host ~ 'sjc*'
  15. }
  16.  
  17. sub bumpy_cpu(from, to) {
  18. demo cdn metrics 'cpu' -from from -to to -every :5m:
  19. -nhosts 4 -dos .3 -dos_dur :15s: -daily 3
  20. | filter host ~ 'sjc*'
  21. }
  22.  
  23. // exponentially weighted moving avarage smoother.
  24. // alpha [0..1] is the smoothing factor.
  25. reducer ewma(fld, alpha) {
  26. var yhat;
  27. var initial = true;
  28.  
  29. function update() {
  30. if (initial) {
  31. yhat = *fld;
  32. initial = false;
  33. } else {
  34. yhat = alpha * *fld + (1 - alpha) * yhat;
  35. }
  36. }
  37. function result() {
  38. return yhat;
  39. }
  40. function reset() {
  41. initial = true;
  42. }
  43. }
  44.  
  45. // Holt-Winters forecaster, which models an unobserved
  46. // level and trend component in a noisy signal, along with
  47. // optional "seasonal" effects for periodic daily variations and weekly variations
  48. // (scaling factors applied to the current level). the result is a smoothed
  49. // version of the signal which may be used to forecast future values.
  50. //
  51. // Y is the point field to smooth.
  52. // level, trend, daily, weekly are smoothing factors, numbers ranging [0..1] or null.
  53. // They determine how quickly the smoother adjusts to new values as they arrive.
  54. // Setting a factor to 0 freezes the feature at its initial estimate,
  55. // while setting a factor to 1 causes that feature to depend solely
  56. // on the most recent point. Setting a factor to null removes that feature
  57. // from the model (except for level, which must be [0..1])
  58. //
  59. // smooth assumes arriving points are equally spaced in time.
  60. // smooth returns the next-step forecast of the model, an array containing:
  61. // level, trend, daily multiplier, weekly multiplier
  62. // (level is the estimate of the next series value, trend the estimate of the
  63. // next series slope, etc).
  64. reducer smooth(Y, level, trend, daily, weekly) {
  65. var lv, tr, lv_1, tr_1;
  66. var initial = true;
  67.  
  68. function update() {
  69. if (initial) {
  70. lv = *Y;
  71. tr = 0;
  72. lv_1 = *Y;
  73. tr_1 = 0;
  74. initial = false;
  75. } else {
  76. lv = level * *Y + (1 - level) * (lv_1 + tr_1);
  77. if (trend != null) {
  78. tr = trend * (lv - lv_1) + (1 - trend) * tr_1;
  79. }
  80. lv_1 = lv;
  81. tr_1 = tr;
  82. }
  83. }
  84. function result() {
  85. return [lv + tr, tr];
  86. }
  87. function reset() {
  88. initial = true;
  89. }
  90. }
  91. // smoother/estimator for standard deviation of a time series
  92. // alpha [0..1] is the smoothing factor.
  93. reducer smooth_sd(Y, alpha) {
  94. var sigma2, last;
  95. var initial = true;
  96.  
  97. function update() {
  98. if (initial) {
  99. sigma2 = 0;
  100. last = *Y;
  101. initial = false;
  102. } else {
  103. sigma2 = alpha * (*Y - last) * (*Y - last) + (1 - alpha) * sigma2;
  104. last = *Y;
  105. }
  106. }
  107. function result() {
  108. return Math.sqrt(sigma2);
  109. }
  110. function reset() {
  111. initial = true;
  112. }
  113. }
  114. sub forecastPI(Y, HI, LOW, STDERR, z, alpha, level, trend, daily, weekly) {
  115. put state = smooth(Y, level, trend, daily, weekly)
  116. | put yhat = state[0], tr=state[1]
  117. | put err2 = (*Y - yhat) * (*Y - yhat)
  118. | put *STDERR = smooth(err2, alpha, null, null, null)[0]
  119. | put *HI = yhat + z * *STDERR
  120. | put *LOW = yhat - z * *STDERR
  121. }
  122.  
  123. sub trend_anomalies() {
  124. }
  125.  
  126. //cpu -from :2014-01-01: -to :2014-01-01T01:20:00:
  127. //daily_cpu -from :2014-01-01: -to :2014-01-02T01:20:00:
  128. bumpy_cpu -from :2014-01-01T13:00:00: -to :2014-01-01T21:00:00:
  129. | forecastPI -Y "value" -HI "hi" -LOW "low" -STDERR "stderr"
  130. -z 2 -alpha .8 -level .8 -trend .8 -daily null -weekly null
  131. | remove low, hi, state, yhat
  132. | split low, hi, stderr, value, yhat, err2,tr
  133. | @timechart
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement