content: /*
   * Copyright 2018 Andrey Novikov
   *
   * This program is free software: you can redistribute it and/or modify it under the
   * terms of the GNU Lesser General Public License as published by the Free Software
   * Foundation, either version 3 of the License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful, but WITHOUT ANY
   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
   * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public License along with
   * this program. If not, see <http://www.gnu.org/licenses/>.
   *
   */
  
  package mobi.maptrek.util;
  
  public class MeanValue {
      private float K = 0;
      private float n = 0;
      private float Ex = 0;
      private float Ex2 = 0;
  
      public void addValue(float x) {
          if (n == 0) {
              K = x;
          }
          n = n + 1;
          Ex = Ex + (x - K);
          Ex2 = Ex2 + (x - K) * (x - K);
      }
  
      public void removeValue(float x) {
          n = n - 1;
          Ex = Ex - (x - K);
          Ex2 = Ex2 - (x - K) * (x - K);
      }
  
      public float getMeanValue() {
          return K + Ex / n;
      }
  
      public float getVariance() {
          return (Ex2 - (Ex * Ex) / n) / (n - 1);
      }
  }
  