oeo4b / kriging.js

Javascript library for geospatial prediction and mapping via ordinary kriging
oeo4b.github.io
MIT License
325 stars 130 forks source link

sqrt of negative number #4

Open ssured opened 10 years ago

ssured commented 10 years ago

Hi!

Great to see your progress, the API improved ++!

I'm having trouble with my dataset (wind speeds in Netherlands).

  var krigingData = [{"x":5.183333333333334,"y":52.1,"t":7},{"x":5.883333333333333,"y":52.06666666666667,"t":6},{"x":4.716666666666667,"y":53,"t":9}];
  var t = krigingData.map(function(d){ return d.t; });
  var x = krigingData.map(function(d){ return d.x; });
  var y = krigingData.map(function(d){ return d.y; });
  var model = "exponential";
  var alpha = 100, beta = 1;
  var variogram = kriging.train(t, x, y, model, alpha, beta);

This fails as it produces NaN values in the C matrix on line https://github.com/oeo4b/kriging.js/blob/master/kriging.js#L234.

The NaN values originate from line https://github.com/oeo4b/kriging.js/blob/master/kriging.js#L67, because p[i] turned negative there.

Using var krigingData = [{"x":5.883333333333333,"y":52.06666666666667,"t":6},{"x":4.716666666666667,"y":53,"t":9},{"x":6.583333333333333,"y":53.13333333333333,"t":7}]; it does work, so it seems something in my dataset is bothering the algorithm, though I can't see what.

BTW the full dataset I want to plot is, which is wind speed in m/s on 2013/12/23 at 12:00 local time.

[{"x":6.2,"y":51.5,"t":6},{"x":4.983333333333333,"y":52.65,"t":7},{"x":4.933333333333334,"y":51.96666666666667,"t":7},{"x":5.183333333333334,"y":52.1,"t":7},{"x":5.883333333333333,"y":52.06666666666667,"t":6},{"x":4.716666666666667,"y":53,"t":9},{"x":6.583333333333333,"y":53.13333333333333,"t":7},{"x":5.416666666666667,"y":51.45,"t":6},{"x":5.766666666666667,"y":51.2,"t":7},{"x":4.933333333333334,"y":51.56666666666667,"t":7},{"x":6.266666666666667,"y":52.43333333333333,"t":4},{"x":5.15,"y":51.86666666666667,"t":7},{"x":4.1,"y":51.983333333333334,"t":11},{"x":6.516666666666667,"y":52.733333333333334,"t":7},{"x":5.433333333333334,"y":52.53333333333333,"t":10},{"x":6.65,"y":52.06666666666667,"t":5},{"x":4.55,"y":52.416666666666664,"t":10},{"x":6.2,"y":53.416666666666664,"t":9},{"x":5.766666666666667,"y":53.21666666666667,"t":8},{"x":5.533333333333333,"y":52.45,"t":8},{"x":5.783333333333333,"y":50.916666666666664,"t":9},{"x":5.883333333333333,"y":52.7,"t":7},{"x":7.15,"y":53.2,"t":8},{"x":4.45,"y":51.95,"t":8},{"x":4.766666666666667,"y":52.3,"t":9},{"x":5.383333333333334,"y":52.88333333333333,"t":10},{"x":5.216666666666667,"y":53.36666666666667,"t":12},{"x":6.9,"y":52.266666666666666,"t":6},{"x":4.416666666666667,"y":52.18333333333333,"t":8},{"x":4.95,"y":53.25,"t":11},{"x":3.6,"y":51.45,"t":12},{"x":5.7,"y":51.65,"t":7},{"x":3.8333333333333335,"y":51.233333333333334,"t":9},{"x":3.9,"y":51.53333333333333,"t":8},{"x":4.333333333333333,"y":51.45,"t":6}]
oeo4b commented 10 years ago

Hello, Yes I agree, this rewrite is an improvement! I'm still working on fixing up some of the bugs, but that error you're getting is from calling the cholesky decomposition function with a non-positive definite matrix. Should be solved by increasing the variance for the noise process, I'll try to address this issue in the README.

This problem was actually one of the motivating reasons for adding these adjustable parameters to this new version of the code. Especially with these small sample sizes, since I'm using least squares, a lot of the times you're dealing with these non-positive definite matrices that can only be inverted after some penalization.

ssured commented 10 years ago

Maybe we should throw a descriptive error if this happens? Also is it possible to auto adjust the parameters so the inversion can be done? I dont understand the exact math involved here...