Skip to content

approximating polynomials

Some useful code to approximate inverse cumulative distribution functions to produce approximate random variables by the inverse transform method.

dyadic_intervals_in_half_interval(n_intervals)

Computed the dyadic intervals in [0, 1/2].

Parameters:

Name Type Description Default
n_intervals int

The number of intervals.

required

Returns:

Type Description
intervals

The dyadic intervals. e.g. [[1/2, 1/2], [1/4, 1/2], [1/8, 1/4], ... [0, 1/16]]

Source code in src/pyarv/_approximation_utils/approximating_polynomials.py
def dyadic_intervals_in_half_interval(n_intervals: int) -> list[list[float]]:
    """
    Computed the dyadic intervals in [0, 1/2].

    Parameters
    ----------
    n_intervals
        The number of intervals. 

    Returns
    -------
    intervals
        The dyadic intervals. e.g. `[[1/2, 1/2], [1/4, 1/2], [1/8, 1/4], ... [0, 1/16]]`
    """
    intervals = [[0.5 ** (i + 1), 0.5 ** i] for i in range(n_intervals)]
    intervals[0] = [0.5, 0.5]
    intervals[-1][0] = 0.0
    return intervals

optimal_polynomial_coefficients(*, f, polynomial_order, lower_limit, upper_limit)

Calculates the optimal coefficients of a polynomial approximation to a function .

Parameters:

Name Type Description Default
f Callable

required
polynomial_order int

Order of the polynomial approximation.

required
lower_limit float

required
upper_limit float

required

Returns:

Type Description
coefficients

Polynomial coefficients.

Source code in src/pyarv/_approximation_utils/approximating_polynomials.py
def optimal_polynomial_coefficients(*,
                                    f: Callable,
                                    polynomial_order:int,
                                    lower_limit:float,
                                    upper_limit:float) -> Array:
    r"""
    Calculates the \( L^2 \) optimal coefficients of a polynomial approximation to a function \( f \colon (a, b) \to \mathbb{R} \).

    Parameters
    ----------
    f
        \( f \)
    polynomial_order
        Order of the polynomial approximation. 
    lower_limit
        \( a \) 
    upper_limit
        \( b \) 

    Returns
    -------
    coefficients
        Polynomial coefficients. 
    """
    B = [_integrate(lambda u: u ** i * f(u), lower_limit, upper_limit) for i in range(polynomial_order + 1)]
    A = [[(upper_limit ** (i + j + 1) - lower_limit ** (i + j + 1)) / (i + j + 1.0) for i in range(polynomial_order + 1)] for j in range(polynomial_order + 1)]
    return solve(A, B)

piecewise_polynomial_coefficients_in_half_interval(f, n_intervals, polynomial_order)

Computes the coefficients of a piecewise polynomial approximation to a function using dyadic intervals in .

Parameters:

Name Type Description Default
f Callable

.

required
n_intervals int

The number of intervals.

required
polynomial_order int

The polynomial order.

required

Returns:

Type Description
Array

The polynomial coefficient tables.

Source code in src/pyarv/_approximation_utils/approximating_polynomials.py
def piecewise_polynomial_coefficients_in_half_interval(f: Callable, 
                                                       n_intervals: int, 
                                                       polynomial_order: int) -> Array:
    """
    Computes the coefficients of a piecewise polynomial approximation to a function \( f \)
    using dyadic intervals in \( [0, 1/2] \).

    Parameters
    ----------
    f
        \( f \). 
    n_intervals
        The number of intervals. 
    polynomial_order
        The polynomial order. 

    Returns
    -------
    Array
        The polynomial coefficient tables. 
    """
    intervals = dyadic_intervals_in_half_interval(n_intervals)
    coefficients = zeros((polynomial_order + 1, n_intervals))
    for i in range(n_intervals):
        a, b = intervals[i]
        coefficients[:, i] = optimal_polynomial_coefficients(f=f, polynomial_order=polynomial_order, lower_limit=a, upper_limit=b) if a != b else f(b)
    return coefficients