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 The quantity of prior samples (by weight) used
  23. * when creating a new estimate. Those prior samples make up half of the new
  24. * estimate.
  25. * @struct
  26. * @constructor
  27. */
  28. shaka.abr.Ewma = function(halfLife) {
  29. goog.asserts.assert(halfLife > 0, 'expected halfLife to be positive');
  30. /**
  31. * Larger values of alpha expire historical data more slowly.
  32. * @private {number}
  33. */
  34. this.alpha_ = Math.exp(Math.log(0.5) / halfLife);
  35. /** @private {number} */
  36. this.estimate_ = 0;
  37. /** @private {number} */
  38. this.totalWeight_ = 0;
  39. };
  40. /**
  41. * Takes a sample.
  42. *
  43. * @param {number} weight
  44. * @param {number} value
  45. */
  46. shaka.abr.Ewma.prototype.sample = function(weight, value) {
  47. let adjAlpha = Math.pow(this.alpha_, weight);
  48. let newEstimate = value * (1 - adjAlpha) + adjAlpha * this.estimate_;
  49. if (!isNaN(newEstimate)) {
  50. this.estimate_ = newEstimate;
  51. this.totalWeight_ += weight;
  52. }
  53. };
  54. /**
  55. * @return {number}
  56. */
  57. shaka.abr.Ewma.prototype.getEstimate = function() {
  58. let zeroFactor = 1 - Math.pow(this.alpha_, this.totalWeight_);
  59. return this.estimate_ / zeroFactor;
  60. };