API
MonotoneSplines.Spl
— TypeA Spl
object.
Fields
H
: anRObject
generated bysplines::bs()
β
: the coefficients for the B-spline.
MonotoneSplines.aug
— Methodaug(λ::AbstractFloat)
Augment λ
with 8 different functions.
MonotoneSplines.build_model
— Methodbuild_model(x::AbstractVector{T}; <keyword arguments>)
Construct design matrix and other internal variables for smoothing spline.
Arguments
all_knots = false
: whether to use all knots. Iffalse
, use the same rule as in R'ssmooth.spline
.prop_nknots = 1.0
: a proportion for using fewer knots. Suppose the number of knots isnknots
, then the final number of knots isprop_nknots
*nknots
. Currently, it is only effective whenall_knots = false
.ε = 6.06e-6
: a small number added to the diagonal of matrix Ω to ensure it is positive definite.
Returns
- B-spline design matrix
B
atx
for cubic splines L
: cholesky decomposition of Ω = LL'J
: number of basis functions, which does not change for cubic splines, so it is only intended for smoothing splines
the above four are shared with the method for cubic splines, but for smoothing splines, it also returns
mx, rx, idx, idx0
: only for smoothing splines
MonotoneSplines.build_model
— Methodbuild_model(x::AbstractVector{T}, J::Int; <keyword arguments>)
Construct design matrix and other internal variables for cubic spline with J
basis functions.
Returns
B
: B-spline design matrixB
atx
for cubic splinesrB
: raw RObject ofB
MonotoneSplines.check_CI
— Methodcheck_CI(; <keyword arguments>)
Conduct repeated experiments to check the overlap of confidence bands (default, check_acc = false
) or accuracy of fitting curves (check_acc = true
) between MLP generator and OPT solution.
Arguments
n = 100
: sample sizeσ = 0.1
: noise levelf::Function = exp
: the truth curveseed = 1234
: random seed for the simulated datacheck_acc = false
: check overlap of confidence bands (default: false) or accuracy of fitting curves (true)nepoch0 = 5
: number of epoch in the first step to fit the curvenepoch = 50
: number of epoch in the second step to obtain the confidence bandniter_per_epoch = 100
: number of iterations in each epochη0 = 1e-4
: learning rate in step 1η = 1e-4
: learning rate in step 2 (NOTE: lr did not make much difference, unify these two)K0 = 32
: Monte Carlo size for averagingλ
in step 2K = 32
: Monte Carlo size for averagingλ
in step 1 and for averagingy
in step 2. (NOTE: unify these two Monte Carlo size)nB = 2000
: number of bootstrap replicationsnrep = 5
: number of repeated experimentsfig = true
: whether to plotfigfolder = ~
: folder for saving the figures iffig = true
λs = exp.(range(-8, -2, length = 10))
: region of continuousλ
nhidden = 1000
: number of hidden layersdepth = 2
: depth of MLPdemo = false
: whether to save internal results for demo purposemodel_file = nothing
: if not nothing, load the model from the file.gpu_id = 0
: specify the id of GPU, -1 for CPU.prop_nknots = 0.2
: proportion of number of knots in B-spline basis.backend = "flux"
: train MLP generator with Flux or PyTorch
MonotoneSplines.ci_mono_ss_mlp
— Methodci_mono_ss_mlp(x::AbstractVector{T}, y::AbstractVector{T}, λs::AbstractVector{T}; )
Fit data x, y
at each λs
with confidence bands.
Arguments
prop_nknots = 0.2
: proportion of number of knotsbackend = "flux"
:flux
orpytorch
model_file
: path for saving trained modelnepoch0 = 3
: number of epoch in training step 1nepoch = 3
: number of epoch in training step 2niter_per_epoch = 100
: number of iterations in each epochM = 10
: Monte Carlo sizenhidden = 100
: number of hidden unitsdisable_progressbar = false
: set true if generating documentationdevice = :cpu
: train using:cpu
or:gpu
sort_in_nn = true
: (only for backend = "flux") whether putsort
inMLP
eval_in_batch = false
: (only for backend = "flux") Currently,Flux
does not supportsort
in batch mode. A workaround with customizedZygote.batch_sort
needs further verifications.
MonotoneSplines.conf_band_width
— Methodconf_band_width(CIs::AbstractMatrix)
Calculate width of confidence bands.
MonotoneSplines.coverage_prob
— Methodcoverage_prob(CIs::AbstractMatrix, y0::AbstractVector)
Calculate coverage probability given n x 2
CI matrix CIs
and true vector y0
of size n
.
MonotoneSplines.cv_mono_ss
— Methodcv_mono_ss(x::AbstractVector{T}, y::AbstractVector{T}, λs::AbstractVector{T})
Cross-validation for monotone fitting with smoothing spline on y ~ x
among parameters λs
.
MonotoneSplines.div_into_folds
— Methoddiv_into_folds(N::Int; K = 10, seed = 1234)
Equally divide 1:N
into K
folds with random seed seed
. If seed
is negative, it is a non-random division, where the i
-th fold would be the i
-th equidistant range.
MonotoneSplines.eval_penalty
— Methodeval_penalty(model::Spl{T}, x::AbstractVector{T})
Evaluate the penalty matrix by R's fda::eval.penalty
. To make sure the corresponding design matrix contructed by fda::eval.basis
is the same as model.H
, it asserts the norm difference should be smaller than sqrt(eps())
.
MonotoneSplines.fit
— Functionfit(X, y, paras, method)
paras
is either the number of basis functions, or the sequence of interior knots. Return a Spl
object.
n = 100
x = rand(n) * 2 .- 1
y = x .^3 + randn(n) * 0.01
res = fit(x, y, 10, "monotone")
MonotoneSplines.gen_data
— Methodgen_data(n, σ, f::Union{Function, String}; xmin = -1, xmax = 1, k = 10)
Generate n
data points (xi, yi)
from curve f
with noise level σ
, i.e., yi = f(xi) + N(0, σ^2)
.
It returns four vectors, x, y, x0, y0
, where
x, y
: pair points of lengthn
.x0, y0
: true curve without noise, represented byk*n
points.
MonotoneSplines.jaccard_index
— Methodjaccard_index(a::AbstractVector, b::AbstractVector)
Calculate Jaccard Index for two confidence intervals a
and b
jaccard_index(a::AbstractMatrix, b::AbstractMatrix)
Calculate Jaccard Index for two confidence intervals a[i, :]
and b[i, :]
MonotoneSplines.load_model
— Methodload_model(n::Int, J::Int, nhidden::Int, model_file::String; dim_lam = 8, gpu_id = 3)
Load trained model from model_file
.
MonotoneSplines.mono_cs
— Functionmono_cs(x::AbstractVector, y::AbstractVector, J::Int = 4; increasing::Bool = true)
Monotone splines with cubic splines.
MonotoneSplines.mono_ss
— Functionmono_ss(x::AbstractVector, y::AbstractVector, λ = 1.0; prop_nknots = 1.0)
Monotone splines with smoothing splines, return a MonotoneSS
object.
MonotoneSplines.mono_ss
— Functionmono_ss(B::AbstractMatrix, y::AbstractVector, L::AbstractMatrix, J::Int, λ::AbstractFloat)
Monotone Fitting with Smoothing Splines given design matrix B
and cholesky-decomposed matrix L
.
Returns
βhat
: estimated coefficientyhat
: fitted values- (optional)
B
andL
MonotoneSplines.mono_ss_mlp
— Methodmono_ss_mlp(x::AbstractVector, y::AbstractVector; λl, λu)
Fit monotone smoothing spline by training a MLP generator.
Arguments
prop_nknots = 0.2
: proportion of number of knotsbackend = flux
: useflux
orpytorch
device = :cpu
: use:cpu
or:gpu
nhidden = 100
: number of hidden unitsdisable_progressbar = false
: disable progressbar (useful in Documenter.jl)
MonotoneSplines.py_train_G_lambda
— Methodpy_train_G_lambda(y::AbstractVector, B::AbstractMatrix, L::AbstractMatrix; <keyword arguments>)
Wrapper for training MLP generator using PyTorch.
Arguments
η0
,η
: learning rateK0
,K
: Monte Carlo sizenepoch0
,nepoch
: number of epochnhidden
,depth
: size of MLPλl
,λu
: range ofλ
use_torchsort = false
:torch.sort
(default: false) ortorchsort.soft_sort
(true)sort_reg_strength = 0.1
: tuning parameter whenuse_torchsort = true
.model_file
: path for saving trained modelgpu_id = 0
: use specified GPUniter_per_epoch = 100
: number of iterations in each epochdisable_tqdm = false
: settrue
when generating documentationλs_opt_train = nothing
: if notnothing
and instead a vector ofλ
(dim:N_t
) used in the classical ML training step, then return the beta loss of GpBS procedureλs_opt_val = nothing
: if notnothing
and instead a vector ofλ
(dim:N_v
) used in the classical ML validation step (integrative predictive loss), then return the beta loss of GpBS procedureβs_opt_train = nothing
: if notnothing
and instead a matrix ofβ
(dim:N_t x J
) used in the classical ML training step, then return the beta loss of GpBS procedureβs_opt_val = nothing
: if notnothing
and instead a matrix ofβ
(dim:N_v x J
) used in the classical ML validation step, then return the beta loss of GpBS procedure
MonotoneSplines.smooth_spline
— Methodsmooth_spline(x::AbstractVector, y::AbstractVector, xnew::AbstractVector)
Perform smoothing spline on (x, y)
, and make predictions on xnew
.
Returns: yhat
, ynewhat
,....
MonotoneSplines.train_Gyλ
— Methodtrain_Gyλ(rawy::AbstractVector, rawB::AbstractMatrix, rawL::AbstractMatrix, model_file::String)
Train MLP generator G(y, λ) for λ ∈ [λl, λu] and y ~ N(f, σ²)
MonotoneSplines.train_Gλ
— Methodtrain_Gλ(rawy::AbstractVector, rawB::AbstractMatrix, rawL::AbstractMatrix; λl, λu)
Train MLP generator G(λ) for λ ∈ [λl, λu].
RCall.rcopy
— Methodrcopy(s::Spl)
Convert RObject s.H
as a Julia matrix, and s.β
keeps the same.
StatsAPI.predict
— Methodpredict(model::Spl{T}, xs::AbstractVector{T})
predict(X::Vector{Float64}, y::Vector{Float64}, J::Int, Xnew::AbstractVector{Float64}, ynew::AbstractVector{Float64}
predict(X::Vector{Float64}, y::Vector{Float64}, J::Int, Xnew::Vector{Float64}, ynew::Vector{Float64}, σ::Vector{Float64}
Make prediction based on fitted Spl
on new points xs
. If Xnew
is provided, then also returns the prediction error ‖yhat - ynew‖_2^2
.