Topic: Some trouble with the sum function.

Hello,

I'm trying to use the sum function to sum a bunch of binomial distributions together, and the factorial functions seems to break when its used inside the sum function, at least in some situations. I have attached a file to illustrate the problem.

The first few functions in the file are an example of what I'm trying to automate using the sum function. The custom functions in the file are:

1. nchoosek(n,k)  the binomail coeficcient.
2. bin(k,n,p)          the binomial distribution for k successes in n trials, with p probability per trial. This uses the nchoosek function.
3. perbin(k,n,p)    makes the probabilities into percents by passing p/100 to bin() and returning 100* the answer.
4. sums(n,k,p). This uses the sum function to sum the perbin function over values of k from 0 to k. The result is supposed to be the probability of achieving k or fewer successes in n trials.
5. sums2(k,n,p)    Same as above, but instead of using nested functions, it's all in one giant lump in one function. This version actually works, despite the fact that it should be doing exactly the same thing as the above function.

Check out the file and this will all make more sense. Can anyone figure out what's going on here?

Thanks!
-James

Post's attachments

Attachment icon Binomial Distributions.grf 1.87 kb, 612 downloads since 2011-02-26 

Re: Some trouble with the sum function.

I just noticed something else as well, functions with sum() can't be differentiated in graph, which is a bit odd; it seems like a perfectly straightforward function to differentiate. Wouldn't it just be the same sum over the derivative of the function in the first parameter?

Also, graph doesn't seem to like to differentiate functions with fact() in them even if the variable the derivative is being taken with respect to isn't part of the factorial. For example:

y(x)=fact(2)*x can't be differentiated, even though y(x)=2*x obviously can, and is the same function.

Are there any plans to make these improvements to the differentiating engine in graph?

Re: Some trouble with the sum function.

James wrote:

Check out the file and this will all make more sense. Can anyone figure out what's going on here?

There was a bug in Graph, which caused the sum function to fail when the expression in the first argument to sum was a function with more than one argument. I have now fixed it in the beta version.

James wrote:

I just noticed something else as well, functions with sum() can't be differentiated in graph, which is a bit odd; it seems like a perfectly straightforward function to differentiate. Wouldn't it just be the same sum over the derivative of the function in the first parameter?

Well, straightforward is not the term I would have used. But you are right that it should be possible to differentiate the sum function, and I implemented that in the beta version now.

James wrote:

Also, graph doesn't seem to like to differentiate functions with fact() in them even if the variable the derivative is being taken with respect to isn't part of the factorial. For example:

I never really considered that but it is very easy to fix and it is fixed now in the beta version.

You can find the latest beta version at http://www.padowan.dk/graph/Beta.php

Re: Some trouble with the sum function.

Wow, brilliant. Thanks Ivan!

I can't thank you enough for creating this extremely useful piece of software.

Re: Some trouble with the sum function.

Been looking for that fix! Thanks James!