calculateWeights( i, xa, ya, windowXa, windowYa, weights, window );
int total = xa.length; int windowStart = 0; double maxDistance = 0;   if ( index < window ) { for ( int i = 0; i < window; i++ ) { if ( ( xa[index] - xa[i] ) <= ( xa[window + i] - xa[index] ) ) { windowStart = i; maxDistance = Math.max( xa[index] - xa[windowStart], xa[windowStart + window - 1] - xa[index] ); break; } } } else if ( index >= window && index < total - window ) { for ( int i = 0; i < window; i++ ) { if ( xa[index] - xa[index - window + 1 + i] <= xa[index + 1 + i] - xa[index] ) { windowStart = index - window + 1 + i; maxDistance = Math.max( xa[index] - xa[windowStart], xa[windowStart + window - 1] - xa[index] ); break; } } } else { for ( int i = 0; i < window; i++ ) { if ( ( xa[total - 1 - i] - xa[index] ) <= ( xa[index] - xa[total - window - 1 - i] ) ) { windowStart = total - window - i; maxDistance = Math.max( xa[index] - xa[windowStart], xa[windowStart + window - 1] - xa[index] ); break; } } }   System.arraycopy( xa, windowStart, windowXa, 0, window ); System.arraycopy( ya, windowStart, windowYa, 0, window );   int windowIndex = index - windowStart;  for ( int i = 0; i < window; i++ ) { double distance = Math.abs( windowXa[windowIndex] - windowXa[i] );  weights[i] = Math.pow( 1.0 - Math.pow( ( distance / maxDistance ), 3.0 ), 3.0 ); }
	public void calculate( double[] xa, double[] ya, double windowProportion )	{		fittedValue = new double[xa.length];		int window = (int) Math.round( xa.length * windowProportion );		if ( window > 1 )		{			double[] windowXa = new double[window];			double[] windowYa = new double[window];			double[] weights = new double[window];			Matrix X = new Matrix( window, 2 );			Matrix Y = new Matrix( window, 1 );			Matrix W = new Matrix( window, window );			// iterate each given point.			for ( int i = 0; i < xa.length; i++ )			{				calculateWeights( i,						xa,						ya,						windowXa,						windowYa,						weights,						window );				// Apply WLS(Weighted Least Square) regression method				// ===================================================				// XL = Y				// WXL = WY				// XtWXL = XtWY				// (XtWX)i(XtWX)L = (XtWX)i(XtWY)				// L = (XtWX)i(XtWY)				//				// ** (M)i is the inverse of M.				for ( int j = 0; j < window; j++ )				{					X.set( j, 0, 1 );					X.set( j, 1, windowXa[j] );					Y.set( j, 0, windowYa[j] );					W.set( j, j, weights[j] );				}				Matrix XTW = X.transpose( ).times( W );				Matrix L;				try				{					L = XTW.times( X ).inverse( ).times( XTW.times( Y ) );				}				catch ( Exception ex )				{					// in some cases, the matrix may be singular due to too many					// null weights, just use original value as the estimation.					L = new Matrix( 2, 1 );					L.set( 0, 0, ya[i] );					L.set( 1, 0, 0 );				}				fittedValue[i] = L.get( 0, 0 ) + L.get( 1, 0 ) * xa[i];			}		}		else		{			// use original y value as estimation for trivial cases.			for ( int i = 0; i < xa.length; i++ )			{				fittedValue[i] = ya[i];			}		}	}