Example: ScaleFilter

In this example three sets of data, X0, X1, and X2 are scaled using the methods described in the following table:

Variables and Scaling Methods

Variable Method Description

X0

0

No Scaling

X1

4

Bounded Z-score scaling using the mean and standard deviation of X1

X2

5

Bounded Z-score scaling  using the median and MAD of X2

The bounds, measures of center and spread for X1 and X2 are:

Scaling Limits and Measures of Center and Spread

Variable

Real Limits

Target Limits

Measure of Center

Measure of Spread

X1

(-6, +6)

(-3, +3)

3.4
(Mean)

1.7421
(Std. Dev.)

X2

(-3, +3)

(-3, +3)

2.4
(Median)

1.3343
(MAD/0.6745)

The real and target limits are used for bounded scaling.  The measures of center and spread are used to calculate z-scores. Using these values for  x1[0]=3.5   yields the following calculations:

For x1[0] , the scale factor is calculated using the real and target limits in the above table:

r = (3-(-3))/(6-(-6)) = 0.5

The z-score for x1[0] is calculated using the measures of center and spread:

z1[0] = (3.5 - 3.4)/1.7421 = 0.057402

Since method=4 is used for x1 , this z-score is bounded (scaled) using the real and target limits:

z1(bounded) = r (z1[0] ) - r(realMin) + (targetMin)
                            = 0.5(0.057402) - 0.5(-6) + (-3) = 0.029

The calculations for x2[0] are nearly identical, except that since method=5 for x2 , the median and MAD replace the mean and standard deviation used to calculate z1(bounded):

r = (3-(-3))/(3-(-3)) = 1,

z2[0] = (3.1 - 2.4)/1.3343 = 0.525, and

z2(bounded) = r (z2[0] ) - r(realMin) + (targetMin)
               = 1(0.525) - 1(-3) + (-3) = 0.525

import com.imsl.stat.*;
import com.imsl.math.*;
import com.imsl.datamining.neural.*;

public class ScaleFilterEx1 {
    public static void main(String args[]) throws Exception {
        ScaleFilter[] scaleFilter = new ScaleFilter[3];
        scaleFilter[0] = new ScaleFilter(ScaleFilter.NO_SCALING);
        scaleFilter[1] = 
            new ScaleFilter(ScaleFilter.BOUNDED_Z_SCORE_SCALING_MEAN_STDEV);
        scaleFilter[1].setBounds(-6.0, 6.0, -3.0, 3.0);
        scaleFilter[2] = 
            new ScaleFilter(ScaleFilter.BOUNDED_Z_SCORE_SCALING_MEDIAN_MAD);
        scaleFilter[2].setBounds(-3.0, 3.0, -3.0, 3.0);
        int nObs = 5;
        double[] y0, y1, y2;
        double[] x0 = {1.2, 0.0, -1.4, 1.5, 3.2};
        double[] x1 = {3.5, 2.4, 4.4, 5.6, 1.1};
        double[] x2 = {3.1, 1.5, -1.5, 2.4, 4.2};

        // Perform forward filtering
        y0 = scaleFilter[0].encode(x0);
        y1 = scaleFilter[1].encode(x1);
        y2 = scaleFilter[2].encode(x2);
        // Display x0
        System.out.print("X0 = {");
        for (int i=0; i<4; i++) System.out.print(x0[i]+", ");
        System.out.println(x0[4]+"}");
        // Display summary statistics for X1
        System.out.print("\nX1 = {");
        for (int i=0; i<4; i++) System.out.print(x1[i]+", ");
        System.out.println(x1[4]+"}");
        System.out.println("X1 Mean:       "+scaleFilter[1].getCenter());
        System.out.println("X1 Std. Dev.:  "+scaleFilter[1].getSpread());
        // Display summary statistics for X2
        System.out.print("\nX2 = {");
        for (int i=0; i<4; i++) System.out.print(x2[i]+", ");
        System.out.println(x2[4]+"}");
        System.out.println("X2 Median:     "+scaleFilter[2].getCenter());
        System.out.println("X2 MAD/0.6745: "+scaleFilter[2].getSpread());
        System.out.println("");
        PrintMatrix pm = new PrintMatrix();        
        pm.setTitle("Filtered X0 Using Method=0 (no scaling)");
        pm.print(y0);
        pm.setTitle("Filtered X1 Using Bounded Z-score Scaling\n"+
                    "with Center=Mean and Spread=Std. Dev.");
        pm.print(y1);
        pm.setTitle("Filtered X2 Using Bounded Z-score Scaling\n"+
                    "with Center=Median and Spread=MAD/0.6745");
        pm.print(y2);
        
        // Perform inverse filtering
        double[] z0, z1, z2;
        z0 = scaleFilter[0].decode(y0);
        z1 = scaleFilter[1].decode(y1);
        z2 = scaleFilter[2].decode(y2);
        pm.setTitle("Decoded Z0");
        pm.print(z0);
        pm.setTitle("Decoded Z1");
        pm.print(z1);
        pm.setTitle("Decoded Z2");
        pm.print(z2);

    }
}

Output

X0 = {1.2, 0.0, -1.4, 1.5, 3.2}

X1 = {3.5, 2.4, 4.4, 5.6, 1.1}
X1 Mean:       3.4
X1 Std. Dev.:  1.7421251390184345

X2 = {3.1, 1.5, -1.5, 2.4, 4.2}
X2 Median:     2.4
X2 MAD/0.6745: 1.3343419966550414

Filtered X0 Using Method=0 (no scaling)
    0    
0   1.2  
1   0    
2  -1.4  
3   1.5  
4   3.2  

Filtered X1 Using Bounded Z-score Scaling
with Center=Mean and Spread=Std. Dev.
     0     
0   0.029  
1  -0.287  
2   0.287  
3   0.631  
4  -0.66   

Filtered X2 Using Bounded Z-score Scaling
with Center=Median and Spread=MAD/0.6745
     0     
0   0.525  
1  -0.674  
2  -2.923  
3   0      
4   1.349  

Decoded Z0
    0    
0   1.2  
1   0    
2  -1.4  
3   1.5  
4   3.2  

Decoded Z1
    0   
0  3.5  
1  2.4  
2  4.4  
3  5.6  
4  1.1  

Decoded Z2
    0    
0   3.1  
1   1.5  
2  -1.5  
3   2.4  
4   4.2  

Link to Java source.