Whether hygeinv command returns 'NaN' depends on form of vector

1 view (last 30 days)
Hi,
I am analyzing the median of a hypergeometric distribution using MatLab. I know the hygeinv command is known to return "NaN", but I have a strange case where this problem seems to depend only on the way I input a vector.
So I have X~hyper(N,n,n) where N = size of population and n=sample size and n=number of successes. I express n as a % of N, so I define a vector of percentages
Pi_n = [0.1:0.1:0.5];
from which n is computed as n=N*Pi_n and the median is computed using hygeinv(0.5,N,n,n)
When I do this (and the code is shown below), MatLab returns "NaN" for p=0.3
BUT, when I enter the values of ther percenatges explicityl
Pi_n = [0.1,0.2,0.3,0.4,0.5];
I have no problems.
%%%%%%%%%%%%%%%%%%%%%% CODE %%%%%%%%%%%%%%%%%%%%%%%%
% II INITIALIZING VARIABLE AND ARRAY
clear all
N = 100; % Population size
Pi_n = [0.1:0.1:0.5]; % sample size as % of popltn size (DOES NOT WORK)
% Pi_n = [0.1,0.2,0.3,0.4,0.5]; % sample size as % of popltn size (DOES WORK)
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% III CALCULATIONS FOR MEDIAN
n = N*Pi_n; % compute n, which is both sample size & # successes
Median = hygeinv(0.5,N,n,n);
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% IV OUTPUT
plot(100*Pi_n,Median(1,:),'-or','Linewidth',2)
  7 Comments
Stephen23
Stephen23 on 21 Dec 2020
Edited: Stephen23 on 21 Dec 2020
"The first statement suggests the operations (which I take you to mean by "numeric methods") could affect the precision..."
The (binary) precision of a binary floating point number does not change. Different operations affect the values, not the precision. Even changing the order of the same operations can affect the values: associative laws of algebra do not necessarily hold for floating-point numbers. But the precision of the data class cannot change (subnormals excluded).
"it seems that it shouldn't work for any method"
I don't see why that would be true. An algorithm's implementation can be sensitive to accumulated error for specific values, for specific edge-cases, for error accumulating in one particular direction (but not the other), or ... etc. etc. Apparently the algorithm implemented in that function is numerically unstable: use the debugging tools to find out why (it is also possible that there is a bug in the code... such things are known to occur).
"... it is a pain because it means that the shorthand way of defining vectors may be less reliable for some applications and I may have to write out the vector in some cases"
Neither is more "reliable" than the other, they are both "correct" within the precision of the data that you are using. Understanding that is key to understanding how to write robust code that works on binary floating point numbers.
Matthew Brenneman
Matthew Brenneman on 21 Dec 2020
Again, I really appreciate your patience and all the information! Thanks

Sign in to comment.

Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!