Closed yuriy0 closed 7 years ago
A negative weight is always an error. Something, somewhere, went wrong. Looks like a -
got dropped.
If you look at (or even trace
) how factorize
(in NewSLO/From.mpl
) calls list_of_mul
, you'll see that we do try to keep weights positive. For example, applying unintegrate
to Int((1-p^2)*(1+p)*applyintegrand(...),p=0..1)
should not yield Weight(-1,...)
But the problem in this issue seems different because, as @JacquesCarette puts it, it looks like a -
got dropped, and it's not canceled out by another -
. Maybe it would still help to trace(unintegrate)
As it turns out, this wasn't any (machine) making a mistake - the expected output program itself was completely bogus. It was:
t65' =
lam $ \t ->
uniform_0_1 >>= \x->
withWeight (... &&
x < t)
(unsafeProb (recip (t * real_ (-1) + x)))
but of course if x < t
then x - t
is negative.
For example, applying unintegrate to Int((1-p^2)(1+p)applyintegrand(...),p=0..1) should not yield Weight(-1,...)
For the sake of posterity: this Int
does not get to unintegrate
- it is evaluated by elim_intsum
.
Haskell throws an error while parsing (or typechecking) the result of the following:
Presumably Haskell produces this error because the output from Maple contains a weight which is identically non-positive:
You can optionally squint at the above, or look at the nice plots Maple will make:
The 'simplify' interpretation of weights is that Maple may scale weights to it's hearts content; but Hakaru has a more strict interpretation. The weight we really want is:
This handwritten program is the expected result:
which only differs from the actual result by a
negate
, and correctly roundtrips through 'simplify'.It seems the correct thing to do here is to detect in Maple when a weight is identically non-positive, and just negate it. But such a check is potentially expensive, and should likely likely be done as a final step before sending back to Haskell. And it is an extremely specific operations which has zero likelihood to affect Maple's ability to simplify the program. The question lies in whether this particular case should be implemented, or some more general class of 'weight normalization' should be done by Maple.