Logo
Distributed Genetic Programming Framework
print print

File org.sfc.math.stochastic.statistics.StatisticInfoBag.java

Here you can find all the information about the file org.sfc.math.stochastic.statistics.StatisticInfoBag.java. You may explore it here or download it onto your local disk.
/*
 * Copyright (c) 2006 Thomas Weise
 * 
 * E-Mail           : tweise@gmx.de
 * Creation Date    : 2006-05-23 15:11:34
 * Original Filename: org.sfc.math.stochastic.statistics.StatisticInfoBag.java
 * Version          : 1.0.0
 * Last modification: 2006-05-23
 *                by: Thomas Weise
 * 
 * License          : GNU LESSER GENERAL PUBLIC LICENSE
 *                    Version 2.1, February 1999
 *                    You should have received a copy of this license along
 *                    with this library; if not, write to the Free Software
 *                    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *                    MA 02111-1307, USA or download the license under
 *                    http://www.gnu.org/copyleft/lesser.html.
 *                    
 * Warranty         : This software is provided "as is" 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.
 */

 
package org.sfc.math.stochastic.statistics;

import java.io.Serializable;

import org.sfc.collections.Arrays;


  
/**
 * You can create instances of statistic info only via a statistic mutator.
 *
 * @author Thomas Weise
 */

public class StatisticInfoBag  implements  Serializable
  {
/**
 * The serial version uid.
 */

  private static final long serialVersionUID = 1;
    
/**
 * The statistic info record.
 */

  final StatisticInfo m_info  ;
    
/**
 * Create a new statistic mutator.
 * This new internal instance of <code>StatisticInfo</code> will have all
 * its information set to <code>0</code>.
 */

  public  StatisticInfoBag  ()
    {
    super();
    this.m_info = new StatisticInfo();
    }
    
/**
 * Returns the internal instance of <code>StatisticInfo</code> that can be
 * only mutated by this bag.
 * @return The internal instance of <code>StatisticInfo</code> that can be
 *         only mutated by this bag.
 */

  public  final StatisticInfo unpack()
    {
    return this.m_info;
    }
    
/**
 * Clear the owned statistic information.
 */

  public  final void  clear ()
    {
    this.m_info.clear();
    }
    
    
/**
 * Assign this statistic information record to another one.
 * @param p_info  The statistic information record to copy into this one.
 */

  public  final void  assign  (final StatisticInfo p_info)
    {
    this.m_info.assign(p_info);
    }


/**
 * Gather statistical info from a collection.
 * <strong>Warning: For the median to be correctly computed, the collection
 * must be sorted. The calculated median will be bogus otherwise.</strong>
 * @param p_collection    The collection to extract the information from.
 * @param p_extractor     The <code>ValueExtractor</code> to be
 *                        used to access the collection's elements.
 * @param p_start         The starting index from where to extract data.
 * @param p_count         The count of collection elements to visit.
 * @param <CollectionType>  The type of the collection that is passed in.
 */

  public final <CollectionType> void gather_info_sorted(
                  final CollectionType                  p_collection,
                  final ValueExtractor<CollectionType>  p_extractor,
                        int                             p_start,
                  final int                             p_count)
    {
    double        l_v, l_min, l_max, l_sum, l_sum_sqr;
    int           l_c;
    StatisticInfo l_info;

    l_info = this.m_info;
    l_info.clear();

    if(p_count <= 0) return;

    l_c   = (p_start + (p_count >>> 1));
    l_v   = p_extractor.get_value(p_collection, l_c);

    if( (p_count & 1) == 0 )
      {
      l_v = (0.5d * (l_v + p_extractor.get_value(p_collection, l_c - 1)));
      }


    l_info.m_median = l_v;
    l_info.m_count  = p_count;

    l_c          = p_count;
    l_v          = p_extractor.get_value(p_collection, p_start);
    l_min        = l_v;
    l_max        = l_v;
    l_sum        = l_v;
    l_sum_sqr    = (l_v*l_v);
    l_c--;
    if(l_c > 0)
      {
      l_v        = p_extractor.get_value(p_collection, p_start+l_c);
           if(l_v < l_min) l_min = l_v;
      else if(l_v > l_max) l_max = l_v;
      l_sum     += l_v;
      l_sum_sqr += (l_v*l_v);
      p_start++;

      for(--l_c; l_c > 0; p_start++, l_c--)
        {
        l_v        = p_extractor.get_value(p_collection, p_start);
        l_sum     += l_v;
        l_sum_sqr += (l_v * l_v);
        }
      }

    l_info.m_max      = l_max;
    l_info.m_min      = l_min;
    l_info.m_sum      = l_sum;
    if(l_sum_sqr < 0.0d) l_sum_sqr = 0.0d;
    l_info.m_sum_sqr  = l_sum_sqr;
    l_v               = (1.0d / p_count);
    l_sum            *= l_v;
    l_info.m_avg      = Math.max(l_min, Math.min(l_max, l_sum));
    l_sum_sqr        *= l_v;

    l_sum            *= l_sum;
    if(l_sum < 0.0d) l_sum = 0.0d;
    l_sum_sqr         = (l_sum_sqr - l_sum);
    if(l_sum_sqr < 0.0d) l_sum_sqr = 0.0d;
    l_info.m_var      = l_sum_sqr;
    }

/**
 * Gather statistical info from a collection.
 * <strong>Warning: For the median to be correctly computed, the collection
 * doesnt need to be sorted.
 * The calculated median will be bogus otherwise.</strong>
 * @param p_collection    The collection to extract the information from.
 * @param p_extractor     The <code>ValueExtractor</code> to be
 *                        used to access the collection's elements.
 * @param p_start         The starting index from where to extract data.
 * @param p_count         The count of collection elements to visit.
 * @param <CollectionType>  The type of the collection that is passed in.
 */

  public    final <CollectionType> void gather_info_unsorted
    (final CollectionType                  p_collection,
     final ValueExtractor<CollectionType>  p_extractor,
           int                             p_start,
     final int                             p_count)
    {
    final double[]  l_d;
          int       l_i;

    if(p_count <= 0)
      {
      this.clear();
      }
    else
      {
      l_d = new double[p_count];
      for(l_i = (p_count-1); l_i >= 0; l_i--, p_start++)
        {
        l_d[l_i] = p_extractor.get_value(p_collection, p_start);
        }
      Arrays.sort(l_d, p_count);
      gather_info_sorted(l_d, ValueExtractor.DOUBLE_ARRAY_INSTANCE,
                  0, p_count);
      }
    }
  }

File Information:

file name:StatisticInfoBag.java
package:org.sfc.math.stochastic.statistics
qualified name:org.sfc.math.stochastic.statistics.StatisticInfoBag.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/sfc/math/stochastic/statistics/StatisticInfoBag.java
size:6.527 KB (6684 B)
uploaded: 2018-01-07 12:03:36 GMT+0000
last update: 2006-05-23 12:15:00 GMT+0000
last access: 2018-04-26 04:09:49 GMT+0000

statistics online since 2006-01-02.   RSS Feed
Contact us by sending an email to tweise@gmx.de to receive further information, to report errors, or to join our project.
All content on this site (http://dgpf.sourceforge.net/) is LGPL-licensed.
http://dgpf.sourceforge.net/scripts/source/source.php last modified at 2018-01-07 12:03:34 GMT+0000 served at 2018-04-26 04:09:49 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo