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

propagate to add two stochastic triples together #125

Open
GuusAvis opened this issue Jun 6, 2024 · 5 comments · May be fixed by #126
Open

propagate to add two stochastic triples together #125

GuusAvis opened this issue Jun 6, 2024 · 5 comments · May be fixed by #126

Comments

@GuusAvis
Copy link

GuusAvis commented Jun 6, 2024

Hello!

I'm trying to use the propagate function to extract derivatives from some Monte Carlo simulations. However, I noticed that the function does not behave as I would expect in even some simple use cases. A minimal example is given by simply adding two stochastic triples together:

using StochasticAD

function add(x::Real, y)
    x + y
end

function add(x::StochasticAD.StochasticTriple, y)
    StochasticAD.propagate(add, x, y; keep_deltas=true)
end

# x = 1
x = stochastic_triple(1)
y = stochastic_triple(5)

add(x, y)  # 6 + 0ϵ
x + y  # 6 + 2ϵ
add(x, y) == x + y  # false

I expected that propagate would automatically ensure that this simple function would correctly propagate the perturbations but this does not seem to be the case. Am I doing something wrong or is this a bug?

Thanks!

@gaurav-arya
Copy link
Owner

At quick glance, I think it's because you need keep_deltas = Val(true), not just true. Although we should definitely catch the latter case and error.

Krastanov added a commit to Krastanov/StochasticAD.jl that referenced this issue Jun 6, 2024
@Krastanov Krastanov linked a pull request Jun 6, 2024 that will close this issue
@GuusAvis
Copy link
Author

GuusAvis commented Jun 7, 2024

At quick glance, I think it's because you need keep_deltas = Val(true), not just true. Although we should definitely catch the latter case and error.

Many thanks for the quick reply and catching that silly mistake on my end! However I don't think replacing true with Val(true) fixes the issue. Moreover, I noticed that it persists even when one of the numbers is not a stochastic triple:

using StochasticAD

function add(x, y)
    x + y
end

function add(x::StochasticAD.StochasticTriple, y)
    StochasticAD.propagate(add, x, y; keep_deltas=Val(true))
end

x = stochastic_triple(1)
y = 5

add(x, y)  # 6 + 0ϵ
x + y  # 6 + 1ϵ
add(x, y) == x + y  # false

@gaurav-arya
Copy link
Owner

Just a hunch: can you try writing stochastic_triple(1.0)?

@GuusAvis
Copy link
Author

GuusAvis commented Jun 7, 2024

Just a hunch: can you try writing stochastic_triple(1.0)?

Thanks, that does the trick! Any idea why the behavior for triples for floats is different than that for triples of ints?

@gaurav-arya
Copy link
Owner

Well, an integer shouldn't really be able to possess continuous perturbations. But again, there definitely shouldn't be a silent error here, I need to fix that!

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 a pull request may close this issue.

2 participants