binomial_test_fl()

The function binomial_test_fl() performs the binomial test.

Note

Syntax

T | invoke binomial_test_fl(successes, trials, success_prob, alt_hypotheis)

Arguments

  • successes: The name of the column containing the number of success results.
  • trials: The name of the column containing the total number of trials.
  • p_value: The name of the column to store the results.
  • success_prob: The success probability, default is 0.5.
  • alt_hypotheis: The alternative hypothesis can be either 'two-sided', 'greater', or 'less'. The default is 'two-sided'.

Usage

binomial_test_fl() is a user-defined tabular function, to be applied using the invoke operator. You can either embed its code in your query, or install it in your database. There are two usage options: ad hoc and persistent usage. See the below tabs for examples.

For ad hoc usage, embed its code using the let statement. No permission is required.

let binomial_test_fl = (tbl:(*), successes:string, trials:string, p_value:string, success_prob:real=0.5, alt_hypotheis:string='two-sided')
{
    let kwargs = pack('successes', successes, 'trials', trials, 'p_value', p_value, 'success_prob', success_prob, 'alt_hypotheis', alt_hypotheis);
    let code = ```if 1:
        from scipy import stats
        
        successes = kargs["successes"]
        trials = kargs["trials"]
        p_value = kargs["p_value"]
        success_prob = kargs["success_prob"]
        alt_hypotheis = kargs["alt_hypotheis"]
        
        def func(row, prob, h1):
            pv = stats.binom_test(row[successes], row[trials], p=prob, alternative=h1)
            return pv
        result = df
        result[p_value] = df.apply(func, axis=1, args=(success_prob, alt_hypotheis), result_type="expand")
    ```;
    tbl
    | evaluate python(typeof(*), code, kwargs)
}
;
datatable(id:string, x:int, n:int) [
'Test #1', 3, 5,
'Test #2', 5, 5,
'Test #3', 3, 15
]
| extend p_val=0.0
| invoke binomial_test_fl('x', 'n', 'p_val', success_prob=0.2, alt_hypotheis='greater')
id	x	n	p_val
Test #1	3	5	0.05792
Test #2	5	5	0.00032
Test #3	3	15	0.601976790745087