# commons-user mailing list archives

##### Site index · List index
Message view
Top
From "VanIngen, Erik (FIPS)" <Erik.VanIn...@fao.org>
Subject RE: [math]How to do standardizing (normalizing)
Date Fri, 01 Oct 2010 12:32:08 GMT
```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?

/**
* 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++) {
}

// 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++) {
}

double distance = 1E-10;
assertEquals(0.0, stats.getMean(), distance);
assertEquals(1.0, stats.getStandardDeviation(), distance);

}

}

-----Original Message-----
From: Luc Maisonobe [mailto:Luc.Maisonobe@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();
> ....
> // 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-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org