LocalPolynomialRegression

Struct LocalPolynomialRegression 

Source
pub struct LocalPolynomialRegression<F>
where F: Float + FromPrimitive + Debug,
{ /* private fields */ }
Expand description

Local Polynomial Regression model

This model fits a polynomial of specified degree locally around each prediction point using weighted least squares. The weights depend on the distance between the prediction point and the data points.

The implementation includes:

  • Multiple weight function options
  • Polynomial basis options (constant, linear, quadratic)
  • Standard error estimates
  • Optional confidence intervals
  • Local R² and effective degrees of freedom
  • Support for robust standard errors

§Examples

use scirs2_core::ndarray::{Array1, Array2, Axis};
use scirs2_interpolate::local::polynomial::{
    LocalPolynomialRegression, LocalPolynomialConfig
};
use scirs2_interpolate::local::mls::{WeightFunction, PolynomialBasis};

// Create some 1D data with noise
let x = Array1::<f64>::linspace(0.0, 10.0, 50);
let mut y = Array1::<f64>::zeros(50);
for (i, x_val) in x.iter().enumerate() {
    // y = sin(x) + some noise
    y[i] = x_val.sin() + 0.1 * 0.3;
}

// Create a 2D array of points
let points = x.clone().insert_axis(Axis(1));

// Configure and create the local polynomial regression model
let config = LocalPolynomialConfig::<f64> {
    bandwidth: 1.0,
    weight_fn: WeightFunction::Gaussian,
    basis: PolynomialBasis::Quadratic,
    confidence_level: Some(0.95),
    ..LocalPolynomialConfig::default()
};

let loess = LocalPolynomialRegression::with_config(
    points,
    y,
    config
).unwrap();

// Predict at a new point
let query = Array1::from_vec(vec![5.0]);
let result = loess.fit_at_point(&query.view()).unwrap();

// Access the fitted value
println!("Fitted value: {}", result.value);

// Access confidence interval
if let Some((lower, upper)) = result.confidence_interval {
    println!("95% CI: ({}, {})", lower, upper);
}

Implementations§

Source§

impl<F> LocalPolynomialRegression<F>
where F: Float + FromPrimitive + Debug,

Source

pub fn response_sd(&self) -> F

Get the precomputed standard deviation of the response

Source§

impl<F> LocalPolynomialRegression<F>
where F: Float + FromPrimitive + Debug + 'static,

Source

pub fn new( points: Array2<F>, values: Array1<F>, bandwidth: F, ) -> InterpolateResult<Self>

Create a new LocalPolynomialRegression with default configuration

§Arguments
  • points - Point coordinates with shape (n_points, n_dims)
  • values - Values at each point with shape (n_points,)
  • bandwidth - Bandwidth parameter controlling locality (larger = smoother)
§Returns

A new LocalPolynomialRegression model

Source

pub fn with_config( points: Array2<F>, values: Array1<F>, config: LocalPolynomialConfig<F>, ) -> InterpolateResult<Self>

Create a new LocalPolynomialRegression with custom configuration

§Arguments
  • points - Point coordinates with shape (n_points, n_dims)
  • values - Values at each point with shape (n_points,)
  • config - Configuration for the regression
§Returns

A new LocalPolynomialRegression model

Source

pub fn fit_at_point( &self, x: &ArrayView1<'_, F>, ) -> InterpolateResult<RegressionResult<F>>

Fit the local polynomial regression at a given point

§Arguments
  • x - Query point with shape (n_dims,)
§Returns

Regression result at the query point

Source

pub fn fit_multiple( &self, points: &ArrayView2<'_, F>, ) -> InterpolateResult<Array1<F>>

Fit the local polynomial regression at multiple points

§Arguments
  • points - Query points with shape (n_points, n_dims)
§Returns

Array of fitted values at the query points

Source

pub fn config(&self) -> &LocalPolynomialConfig<F>

Get the configuration used by this local polynomial regression

Source

pub fn points(&self) -> &Array2<F>

Get the points used by this local polynomial regression

Source

pub fn values(&self) -> &Array1<F>

Get the values used by this local polynomial regression

Source

pub fn select_bandwidth(&self, bandwidths: &[F]) -> InterpolateResult<F>

Compute the optimal bandwidth using leave-one-out cross-validation

§Arguments
  • bandwidths - Array of bandwidths to evaluate
§Returns

The bandwidth that minimizes prediction error in cross-validation

Trait Implementations§

Source§

impl<F> Clone for LocalPolynomialRegression<F>
where F: Float + FromPrimitive + Debug + Clone,

Source§

fn clone(&self) -> LocalPolynomialRegression<F>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<F> Debug for LocalPolynomialRegression<F>
where F: Float + FromPrimitive + Debug + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V