Example: Radial Basis Function Approximation

The function
e^{-\|\vec{x}\|^2/d}
where d is the dimension, is evaluated at a set of randomly chosen points. Random noise is added to the values and a radial basis approximated to the noisy data is computed. The radial basis fit is then compared to the original function at another set of randomly chosen points. Both the average error and the maximum error are computed and printed.

In this example, the dimension d =10. The function is sampled at 200 random points, in the [-1,1]^d cube, to which what noise in the range [-0.2,0.2] is added. The error is computed at 1000 random points, also from the [-1,1]^d cube. The compute errors are less than the added noise.

using System;
using Imsl.Math;

public class RadialBasisEx1
{
    public static void  Main(String[] args)
    {
        int nDim = 10;
        
        // Sample, with noise, the function at 100 randomly chosen points
        int nData = 200;
        double[,] xData = new double[nData,nDim];
        double[] fData = new double[nData];
        Imsl.Stat.Random rand = new Imsl.Stat.Random(234567);
        double[] tmp = new double[nDim];
        for (int k = 0; k < nData; k++)
        {
            for (int i = 0; i < nDim; i++)
            {
                tmp[i] = xData[k,i] = 2.0 * rand.NextDouble() - 1.0;
            }
            // noisy sample
            fData[k] = 
                fcn(tmp) + 0.20 * (2.0 * rand.NextDouble() - 1.0);
        }
        
        // Compute the radial basis approximation using 25 centers
        int nCenters = 25;
        RadialBasis rb = new RadialBasis(nDim, nCenters);
        rb.Update(xData, fData);
        
        // Compute the error at a randomly selected set of points
        int nTest = 1000;
        double maxError = 0.0;
        double aveError = 0.0;
        double[] x = new double[nDim];
        for (int k = 0; k < nTest; k++)
        {
            for (int i = 0; i < nDim; i++)
            {
                x[i] = 2.0 * rand.NextDouble() - 1.0;
            }
            double error = Math.Abs(fcn(x) - rb.Eval(x));
            aveError += error;
            maxError = Math.Max(error, maxError);
            double f = fcn(x);
        }
        aveError /= nTest;
        
        Console.WriteLine("average error is " + aveError);
        Console.WriteLine("maximum error is " + maxError);
    }
    
    
    // The function to approximate
    internal static double fcn(double[] x)
    {
        double sum = 0.0;
        for (int k = 0; k < x.Length; k++)
        {
            sum += x[k] * x[k];
        }
        sum /= x.Length;
        return Math.Exp(-sum);
    }
}

Output

average error is 0.0419789795502543
maximum error is 0.171666811944547

Link to C# source.