# Focal Statistics Tool Weighted Mean Erroneous Calculation

Discussion created by JGWjr on Sep 6, 2011
Latest reply on Oct 18, 2014 by curtvprice
Use of the Spatial Analyst focal statistics tool with the Weight neighborhood type and an arbitrary weight kernel file to compute a mean, the result computed for any processing cell is NOT the expected "weighted mean" of the values of the cells within its neighborhood.

That this is the case may be seen from the following example which uses an input processing raster filled with constant values and the kernel file shown below.

Input processing raster:

1000 1000 1000 1000 1000 1000 1000 1000 1000 1000
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000

Kernel file:

5 5
1 2 3 2 1
2 4 6 4 2
3 6 9 6 3
2 4 6 4 2
1 2 3 2 1

Output raster (NoData cells ignored):

4000 4000 3600 3600 3600 3600 3600 3600 4000 4000
4000 4000 3600 3600 3600 3600 3600 3600 4000 4000
3600 3600 3240 3240 3240 3240 3240 3240 3600 3600
3600 3600 3240 3240 3240 3240 3240 3240 3600 3600
3600 3600 3240 3240 3240 3240 3240 3240 3600 3600
3600 3600 3240 3240 3240 3240 3240 3240 3600 3600
3600 3600 3240 3240 3240 3240 3240 3240 3600 3600
3600 3600 3240 3240 3240 3240 3240 3240 3600 3600
4000 4000 3600 3600 3600 3600 3600 3600 4000 4000
4000 4000 3600 3600 3600 3600 3600 3600 4000 4000

The value of 3240 seen for the cells in the central part of the output raster is 81000 / 25. (The other values near the array edges are different because the processing has encountered NoData cells. No further discussion will be given here of any cells in the output array near the edges.)

The value 81000  for the cells in the central part of the output raster is the weighted sum of the product of the input cell raster cell values and the kernel weights (as it should be). This can be confirmed by computing the sum statistic, instead of the mean statistic, using the focal statistics tool on the same input raster and with the same kernel.

The problem, then, is the tool's division of the weighted sum values by 25 (the number of cells in the kernel neighborhood, rather than by 81 (the sum of the weights used in that kernel neighborhood. Conventionally, and intuitively, the divisor by which a weighted mean should be created from a weighted sum should be the sum of the weights. That is not what this tool does.

A workaround for this error is easily contrived. Since the weighted sum statistic is computed correctly for a non-uniform kernel, the weighted sum tool can be used to compute the weighted mean for any kernel by proportionally reducing each of the kernel values so that the sum of all weights in the kernel is unity. For my example above, since the kernel weights add to 81, each should be divided by 81 to form a new kernel file. With that change, then use of the focal statistics tool to compute a weighted sum statistic does produce values of 1000 throughout the central portion of the output raster.