Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a simple DFT algorithm when FFTW is not in Base #641

Merged
merged 2 commits into from
Jul 17, 2017
Merged

Conversation

ararslan
Copy link
Member

@ararslan ararslan commented Jul 15, 2017

Avoids having to introduce a dependency on FFTW, which has binary dependencies, just for this one distribution. Distributions is already a pretty heavyweight dependency for packages; FFTW (once it actually works) will only make that worse. Thus I'm proposing this simple, standalone DFT implementation for the Poisson binomial PDF.

@ararslan
Copy link
Member Author

The nightly failures are due to unescaped backslashes in docstrings, which will hopefully be fixed by #640.

if VERSION >= v"0.7.0-DEV.602"
function _dft(x::Vector{T}) where T
n = length(x)
y = fill!(copy(x), zero(complex(float(T))))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please look at this one again. I'm pretty sure it is not doing what you intended it to do. Furthermore, there is no reason to initialize the memory since that is done in the loop below.

n = length(x)
y = fill!(copy(x), zero(complex(float(T))))
@inbounds for j = 0:n-1, i = 0:n-1
y[k+1] += x[j+1] * cis(-2π * j * k / n)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

k should be i or vice versa.

Copy link
Contributor

@jmxpearson jmxpearson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it also be possible to have a couple of quick tests showing that _dft and fft! return the same result? Also, _dft -> _dft!?

@ararslan
Copy link
Member Author

Also, _dft -> _dft!?

Calling this _dft! would be misleading since it doesn't compute the FFT in-place on the input; it allocates a second array and modifies that. I'm honestly not familiar enough with FFT algorithms to implement anything other than the not-in-place N2 algorithm. I figured that we could put something simple in place and improve it as needed.

Also the things that Andreas noted are really embarrassing. 😐 Sorry about that...

@andreasnoack
Copy link
Member

Btw. I looked at the pdf for the Poisson-Binomial and the n^2 might be better suited here in any case because I think that n typically will be quite small.

@ararslan ararslan mentioned this pull request Jul 16, 2017
@jmxpearson
Copy link
Contributor

@ararslan Ah, I see. My mistake. I got confused about it being a drop-in for fft!. Will double-check everything later tonight.

@andreasnoack andreasnoack changed the title Use a simple FFT algorithm when FFTW is not in Base Use a simple DFT algorithm when FFTW is not in Base Jul 17, 2017
@andreasnoack andreasnoack merged commit a0a0917 into master Jul 17, 2017
@ararslan ararslan deleted the aa/ffts branch July 17, 2017 00:43
@ararslan
Copy link
Member Author

Looks like this isn't retaining sufficient accuracy for the Poisson binomial PDF tests to pass. In particular:

Expression: isapprox(pdf(d, k), m, atol=1.0e-15)
Evaluated: isapprox(1.0373646386341306e-15, 3.099087406079243e-76; atol=1.0e-15)

We could widen the tolerance for the test or find a better algorithm...

@jmxpearson
Copy link
Contributor

If the tolerance is widened, would it be helpful to issue a warning (at least until this workaround is unnecesary)?

@ararslan
Copy link
Member Author

The workaround is more or less permanent; the FFTW bindings (and the FFT functionality as a whole) were removed from Base in Julia 0.7 and this replaces the uses of it with a simple FFT implementation.

@jmxpearson
Copy link
Contributor

jmxpearson commented Jul 17, 2017 via email

@andreasnoack
Copy link
Member

I believe that tests here are deterministic. It loops through a fixed set of parameter values and compares the pdf with a slower more precise version. I printed all the values for the previous FFTW based version and it produced values around 0.0 and eps() while the new one is up to 8eps() so neither of them have any relative accuracy in the tail. If we really care about the relative accuracy, we should probably change algorithm completely to the version used in the tests but I'm not sure accuracy at that level is required.

@jmxpearson
Copy link
Contributor

jmxpearson commented Jul 17, 2017 via email

@ararslan
Copy link
Member Author

Good idea. Let's merge #640 first so that the note in the docs can be applied in the right place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants