mgwr icon indicating copy to clipboard operation
mgwr copied to clipboard

LinAlgError with Sel_BW and fixed=True

Open kielc opened this issue 4 years ago • 3 comments

Getting a warning and error with a bandwidth selection search using the below code. There is no error when using adaptive bandwidth.

bw = Sel_BW(coords_mercator, y, X, fixed=True).search()

C:\Users\user\AppData\Local\Continuum\anaconda3\lib\site-packages\spglm\iwls.py:37: LinAlgWarning: Ill-conditioned matrix (rcond=9.61489e-19): result may not be accurate. xtx_inv_xt = linalg.solve(xtx, xT)


LinAlgError Traceback (most recent call last) in 1 # optimal bandwith selection search 2 # default golden section search using AICc criterion ----> 3 bw = Sel_BW(coords_mercator, y, X, fixed=True).search() 4 bw

~\AppData\Local\Continuum\anaconda3\lib\site-packages\mgwr\sel_bw.py in search(self, search_method, criterion, bw_min, bw_max, interval, tol, max_iter, init_multi, tol_multi, rss_score, max_iter_multi, multi_bw_min, multi_bw_max, bws_same_times, pool, verbose) 316 -1] #scalar, optimal bw from initial gwr model 317 else: --> 318 self._bw() 319 320 self.pool = None

~\AppData\Local\Continuum\anaconda3\lib\site-packages\mgwr\sel_bw.py in _bw(self) 335 self.bw = golden_section(a, c, delta, gwr_func, self.tol, 336 self.max_iter, self.int_score, --> 337 self.verbose) 338 elif self.search_method == 'interval': 339 self.bw = equal_interval(self.bw_min, self.bw_max, self.interval,

~\AppData\Local\Continuum\anaconda3\lib\site-packages\mgwr\search.py in golden_section(a, c, delta, function, tol, max_iter, int_score, verbose) 60 score_b = dict[b] 61 else: ---> 62 score_b = function(b) 63 dict[b] = score_b 64 if verbose:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\mgwr\sel_bw.py in (bw) 325 self.coords, self.y, self.X_loc, bw, family=self.family, kernel= 326 self.kernel, fixed=self.fixed, constant=self.constant, offset=self. --> 327 offset, spherical=self.spherical).fit(lite=True, pool=self.pool)) 328 329 self._optimized_function = gwr_func

~\AppData\Local\Continuum\anaconda3\lib\site-packages\mgwr\gwr.py in fit(self, ini_params, tol, max_iter, solve, lite, pool) 333 rslt = map(self._local_fit, range(m)) #sequential 334 --> 335 rslt_list = list(zip(*rslt)) 336 influ = np.array(rslt_list[0]).reshape(-1, 1) 337 resid = np.array(rslt_list[1]).reshape(-1, 1)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\mgwr\gwr.py in _local_fit(self, i) 249 250 if isinstance(self.family, Gaussian): --> 251 betas, inv_xtx_xt = _compute_betas_gwr(self.y, self.X, wi) 252 predy = np.dot(self.X[i], betas)[0] 253 resid = self.y[i] - predy

~\AppData\Local\Continuum\anaconda3\lib\site-packages\spglm\iwls.py in _compute_betas_gwr(y, x, wi) 35 xT = (x * wi).T 36 xtx = np.dot(xT, x) ---> 37 xtx_inv_xt = linalg.solve(xtx, xT) 38 betas = np.dot(xtx_inv_xt, y) 39 return betas, xtx_inv_xt

~\AppData\Local\Continuum\anaconda3\lib\site-packages\scipy\linalg\basic.py in solve(a, b, sym_pos, lower, overwrite_a, overwrite_b, debug, check_finite, assume_a, transposed) 214 (a1, b1)) 215 lu, ipvt, info = getrf(a1, overwrite_a=overwrite_a) --> 216 _solve_check(n, info) 217 x, info = getrs(lu, ipvt, b1, 218 trans=trans, overwrite_b=overwrite_b)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\scipy\linalg\basic.py in _solve_check(n, info, lamch, rcond) 29 '.'.format(-info)) 30 elif 0 < info: ---> 31 raise LinAlgError('Matrix is singular.') 32 33 if lamch is None:

LinAlgError: Matrix is singular.

kielc avatar Jan 29 '20 20:01 kielc

If you set the fixed = True, which is using distance rather than the nearest neighbors, I would also suggest to set the kernel function to be "gaussian". The default is "bisquare", in your current code bw = Sel_BW(coords_mercator, y, X, fixed=True).search().

So when fixed bi-square kernel is used, my guess is that for a smaller bandwidth, there are locations that they don't have enough nearby data points for the local regression during the bandwidth searching, thus yielding this error.

Again, the workaround would be using a adaptive bisquare kernel or a fixed gaussian kernel.

Ziqi-Li avatar Feb 19 '20 16:02 Ziqi-Li

@kielc Did you ever find a solution? I'm having a similar issue, but the adaptive bisquare kernel or a fixed gaussian kernel are not helping.

jamisbruening avatar Oct 14 '21 17:10 jamisbruening

@jamisbruening No, unfortunately I wasn't able to resolve the issue. I ended up implementing a simpler geographical regression instead of using mgwr which was more complicated than I needed for my project.

kielc avatar Oct 14 '21 20:10 kielc