Sloppy code
This code is extremely sloppy, making it very hard to review. Next time, before posting here, please paste it on http://jshint.com/ or http://jslint.com/ andfirst, and make the suggested corrections first.
Don't repeat yourself
Copy-pasting code is a very dangerous practice. Don't do it. For example:
var dx = cx.map(function(a){ return a.map(function(_, i){return a[i]-a[i-1]}).slice(1, a.length) }), dy = cy.map(function(a){ return a.map(function(_, i){return a[i]-a[i-1]}).slice(1, a.length) });
You do this in many other places too:
repeatyou write the same anonymous function twice.
Give the function a nice, descriptive name,
make it nicely indented and readable,
and rewrite the two map calls in terms of it.
For example:
// note: try to give this a more descriptive name than "mapper"
function mapper(a) {
return a.map(function(_, i) { return a[i] - a[i-1] }).slice(1, a.length)
}
var dx = cx.map(mapper),
dy = cy.map(mapper);
ActuallyOn closer look, the entire stats method is about deriving Mx and My by applying the exact same sequence of operations on grid and tgrid, respectively.
Instead of writing all those non-trivial steps twice,
it would be better to move the common logic to a helper function with no duplicated code,
so that you can rewrite as:
var Mx = helper(grid),
My = helper(tgrid);