Basic Examples (2)
Find the location of the first Fibonacci number bigger than 100:
Verify:
Scope (2)
If the monotone predicate does not contain a boundary, the function returns All or None. For example, all Fibonacci numbers are less than ∞:
No Fibonacci numbers are negative:
IntegerBinarySearch returns the index on the boundary where the predicate was True. Find the location of the last Fibonacci number less than 30:
Find the location of the first Fibonacci number > 30:
IntegerBinarySearch[f,{max}] is equivalent to IntegerBinarySearch[f,{1,max}]:
Applications (4)
Find the first regular polygon with area larger than 3 and display it:
Find the first Fibonacci number that has 100 digits:
Find the first n where the relative error of Stirling's factorial approximation is no bigger than one part in a million:
Find out how many coin flips one should do in order to see 100 heads with 95% probability:
Possible Issues (3)
The function may not return expected results if the search range is not sufficiently large:
Increasing the search interval solves the problem:
Binary search may fail on non-monotone functions:
But if the interval bounds capture the flip, at least some position will be found:
If the predicate is not boolean, an error will occur:
Neat Examples (4)
Design the optimal threshold for a statistical binomial hypothesis test. In this example:
α is a type I error (rejection of H0 when H0 is true),
β is a type II error (rejection of Ha when Ha is true)
when H0 is rejected if observed sample is >= q:
A test for H0: p=0.5 versus alternative hypothesis Ha: p=0.6 for 1000 draws:
Plot both error types for various threshold cutoff values:
The optimal (pessimistic worst error) threshold is somewhere where both errors meet:
Conclusion: the optimal cutoff threshold for a binomial statistical test for H0: p=0.5 versus alternative hypothesis Ha: p=0.6 for 1000 draws is 551