You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by md...@apache.org on 2003/07/15 05:45:10 UTC
cvs commit: jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat UnivariateImpl.java AbstractStoreUnivariate.java ListUnivariateImpl.java AbstractUnivariate.java StoreUnivariateImpl.java
mdiggory 2003/07/14 20:45:10
Modified: math/src/java/org/apache/commons/math/stat
UnivariateImpl.java AbstractStoreUnivariate.java
ListUnivariateImpl.java AbstractUnivariate.java
StoreUnivariateImpl.java
Log:
Application of "apply(Functor x)" strategy (thank you Al Chou) for evaluating UnivariateStatistics against the internal storage collection without exposing the collection or its bounds.
Revision Changes Path
1.19 +15 -19 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/UnivariateImpl.java
Index: UnivariateImpl.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/UnivariateImpl.java,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- UnivariateImpl.java 9 Jul 2003 21:45:23 -0000 1.18
+++ UnivariateImpl.java 15 Jul 2003 03:45:10 -0000 1.19
@@ -54,6 +54,9 @@
package org.apache.commons.math.stat;
import java.io.Serializable;
+
+import org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic;
+import org.apache.commons.math.stat.univariate.UnivariateStatistic;
import org.apache.commons.math.util.FixedDoubleArray;
/**
@@ -120,7 +123,7 @@
sumsq.increment(value);
sumLog.increment(value);
geoMean.increment(value);
-
+
moment.increment(value);
//mean.increment(value);
//variance.increment(value);
@@ -158,25 +161,18 @@
}
}
- /**
- * @see org.apache.commons.math.stat.AbstractUnivariate#internalValues()
- */
- protected double[] internalValues() {
- return storage == null ? null : storage.getValues();
- }
-
- /**
- * @see org.apache.commons.math.stat.AbstractUnivariate#start()
+ /* (non-Javadoc)
+ * @see org.apache.commons.math.stat.AbstractUnivariate#apply(org.apache.commons.math.stat.univariate.UnivariateStatistic)
*/
- protected int start() {
- return storage.start();
- }
+ public double apply(UnivariateStatistic stat) {
+
+ if (storage != null) {
+ return stat.evaluate(storage.getValues(), storage.start(), storage.getNumElements());
+ } else if (stat instanceof StorelessUnivariateStatistic) {
+ return ((StorelessUnivariateStatistic) stat).getResult();
+ }
- /**
- * @see org.apache.commons.math.stat.AbstractUnivariate#size()
- */
- protected int size() {
- return storage.getNumElements();
+ return Double.NaN;
}
}
1.10 +2 -2 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/AbstractStoreUnivariate.java
Index: AbstractStoreUnivariate.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/AbstractStoreUnivariate.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- AbstractStoreUnivariate.java 9 Jul 2003 21:45:23 -0000 1.9
+++ AbstractStoreUnivariate.java 15 Jul 2003 03:45:10 -0000 1.10
@@ -88,7 +88,7 @@
*/
public double getPercentile(double p) {
percentile.setPercentile(p);
- return percentile.evaluate(this.getValues(), this.start(), this.size());
+ return apply(percentile);
}
/**
1.4 +13 -19 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/ListUnivariateImpl.java
Index: ListUnivariateImpl.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/ListUnivariateImpl.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ListUnivariateImpl.java 9 Jul 2003 21:45:23 -0000 1.3
+++ ListUnivariateImpl.java 15 Jul 2003 03:45:10 -0000 1.4
@@ -54,6 +54,8 @@
package org.apache.commons.math.stat;
import java.util.List;
+
+import org.apache.commons.math.stat.univariate.UnivariateStatistic;
import org.apache.commons.math.util.DefaultTransformer;
import org.apache.commons.math.util.NumberTransformer;
@@ -182,27 +184,19 @@
super.clear();
list.clear();
}
-
- /**
- * @see org.apache.commons.math.stat.AbstractUnivariate#internalValues()
- */
- protected double[] internalValues() {
- return getValues();
- }
-
- /**
- * @see org.apache.commons.math.stat.AbstractUnivariate#start()
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.math.stat.AbstractUnivariate#apply(org.apache.commons.math.stat.univariate.UnivariateStatistic)
*/
- protected int start() {
- return 0;
- }
+ public double apply(UnivariateStatistic stat) {
+ double[] v = this.getValues();
- /**
- * @see org.apache.commons.math.stat.AbstractUnivariate#size()
- */
- protected int size() {
- return getN();
+ if (v != null) {
+ return stat.evaluate(v, 0, v.length);
+ }
+ return Double.NaN;
}
+
/**
* @return
*/
1.2 +14 -73 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/AbstractUnivariate.java
Index: AbstractUnivariate.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/AbstractUnivariate.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- AbstractUnivariate.java 9 Jul 2003 21:45:23 -0000 1.1
+++ AbstractUnivariate.java 15 Jul 2003 03:45:10 -0000 1.2
@@ -53,6 +53,7 @@
*/
package org.apache.commons.math.stat;
+import org.apache.commons.math.stat.univariate.UnivariateStatistic;
import org.apache.commons.math.stat.univariate.moment.FourthMoment;
import org.apache.commons.math.stat.univariate.moment.GeometricMean;
import org.apache.commons.math.stat.univariate.moment.Kurtosis;
@@ -139,23 +140,8 @@
setWindowSize(window);
}
- /**
- * Returns the internalValues array.
- * @return the array
- */
- protected abstract double[] internalValues();
-
- /**
- * Returns the start index of the array
- * @return start index
- */
- protected abstract int start();
-
- /**
- * Returns the size of the array appropriate for doing calculations.
- * @return Usually this is just numElements.
- */
- protected abstract int size();
+ public abstract double apply(UnivariateStatistic stat);
+
/**
* If windowSize is set to Infinite,
@@ -178,36 +164,21 @@
* @see org.apache.commons.math.stat.Univariate#getSum()
*/
public double getSum() {
- double[] v = internalValues();
- if (v != null) {
- return sum.evaluate(v, this.start(), this.size());
- }
-
- return sum.getResult();
+ return apply(sum);
}
/**
* @see org.apache.commons.math.stat.Univariate#getSumsq()
*/
public double getSumsq() {
- double[] v = internalValues();
- if (v != null) {
- return sumsq.evaluate(v, this.start(), this.size());
- }
-
- return sumsq.getResult();
+ return apply(sumsq);
}
/**
* @see org.apache.commons.math.stat.Univariate#getMean()
*/
public double getMean() {
- double[] v = internalValues();
- if (v != null) {
- return mean.evaluate(v, this.start(), this.size());
- }
-
- return mean.getResult();
+ return apply(mean);
}
/**
@@ -239,12 +210,7 @@
* a <= 1 value set.
*/
public double getVariance() {
- double[] v = internalValues();
- if (v != null) {
- return variance.evaluate(v, this.start(), this.size());
- }
-
- return variance.getResult();
+ return apply(variance);
}
/**
@@ -256,12 +222,7 @@
* <= 2 value set.
*/
public double getSkewness() {
- double[] v = internalValues();
- if (v != null) {
- return skewness.evaluate(v, this.start(), this.size());
- }
-
- return skewness.getResult();
+ return apply(skewness);
}
/**
@@ -274,12 +235,7 @@
* value set.
*/
public double getKurtosis() {
- double[] v = internalValues();
- if (v != null) {
- return kurtosis.evaluate(v, this.start(), this.size());
- }
-
- return kurtosis.getResult();
+ return apply(kurtosis);
}
/**
@@ -301,38 +257,23 @@
* @see org.apache.commons.math.stat.Univariate#getMax()
*/
public double getMax() {
- double[] v = internalValues();
- if (v != null) {
- return max.evaluate(v, this.start(), this.size());
- }
-
- return max.getResult();
+ return apply(max);
}
/**
* @see org.apache.commons.math.stat.Univariate#getMin()
*/
public double getMin() {
- double[] v = internalValues();
- if (v != null) {
- return min.evaluate(v, this.start(), this.size());
- }
-
- return min.getResult();
+ return apply(min);
}
/**
* @see org.apache.commons.math.stat.Univariate#getGeometricMean()
*/
public double getGeometricMean() {
- double[] v = internalValues();
- if (v != null) {
- return geoMean.evaluate(v, this.start(), this.size());
- }
-
- return geoMean.getResult();
+ return apply(geoMean);
}
-
+
/**
* Generates a text report displaying
* univariate statistics from values that
1.5 +9 -19 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/StoreUnivariateImpl.java
Index: StoreUnivariateImpl.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/StoreUnivariateImpl.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- StoreUnivariateImpl.java 9 Jul 2003 21:45:23 -0000 1.4
+++ StoreUnivariateImpl.java 15 Jul 2003 03:45:10 -0000 1.5
@@ -53,6 +53,7 @@
*/
package org.apache.commons.math.stat;
+import org.apache.commons.math.stat.univariate.UnivariateStatistic;
import org.apache.commons.math.util.ContractableDoubleArray;
/**
@@ -143,24 +144,13 @@
}
}
- /**
- * @see org.apache.commons.math.stat.AbstractUnivariate#internalValues()
- */
- protected double[] internalValues() {
- return eDA.getValues();
- }
-
- /**
- * @see org.apache.commons.math.stat.AbstractUnivariate#start()
+ /* (non-Javadoc)
+ * @see org.apache.commons.math.stat.AbstractUnivariate#apply(org.apache.commons.math.stat.univariate.UnivariateStatistic)
*/
- protected int start() {
- return eDA.start();
- }
-
- /**
- * @see org.apache.commons.math.stat.AbstractUnivariate#size()
- */
- protected int size() {
- return eDA.getNumElements();
+ public double apply(UnivariateStatistic stat) {
+ if (eDA != null) {
+ return stat.evaluate(eDA.getValues(), eDA.start(), eDA.getNumElements());
+ }
+ return Double.NaN;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [math] Re: cvs commit: "apply(Functor x)" strategy
Posted by "Mark R. Diggory" <md...@latte.harvard.edu>.
Yes, JUnit tests passed on my machine, if others do encounter problems,
let me know and I'll debug further.
Al, there is no lost of efficiency (in fact theres a minor improvement).
The previous path of action on calculating any statistic in
AbstractUnivariate was basically like the following:
Univariate.getVariance(){
return variance.evaluate(
this.getValues(),
this.getStart(),
this.getNumElements()
);
}
protected double[] internalValues() {
return storage == null ? null : storage.getValues();
}
protected int start() {
return storage.start();
}
protected int size() {
return storage.getNumElements();
}
where these methods were used to expose the internal double[] of data
(if possible) or generate a double[] of data in the case of
ListUnivariateImpl and BeanListUnivariateImpl. The above example was
what existed in UnivariateImpl and basically ends up delegating to the
"storage" DoubleArray methods. So this equates to about 7 method calls
(1*evaluate, 2*internalValues, 2*start, 2*size).
The using the "applicable" strategy we get the following path of action
instead:
Univariate.getVariance(){
return this.apply(variance);
}
public double apply(UnivariateStatistic stat) {
return stat.evaluate(
storage.getValues(),
storage.start(),
storage.getNumElements()
);
}
which is more direct and only equates to about 4 method calls for the
same process to occur (1*evaluate, 1*getValues, 1*start, 1*size).. Not
that this is really a significant improvement, but its just added
benefit on top of the following:
(1) It frees up the implementation not to have to expose its internal
storage to apply a statistic against it.
(2) If you write your own "UnivariateStatistic" you can easily apply it
against the same contents as the internally defined stats (mean,var,...).
I'd like to propose we add the apply method to the Univariate Interface,
or create a generic Interface called "Applicable" that Univariate can
extend to allow the User to see the apply method from the Univariate
interface.
-Mark
Al Chou wrote:
> --- mdiggory@apache.org wrote:
>
>>mdiggory 2003/07/14 20:45:10
>>
>> Modified: math/src/java/org/apache/commons/math/stat
>> UnivariateImpl.java AbstractStoreUnivariate.java
>> ListUnivariateImpl.java AbstractUnivariate.java
>> StoreUnivariateImpl.java
>> Log:
>> Application of "apply(Functor x)" strategy (thank you Al Chou) for
>>evaluating UnivariateStatistics against the internal storage collection
>>without exposing the collection or its bounds.
>
>
> Mark,
>
> I assume all the existing unit tests passed. I don't think you mentioned
> performance testing in your last message. I'm curious what effect the
> "applyable" strategy has on efficiency.
>
>
> Al
>
> =====
> Albert Davidson Chou
>
> Get answers to Mac questions at http://www.Mac-Mgrs.org/ .
>
> __________________________________
> Do you Yahoo!?
> SBC Yahoo! DSL - Now only $29.95 per month!
> http://sbc.yahoo.com
>
> ---------------------------------------------------------------------
> 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
[math] Re: cvs commit: "apply(Functor x)" strategy
Posted by Al Chou <ho...@yahoo.com>.
--- mdiggory@apache.org wrote:
> mdiggory 2003/07/14 20:45:10
>
> Modified: math/src/java/org/apache/commons/math/stat
> UnivariateImpl.java AbstractStoreUnivariate.java
> ListUnivariateImpl.java AbstractUnivariate.java
> StoreUnivariateImpl.java
> Log:
> Application of "apply(Functor x)" strategy (thank you Al Chou) for
> evaluating UnivariateStatistics against the internal storage collection
> without exposing the collection or its bounds.
Mark,
I assume all the existing unit tests passed. I don't think you mentioned
performance testing in your last message. I'm curious what effect the
"applyable" strategy has on efficiency.
Al
=====
Albert Davidson Chou
Get answers to Mac questions at http://www.Mac-Mgrs.org/ .
__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org