Neighborhood covariance for map comparison - Map Algebra help needed

Discussion created by dgboss on Feb 25, 2011
Latest reply on Mar 9, 2011 by dgboss
I had posted in the Imagery and Raster forum a few days ago, but have not had any response as I think my question was poorly phrased and rather vague.  Also, I believe the Spatial Analyst forum may be a more appropriate place for my inquiry, so I apologize for the cross post!

I am looking at local map comparison metrics for maps with continuous attributes.  One of the metrics involves calculating the local covariance between two maps in a n x n moving window.  The formula for covariance is CV(x,y) = 1/N * SUM (x_i - x_bar)(y_i - y_bar). 

For example, in raster x, for the cell in row 1, column 1 of my moving window I need to subtract the local mean from this cell's value.  I do the same for the corresponding cell in raster y and multiply these values.  This calculation is performed for each cell in my moving window, the results for that neighborhood are summed, divided by n and assigned to the focal cell in the output raster.

I can envision a very slow, poor use of PixelBlocks where I define a PixelBlock of size n x n and access the results in the PixelBlock's safe array, perform the calculations and write to the output raster, but I feel this would be very inefficient.  I can also envision a method wherein I convert the source rasters to some form of text file and use clever indexing to get the pixel values I need, perform the calculation and write to the output.  If I use either of these approaches I will work in VBA.

I can sense that there is a way of doing this via map algebra, but I cannot quite come to a solution.  Perhaps someone (Bill Huber?!) has some ideas?

Edit - I have been reading through a lot of posts and my level of hope has increased somewhat.  In a post whuber stated, "For integer grids, though, focal stats with an appropriate custom neighborhood will pick out the neighboring value in any position you like."  Does anyone know how this is accomplished with integer grids?  Can the idea be extended to grids with real numbers?

Thanks for your help!