Giter Site home page Giter Site logo

kriging.js's Introduction

kriging.js

kriging.js is a Javascript library providing spatial prediction and mapping capabilities via the ordinary kriging algorithm.

Kriging is a type of gaussian process where 2-dimensional coordinates are mapped to some target variable using kernel regression. This algorithm has been specifically designed to accurately model smaller data sets by assigning a prior to the variogram parameters.

Fitting a Model

The first step is to link kriging.js to your html code and assign your coordinate and target variables to 3 separate arrays.

<script src="kriging.js" type="text/javascript"></script>
<script type="text/javascript">
	var t = [ /* Target variable */ ];
	var x = [ /* X-axis coordinates */ ];
	var y = [ /* Y-axis coordinates */ ];
	var model = "exponential";
	var sigma2 = 0, alpha = 100;
	var variogram = kriging.train(t, x, y, model, sigma2, alpha);
</script>

The train method in the kriging object fits your input to whatever variogram model you specify - gaussian, exponential or spherical - and returns a variogram object.

Error and Bayesian Prior

Notice the σ2 (sigma2) and α (alpha) variables, these correspond to the variance parameters of the gaussian process and the prior of the variogram model, respectively. A diffuse α prior is typically used; a formal mathematical definition of the model is provided below.

Predicting New Values

Values can be predicted for new coordinate pairs by using the predict method in the kriging object.

  var xnew, ynew /* Pair of new coordinates to predict */;
  var tpredicted = kriging.predict(xnew, ynew, variogram);
  

Creating a Map

Variogram and Probability Model

The various variogram models can be interpreted as kernel functions for 2-dimensional coordinates a, b and parameters nugget, range, sill and A. Reparameterized as a linear function, with w = [nugget, (sill-nugget)/range], this becomes:

  • Gaussian: k(a,b) = w[0] + w[1] * ( 1 - exp{ -( ||a-b|| / range )2 / A } )
  • Exponential: k(a,b) = w[0] + w[1] * ( 1 - exp{ -( ||a-b|| / range ) / A } )
  • Spherical: k(a,b) = w[0] + w[1] * ( 1.5 * ( ||a-b|| / range ) - 0.5 * ( ||a-b|| / range )3 )

The variance parameter α of the prior distribution for w should be manually set, according to:

  • w ~ N(w|0, αI)

Using the fitted kernel function hyperparameters and setting K as the Gram matrix, the prior and likelihood for the gaussian process become:

  • y ~ N(y|0, K)
  • t|y ~ N(t|y, σ2I)

The variance parameter σ2 of the likelihood reflects the error in the gaussian process and should be manually set.

kriging.js's People

Contributors

oeo4b avatar omarx5 avatar richorama avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kriging.js's Issues

Array.prototype.rep is unnecessary and causing errors

The function Array.prototype.rep is essentially just creating an array of size n with a specific value. It is only used creating issues in kriging_matrix_diag, which creates an array of size n^2 with the value 0. It is also a recursive function and will create a massive amount of recursive calls. When n is around ~350, javascript (at least in my version of chrome) will respond with a nice "javascript Uncaught RangeError: Maximum call stack size exceeded" error.

I solved this by replacing Z = [ 0 ].rep(n * n) in kriging_matrix_diag with:

    var Z = [];
    var len = n * n;
    for ( var i = 0; i < len; i++) {
        Z.push(0);
    }

Of course, if this is needed elsewhere, it is quite easy to extract this as a method of len and value and apply to other places where rep is called :) This method is much faster than the rep one, so that should be done.

Can't run the application

Hi Sir,
is it possible to send me an example of the use of kriging.js ?
I thank you in advance.

Ps: it's really matter to me.

kriging_matrix_diag is not defined

I use vue3+typescript and import kriging like:

 import kriging from "/src/utils/kriging.js";

then i got this error:
Uncaught ReferenceError: kriging_matrix_diag is not defined
at kriging.js:37:25
at kriging.js:481:2

Performance is poor

With data sets > 1000 points, the training time gets too long.

Not sure if there's anything that can be done..

javascript error in chrome canary

Sometimes google chrome canary show this error in console:
Uncaught ReferenceError: Invalid left-hand side expression in prefix operation in file kriging.js:147

Chrome version: Version 38.0.2100.0 canary (64-bit)

Gaussian and Exponential Not entirely true

Reference address

https://pro.arcgis.com/en/pro-app/latest/tool-reference/3d-analyst/how-kriging-works.htm#:~:text=Semivariogram%20modeling%20is%20a%20key%20step%20between%20spatial,provides%20information%20on%20the%20spatial%20autocorrelation%20of%20datasets.

Fix Code


    // Variogram models
	//nugget c0
	//c=(sill-nugget)/range;
	//a=range
    var kriging_variogram_gaussian = function(h, nugget, range, sill, A) {
		if(h>0){
		let c=(sill-nugget)/range;
		return nugget+c*( 1.0 - Math.exp(-(Math.pow(h/range, 2))));
		}
		return 0.0;
    };
    var kriging_variogram_exponential = function(h, nugget, range, sill, A) {
		let c=(sill-nugget)/range;
	return nugget + c*( 1.0 - Math.exp(-h/range));
    };
    var kriging_variogram_spherical = function(h, nugget, range, sill, A) {
		let c=(sill-nugget)/range;
	if(h>range) return nugget + c;
	if(0<h<=range) return nugget + c*( 1.5*(h/range) - 0.5*Math.pow(h/range, 3) );
    };
	
	var kriging_variogram_circular = function(h, nugget, range, sill, A) {
		let c=(sill-nugget)/range;
		if(0<h<=range){
			let temp=(h/range)*Math.sqrt(1-Math.pow(h/range),2)+Math.asin(h/range);
			return nugget+(2*c/Math.PI)*temp;
		}
		if(h>A){
			return nugget+c;
		}
		return 0.0;
	};

	var kriging_variogram_linear = function(h, nugget, range, sill, A) {
		let c=(sill-nugget)/range;
	if(0<h<=range){
		return nugget+c*(h/range);
	}
	if(h>A){
		return nugget+c;
	}
		return 0.0;
	};

There missed a ending symbol

I have download the kriging.js and use it in my project,but there is a error when i try to change the code like this :

(function(a){
	console.log(a);  
}(1234));

Or like this:

(function(a){
	console.log(a);  
})(1234);

And i got a error
(intermediate value)(...) is not a function

But everything is ok when use it like this:

var fn=function(a){
	console.log(a);   
}(12345678)

And finally,i find there missed a ";" in line 27,the old is:

Array.prototype.pip = function(x, y) {
    var i, j, c = false;
    for(i=0,j=this.length-1;i<this.length;j=i++) {
	if( ((this[i][1]>y) != (this[j][1]>y)) && 
	    (x<(this[j][0]-this[i][0]) * (y-this[i][1]) / (this[j][1]-this[i][1]) + this[i][0]) ) {
	    c = !c;
	}
    }
    return c;
}

When add the ";" at last like this,the error is been fixed

Array.prototype.pip = function(x, y) {
    var i, j, c = false;
    for(i=0,j=this.length-1;i<this.length;j=i++) {
	if( ((this[i][1]>y) != (this[j][1]>y)) && 
	    (x<(this[j][0]-this[i][0]) * (y-this[i][1]) / (this[j][1]-this[i][1]) + this[i][0]) ) {
	    c = !c;
	}
    }
    return c;
};

So don't be lazy,add the ";" at the end of every assignment statement is very important!!!

large data set

Hello,
Thanks a lot for your work with kriging.js

Hear is the problem.
I have a data set with
118632 register
sampledata

whe I create a model
var model = "spherical";
var sigma2 = 0, alpha = 100;
var variogram = kriging.train(array_z, array_x, array_y, model, sigma2, alpha);

I get error "RangeError: invalid array length @ file:///E:/kringingjs/javascript/kriging.js-master/kriging.js:233"
image

What can I do to make kriging with this dataset?
maybe select few data. convert 118632 samples in 500 samples?

Integrate with D3

Hi

A couple of weeks ago I mentioned your code to Mike Bostock, author of D3, see d3/d3#1617

It would be awesome to integrate your kriging implementation into D3. Are you interested in creating a plugin?

Cheers,
Sjoerd

License?

Hi, Thanks for the library. I notice there's no license file, is this project meant to be open source?

Contour Map Sample

Hi,
Do you have an example implementation of the code Kriging?
I could not run the application only with javascript.

thanks

Polygon outline

Very good this book!
Does anyone know how I could add a contour between the polygon? I have a map of a farm that have several polignos (which are the divisions of the farm and smaller parcels), I need it to have a contour in each polygon.

kriging.js:15 Uncaught RangeError: Maximum call stack size exceeded

Hii, I'm using around 1000 Data Points for Kriging and upto the point I understood it requests exponential memory and Max Call Stack Exceeds. Do you have any solution or tweak to handle this situation efficiently?
I'm sure it is somehow made to handle average sized dataset.

Moreover, is it possible to use Bayesian Kriging or any help?

Semi Variogram

Hi there, first off thank you so much for developing this and doing all the hard math! I am a pretty green JS and kriging person and hopefully you can help me out.

My question for you is how to go about getting the data for building a variogram plot, like here https://vsp.pnnl.gov/help/Vsample/Kriging_Variogram_Model.htm

Plotting the theoretical model (e.g. the line) should be straightforward, i can just apply the .model property on the variogram object at different h values then square it.

However to get the pairwise distances to plot the data points, and their associated variances.... I don't know. I see on the variogram there are "t", 'K" and "M". K seems to be something to do with the pairwise, as it's the length of the total number of points squared... There is also a kriging.variance method I found in the source code that might also be part of the equation... I could really use an explanation! Thanks so much.

Kriging on Google Maps

Awesome work on this! I am new to this and am trying to render data to a google map, using kriging - could you give any pointers on how to do this or are there examples somewhere? It seems there is a dependency on providing a map polygon atm.

Adding enumerable properties to Array

It is adding max, min, mean, rep, and pip as an enumerable property to all the arrays.
As a result of which these properties are coming up in all for..in loops (even on empty arrays).
As a temporary fix, I had to modify all the additions in the Array.prototype using Object.defineProperty.

sqrt of negative number

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}]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.