www.pudn.com > ghs1.2.rar > statistics.cpp


/*
 * Copyright (c) 2000-2003 Illinois Institute of Technology.
 *                         All rights reserved.
 *
 * This file is part of the GHS software package.  For license
 * information, see the LICENSE file in the top level directory of the
 * GHS source distribution.
 *
 * Released Date: August 18 2005
 * This is the GHS I release by the
 *   SCS Group,
 *   http://www.cs.iit.edu/~scs
 *   Department of Computer Science,
 *   Illinois Institute of Technology.
 *
 * This release has been tested under SUN OS 5.9.
 *
 * Supervisor
 * ----------
 * Illiniois Institute of Technology:
 * -Dr. Xian-He Sun
 *
 * Author(s)
 * -----------------
 * Illinois Institute of Technology:
 * -Ming Wu
 * -Xian-He Sun
 *
 */

#include 
#include 
#include 
#include "statistics.h"
#include "ghs.h"

using namespace std;

/* This function calculates the mean value of all the values passed
 *      by the parameter *values. The number of values is also passed in
 *      parameter num_elements
 */

double 
Statistics::CalculateMean(const vector& values)
{
  int numElements = static_cast (values.size());

  if (!numElements) {
    cout << "number of values is 0 " << endl;
    exit(1);
  }

  double mean = 0.0;
      
  for(int i = 0; i < numElements; i++)
    mean += values[i];
  mean /= numElements;

  return mean;

}

/* This function calculates the standard deviation of all the values passed
 * by the parameter *values. The number of values is also passed in 
 * parameter numElements
 */

double 
Statistics::CalculateSTD(const vector& values)
{

  double mean = 0.0, sd = 0.0, temp = 0.0, var = 0.0;

  if (!values.size()) {
    cout << "number of values is 0 " << endl;
    exit(1);
  }

  mean = CalculateMean(values);

  for(int i = 0; i < (int) values.size(); i++)
  {
    temp = values[i] - mean;
    temp = pow(temp, 2.0);
    var += temp;
  }

  if (values.size() > 1) sd = var / (values.size() - 1);
  else sd = var / values.size();

  sd = sqrt(sd);
  return sd;

}

/* This function calculates the mean value of the lastest elements passed
 * by the parameter *values. The number of elements is also passed in 
 * parameter num_elements 
 * 
 * This method needs to be modified, it should be able to choose randomaly
 * elements to calculate the mean value
 */

double 
Statistics::CalculateRandomAver(const vector &values, int maxnum, int numElements, int randvalue)
{
  int i = 0, elementIndex;
  double mean = 0.0, sd = 0.0, temp = 0.0, var = 0.0;

  srand(randvalue);

  for (i = 0; i < numElements; i++)
  {
    elementIndex = (int) ( (maxnum-1) * 1.0 * rand() / (RAND_MAX + 1.0) );
    mean += values[elementIndex];
  }
  mean /= numElements;

  return mean;

}

/* This function calculates the mean value of the J_lambda samples in
 * the last hour.
 * parameter hour_index gives the current hour's index
 */

double 
Statistics::CalculateNonZeroMean(const vector& values)
{
  int numElements = static_cast (values.size());
  int counter = 0;

  if (!numElements) {
    cout << "number of values is 0 " << endl;
    exit(1);
  }

  double sum = 0.0;
      
  for(int i = 0; i < numElements; i++) {
    sum += values[i];
    if (values[i]) counter++;
  }

  if (counter != 0) {
    return (sum / counter);
  } else return 0;

}


/* This function calculates the mean value of the J_lambda samples in 
 * the last hours. 
 * parameter hour_index gives the current hour's index
 * parameter hour_number shows the number of hours whose samples are going to
 * be calculated
 */

double 
Statistics::CalculateAdaptiveMean(int hourIndex, int hourNumber, vector< vector >& values)
{
  int counter = 0, i = 0, j = 0;
  double sum = 0.0;

  if (DEBUG_ON == 1) cout << "the Values is \n";
		
  for (i = hourIndex; i >= (hourIndex-hourNumber+1); i--)
  {
    for (j = 0; j < values[i].size() ; j++) // why start from 1;
    {
      if (DEBUG_ON == 1) cout << values[i][j] << " ";
      sum += values[i][j];
      counter++;
    }
    if (DEBUG_ON == 1) cout << endl;
  }
  
  if (counter != 0) {
    return (sum / counter);
  } else return 0; 

}

/* This function calculates the std of the J_lambda samples in 
 * the last 24 hours, if have. 
 * parameter hour_index gives the current hour's index
 * parameter hour_number shows the number of hours whose samples are going to
 * be calculated
 */
double 
Statistics::CalculateAdaptiveSTD(vector< vector >& values, int hourIndex, vector& meanvalues, vector& stdvalues )
{
  int counter = 0, i = 0, j = 0, k = 0, startingHour = 0;
  double sum = 0.0, temp;
  double average = 0.0;

  if (hourIndex >= 24) 
  {
    startingHour = hourIndex-24+1;
  }
  else 
  {
    startingHour = 0;
  }

  for (k = 0; k < (hourIndex-startingHour+1); k++) 
  {
    sum = 0.0;
    counter = 0;	
    meanvalues.push_back(CalculateAdaptiveMean(hourIndex, k+1, values));
    //average = lambdaMeanPredict[k];
    average = meanvalues[k];
    for (i = hourIndex; i >= (hourIndex-k); i--)
    {
      for (j = 0; j< values[i].size(); j++) // why start from 1;
      {
        temp = values[i][j] - average;
        temp = pow(temp,2.0);
        sum += temp;
        counter++;
      }
    }
    stdvalues.push_back(sqrt(sum/(counter-1))); 	
  }

  return (sqrt(sum / (counter - 1)));

}