Core API¶
The core module contains the main PSpline
class that implements penalized B-spline smoothing.
PSpline Class¶
Univariate penalised B-spline smoother.
fit ¶
fit(
*, xl: float | None = None, xr: float | None = None
) -> PSpline
Fit the P-spline model.
Parameters:
-
xl
(float
, default:None
) –Left boundary of the domain. Defaults to min(x).
-
xr
(float
, default:None
) –Right boundary of the domain. Defaults to max(x).
Returns:
-
PSpline
–The fitted spline object.
Source code in src/psplines/core.py
predict ¶
predict(
x_new: ArrayLike,
*,
derivative_order: int | None = None,
return_se: bool = False,
se_method: str = "analytic",
B_boot: int = 5000,
seed: int | None = None,
n_jobs: int = 1,
hdi_prob: float = 0.95,
) -> NDArray | tuple[NDArray, ...]
Predict smooth (or derivative) with optional uncertainty.
Parameters:
-
x_new
(array - like
) –Points at which to evaluate the spline.
-
derivative_order
(int
, default:None
) –Order of derivative to compute. None for function values.
-
return_se
(bool
, default:False
) –Whether to return standard errors.
-
se_method
(str
, default:"analytic"
) –Method for computing uncertainty: - 'analytic': delta-method SE -> (fhat, se) - 'bootstrap': parametric bootstrap SEs -> (fhat, se) - 'bayes': posterior mean + HDI credible band -> (mean, lower, upper)
-
B_boot
(int
, default:5000
) –Number of bootstrap replicates (for bootstrap method).
-
seed
(int
, default:None
) –Random seed (for bootstrap method).
-
n_jobs
(int
, default:1
) –Number of parallel jobs (for bootstrap method).
-
hdi_prob
(float
, default:0.95
) –Probability for HDI credible band (for Bayesian method).
Returns:
-
NDArray or tuple
–Predictions, optionally with uncertainty estimates.
Source code in src/psplines/core.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
|
derivative ¶
derivative(
x_new: ArrayLike,
*,
deriv_order: int = 1,
return_se: bool = False,
se_method: str = "analytic",
**kwargs: Any,
) -> NDArray | tuple[NDArray, ...]
Compute k-th derivative of the fitted spline.
Parameters:
-
x_new
(array - like
) –Points at which to evaluate the derivative.
-
deriv_order
(int
, default:1
) –Order of derivative to compute.
-
return_se
(bool
, default:False
) –Whether to return standard errors.
-
se_method
(str
, default:"analytic"
) –Method for computing standard errors ("analytic" or "bootstrap").
-
**kwargs
(Any
, default:{}
) –Additional arguments passed to bootstrap method if applicable.
Returns:
-
NDArray or tuple
–Derivative values, optionally with standard errors.
Source code in src/psplines/core.py
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
|
bayes_fit ¶
bayes_fit(
a: float = 2.0,
b: float = 0.1,
c: float = 2.0,
d: float = 1.0,
draws: int = 2000,
tune: int = 2000,
chains: int = 4,
cores: int = 4,
target_accept: float = 0.9,
random_seed: int | None = None,
) -> Any
Source code in src/psplines/core.py
plot_lam_trace ¶
plot_alpha_trace ¶
Plot trace and marginal posterior for alpha coefficients.
plot_posterior ¶
Plot posterior
Usage Examples¶
Basic Usage¶
import numpy as np
from psplines import PSpline
# Generate data
x = np.linspace(0, 1, 50)
y = np.sin(2 * np.pi * x) + 0.1 * np.random.randn(50)
# Create and fit spline
spline = PSpline(x, y, nseg=20, lambda_=1.0)
spline.fit()
# Make predictions
x_new = np.linspace(0, 1, 100)
y_pred = spline.predict(x_new)
With Uncertainty Quantification¶
# Analytical standard errors
y_pred, se = spline.predict(x_new, return_se=True)
# Bootstrap confidence intervals
y_pred, se_boot = spline.predict(x_new, return_se=True,
se_method="bootstrap", B_boot=1000)
Derivative Computation¶
# First derivative
dy_dx = spline.derivative(x_new, deriv_order=1)
# Second derivative with uncertainty
d2y_dx2, se = spline.derivative(x_new, deriv_order=2, return_se=True)
Bayesian Inference¶
# Fit Bayesian model
trace = spline.bayes_fit(draws=2000, tune=1000)
# Get posterior credible intervals
mean, lower, upper = spline.predict(x_new, se_method="bayes")
Model Parameters¶
Basis Function Parameters¶
-
nseg
: Number of B-spline segments- Controls the flexibility of the basis
- More segments = more flexible fit
- Typical values: 10-50
-
degree
: Degree of B-spline basis functions- Controls local smoothness
- Higher degree = smoother derivatives
- Typical values: 1-4 (3 is most common)
Penalty Parameters¶
-
lambda_
: Smoothing parameter- Controls the trade-off between fit and smoothness
- Higher values = smoother fits
- Can be selected automatically
-
penalty_order
: Order of the difference penalty- 1: Penalizes first differences (rough penalty on slopes)
- 2: Penalizes second differences (rough penalty on curvature)
- 3: Penalizes third differences (rough penalty on jerk)
Constraint Parameters¶
constraints
: Dictionary specifying boundary constraints"deriv"
: Derivative constraints at boundaries- Example:
{"deriv": {"order": 1, "initial": 0, "final": 0}}
Model Attributes¶
After fitting, the following attributes are available:
coef
: B-spline coefficientsfitted_values
: Fitted values at input pointsknots
: Knot vector used for B-splinesED
: Effective degrees of freedomsigma2
: Residual variance estimatese_coef
: Standard errors of coefficientsse_fitted
: Standard errors of fitted values
Error Handling¶
The PSpline
class includes comprehensive input validation:
- Array validation: Checks for proper dimensions, finite values
- Parameter validation: Ensures valid ranges and types
- State validation: Verifies model is fitted before prediction
- Constraint validation: Validates constraint specifications
All errors provide descriptive messages with suggested solutions.