There is little need to 'calculate' the 'weight function' (coefficients?) of a mean filter: make all coefficients 1 - that is: sum up all pixels affected and divide the resulting value by the number of pixels. This is how the arithmetic mean value is defined.
I believe Dirah is referring to a non-local mean filter, where a target pixel's filtered value is affected by the means of pixels close it, where each of those means are multiplied by a coefficient x their Euclidean distance from the target pixel.
Dirah, I am not an expert in non-local means, but I believe the coefficient, along with the window size about each pixel, are both parameters you can, to adjust the performance of the filter.
Are you asking how to calculate this coefficient to "optimize" the performance of the filter?