Skip to content

Suggest replacing scipy.stats.norm.cdf with scipy.special.ndtr for performance improvement #1468

@SaFE-APIOpt

Description

@SaFE-APIOpt

return pd.Series(scipy.stats.norm.cdf(s), index=s.index)

Current Code:
return pd.Series(scipy.stats.norm.cdf(s), index=s.index)
Suggested Replacement:

from scipy.special import ndtr
return pd.Series(ndtr(s), index=s.index)

The current implementation uses scipy.stats.norm.cdf(s) to compute the cumulative distribution function (CDF) of a standard normal distribution. While this approach is correct and expressive, it introduces unnecessary overhead because norm.cdf constructs and evaluates a frozen distribution object under the hood.

In contrast, scipy.special.ndtr(s) provides a low-level, highly optimized C implementation that computes the standard normal CDF directly. It avoids object creation and internal delegation, making it significantly faster—especially when applied to large arrays or inside performance-critical loops.

Since both norm.cdf(s) and ndtr(s) produce numerically identical results for the standard normal distribution, replacing one with the other is a safe and efficient optimization.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions