On 10/1/10 8:32 AM, VanIngen, Erik (FIPS) wrote:
Hi Luc and others,
I have written the standardize function by myself (see below, including the
tests). Would it be possible to have this added to Apache Math Commons?
Thanks for contributing!
We should take discussion of this new feature to the dev list. It
would be great if you could open a JIRA ticket and attach a patch
including implementation code.
We can talk about how to integrate this into [math] in JIRA comments
and / or on the dev list. For now, I will just say that the
simplest way to add this would be to add a static method called
something like "normalize" to org.apache.commons.math.stat.StatUtils.
See http://commons.apache.org/patches.html for info on how to create
patches and attach them to JIRA tickets. Do not hesitate to ask
either on dev list or in private emails if you need help getting set up.
Thanks!
Phil
/**
* The standardise function does not seem to be in Apache math commons.
*
*
* @author Erik van Ingen
*
*/
public class Standardize {
/**
* Standardise the series, so in the end it is having mean of 0 and a
standard deviation of 1.
*
*
* @param series
* @return
*/
public static double[] run(double[] series) {
DescriptiveStatistics stats = new DescriptiveStatistics();
// Add the data from the array
for (int i = 0; i< series.length; i++) {
stats.addValue(series[i]);
}
// Compute mean and standard deviation
double currentMean = stats.getMean();
double currentstandardDeviation = stats.getStandardDeviation();
// z = (x- mean)/standardDeviation
double[] newSeries = new double[series.length];
for (int i = 0; i< series.length; i++) {
newSeries[i] = (series[i] - currentMean) /
currentstandardDeviation;
}
return newSeries;
}
}
public class StandardizeTest {
/**
* Run the test with the values 50 and 100 and assume standardized
values with a dinstance of 0.01
*/
@Test
public void testRun1() {
double series[] = { 50, 100 };
double expectedSeries[] = { -0.7, 0.7 };
double[] out = Standardize.run(series);
for (int i = 0; i< out.length; i++) {
assertEquals(out[i], expectedSeries[i], 0.01);
}
}
/**
* Run with 77 random values, assuming that the outcome has a mean of
0 and a standard deviation of 1.
*
*
*
*/
@Test
public void testRun2() {
int length = 77;
double series[] = new double[length];
for (int i = 0; i< length; i++) {
series[i] = Math.random();
}
double standardizedSeries[] = Standardize.run(series);
DescriptiveStatistics stats = new DescriptiveStatistics();
// Add the data from the array
for (int i = 0; i< length; i++) {
stats.addValue(standardizedSeries[i]);
}
double distance = 1E-10;
assertEquals(0.0, stats.getMean(), distance);
assertEquals(1.0, stats.getStandardDeviation(), distance);
}
}
-----Original Message-----
From: Luc Maisonobe [mailto:luc.maison...@free.fr]
Sent: 29 September 2010 18:54
To: Commons Users List
Subject: Re: [math]How to do standardizing (normalizing)
Le 29/09/2010 12:13, VanIngen, Erik (FIPS) a écrit :
Hi Apache Commons Math users
I am looking for an easy way of standardizing my values a mean 0 and a
standard deviation of 1. What is the best way to do that?
I have tried this:
DescriptiveStatistics stats = new DescriptiveStatistics();
// adding values
....
// Compute Mean and StandardDeviation
double mean = stats.getMean();
double std = stats.getStandardDeviation();
and then standardize each value according z = (x- mean)/std
But I would like to have just a function of standardize an array
according the parameters mean and std. Is there something like this in
Apache Math Commons?
I don't think we have such a function.
Luc
Erik
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@commons.apache.org
For additional commands, e-mail: user-h...@commons.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@commons.apache.org
For additional commands, e-mail: user-h...@commons.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@commons.apache.org
For additional commands, e-mail: user-h...@commons.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@commons.apache.org
For additional commands, e-mail: user-h...@commons.apache.org