> >Maybe we could make composite statistic objects that can compute
more
> >than one metric.  The composite would conform to the same statistic
> >interface and would be adaptable into individual metrics.  Also, the
> >responsibility of computation ordering would be hidden in the
> >composite, removed from univariate.
> >
> >Brent Worden
> >http://www.brent.worden.org/
> >  
> >
> Hmm, can you elaborate a bit on this, I'm not quite sure I
understand 
> how a composite could be adapted into individual metrics while still 
> working with the Statistic interface. Wouldn't you end up back at
> 
> getMean()
> getVar()
> ...
> 
> to recover the separate stats in the composite? and still would'nt
you 
> need to calculate m2 - m4 even in the case that you just wanted to
get 
> the Mean?
> 
> -M.

A Statistic would have the knowledge of what actual metrics it can
compute.  Your Mean statistic would know it computes the mean,
Variances would know is computes the variance, and so on.  A composite
would know is computes both the mean and variance, for example.

The univariate would hold onto a collection of Statistic objects,
either simple or composite.  When you wanted the mean for a univariate
you'd call a method like getStatistic("mean"),
getStatistic(Mean.class) or, something to that effect.  The univariate
would query its statistic collection, finding a Statistic object that
can supply the mean.  If one is found, the value of that object is
returned.

Here's some brief code of the top of my head:

interface Statistic {
    void addValue(double value);
    // notice no get statistic method

    /**
     * return true if this object compute
     * the statistic of the given type.
     */
    boolean hasStatistic(Class type);
}

interface Mean extends Statistic {
    double getMean();
}

interface Variance extends Statistic {
    double getVariance();
}

class ExampleCompositeStatistic implements Variance, Mean {
    double getMean(){
        return m;
    }
    double getVariance(){
        return v;
    }
    void addValue(double d){
        // update all stats.
    }
    boolean hasStatistic(Class type){
        return type.isAssignableFrom(this.getClass());
    }
}

class Univariate {
    Collection statistics;

    void addStatistic(Statistic s){
        statistics.add(s);
    }

    void addValue(double value){
        Iterator iter = statistics.iterator();
        while(iter.hasNext()){
            Statistic stat = (Statistic)iter.next();
            stat.addValue(value);
        }
    }

    Statistic getStatistic(Class type){
        Iterator iter = statistics.iterator();
        while(iter.hasNext()){
            Statistic stat = (Statistic)iter.next();
            if(stat.hasStatistic(type)){
                return stat;
            }
        }
        return null;
    }
}

class ExampleUsage {
    double testStatistic(double[] values, double mu){
        Univariate u = new Univariate();
        u.addStatistic(new ExampleCompositeStatistic());

        for(int i = 0; i < values.length; ++i){
            u.addValue(values[i]);
        }

        double m = ((Mean)u.getStatistic(Mean.class)).getMean();
        double v =
((Variance)u.getStatistic(Variance.class)).getVariance();

        return (m - mu) / Math.Sqrt(v / values.length);
    }
}

Brent Worden
http://www.brent.worden.org/

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to