libMesh::StatisticsVector< T > Class Template Reference

#include <statistics.h>

List of all members.

Public Member Functions

 StatisticsVector (dof_id_type i=0)
 StatisticsVector (dof_id_type i, T val)
virtual ~StatisticsVector ()
virtual Real l2_norm () const
virtual T minimum () const
virtual T maximum () const
virtual Real mean () const
virtual Real median ()
virtual Real median () const
virtual Real variance () const
virtual Real variance (const Real known_mean) const
virtual Real stddev () const
virtual Real stddev (const Real known_mean) const
void normalize ()
virtual void histogram (std::vector< dof_id_type > &bin_members, unsigned int n_bins=10)
void plot_histogram (const std::string &filename, unsigned int n_bins)
virtual void histogram (std::vector< dof_id_type > &bin_members, unsigned int n_bins=10) const
virtual std::vector< dof_id_typecut_below (Real cut) const
virtual std::vector< dof_id_typecut_above (Real cut) const

Detailed Description

template<typename T>
class libMesh::StatisticsVector< T >

The StatisticsVector class is derived from the std::vector<> and therefore has all of its useful features. It was designed to not have any internal state, i.e. no public or private data members. Also, it was only designed for classes and types for which the operators +,*,/ have meaining, specifically floats, doubles, ints, etc. The main reason for this design decision was to allow a std::vector<> to be successfully cast to a StatisticsVector, thereby enabling its additional functionality. We do not anticipate any problems with deriving from an stl container which lacks a virtual destructor in this case.

Where manipulation of the data set was necessary (for example sorting) two versions of member functions have been implemented. The non-const versions perform sorting directly in the data set, invalidating pointers and changing the entries. const versions of the same functions are generally available, and will be automatically invoked on const StatisticsVector objects. A draw-back to the const versions is that they simply make a copy of the original object and therefore double the original memory requirement for the data set.

Most of the actual code was copied or adapted from the GNU Scientific Library (GSL). More precisely, the recursion relations for computing the mean were implemented in order to avoid possible problems with buffer overruns.

Author:
John W. Peterson, 2002

Definition at line 77 of file statistics.h.


Constructor & Destructor Documentation

template<typename T>
libMesh::StatisticsVector< T >::StatisticsVector ( dof_id_type  i = 0  )  [inline, explicit]

Call the std::vector constructor.

Definition at line 85 of file statistics.h.

00085 : std::vector<T> (i) {}

template<typename T>
libMesh::StatisticsVector< T >::StatisticsVector ( dof_id_type  i,
val 
) [inline]

Call the std::vector constructor, fill each entry with val

Definition at line 90 of file statistics.h.

00090 : std::vector<T> (i,val) {}

template<typename T>
virtual libMesh::StatisticsVector< T >::~StatisticsVector (  )  [inline, virtual]

Destructor. Virtual so we can derive from the StatisticsVector

Definition at line 95 of file statistics.h.

00095 {}


Member Function Documentation

template<typename T >
std::vector< dof_id_type > libMesh::StatisticsVector< T >::cut_above ( Real  cut  )  const [inline, virtual]

Returns a vector of dof_id_types which correspond to the indices of every member of the data set above the cutoff value cut. I chose not to combine these two functions since the interface is cleaner with one passed parameter instead of two.

Reimplemented in libMesh::ErrorVector.

Definition at line 365 of file statistics.C.

00366 {
00367   START_LOG ("cut_above()", "StatisticsVector");
00368 
00369   const dof_id_type n   = this->size();
00370 
00371   std::vector<dof_id_type> cut_indices;
00372   cut_indices.reserve(n/2);  // Arbitrary
00373 
00374   for (dof_id_type i=0; i<n; i++)
00375     {
00376       if ((*this)[i] > cut)
00377         {
00378           cut_indices.push_back(i);
00379         }
00380     }
00381 
00382   STOP_LOG ("cut_above()", "StatisticsVector");
00383 
00384   return cut_indices;
00385 }

template<typename T >
std::vector< dof_id_type > libMesh::StatisticsVector< T >::cut_below ( Real  cut  )  const [inline, virtual]

Returns a vector of dof_id_types which correspond to the indices of every member of the data set below the cutoff value "cut".

Reimplemented in libMesh::ErrorVector.

Definition at line 339 of file statistics.C.

00340 {
00341   START_LOG ("cut_below()", "StatisticsVector");
00342 
00343   const dof_id_type n   = this->size();
00344 
00345   std::vector<dof_id_type> cut_indices;
00346   cut_indices.reserve(n/2);  // Arbitrary
00347 
00348   for (dof_id_type i=0; i<n; i++)
00349     {
00350       if ((*this)[i] < cut)
00351         {
00352           cut_indices.push_back(i);
00353         }
00354     }
00355 
00356   STOP_LOG ("cut_below()", "StatisticsVector");
00357 
00358   return cut_indices;
00359 }

template<typename T >
void libMesh::StatisticsVector< T >::histogram ( std::vector< dof_id_type > &  bin_members,
unsigned int  n_bins = 10 
) const [inline, virtual]

A const version of the histogram function.

Definition at line 327 of file statistics.C.

References libMesh::StatisticsVector< T >::histogram().

00329 {
00330   StatisticsVector<T> sv = (*this);
00331 
00332   return sv.histogram(bin_members, n_bins);
00333 }

template<typename T >
void libMesh::StatisticsVector< T >::histogram ( std::vector< dof_id_type > &  bin_members,
unsigned int  n_bins = 10 
) [inline, virtual]

Computes and returns a histogram with n_bins bins for the data set. For simplicity, the bins are assumed to be of uniform size. Upon return, the bin_members vector will contain unsigned integers which give the number of members in each bin. WARNING: This non-const function sorts the vector, changing its order. Source: GNU Scientific Library

Definition at line 189 of file statistics.C.

References end, std::max(), libMesh::StatisticsVector< T >::maximum(), std::min(), libMesh::StatisticsVector< T >::minimum(), libMesh::out, and libMesh::Real.

Referenced by libMesh::StatisticsVector< T >::histogram(), and libMesh::StatisticsVector< T >::plot_histogram().

00191 {
00192   // Must have at least 1 bin
00193   libmesh_assert (n_bins>0);
00194 
00195   const dof_id_type n   = this->size();
00196 
00197   std::sort(this->begin(), this->end());
00198 
00199   // The StatisticsVector can hold both integer and float types.
00200   // We will define all the bins, etc. using Reals.
00201   Real min      = static_cast<Real>(this->minimum());
00202   Real max      = static_cast<Real>(this->maximum());
00203   Real bin_size = (max - min) / static_cast<Real>(n_bins);
00204 
00205   START_LOG ("histogram()", "StatisticsVector");
00206 
00207   std::vector<Real> bin_bounds(n_bins+1);
00208   for (unsigned int i=0; i<bin_bounds.size(); i++)
00209     bin_bounds[i] = min + i * bin_size;
00210 
00211   // Give the last bin boundary a little wiggle room: we don't want
00212   // it to be just barely less than the max, otherwise our bin test below
00213   // may fail.
00214   bin_bounds.back() += 1.e-6 * bin_size;
00215 
00216   // This vector will store the number of members each bin has.
00217   bin_members.resize(n_bins);
00218 
00219   dof_id_type data_index = 0;
00220   for (unsigned int j=0; j<bin_members.size(); j++) // bin vector indexing
00221     {
00222       // libMesh::out << "(debug) Filling bin " << j << std::endl;
00223 
00224       for (dof_id_type i=data_index; i<n; i++) // data vector indexing
00225         {
00226           //    libMesh::out << "(debug) Processing index=" << i << std::endl;
00227           Real current_val = static_cast<Real>( (*this)[i] );
00228 
00229           // There may be entries in the vector smaller than the value
00230           // reported by this->minimum().  (e.g. inactive elements in an
00231           // ErrorVector.)  We just skip entries like that.
00232           if ( current_val < min )
00233             {
00234               //            libMesh::out << "(debug) Skipping entry v[" << i << "]="
00235               //                      << (*this)[i]
00236               //                      << " which is less than the min value: min="
00237               //                      << min << std::endl;
00238               continue;
00239             }
00240 
00241           if ( current_val > bin_bounds[j+1] ) // if outside the current bin (bin[j] is bounded
00242                                                // by bin_bounds[j] and bin_bounds[j+1])
00243             {
00244               // libMesh::out.precision(16);
00245               //            libMesh::out.setf(std::ios_base::fixed);
00246               //            libMesh::out << "(debug) (*this)[i]= " << (*this)[i]
00247               //                      << " is greater than bin_bounds[j+1]="
00248               //                      << bin_bounds[j+1]         << std::endl;
00249               data_index = i; // start searching here for next bin
00250               break; // go to next bin
00251             }
00252 
00253           // Otherwise, increment current bin's count
00254           bin_members[j]++;
00255           // libMesh::out << "(debug) Binned index=" << i << std::endl;
00256         }
00257     }
00258 
00259 #ifdef DEBUG
00260   // Check the number of binned entries
00261   const dof_id_type n_binned = std::accumulate(bin_members.begin(),
00262                                                bin_members.end(),
00263                                                static_cast<dof_id_type>(0),
00264                                                std::plus<dof_id_type>());
00265 
00266   if (n != n_binned)
00267     {
00268       libMesh::out << "Warning: The number of binned entries, n_binned="
00269                 << n_binned
00270                 << ", did not match the total number of entries, n="
00271                 << n << "." << std::endl;
00272       //libmesh_error();
00273     }
00274 #endif
00275 
00276 
00277   STOP_LOG ("histogram()", "StatisticsVector");
00278 }

template<typename T >
Real libMesh::StatisticsVector< T >::l2_norm (  )  const [inline, virtual]

Returns the l2 norm of the data set.

Definition at line 36 of file statistics.C.

References libMesh::Real.

00037 {
00038   Real normsq = 0.;
00039   for (dof_id_type i = 0; i != this->size(); ++i)
00040     normsq += ((*this)[i] * (*this)[i]);
00041 
00042   return std::sqrt(normsq);
00043 }

template<typename T >
T libMesh::StatisticsVector< T >::maximum (  )  const [inline, virtual]

Returns the maximum value in the data set.

Definition at line 62 of file statistics.C.

References end, and std::max().

Referenced by libMesh::StatisticsVector< T >::histogram(), libMesh::StatisticsVector< T >::normalize(), and libMesh::StatisticsVector< T >::plot_histogram().

00063 {
00064   START_LOG ("maximum()", "StatisticsVector");
00065 
00066   const T max = *(std::max_element(this->begin(), this->end()));
00067 
00068   STOP_LOG ("maximum()", "StatisticsVector");
00069 
00070   return max;
00071 }

template<typename T >
Real libMesh::StatisticsVector< T >::mean (  )  const [inline, virtual]

Returns the mean value of the data set using a recurrence relation. Source: GNU Scientific Library

Reimplemented in libMesh::ErrorVector.

Definition at line 77 of file statistics.C.

References libMesh::Real.

Referenced by libMesh::StatisticsVector< ErrorVectorReal >::variance().

00078 {
00079   START_LOG ("mean()", "StatisticsVector");
00080 
00081   const dof_id_type n = this->size();
00082 
00083   Real the_mean = 0;
00084 
00085   for (dof_id_type i=0; i<n; i++)
00086     {
00087       the_mean += ( static_cast<Real>((*this)[i]) - the_mean ) /
00088                 static_cast<Real>(i + 1);
00089     }
00090 
00091   STOP_LOG ("mean()", "StatisticsVector");
00092 
00093   return the_mean;
00094 }

template<typename T >
Real libMesh::StatisticsVector< T >::median (  )  const [inline, virtual]

A const version of the median funtion. Requires twice the memory of original data set but does not change the original.

Reimplemented in libMesh::ErrorVector.

Definition at line 137 of file statistics.C.

References libMesh::StatisticsVector< T >::median().

00138 {
00139   StatisticsVector<T> sv = (*this);
00140 
00141   return sv.median();
00142 }

template<typename T >
Real libMesh::StatisticsVector< T >::median (  )  [inline, virtual]

Returns the median (e.g. the middle) value of the data set. This function modifies the original data by sorting, so it can't be called on const objects. Source: GNU Scientific Library

Reimplemented in libMesh::ErrorVector.

Definition at line 100 of file statistics.C.

References end, and libMesh::Real.

Referenced by libMesh::StatisticsVector< T >::median(), and libMesh::ErrorVector::median().

00101 {
00102   const dof_id_type n   = this->size();
00103 
00104   if (n == 0)
00105     return 0.;
00106 
00107   START_LOG ("median()", "StatisticsVector");
00108 
00109   std::sort(this->begin(), this->end());
00110 
00111   const dof_id_type lhs = (n-1) / 2;
00112   const dof_id_type rhs = n / 2;
00113 
00114   Real the_median = 0;
00115 
00116 
00117   if (lhs == rhs)
00118     {
00119       the_median = static_cast<Real>((*this)[lhs]);
00120     }
00121 
00122   else
00123     {
00124       the_median = ( static_cast<Real>((*this)[lhs]) +
00125                  static_cast<Real>((*this)[rhs]) ) / 2.0;
00126     }
00127 
00128   STOP_LOG ("median()", "StatisticsVector");
00129 
00130   return the_median;
00131 }

template<typename T >
T libMesh::StatisticsVector< T >::minimum (  )  const [inline, virtual]

Returns the minimum value in the data set.

Reimplemented in libMesh::ErrorVector.

Definition at line 47 of file statistics.C.

References end, and std::min().

Referenced by libMesh::StatisticsVector< T >::histogram(), and libMesh::StatisticsVector< T >::plot_histogram().

00048 {
00049   START_LOG ("minimum()", "StatisticsVector");
00050 
00051   const T min = *(std::min_element(this->begin(), this->end()));
00052 
00053   STOP_LOG ("minimum()", "StatisticsVector");
00054 
00055   return min;
00056 }

template<typename T >
void libMesh::StatisticsVector< T >::normalize (  )  [inline]

Divides all entries by the largest entry and stores the result

Definition at line 173 of file statistics.C.

References std::max(), libMesh::StatisticsVector< T >::maximum(), and libMesh::Real.

00174 {
00175   const dof_id_type n   = this->size();
00176   const Real max = this->maximum();
00177 
00178   for (dof_id_type i=0; i<n; i++)
00179   {
00180     (*this)[i] = static_cast<T>((*this)[i] / max);
00181   }
00182 }

template<typename T >
void libMesh::StatisticsVector< T >::plot_histogram ( const std::string &  filename,
unsigned int  n_bins 
) [inline]

Generates a Matlab/Octave style file which can be used to make a plot of the histogram having the desired number of bins. Uses the histogram(...) function in this class WARNING: The histogram(...) function is non-const, and changes the order of the vector.

Definition at line 285 of file statistics.C.

References libMesh::StatisticsVector< T >::histogram(), std::max(), libMesh::StatisticsVector< T >::maximum(), std::min(), libMesh::StatisticsVector< T >::minimum(), and libMesh::processor_id().

00287 {
00288   // First generate the histogram with the desired number of bins
00289   std::vector<dof_id_type> bin_members;
00290   this->histogram(bin_members, n_bins);
00291 
00292   // The max, min and bin size are used to generate x-axis values.
00293   T min      = this->minimum();
00294   T max      = this->maximum();
00295   T bin_size = (max - min) / static_cast<T>(n_bins);
00296 
00297   // On processor 0: Write histogram to file
00298   if (libMesh::processor_id()==0)
00299     {
00300       std::ofstream out_stream (filename.c_str());
00301 
00302       out_stream << "clear all\n";
00303       out_stream << "clf\n";
00304       //out_stream << "x=linspace(" << min << "," << max << "," << n_bins+1 << ");\n";
00305 
00306       // abscissa values are located at the center of each bin.
00307       out_stream << "x=[";
00308       for (unsigned int i=0; i<bin_members.size(); ++i)
00309         {
00310           out_stream << min + (i+0.5)*bin_size << " ";
00311         }
00312       out_stream << "];\n";
00313 
00314       out_stream << "y=[";
00315       for (unsigned int i=0; i<bin_members.size(); ++i)
00316         {
00317           out_stream << bin_members[i] << " ";
00318         }
00319       out_stream << "];\n";
00320       out_stream << "bar(x,y);\n";
00321     }
00322 }

template<typename T>
virtual Real libMesh::StatisticsVector< T >::stddev ( const Real  known_mean  )  const [inline, virtual]

Computes the standard deviation of the data set, which is simply the square-root of the variance. This method can be used for efficiency when the mean has already been computed.

Definition at line 174 of file statistics.h.

00175   { return std::sqrt(this->variance(known_mean)); }

template<typename T>
virtual Real libMesh::StatisticsVector< T >::stddev (  )  const [inline, virtual]

Computes the standard deviation of the data set, which is simply the square-root of the variance.

Definition at line 165 of file statistics.h.

00166   { return std::sqrt(this->variance()); }

template<typename T >
Real libMesh::StatisticsVector< T >::variance ( const Real  known_mean  )  const [inline, virtual]

Computes the variance of the data set where the mean is provided. This is useful for efficiency when you have already calculated the mean. Uses a recurrence relation to prevent data overflow for large sums. Note: The variance is equal to the standard deviation squared. Source: GNU Scientific Library

Reimplemented in libMesh::ErrorVector.

Definition at line 148 of file statistics.C.

References libMesh::Real.

00149 {
00150   const dof_id_type n   = this->size();
00151 
00152   START_LOG ("variance()", "StatisticsVector");
00153 
00154   Real the_variance = 0;
00155 
00156   for (dof_id_type i=0; i<n; i++)
00157     {
00158       const Real delta = ( static_cast<Real>((*this)[i]) - mean_in );
00159       the_variance += (delta * delta - the_variance) /
00160                     static_cast<Real>(i + 1);
00161     }
00162 
00163   if (n > 1)
00164     the_variance *= static_cast<Real>(n) / static_cast<Real>(n - 1);
00165 
00166   STOP_LOG ("variance()", "StatisticsVector");
00167 
00168   return the_variance;
00169 }

template<typename T>
virtual Real libMesh::StatisticsVector< T >::variance (  )  const [inline, virtual]

Computes the variance of the data set. Uses a recurrence relation to prevent data overflow for large sums. Note: The variance is equal to the standard deviation squared. Source: GNU Scientific Library

Reimplemented in libMesh::ErrorVector.

Definition at line 145 of file statistics.h.

Referenced by libMesh::StatisticsVector< ErrorVectorReal >::stddev(), and libMesh::StatisticsVector< ErrorVectorReal >::variance().

00146   { return this->variance(this->mean()); }


The documentation for this class was generated from the following files:

Site Created By: libMesh Developers
Last modified: February 05 2013 19:55:39 UTC

Hosted By:
SourceForge.net Logo