Source: lib/abr/ewma.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.abr.Ewma');
  18. goog.require('goog.asserts');
  19. /**
  20. * Computes an exponentionally-weighted moving average.
  21. *
  22. * @param {number} halfLife About half of the estimated value will be from the
  23. * last |halfLife| samples by weight.
  24. * @struct
  25. * @constructor
  26. */
  27. shaka.abr.Ewma = function(halfLife) {
  28. goog.asserts.assert(halfLife > 0, 'expected halfLife to be positive');
  29. /**
  30. * Larger values of alpha expire historical data more slowly.
  31. * @private {number}
  32. */
  33. this.alpha_ = Math.exp(Math.log(0.5) / halfLife);
  34. /** @private {number} */
  35. this.estimate_ = 0;
  36. /** @private {number} */
  37. this.totalWeight_ = 0;
  38. };
  39. /**
  40. * Takes a sample.
  41. *
  42. * @param {number} weight
  43. * @param {number} value
  44. */
  45. shaka.abr.Ewma.prototype.sample = function(weight, value) {
  46. var adjAlpha = Math.pow(this.alpha_, weight);
  47. var newEstimate = value * (1 - adjAlpha) + adjAlpha * this.estimate_;
  48. if (!isNaN(newEstimate)) {
  49. this.estimate_ = newEstimate;
  50. this.totalWeight_ += weight;
  51. }
  52. };
  53. /**
  54. * @return {number}
  55. */
  56. shaka.abr.Ewma.prototype.getEstimate = function() {
  57. var zeroFactor = 1 - Math.pow(this.alpha_, this.totalWeight_);
  58. return this.estimate_ / zeroFactor;
  59. };