Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formula of get_AICc #117

Open
hayato-n opened this issue May 30, 2022 · 4 comments
Open

Formula of get_AICc #117

hayato-n opened this issue May 30, 2022 · 4 comments

Comments

@hayato-n
Copy link

In diagnostics.py, get_AICc formula is

mgwr/mgwr/diagnostics.py

Lines 11 to 30 in 2a95535

def get_AICc(gwr):
"""
Get AICc value
Gaussian: p61, (2.33), Fotheringham, Brunsdon and Charlton (2002)
GWGLM: AICc=AIC+2k(k+1)/(n-k-1), Nakaya et al. (2005): p2704, (36)
"""
n = gwr.n
k = gwr.tr_S
#sigma2 = gwr.sigma2
if isinstance(gwr.family, Gaussian):
aicc = -2.0 * gwr.llf + 2.0 * n * (k + 1.0) / (
n - k - 2.0) #equivalent to below but
#can't control denominator of sigma without altering GLM familt code
#aicc = n*np.log(sigma2) + n*np.log(2.0*np.pi) + n*(n+k)/(n-k-2.0)
elif isinstance(gwr.family, (Poisson, Binomial)):
aicc = get_AIC(gwr) + 2.0 * k * (k + 1.0) / (n - k - 1.0)
return aicc

However, as written in its comment, it depends on the setting of GLM. Thus its result is different from the AICc definition described in Li et al. (2019).
(Even if I changed sigma2_v1 parameter of gwr.GWR), the resulting AICc value did not change.)

I suspect that the following code is consistent with the definition above.

gwr.n * (np.log(np.sum(np.square(gwr.resid_response))) - np.log(gwr.n-gwr.ENP) + np.log(2*np.pi) + (gwr.n+gwr.ENP) / (gwr.n-2-gwr.ENP))

Is there any reason why the current implementation is employed?

  1. Li, Z., Fotheringham, A. S., Li, W., & Oshan, T. (2019). Fast Geographically Weighted Regression (FastGWR): a scalable algorithm to investigate spatial process heterogeneity in millions of observations. International Journal of Geographical Information Science, 33(1), 155–175. https://doi.org/10.1080/13658816.2018.1521523
@Ziqi-Li
Copy link
Member

Ziqi-Li commented May 30, 2022

Hi @hayato-n. The only difference is the denominator when calculating the error variance, where here in mgwr is using the MLE (RSS/n) and in the Li paper is described using the unbiased estimator (RSS/(n-k)). I think it is more common to use the MLE one that is implemented here, so to be consistent, the later update of fastgwr uses MLE (Link).

@hayato-n
Copy link
Author

Hi @Ziqi-Li, thanks for your reply.
I confirmed that the following code is consistent with the get_AICc's behaviour.

# ML
sigma2 = np.sum(np.square(gwr.resid_response)) / gwr.n

# unbiased
# sigma2 = np.sum(np.square(gwr.resid_response)) / (gwr.n - gwr.ENP)

# AICc
gwr.n * (np.log(sigma2) + np.log(2*np.pi) + (gwr.n+gwr.ENP) / (gwr.n-2-gwr.ENP))

I suspect it is not intuitive that the parameter sigma2_v1 does not affect the AICc formula. It will be more desirable if the reason is written in the comment in get_AICc or its documentation.

Thank you again for your helpful comment!

@Ziqi-Li
Copy link
Member

Ziqi-Li commented May 31, 2022

Hi @hayato-n, great you find it consistent now. I think sigma_v1 (which is calculated based on the denominator n-k) is actually not used in the AIC formula so modifying it doesn't affect the outcome.

@hayato-n
Copy link
Author

hayato-n commented Jun 1, 2022

Yes, you are right, sigma_v1 does not affect. I think your comments here are informative, thus I will send a small pull request to clarify the AICc formula. Please check and accept if you like it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants