commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tobr...@transolutions.net (O'brien, Tim)
Subject Re: [math][PATCH] was Re: [math] exceptions or NaN from Univariate
Date Wed, 14 May 2003 14:53:29 GMT
I can see why someone might want to use the Univariate implementation as
implemented currently, it is fast and efficient and requires no
storage.  If I'm trying to get Univariate stats for a group of 1000
longs in J2ME I might be interested in a storage-less implementation of
this. 

I do see that if window == Integer.MAX_VALUE no storage is used, but I'm
wondering if we might want to put this into another implementation -
this implementation should also provide Mode.

I'd like to get a sense from [math] of whether we should modify
Univariate in place or make Univariate an interface and provide multiple
implementations. 

Also, using Integer.MAX_VALUE makes practical sense, but it might be
better to choose a more "meaningless" default value that signifies
infinity.  Double has the concept of POSITIVE_INFINITY, but integers do
not.  "-1" is a common signal that a process has no positive upper
limit.  I know this is a little bit of hair splitting, but I'd like to
see what people think about this one.  I cannot forsee anyone needing to
collect Univariate statistics on more than 2^31 - 1 elements, but I
don't want to get in the business of introducing an arbitrary constant
that causes some catastrophic failure.


On Wed, 2003-05-14 at 09:24, Mark R. Diggory wrote:
> Thought I'd try creating a patch for this, let me know what you think.
> 
> -Mark
> ----
> 

> Index: Univariate.java
> ===================================================================
> RCS file: /home/cvspublic/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/Univariate.java,v
> retrieving revision 1.1
> diff -u -r1.1 Univariate.java
> --- Univariate.java	12 May 2003 19:04:10 -0000	1.1
> +++ Univariate.java	14 May 2003 14:22:37 -0000
> @@ -85,6 +85,12 @@
>      /** display name */
>      private String name = "";
>  
> +	/** Array of values for rolling */ 
> +	private double[] values = null;
> +	
> +	/** Array of values for rolling */ 
> +	private int window = Integer.MAX_VALUE;
> +		
>      /** Creates new univariate */
>      public Univariate() {
>          clear();
> @@ -96,6 +102,18 @@
>          clear();
>      }
>  
> +	/** Creates new univariate */
> +	public Univariate(int window) {
> +		this();
> +		this.window = window;
> +	}
> +		
> +	/** Creates a new univariate with the given name */
> +	public Univariate(java.lang.String name, int window) {
> +		this(name);
> +		this.window = window;
> +	}
> +	
>      /**
>       * Adds the value, updating running sums.<br>
>       * Converts value to a double before adding.
> @@ -167,11 +185,24 @@
>       * @param v the value to be added 
>       */
>      private void insertValue(double v) {
> -        n += 1.0;
> +        
>          if (v < min) min = v;
>          if (v > max) max = v;
>          sum += v;
>          sumsq += v*v;
> +        
> +        if(window != Integer.MAX_VALUE){
> +			n = Math.min(n+=1.0, values.length );
> +			sum -= values[window];
> +			sumsq -= values[window]*values[window];
> +			for(int i = window; i > 0 ;i--){
> +				values[i] = values[i-1];
> +			}
> +			values[0] = v;
> +        }else{
> +			n += 1.0;
> +        }
> +		
>      }
>  
>      /** Getter for property max.
> 
> ----
> 

> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message