-
Notifications
You must be signed in to change notification settings - Fork 5
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
Problems with applying Deriv() to dbinom(). #25
Comments
Thanks for reporting. I confirm both problems that I could reproduce on my side. |
Signed-off-by: Serguei Sokol <sokol@insa-toulouse.fr>
dbinom's rule is fixed in v4.1.4. Could you confirm that it works correctly on your side, please?
|
I sourced my dem01 script and the graph of the derivative of dbinom() now looks correct. I also executed
and got 2.064384 which is the correct answer. However I still get an error when I try to define a differentiation rule in a new environment rather than in "drule". E.g.: I also remain puzzled by the fact that things still don't work with my somewhat shakey "local" version of dbinom(). If I execute
then I get lD2(3,8,0.6) equal to 1.769472, the "old" wrong answer. |
One thing after another. I did not yet get a look at the problem on a new environment. But it is on my "todo" list.
things work as expected. |
Ah!!! The light dawns. I really was being silly though --- it's now clear in Perhaps the help file for Deriv should warn the user NOT to use variables named ".e1", ";e2", ... etc. when constructing new rules. Good luck with fixing the problem with putting rules in new environments. |
Here is another annoyance/item to add to your already overfull to-do list. I have found that when the "x" argument, provided to the derivative of dbinom() ,is a vector, disconcerting warning messages result. E.g.: d1 <- Deriv(dbinom,"prob") I think that this can be fixed by changing the rule for dbinom() to something like the following: prob=(ifelse(x %in% 0, -size * (1 - prob)^(size - 1), You think? |
Even if Deriv() pretends to work with scalar expressions, you are right, it's better to make it as much compatible as possible with vector expressions. That will require to fix many other similar expressions. |
The situation was revealed to be worthier than that. Even if you use |
current version fixes both new.env() and compound expressions in drule. Does it work well on your side ? |
The "new.env()" problem seems to be solved: tempEnv <- new.env() No error thrown, and the answer looks correct to me. However there seems to be a new (?) problem with providing a vector argument to the output of Deriv(): xxx <- Deriv(dbinom,"prob") Sorry 'bout that. |
Right. It's because |
Arrghhh! I see the problem. Thanks for working out what was going wrong. In the application that I have in mind, I really need x to be allowed to be a vector quantity. I thought just now that I had worked out a rule that would accommodate this desideratum (not using ifelse()). However I cannot get my idea to work. Also, this idea involves using lists and unlist() so even if I could get it working for first derivatives, I suspect that it would break for second and higher derivatives. So, yes. Go back to the if / else construction and eschew ifelse(). I think that I will be able to handle things in my application by using sapply() on vector valued x. Sorry for introducing this red herring. |
(1) I believe the code in the rule for differentiating dbinom(), found
in the drule environment, is incorrect. The attached script "demo01.txt"
provides evidence for my belief. (The derivative produced by Deriv()
is always negative, whereas it should be positive for prob < y/size
and negative for prob > y/size.)
The script demo01.txt also contains code for a proposed correction to the
rule.
Note in addition that "demo01.txt" raises the issue that if a rule is placed
in a new environment, rather than being placed in "drule", then an
error is thrown when one attempts to calculate a second derivative.
(Calculation of the first derivative seems to be OK.) This phenomenon
is illustrated in the attached script "demo02.txt".
(2) Incorrect results seem to be produced when second derivatives are
calculated when the derivative depends on a rule in "drule". This is
illustrated in the attached script "scr01.txt" This script also produces
correct values using calculations of first and second derivative done
"by hand". Note that the function value f1 agrees with the correct
function value f0, as does the first derivative df1 with the correct
value df0. However the second derivative value d2f1 is 1.769472 which
differs from the correct value, d2f0 = 2.064384. When derivatives are
calculated without applying a rule from "drule", as in the attached
script "scr02.txt", d2f1 agrees with the correct value.
Note that the problem is illustrated using a "local" version of dbinom()
called ldb() which has been simplified so as to clarify the issue. The
rule that I placed in "drule" for differentiating ldb() is also made
to be very simplistic so as to illustrate the problem clearly. This
rule is obviously not "robust" and would result in problems with
borderline cases.
demo01.txt
demo02.txt
scr01.txt
scr02.txt
The text was updated successfully, but these errors were encountered: