scikit-learn/scikit-learn — pull request #3656 by dougalsutherland (2014-09-11 07:13:19+00:00)
Title: RBFSampler off by a factor of 2

`RBFSampler` is missing a factor of 2 in the Gaussian to sample from. (The paper talks about the kernel `exp(1/(2 σ^2) ||x-y||^2)` Fourier-transforming into a Gaussian with variance `1/σ^2`; the current code has `exp(ɣ ||x-y||^2)` Fourier-transforming to a Gaussian with variance `ɣ`, dropping the factor of 2.)

Demonstration: here's the value of the RBF kernel between 0 and 1 for varying values of `gamma`, via `rbf_kernel`, `RBFSampler(gamma)`, and `RBFSampler(2 * gamma)`:
![](https://i.imgur.com/1z4Fk4u.png).

The reason `test_rbf_sampler` works is because the test is too loose, and it happens to be somewhat close at the values tested:
![](https://i.imgur.com/97fsDz5.png)
(x is true value, y is the approximated value). I just upped the number of Monte Carlo samples and increased it to 2 decimals, giving this new scatterplot:
![](http://i.imgur.com/4KnURsY.png)
But `assert_array_almost_equal` isn't really the right tool for this job (we can still only use 2 decimals here, with 10k samples): it should probably test a fairly tight bound required on the mean absolute difference between the two and a looser max bound, or something.

Here's [a notebook](http://nbviewer.ipython.org/gist/dougalsutherland/11fd9895924c3c1aed19) that makes these pictures.