The Kernel of the Guided Image Filter
The Guided Image Filter is a filter which assumes Piece Wise Linear model of the image.
Basically, it tries to estimate the best Linear Estimator per patch / pixel of the image.

Since it is Linear its coefficients are based on the first and second moments of the local patch.

You can see the code in my Q42415 GitHub Repository which also includes an implementation in Kernel Form in ApplyLocalLinearKernel().
Here are some results (Output of Q42415):