FILE: docs/api/scipy.md¶
SciPy API Reference¶
Functions for statistical testing, effect sizes, multiple comparisons correction, and curve fitting.
compare_groups(a, b, alpha=0.05, paired=False, verbose=True)¶
Compare two groups using the appropriate statistical test. Automatically checks normality and selects t‑test or Mann‑Whitney/Wilcoxon.
Parameters:
- a, b – array‑like, the two groups to compare
- alpha – significance level (default 0.05)
- paired – whether the groups are paired observations
- verbose – print the full report
Returns: Dict with keys:
- test_name – name of the test used
- statistic – test statistic
- pvalue – p‑value
- significant – boolean (p < alpha)
- alpha – significance level
- effect_size – Cohen's d
- effect_label – 'negligible', 'small', 'medium', 'large'
- normality_a, normality_b – bool
- n_a, n_b – sample sizes
- interpretation – plain‑English summary
Returns None if inputs are invalid.
Example:
result = dk.compare_groups(group1, group2)
print(result['interpretation'])
result = dk.compare_groups(before, after, paired=True)
cohen_d(a, b)¶
Compute Cohen's d effect size for two independent groups.
Parameters:
- a, b – array‑like
Returns: (d_value, label) where label is 'negligible', 'small', 'medium', or 'large'. Returns (None, None) if pooled standard deviation is zero.
Example:
d, label = dk.cohen_d(treatment, control)
correct_pvalues(pvalues, method='fdr_bh', alpha=0.05, verbose=True)¶
Apply multiple comparisons correction to a list of p‑values.
Parameters:
- pvalues – list or array of p‑values
- method – 'bonferroni', 'fdr_bh' (Benjamini‑Hochberg), 'fdr_by', 'holm'
- alpha – family‑wise error rate
- verbose – print a table of original vs corrected p‑values
Returns: Dict with keys:
- reject – boolean array
- pvalues_corrected – array
- method, alpha
- n_rejected, n_total
Returns None if statsmodels is not installed.
Example:
corrected = dk.correct_pvalues(pvals, method='bonferroni')
print(f"Rejected {corrected['n_rejected']} hypotheses")
fit_and_plot(model_fn, x, y, p0=None, plot_residuals=True, figsize=(12, 4), show=True, save=None, dpi=150, verbose=True)¶
Fit a custom model function to data and plot the fit (and optionally residuals).
Parameters:
- model_fn – function f(x, *params) → y
- x, y – array‑like, data points
- p0 – initial guess for parameters (optional)
- plot_residuals – show residuals plot (right panel)
- figsize – figure size (width, height)
- show – call plt.show()
- save – file path to save figure
- dpi – resolution
- verbose – print parameter estimates and standard errors
Returns: Dict with keys:
- popt – optimal parameters
- pcov – covariance matrix
- perr – standard errors
- residuals – array
- r_squared – R² value
- fig – matplotlib Figure
- axes – list of Axes objects
Returns None if fitting fails.
Example:
def linear(x, a, b):
return a * x + b
result = dk.fit_and_plot(linear, x, y)
print(f"Slope: {result['popt'][0]:.3f}, R²: {result['r_squared']:.3f}")