printingoctavebinomial-cdf

Attempt to get numbers instead of NaN values when printing


I've a simple program with a for loop where i calculate some value that I print to the screen, but only the first value is printed, the rest is just NaN values. Is there any way to fix this? I suppose the numbers might have a lot of decimals thus the NaN issue.

Output from program:

0.18410
NaN
NaN
NaN
NaN

etc.

This is the code, maybe it helps:

for i=1:30
    t = (100*i)*1.1*0.5;
    b = factorial(round(100*i)) / (factorial(round((100*i)-t)) * factorial(round(t)));

    % binomial distribution
    d = b * 0.5^(t) * 0.5^(100*i-(t));

    % cumulative 
    p = binocdf(1.1 * (100*i) * 0.5,100*i,0.5);

    % >=  AT LEAST
    result = 1-p + d;
    disp(result);
end

Solution

  • You could do the calculation of the fraction yourself. Therefore you need to calculate $d$ directly. Then you can get all values of the numerators and the denominators and multiply them by hand and make sure that the result will not get too big. The following code is poorly in terms of speed and memory, but it may be a good start:

    for i=1:30
    t = (55*i);
    b = factorial(100*i) / (factorial(100*i-t) * factorial(t));
    
    % binomial distribution
    d = b * 0.5^(t) * 0.5^(100*i-(t));
    
    numerators = 1:(100*i);
    denominators = [1:(100*i-t),1:55*i,ones(1,100*i)*2];
    value = 1;
    while length(numerators) > 0 || length(denominators) > 0
        if length(numerators) == 0 
            value = value/denominators(1);
            denominators(1) = [];
        elseif length(denominators) == 0
            value = value* numerators(1);
            numerators(1) = [];
        elseif value > 10000
            value = value/denominators(1);
            denominators(1) = [];
        else
            value = value* numerators(1);
            numerators(1) = [];
        end
    end
    
    % cumulative 
    p = binocdf(1.1 * (100*i) * 0.5,100*i,0.5);
    
    % >=  AT LEAST
    result = 1-p + value;
    disp(result);
    end
    

    output:

    0.1841
    0.0895
    0.0470
    0.0255
    0.0142
    0.0080
    0.0045
    ...