Skip to main content

PcaProjection

Struct PcaProjection 

Source
pub struct PcaProjection { /* private fields */ }
Expand description

Corpus-fitted projection via spherical PCA.

Finds the 3 principal directions of maximum angular variance in the embedding space, then projects new embeddings onto them. This preserves angular (cosine similarity) relationships as faithfully as possible in 3 dimensions.

Fitting: O(N·n·k·iters) where N=corpus size, n=dimension, k=3. Projection: O(n) per embedding.

Implementations§

Source§

impl PcaProjection

Source

pub fn fit( embeddings: &[Embedding], radial: RadialStrategy, ) -> Result<Self, ProjectionError>

Fit the top-3 principal components on embeddings.

Returns ProjectionError::EmptyCorpus if the slice is empty, ProjectionError::DimensionTooLow if dim < 3, and ProjectionError::InconsistentDimension if any row’s dimensionality disagrees with the first. Previously these paths panicked via assert!, which surfaced as a PanicException in Python / WASM bindings.

Source

pub fn fit_default(embeddings: &[Embedding]) -> Result<Self, ProjectionError>

Source

pub fn fit_weighted( embeddings: &[Embedding], weights: &[f64], radial: RadialStrategy, ) -> Result<Self, ProjectionError>

Fit the top-3 principal components with per-sample weights.

Weighted PCA finds the top eigenvectors of the weighted covariance matrix Σ wᵢ (xᵢ − μ_w)(xᵢ − μ_w)ᵀ / Σ wᵢ, where μ_w = Σ wᵢ xᵢ / Σ wᵢ. With uniform weights this collapses to the same answer as Self::fit.

The intended use is rebalancing covariance estimates over imbalanced corpora. Setting wᵢ = 1 / sqrt(|category(i)|) gives a category of size m total covariance mass m · (1/√m) = √m, compressing category influence from linear to square-root in its size. For exactly equal per-category mass use wᵢ = 1 / |category(i)|; the square-root compromise keeps large categories’ internal variance structure from being washed out entirely while still letting small categories register.

Returns the same error variants as Self::fit, plus ProjectionError::SliceLengthMismatch when weights.len() != embeddings.len(). Negative weights are treated as zero.

Source

pub fn with_volumetric(self, enabled: bool) -> Self

Enable volumetric mode: r comes from the PCA projection magnitude instead of the embedding magnitude. Points distribute through the full 3D volume rather than clustering on the sphere surface.

Source

pub fn explained_variance_ratio(&self) -> f64

The fraction of total variance captured by the top-3 PCA components. A global quality metric for the projection — higher means less information lost.

Trait Implementations§

Source§

impl Clone for PcaProjection

Source§

fn clone(&self) -> PcaProjection

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl From<PcaProjection> for ConfiguredProjection

Source§

fn from(p: PcaProjection) -> Self

Converts to this type from the input type.
Source§

impl Projection for PcaProjection

Source§

fn project(&self, embedding: &Embedding) -> SphericalPoint

Source§

fn project_rich(&self, embedding: &Embedding) -> ProjectedPoint

Project with rich metadata: certainty, intensity, projection magnitude.
Source§

fn dimensionality(&self) -> usize

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> 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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more