weighted_moving_linear_regression_unittest.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "chromecast/base/statistics/weighted_moving_linear_regression.h"
  5. #include "testing/gtest/include/gtest/gtest.h"
  6. namespace chromecast {
  7. TEST(WeightedMovingLinearRegressionTest, NotEnoughSamples) {
  8. for (int num_samples = 0; num_samples <= 2; ++num_samples) {
  9. WeightedMovingLinearRegression linear(1e6);
  10. for (int s = 0; s < num_samples; ++s)
  11. linear.AddSample(s, s, 1.0);
  12. int64_t y = 12345;
  13. double error = 12345.0;
  14. EXPECT_FALSE(linear.EstimateY(0, &y, &error));
  15. EXPECT_EQ(12345, y);
  16. EXPECT_EQ(12345.0, error);
  17. double slope = 12345.0;
  18. EXPECT_FALSE(linear.EstimateSlope(&slope, &error));
  19. EXPECT_EQ(12345.0, slope);
  20. EXPECT_EQ(12345.0, error);
  21. }
  22. }
  23. TEST(WeightedMovingLinearRegressionTest, NoXVariance) {
  24. WeightedMovingLinearRegression linear(1e6);
  25. for (int s = 0; s < 10; ++s)
  26. linear.AddSample(0, s, 1.0);
  27. int64_t y = 12345;
  28. double error = 12345.0;
  29. EXPECT_FALSE(linear.EstimateY(0, &y, &error));
  30. EXPECT_EQ(12345, y);
  31. EXPECT_EQ(12345.0, error);
  32. double slope = 12345.0;
  33. EXPECT_FALSE(linear.EstimateSlope(&slope, &error));
  34. EXPECT_EQ(12345.0, slope);
  35. EXPECT_EQ(12345.0, error);
  36. }
  37. TEST(WeightedMovingLinearRegressionTest, ZeroWeight) {
  38. WeightedMovingLinearRegression linear(1e6);
  39. for (int s = 0; s < 10; ++s)
  40. linear.AddSample(s, s, 0.0);
  41. int64_t y = 12345;
  42. double error = 12345.0;
  43. EXPECT_FALSE(linear.EstimateY(0, &y, &error));
  44. EXPECT_EQ(12345, y);
  45. EXPECT_EQ(12345.0, error);
  46. double slope = 12345.0;
  47. EXPECT_FALSE(linear.EstimateSlope(&slope, &error));
  48. EXPECT_EQ(12345.0, slope);
  49. EXPECT_EQ(12345.0, error);
  50. }
  51. TEST(WeightedMovingLinearRegressionTest, SimpleLine) {
  52. WeightedMovingLinearRegression linear(1e6);
  53. for (int s = 0; s < 3; ++s)
  54. linear.AddSample(s, s, 1.0);
  55. int64_t y;
  56. double error;
  57. EXPECT_TRUE(linear.EstimateY(20, &y, &error));
  58. EXPECT_EQ(20, y);
  59. EXPECT_DOUBLE_EQ(0.0, error);
  60. double slope;
  61. EXPECT_TRUE(linear.EstimateSlope(&slope, &error));
  62. EXPECT_DOUBLE_EQ(1.0, slope);
  63. EXPECT_DOUBLE_EQ(0.0, error);
  64. }
  65. TEST(WeightedMovingLinearRegressionTest, SimpleLineHighX) {
  66. WeightedMovingLinearRegression linear(1e6);
  67. for (int s = 0; s < 10; ++s)
  68. linear.AddSample(1000000000 + s, s, 1.0);
  69. int64_t y;
  70. double error;
  71. EXPECT_TRUE(linear.EstimateY(0, &y, &error));
  72. EXPECT_EQ(-1000000000, y);
  73. EXPECT_DOUBLE_EQ(0.0, error);
  74. EXPECT_TRUE(linear.EstimateY(1000000020, &y, &error));
  75. EXPECT_EQ(20, y);
  76. EXPECT_DOUBLE_EQ(0.0, error);
  77. double slope;
  78. EXPECT_TRUE(linear.EstimateSlope(&slope, &error));
  79. EXPECT_DOUBLE_EQ(1.0, slope);
  80. EXPECT_DOUBLE_EQ(0.0, error);
  81. }
  82. TEST(WeightedMovingLinearRegressionTest, Weighted) {
  83. WeightedMovingLinearRegression linear(1e6);
  84. // Add some weight 1.0 points on the line y = x/2, and some weight 2.0 points
  85. // on the line y = x/2 + 4.5.
  86. for (int s = 0; s < 1000; ++s) {
  87. linear.AddSample(2 * s, s, 1.0);
  88. linear.AddSample(2 * s + 1, s + 5, 2.0);
  89. }
  90. // The resulting estimate should be y = x/2 + 3.
  91. int64_t y;
  92. double error;
  93. EXPECT_TRUE(linear.EstimateY(20, &y, &error));
  94. EXPECT_EQ(13, y);
  95. EXPECT_TRUE(linear.EstimateY(-20, &y, &error));
  96. EXPECT_EQ(-7, y);
  97. EXPECT_NEAR(0.0, error, 0.1);
  98. double slope;
  99. EXPECT_TRUE(linear.EstimateSlope(&slope, &error));
  100. EXPECT_NEAR(0.5, slope, 0.001);
  101. EXPECT_NEAR(0.0, error, 0.001);
  102. }
  103. TEST(WeightedMovingLinearRegressionTest, DropOldSamples) {
  104. WeightedMovingLinearRegression linear(1999);
  105. // First add some points that will fall outside of the window.
  106. for (int s = -1000; s < 0; ++s)
  107. linear.AddSample(s, 0, 1.0);
  108. // Add some weight 1.0 points on the line y = x/2, and some weight 2.0 points
  109. // on the line y = x/2 + 4.5.
  110. for (int s = 0; s < 1000; ++s) {
  111. linear.AddSample(2 * s, s, 1.0);
  112. linear.AddSample(2 * s + 1, s + 5, 2.0);
  113. }
  114. // The resulting estimate should be y = x/2 + 3.
  115. int64_t y;
  116. double error;
  117. EXPECT_TRUE(linear.EstimateY(20, &y, &error));
  118. EXPECT_EQ(13, y);
  119. EXPECT_TRUE(linear.EstimateY(-20, &y, &error));
  120. EXPECT_EQ(-7, y);
  121. EXPECT_NEAR(0.0, error, 0.1);
  122. double slope;
  123. EXPECT_TRUE(linear.EstimateSlope(&slope, &error));
  124. EXPECT_NEAR(0.5, slope, 0.001);
  125. EXPECT_NEAR(0.0, error, 0.001);
  126. }
  127. } // namespace chromecast