import sys
sys.path.append("C:/Users/msachde1/Downloads/Research/Development/mgwr")
import warnings
warnings.filterwarnings("ignore")
import pandas as pd
import numpy as np
from mgwr.gwr import GWR
from spglm.family import Gaussian, Binomial, Poisson
from mgwr.gwr import MGWR
from mgwr.sel_bw import Sel_BW
import multiprocessing as mp
pool = mp.Pool()
from scipy import linalg
import numpy.linalg as la
from scipy import sparse as sp
from scipy.sparse import linalg as spla
from spreg.utils import spdot, spmultiply
from scipy import special
import libpysal as ps
import seaborn as sns
import matplotlib.pyplot as plt
from copy import deepcopy
import copy
from collections import namedtuple
import spglm
x = np.linspace(0, 25, 25)
y = np.linspace(25, 0, 25)
X, Y = np.meshgrid(x, y)
lon = X.reshape(-1,1)
lat = Y.reshape(-1,1)
coords = np.array(list(zip(lon,lat)))
x1=np.random.normal(0,1,625)
x2=np.random.normal(0,1,625)
error = np.random.normal(0,0.1,625)
z = 1+2*x1+3*x2+error
x1=x1.reshape(-1,1)
x2=x2.reshape(-1,1)
pr = 1/(1+np.exp(-z))
y = np.random.binomial(1,pr)
X = np.hstack([x1,x2])
y = np.array(y).reshape((-1,1))
bw = Sel_BW(coords,y,X,family=Binomial())
bw=bw.search()
gwr_mod = GWR(coords, y, X, bw,family=Binomial()).fit()
selector = Sel_BW(coords,y,X,multi=True,family=Binomial())
selector.search(verbose=True,max_iter_multi=50)
mgwr_mod = MGWR(coords, y, X, selector,family=Binomial()).fit()
np.mean(gwr_mod.params,axis=0), np.mean(mgwr_mod.params,axis=0)
gwr_mod.aic, mgwr_mod.aic
def add(a,b):
return 1+((1/12)*(a+b))
def con(u,v):
return (0*(u)*(v))+1
def sp(u,v):
return 1+1/324*(36-(6-u/2)**2)*(36-(6-v/2)**2)
def med(u,v):
B = np.zeros((25,25))
for i in range(25):
for j in range(25):
if u[i][j]<=8:
B[i][j]=0.2
elif u[i][j]>17:
B[i][j]=0.7
else:
B[i][j]=0.5
return B
x = np.linspace(0, 25, 25)
y = np.linspace(25, 0, 25)
X, Y = np.meshgrid(x, y)
x1=np.random.normal(0,1,625)
x2=np.random.normal(0,1,625)
error = np.random.normal(0,0.1,625)
B0=con(X,Y)
B1=sp(X,Y)
B2=med(X,Y)
plt.imshow(B1, extent=[0,10, 0, 10], origin='lower',cmap='RdBu_r')
plt.colorbar()
plt.axis(aspect='image')
plt.xticks([])
plt.yticks([])
plt.imshow(B2, extent=[0,25, 0, 25], origin='lower',cmap='RdBu_r')
plt.colorbar()
plt.axis(aspect='image')
plt.xticks([])
plt.yticks([])
B0=B0.reshape(-1,1)
B1=B1.reshape(-1,1)
B2=B2.reshape(-1,1)
f, axes = plt.subplots(1, 2, figsize=(4, 4), sharex=True)
sns.despine(left=True)
sns.distplot(B0,ax=axes[0])
sns.distplot(B1,ax=axes[1])
plt.setp(axes, yticks=[])
plt.tight_layout()
sns.distplot(B2)
lat=Y.reshape(-1,1)
lon=X.reshape(-1,1)
x1=x1.reshape(-1,1)
x2=x2.reshape(-1,1)
param = np.hstack([B0,B1,B2])
param.shape
cons=np.ones_like(x1)
cons=cons.reshape(-1,1)
X=np.hstack([cons,x1,x2])
X.shape
Incorporating step from - Chapter 6. Simulating Generalized Linear Models, p. 156
#binomial y
y_exp=((np.exp(np.sum(X * param, axis=1)+error)/(1+(np.exp(np.sum(X * param, axis=1)+error))))).reshape(-1,1)
np.mean(y_exp)
y_exp.shape
sns.distplot(y_exp)
y = np.random.binomial(1,y_exp)
pd.Series(y_exp.reshape(-1), index=x1).plot(style='.')
pd.Series(y.reshape(-1), index=x1).plot(style='.')
coords = np.array(list(zip(lon,lat)))
y = np.array(y).reshape((-1,1))
X=np.hstack([x1,x2])
bw=Sel_BW(coords,y,X,family=Binomial())
bw=bw.search()
gwr_model=GWR(coords,y,X,bw,family=Binomial()).fit()
bw
gwr_model.aic
selector=Sel_BW(coords,y,X,multi=True,family=Binomial())
selector.search(verbose=True)
mgwr_model=MGWR(coords,y,X,selector,family=Binomial()).fit()
np.mean(mgwr_model.params,axis=0),np.mean(gwr_model.params,axis=0)
np.mean(B0),np.mean(B1),np.mean(B2)
mgwr_model.aicc,gwr_model.aicc