Getting Started with ApproxOp: Approximation, Moments, and Error Analysis

Introduction

ApproxOp offers a unified computational interface for positive linear approximation operators. This vignette demonstrates a reproducible workflow: construct operators, approximate functions, compute and check moments, quantify numerical errors, compare operator families, investigate parameters, and register a custom operator.

The examples use version 0.1.0 of the package. The operator families shown here use the "discrete" variant. Numerical results on finite grids are diagnostics, not general approximation theorems.

library(ApproxOp)
f <- function(x) x^2
grid <- seq(0, 1, length.out = 101)

Constructing and evaluating Bernstein operators

For \(x\in[0,1]\), the Bernstein operator is \[ B_n(f;x)=\sum_{k=0}^{n}{n\choose k}x^k(1-x)^{n-k}f(k/n). \]

B20 <- approx_operator(family = "bernstein", n = 20)
print(B20)
#> <ApproxOp approximation operator>
#>   Family:     Bernstein
#>   Variant:    discrete
#>   n:          20
#>   Domain:     [0, 1]
x <- c(0.25, 0.50, 0.75)
data.frame(x = x, exact = f(x),
           approximated = approximate(B20, f, x))
#>      x  exact approximated
#> 1 0.25 0.0625     0.071875
#> 2 0.50 0.2500     0.262500
#> 3 0.75 0.5625     0.571875

For the quadratic test function, \[ B_n(t^2;x)=x^2+\frac{x(1-x)}{n}. \] This provides an independent analytic check.

observed <- as.numeric(approximate(B20, f, x))
expected <- x^2 + x * (1-x)/20
stopifnot(isTRUE(all.equal(observed, expected, tolerance = 1e-8)))

Plotting approximation and error

plot(B20, f = f, grid = grid)
Bernstein approximation of the quadratic function.

Bernstein approximation of the quadratic function.

plot(B20, f = f, grid = grid, type = "error")
Pointwise Bernstein approximation error.

Pointwise Bernstein approximation error.

Moments and numerical verification

The first ordinary moments are \(B_n(1;x)=1\), \(B_n(t;x)=x\), and \(B_n(t^2;x)=x^2+x(1-x)/n\). In particular, \(\mu_{n,1}(x)=0\) and \(\mu_{n,2}(x)=x(1-x)/n\).

ordinary <- moments(B20, order = 2, x = x)
central <- moments(B20, order = 2, x = x, central = TRUE)
print(ordinary)
#>      m0   m1       m2
#> [1,]  1 0.25 0.071875
#> [2,]  1 0.50 0.262500
#> [3,]  1 0.75 0.571875
print(central)
#>      mu0           mu1      mu2
#> [1,]   1 -2.775558e-17 0.009375
#> [2,]   1  0.000000e+00 0.012500
#> [3,]   1  0.000000e+00 0.009375
stopifnot(isTRUE(all.equal(as.numeric(ordinary[, 3]),
                           x^2 + x*(1-x)/20, tolerance = 1e-8)))
stopifnot(isTRUE(all.equal(as.numeric(central[, 3]),
                           x*(1-x)/20, tolerance = 1e-8)))
verify_moments(B20, order = 2, x = 0.5)
#>     x order computed numerical abs_error rel_error status
#> 1 0.5     0   1.0000    1.0000         0         0   TRUE
#> 2 0.5     1   0.5000    0.5000         0         0   TRUE
#> 3 0.5     2   0.2625    0.2625         0         0   TRUE

verify_moments() compares numerical calculation paths. Agreement alone is not an independent mathematical proof; the explicit Bernstein formulas above serve as separate benchmarks.

Error analysis and convergence

The absolute error at a grid point is \(|L_n(f;x)-f(x)|\). approx_error() reports pointwise diagnostics.

pointwise <- approx_error(B20, f, grid)
head(pointwise)
#>      x  exact approximation signed_error absolute_error relative_error
#> 1 0.00 0.0000      0.000000     0.000000       0.000000             NA
#> 2 0.01 0.0001      0.000595     0.000495       0.000495       4.950000
#> 3 0.02 0.0004      0.001380     0.000980       0.000980       2.450000
#> 4 0.03 0.0009      0.002355     0.001455       0.001455       1.616667
#> 5 0.04 0.0016      0.003520     0.001920       0.001920       1.200000
#> 6 0.05 0.0025      0.004875     0.002375       0.002375       0.950000

For \(f(x)=x^2\), the exact uniform error of Bernstein approximation is \(1/(4n)\). Our grid includes \(x=1/2\), where the maximum occurs.

computed_max <- max(abs(as.numeric(approximate(B20, f, grid))-f(grid)))
stopifnot(isTRUE(all.equal(computed_max, 1/(4*20), tolerance = 1e-8)))
data.frame(computed_max, analytic_max = 1/(4*20))
#>   computed_max analytic_max
#> 1       0.0125       0.0125
degrees <- c(5, 10, 20, 40, 80)
conv <- convergence(B20, f = f, grid = grid, n = degrees)
print(conv)
#>    n max_absolute_error       mae        rmse
#> 1  5           0.050000 0.0330000 0.036333621
#> 2 10           0.025000 0.0165000 0.018166810
#> 3 20           0.012500 0.0082500 0.009083405
#> 4 40           0.006250 0.0041250 0.004541703
#> 5 80           0.003125 0.0020625 0.002270851
data.frame(n = degrees, analytic_max_error = 1/(4*degrees))
#>    n analytic_max_error
#> 1  5           0.050000
#> 2 10           0.025000
#> 3 20           0.012500
#> 4 40           0.006250
#> 5 80           0.003125
plot(conv)
Numerical convergence diagnostics for Bernstein operators.

Numerical convergence diagnostics for Bernstein operators.

Comparing classical operator families

Bernstein operators are defined on \([0,1]\), whereas Szász–Mirakyan and Baskakov operators act on \([0,\infty)\). Comparisons therefore use a common grid inside \([0,1]\), without claiming uniform convergence on the entire unbounded domain.

S20 <- approx_operator(family = "szasz", n = 20)
K20 <- approx_operator(family = "baskakov", n = 20)
comparison <- compare_operators(
  operators = list(Bernstein = B20, Szasz = S20, Baskakov = K20),
  f = f, grid = grid, n = c(10, 20, 40)
)
print(comparison)
#>    operator    family subfamily  variant  n max_absolute_error      mae
#> 1 Bernstein bernstein      <NA> discrete 10            0.02500 0.016500
#> 2 Bernstein bernstein      <NA> discrete 20            0.01250 0.008250
#> 3 Bernstein bernstein      <NA> discrete 40            0.00625 0.004125
#> 4     Szasz     szasz      <NA> discrete 10            0.10000 0.050000
#> 5     Szasz     szasz      <NA> discrete 20            0.05000 0.025000
#> 6     Szasz     szasz      <NA> discrete 40            0.02500 0.012500
#> 7  Baskakov  baskakov      <NA> discrete 10            0.20000 0.083500
#> 8  Baskakov  baskakov      <NA> discrete 20            0.10000 0.041750
#> 9  Baskakov  baskakov      <NA> discrete 40            0.05000 0.020875
#>          rmse
#> 1 0.018166810
#> 2 0.009083405
#> 3 0.004541703
#> 4 0.057879185
#> 5 0.028939592
#> 6 0.014469796
#> 7 0.102127533
#> 8 0.051063767
#> 9 0.025531883
plot(comparison)
Comparison of approximation errors on the common grid.

Comparison of approximation errors on the common grid.

Sheffer-based operators and parameter studies

The package also provides Sheffer-based subfamilies, including Appell, Charlier, and Meixner. Each family has its own parameter restrictions; consult the help pages before changing parameters.

The following Charlier example examines the effect of the parameter a on the same finite evaluation grid.

C20 <- approx_operator(
  family = "sheffer", subfamily = "charlier",
  n = 20, params = list(a = 2)
)
data.frame(x = x, Charlier = approximate(C20, f, x))
#>      x Charlier
#> 1 0.25   0.1175
#> 2 0.50   0.3550
#> 3 0.75   0.7175
scan <- parameter_scan(
  operator = C20, parameter = "a",
  values = c(1.5, 2, 3, 5),
  f = f, grid = grid
)
print(scan)
#>   parameter value  n max_absolute_error     mae       rmse
#> 1         a   1.5 20             0.2550 0.13000 0.14903859
#> 2         a   2.0 20             0.2050 0.10500 0.12010412
#> 3         a   3.0 20             0.1800 0.09250 0.10563794
#> 4         a   5.0 20             0.1675 0.08625 0.09840525
plot(scan)
Charlier parameter scan.

Charlier parameter scan.

For Meixner-based operators, admissible settings include \(0\leq\alpha\leq\beta\), \(\theta>0\), and \(1/2<c<1\). These constraints are essential when designing parameter experiments.

Registering a user-defined operator

To illustrate extensibility, consider \[ Q_n(f;x)=\frac{n}{n+1}f(x)+\frac{1}{n+1}f(1/2), \qquad x\in[0,1]. \] This is a positive linear operator reproducing constants. Moreover, \(\|Q_nf-f\|_\infty\leq 2\|f\|_\infty/(n+1)\).

register_operator(
  family = "example_average",
  domain = c(0, 1),
  evaluation_engine = function(f, x, operator) {
    n <- operator$n
    n/(n+1)*vapply(x, f, numeric(1)) +
      1/(n+1)*f(0.5)
  }
)
Q20 <- approx_operator(family = "example_average", n = 20)
data.frame(x = x, computed = approximate(Q20, f, x),
           exact_formula = 20/21*x^2 + 1/21*0.25)
#>      x   computed exact_formula
#> 1 0.25 0.07142857    0.07142857
#> 2 0.50 0.25000000    0.25000000
#> 3 0.75 0.54761905    0.54761905
q_obs <- as.numeric(approximate(Q20, f, x))
q_exact <- 20/21*x^2 + 0.25/21
stopifnot(isTRUE(all.equal(q_obs, q_exact, tolerance = 1e-8)))

The custom operator illustrates how the framework can be extended without modifying its built-in operator families.

Reproducibility and further reading

All examples above use deterministic functions and explicitly specified evaluation grids. On machines with different numerical settings, minor floating-point differences may occur. For more functions, consult help(package = "ApproxOp") and the individual help pages.

packageVersion("ApproxOp")
#> [1] '0.1.0'
sessionInfo()
#> R version 4.6.2 (2026-10-29)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 26.04.1 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
#> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.32.so;  LAPACK version 3.12.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
#>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
#> 
#> time zone: Etc/UTC
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] ApproxOp_0.1.0 rmarkdown_2.32
#> 
#> loaded via a namespace (and not attached):
#>  [1] digest_0.6.39    R6_2.6.1         fastmap_1.2.0    xfun_0.62       
#>  [5] maketools_1.3.2  cachem_1.1.0     knitr_1.52       htmltools_0.5.9 
#>  [9] buildtools_1.0.0 lifecycle_1.0.5  cli_3.6.6        sass_0.4.10     
#> [13] jquerylib_0.1.4  compiler_4.6.2   sys_3.4.3        tools_4.6.2     
#> [17] evaluate_1.0.5   bslib_0.12.0     yaml_2.3.12      otel_0.3.0      
#> [21] jsonlite_2.0.0   rlang_1.3.0

Development repository: https://github.com/edmond-aliaga/ApproxOp.

Package on CRAN: https://CRAN.R-project.org/package=ApproxOp.