Skip to content

Polars

auc(x, y, method='trapezoidal')

Computes the area under the curve (AUC) via numerical integration.

Parameters:

Name Type Description Default
x IntoExpr

The x-axis

required
y IntoExpr

The y-axis

required
method Literal['rectangular', 'trapezoidal']

If "rectangular", use rectangular integration, if "trapezoidal", use trapezoidal integration, by default "trapezoidal"

'trapezoidal'

Returns:

Type Description
Expr

Raises:

Type Description
ValueError

If method is not one of rectangular or trapezoidal

Source code in python/rapidstats/polars.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def auc(
    x: IntoExpr,
    y: IntoExpr,
    method: Literal["rectangular", "trapezoidal"] = "trapezoidal",
) -> pl.Expr:
    """Computes the area under the curve (AUC) via numerical integration.

    Parameters
    ----------
    x : IntoExpr
        The x-axis
    y : IntoExpr
        The y-axis
    method : Literal["rectangular", "trapezoidal"], optional
        If "rectangular", use rectangular integration, if "trapezoidal", use
        trapezoidal integration, by default "trapezoidal"

    Returns
    -------
    pl.Expr

    Raises
    ------
    ValueError
        If `method` is not one of `rectangular` or `trapezoidal`
    """
    if method == "trapezoidal":
        is_trapezoidal = True
    elif method == "rectangular":
        is_trapezoidal = False
    else:
        raise ValueError("`method` must be one of `rectangular` or `trapezoidal`")

    return pl.plugins.register_plugin_function(
        plugin_path=_PLUGIN_PATH,
        function_name="pl_auc",
        args=[
            _into_expr(x).cast(pl.Float64),
            _into_expr(y).cast(pl.Float64),
            pl.lit(is_trapezoidal),
        ],
        returns_scalar=True,
    )

is_close(x, y, rtol=1e-05, atol=1e-08, null_equal=False)

Compares the relative equality of the inputs.

Parameters:

Name Type Description Default
x IntoExpr
required
y IntoExpr
required
rtol float

Relative tolerance, by default 1e-05

1e-05
atol float

Absolute tolerance, by default 1e-08

1e-08
null_equal bool

If True, considers nulls to be equal, by default False

False

Returns:

Type Description
Expr
Source code in python/rapidstats/polars.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def is_close(
    x: IntoExpr,
    y: IntoExpr,
    rtol: float = 1e-05,
    atol: float = 1e-08,
    null_equal: bool = False,
) -> pl.Expr:
    """Compares the relative equality of the inputs.

    Parameters
    ----------
    x : IntoExpr
    y : IntoExpr
    rtol : float, optional
        Relative tolerance, by default 1e-05
    atol : float, optional
        Absolute tolerance, by default 1e-08
    null_equal : bool, optional
        If True, considers nulls to be equal, by default False

    Returns
    -------
    pl.Expr
    """
    x = _into_expr(x)
    y = _into_expr(y)

    res = x.sub(y).abs().le(pl.lit(atol).add(rtol).mul(y.abs()))

    if null_equal:
        res = res.or_(x.is_null().and_(y.is_null()))

    return res