Representation#

Functional Data#

class FDApy.representation.functional_data.FunctionalData(argvals: Type[Argvals], values: Type[Values])#

Bases: ABC

Metaclass for the definition of diverse functional data objects.

Parameters:
argvals: Type[Argvals]

Sampling points of the functional data.

values: Type[Values]

Values of the functional data.

Attributes:
argvals

Getter for argvals.

argvals_stand

Getter for argvals_stand.

n_dimension

Get the number of input dimension of the functional data.

n_obs

Get the number of observations of the functional data.

n_points

Get the number of sampling points.

values

Getter for values.

Methods

center([mean, smooth])

Center the data.

concatenate(*fdata)

Concatenate FunctionalData objects.

covariance([points, smooth])

Compute an estimate of the covariance.

inner_product([method, smooth, noise_variance])

Compute an estimate of the inner product matrix.

mean([points, smooth])

Compute an estimate of the mean.

noise_variance([order])

Estimate the variance of the noise.

norm([squared, method, use_argvals_stand])

Norm of each observation of the data.

normalize([weights, method, use_argvals_stand])

Normalize the data.

smooth([points, kernel_name, bandwidth, degree])

Smooth the data.

to_long()

Convert the data to long format.

abstract static concatenate(*fdata: Type[FunctionalData]) Type[FunctionalData]#

Concatenate FunctionalData objects.

Parameters:
*fdata: FunctionalData

Functional data to concatenate.

Raises:
ValueError

When all fdata do not have the same dimension.

TypeError

When all fdata do not have the same type.

abstract property argvals: Type[Argvals]#

Getter for argvals.

property argvals_stand: Type[Argvals]#

Getter for argvals_stand.

abstract property values: Type[Values]#

Getter for values.

property n_obs: int#

Get the number of observations of the functional data.

Returns:
int

Number of observations within the functional data.

property n_dimension: int#

Get the number of input dimension of the functional data.

Returns:
int

Number of input dimension with the functional data.

property n_points: Tuple[int, ...] | Dict[int, Tuple[int, ...]]#

Get the number of sampling points.

Returns:
Union[Tuple[int, …], Dict[int, Tuple[int, …]]]

Number of sampling points.

abstract to_long() DataFrame#

Convert the data to long format.

abstract noise_variance(order: int = 2) float#

Estimate the variance of the noise.

abstract smooth(points: DenseArgvals | None = None, kernel_name: str | None = 'epanechnikov', bandwidth: float | None = None, degree: int | None = 1) Type[FunctionalData]#

Smooth the data.

abstract mean(points: DenseArgvals | None = None, smooth: bool = True, **kwargs) DenseFunctionalData#

Compute an estimate of the mean.

abstract center(mean: FunctionalData | None = None, smooth: bool = True, **kwargs) FunctionalData#

Center the data.

abstract norm(squared: bool = False, method: str = 'trapz', use_argvals_stand: bool = False) ndarray[Any, dtype[float64]]#

Norm of each observation of the data.

abstract normalize(weights: float = 0.0, method: str = 'trapz', use_argvals_stand: bool = False, **kwargs) Tuple[FunctionalData, float]#

Normalize the data.

abstract inner_product(method: str = 'trapz', smooth: bool = True, noise_variance: float | None = None, **kwargs) ndarray[Any, dtype[float64]]#

Compute an estimate of the inner product matrix.

abstract covariance(points: DenseArgvals | None = None, smooth: str | None = None, **kwargs) Type[FunctionalData]#

Compute an estimate of the covariance.

class FDApy.representation.functional_data.FunctionalDataIterator(fdata)#

Bases: Iterator

Iterator for FunctionalData object.

class FDApy.representation.functional_data.DenseFunctionalData(argvals: DenseArgvals, values: DenseValues)#

Bases: FunctionalData

Class for defining Dense Functional Data.

A class used to define dense functional data. We denote by \(n\), the number of observations and by \(p\), the number of input dimensions. Here, we are in the case of univariate functional data, and so the output dimension will be \(\mathbb{R}\).

Parameters:
argvals: DenseArgvals

The sampling points of the functional data. Each entry of the dictionary represents an input dimension. The shape of the \(j\) th dimension is \((m_j,)\) for \(0 \leq j \leq p\).

values: DenseValues

The values of the functional data. The shape of the array is \((n, m_1, \dots, m_p)\).

Examples

For 1-dimensional dense data:

>>> argvals = DenseArgvals({'input_dim_0': np.array([1, 2, 3, 4, 5])})
>>> values = DenseValues(np.array([
...     [1, 2, 3, 4, 5],
...     [6, 7, 8, 9, 10],
...     [11, 12, 13, 14, 15]
... ]))
>>> DenseFunctionalData(argvals, values)

For 2-dimensional dense data:

>>> argvals = DenseArgvals({
...     'input_dim_0': np.array([1, 2, 3, 4]),
...     'input_dim_1': np.array([5, 6, 7])
... })
>>> values = DenseValues(np.array([
...     [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]],
...     [[5, 6, 7], [5, 6, 7], [5, 6, 7], [5, 6, 7]],
...     [[3, 4, 5], [3, 4, 5], [3, 4, 5], [3, 4, 5]]
... ]))
>>> DenseFunctionalData(argvals, values)
Attributes:
argvals

Getter for argvals.

argvals_stand

Getter for argvals_stand.

n_dimension

Get the number of input dimension of the functional data.

n_obs

Get the number of observations of the functional data.

n_points

Get the number of sampling points.

values

Getter for values.

Methods

center([mean, smooth])

Center the data.

concatenate(*fdata)

Concatenate DenseFunctional objects.

covariance([points, smooth])

Compute an estimate of the covariance function.

inner_product([method, smooth, noise_variance])

Compute the inner product matrix of the data.

mean([points, smooth])

Compute an estimate of the mean.

noise_variance([order])

Estimate the variance of the noise.

norm([squared, method, use_argvals_stand])

Norm of each observation of the data.

normalize([weights, method, use_argvals_stand])

Normalize the data.

smooth([points, kernel_name, bandwidth, degree])

Smooth the data.

to_long()

Convert the data to long format.

static concatenate(*fdata: DenseFunctionalData) DenseFunctionalData#

Concatenate DenseFunctional objects.

Returns:
DenseFunctionalData

The concatenated object.

property argvals: Type[Argvals]#

Getter for argvals.

property values: Type[Values]#

Getter for values.

to_long() DataFrame#

Convert the data to long format.

This function transform a DenseFunctionalData object into pandas DataFrame. It uses the long format to represent the DenseFunctionalData object as a dataframe. This is a helper function as it might be easier for some computation, e.g., smoothing of the mean and covariance functions to have a long format.

Returns:
pd.DataFrame

The data in a long format.

Examples

>>> argvals = DenseArgvals({'input_dim_0': np.array([1, 2, 3, 4, 5])})
>>> values = DenseValues(np.array([
...     [1, 2, 3, 4, 5],
...     [6, 7, 8, 9, 10],
...     [11, 12, 13, 14, 15]
... ]))
>>> fdata = DenseFunctionalData(argvals, values)
>>> fdata.to_long()
    input_dim_0  id  values
0             1   0       1
1             2   0       2
2             3   0       3
3             4   0       4
4             5   0       5
5             1   1       6
6             2   1       7
7             3   1       8
8             4   1       9
9             5   1      10
10            1   2      11
11            2   2      12
12            3   2      13
13            4   2      14
14            5   2      15
noise_variance(order: int = 2) float#

Estimate the variance of the noise.

This function estimates the variance of the noise. The noise is estimated for each individual curve using the methodology in [1]. As the curves are assumed to be generated by the same process, the estimation of the variance of the noise is the mean over the set of curves.

Parameters:
order: int, default=2

Order of the difference sequence. The order has to be between 1 and 10. See [1] for more information.

Returns:
float

The estimation of the variance of the noise.

References

[1] (1,2)

Hall, P., Kay, J.W. and Titterington, D.M. (1990). Asymptotically Optimal Difference-Based Estimation of Variance in Nonparametric Regression. Biometrika 77, 521–528.

Examples

>>> kl = KarhunenLoeve(
...     basis_name='bsplines',
...     n_functions=5,
...     random_state=42
... )
>>> kl.new(n_obs=100)
>>> kl.add_noise(0.05)
>>> kl.noisy_data.noise_variance(order=2)
0.051922438333740877
smooth(points: DenseArgvals | None = None, kernel_name: str = 'epanechnikov', bandwidth: float | None = None, degree: int = 1) DenseFunctionalData#

Smooth the data.

This function smooths each curves individually. Based on [1], it fits a local smoother to the data (the argument degree controls the degree of the local fits).

Parameters:
points: Optional[DenseArgvals], default=None

Points at which the curves are estimated. The default is None, meaning we use the argvals as estimation points.

kernel_name: str, default=”epanechnikov”

Kernel name used as weight (gaussian, epanechnikov, tricube, bisquare).

bandwidth: float, default=None

Strictly positive. Control the size of the associated neighborhood. If bandwidth == None, it is assumed that the curves are twice differentiable and the bandwidth is set to \(n^{-1/5}\) [2] where \(n\) is the number of sampling points per curve. Be careful that it will not work if the curves are not sampled on \([0, 1]\).

degree: int, default=1

Degree of the local polynomial to fit. If degree=0, we fit the local constant estimator (equivalent to the Nadaraya-Watson estimator). If degree=1, we fit the local linear estimator. If degree=2, we fit the local quadratic estimator.

Returns:
DenseFunctionalData

Smoothed data.

References

[1]

Zhang, J.-T. and Chen J. (2007), Statistical Inferences for Functional Data, The Annals of Statistics, Vol. 35, No. 3.

[2]

Tsybakov, A.B. (2008), Introduction to Nonparametric Estimation. Springer Series in Statistics.

Examples

>>> kl = KarhunenLoeve(
...     basis_name='bsplines',
...     n_functions=5,
...     random_state=42
... )
>>> kl.new(n_obs=1)
>>> kl.add_noise(0.05)
>>> kl.noisy_data.smooth()
Functional data object with 1 observations on a 1-dimensional support.
mean(points: DenseArgvals | None = None, smooth: bool = True, **kwargs) DenseFunctionalData#

Compute an estimate of the mean.

This function computes an estimate of the mean curve of a DenseFunctionalData object. As the curves are sampled on a common grid, we consider the sample mean, as defined in [1]. The sampled mean is rate optimal [2]. We included some smoothing using Local Polynonial Estimators.

Parameters:
points: Optional[DenseArgvals], default=None

The sampling points at which the mean is estimated. If None, the DenseArgvals of the DenseFunctionalData is used. If smooth is False, the DenseArgvals of the DenseFunctionalData is used.

smooth: bool, default=True

Should the mean be smoothed?

**kwargs:
kernel_name: str, default=’epanechnikov’

Name of the kernel used for local polynomial smoothing.

degree: int, default=1

Degree used for local polynomial smoothing.

bandwidth: float

Bandwidth used for local polynomial smoothing. The default bandwitdth is set to be the number of sampling points to the power \(-1/5\) [3].

Returns:
DenseFunctionalData

An estimate of the mean as a DenseFunctionalData object.

References

[1]

Ramsey, J. O. and Silverman, B. W. (2005), Functional Data Analysis, Springer Science, Chapter 8.

[2]

Cai, T.T., Yuan, M., (2011), Optimal estimation of the mean function based on discretely sampled functional data: Phase transition. The Annals of Statistics 39, 2330-2355.

[3]

Tsybakov, A.B. (2008), Introduction to Nonparametric Estimation. Springer Series in Statistics.

Examples

>>> kl = KarhunenLoeve(
...     basis_name='bsplines',
...     n_functions=5,
...     random_state=42
... )
>>> kl.new(n_obs=100)
>>> kl.add_noise(0.01)
>>> kl.noisy_data.mean(smooth=True)
Functional data object with 1 observations on a 1-dimensional support.
center(mean: DenseFunctionalData | None = None, smooth: bool = True, **kwargs) DenseFunctionalData#

Center the data.

Parameters:
mean: Optional[DenseFunctionalData], default=None

A precomputed mean as a DenseFunctionalData object.

smooth: bool, default=True

Should the mean be smoothed? Not used if mean is not None.

**kwargs:
kernel_name: str, default=’epanechnikov’

Name of the kernel used for local polynomial smoothing.

degree: int, default=1

Degree used for local polynomial smoothing.

bandwidth: float

Bandwidth used for local polynomial smoothing. The default bandwitdth is set to be the number of sampling points to the power \(-1/5\).

Returns:
DenseFunctionalData

The centered version of the data.

Examples

>>> kl = KarhunenLoeve(
...     basis_name='bsplines',
...     n_functions=5,
...     random_state=42
... )
>>> kl.new(n_obs=10)
>>> kl.data.center(smooth=True)
Functional data object with 10 observations on a 1-dimensional support.
norm(squared: bool = False, method: str = 'trapz', use_argvals_stand: bool = False) ndarray[Any, dtype[float64]]#

Norm of each observation of the data.

For each observation in the data, it computes its norm [1] defined as

\[\lvert\lvert f \rvert\rvert = \left(\int_{\mathcal{T}} f(t)^2dt\right)^{1\2}, t \in \mathcal{T},\]
Parameters:
squared: bool, default=False

If True, the function calculates the squared norm, otherwise it returns the norm.

method: str, {‘simpson’, ‘trapz’}, default = ‘trapz’

The method used to integrated.

use_argvals_stand: bool, default=False

Use standardized argvals to compute the normalization of the data.

Returns:
npt.NDArray[np.float64], shape=(n_obs,)

The norm of each observations.

References

[1]

Ramsey, J. O. and Silverman, B. W. (2005), Functional Data Analysis, Springer Science, Chapter 2.

Examples

>>> kl = KarhunenLoeve(
...     basis_name='bsplines',
...     n_functions=5,
...     random_state=42
... )
>>> kl.new(n_obs=10)
>>> kl.data.norm()
array([
    0.53253351, 0.42212112, 0.6709846 , 0.26672898, 0.27440755,
    0.37906252, 0.65277413, 0.53998411, 0.2872874 , 0.4934973
])
normalize(weights: float = 0.0, method: str = 'trapz', use_argvals_stand: bool = False, **kwargs) Tuple[DenseFunctionalData, float]#

Normalize the data.

The normalization is performed by divising each functional datum by \(w_j = \int_{T} Var(X(t))dt\).

Parameters:
weights: float, default=0.0

The weights used to normalize the data. If weights = 0.0, the weights are estimated by integrating the variance function [1].

method: str, {‘simpson’, ‘trapz’}, default = ‘trapz’

The method used to integrated.

use_argvals_stand: bool, default=False

Use standardized argvals to compute the normalization of the data.

**kwargs:

Not used here.

Returns:
Tuple[DenseFunctionalData, float]

The normalized data and its weight.

References

[1]

Happ and Greven (2018), Multivariate Functional Principal Component Analysis for Data Observed on Different (Dimensional) Domains. Journal of the American Statistical Association, 113, pp. 649–659.

Examples

>>> kl = KarhunenLoeve(
...     basis_name='bsplines',
...     n_functions=5,
...     random_state=42
... )
>>> kl.new(n_obs=10)
>>> kl.data.normalize()
(Functional data object with 10 observations on a 1-dimensional
support., DenseValues(0.21227413))
inner_product(method: str = 'trapz', smooth: bool = True, noise_variance: float | None = None, **kwargs) ndarray[Any, dtype[float64]]#

Compute the inner product matrix of the data.

The inner product matrix is a n_obs by n_obs matrix where each entry is defined as

\[\langle x, y \rangle = \int_{\mathcal{T}} x(t)y(t)dt, t \in \mathcal{T},\]

where \(\mathcal{T}\) is a one- or multi-dimensional domain [2].

Parameters:
method: str, {‘simpson’, ‘trapz’}, default = ‘trapz’

The method used to integrated.

smooth: bool, default=True

Should the mean be smoothed?

noise_variance: Optional[float], default=None

An estimation of the variance of the noise. If None, an estimation is computed using the methodology in [1].

**kwargs:
kernel_name: str, default=’epanechnikov’

Name of the kernel used for local polynomial smoothing.

degree: int, default=1

Degree used for local polynomial smoothing.

bandwidth: float

Bandwidth used for local polynomial smoothing. The default bandwitdth is set to be the number of sampling points to the power \(-1/5\).

Returns:
npt.NDArray[np.float64], shape=(n_obs, n_obs)

Inner product matrix of the data.

References

[1]

Benko, M., Härdle, W. and Kneip, A. (2009). Common functional principal components. The Annals of Statistics 37, 1–34.

[2]

Ramsey, J. O. and Silverman, B. W. (2005), Functional Data Analysis, Springer Science, Chapter 2.

Examples

For one-dimensional functional data:

>>> kl = KarhunenLoeve(
...     basis_name='bsplines', n_functions=5, random_state=42
... )
>>> kl.new(n_obs=3)
>>> kl.data.inner_product(noise_variance=0)
array([
    [ 0.16288536,  0.01958865, -0.10017322],
    [ 0.01958865,  0.17701988, -0.2459348 ],
    [-0.10017322, -0.2459348 ,  0.42008035]
])

For two-dimensional functional data:

>>> kl = KarhunenLoeve(
...     basis_name='bsplines', dimension='2D', n_functions=5,
...     random_state=42, argvals=np.linspace(0, 1, 11)
... )
>>> kl.new(n_obs=3)
>>> kl.data.inner_product(noise_variance=0)
array([
    [ 0.01669878,  0.00349892, -0.00817676],
    [ 0.00349892,  0.03208174, -0.03777796],
    [-0.00817676, -0.03777796,  0.05083159]
])
covariance(points: DenseArgvals | None = None, smooth: bool = True, **kwargs) DenseFunctionalData#

Compute an estimate of the covariance function.

This function computes an estimate of the covariance surface of a DenseFunctionalData object. As the curves are sampled on a common grid, we consider the sample covariance [1].

Parameters:
points: Optional[DenseArgvals], default=None

The sampling points at which the covariance is estimated. If None, the DenseArgvals of the DenseFunctionalData is used. If smooth is False, the DenseArgvals of the DenseFunctionalData is used.p

smooth: bool, default=True

Should the mean be smoothed?

**kwargs:
kernel_name: str, default=’epanechnikov’

Name of the kernel used for local polynomial smoothing.

degree: int, default=1

Degree used for local polynomial smoothing.

bandwidth: float

Bandwidth used for local polynomial smoothing. The default bandwitdth is set to be the number of sampling points to the power \(-1/5\) [3].

Returns:
DenseFunctionalData

An estimate of the covariance as a two-dimensional DenseFunctionalData object.

References

[1]

Ramsey, J. O. and Silverman, B. W. (2005), Functional Data Analysis, Springer Science, Chapter 8.

[2]

Yao, F., Müller, H.-G., Wang, J.-L. (2005). Functional Data Analysis for Sparse Longitudinal Data. Journal of the American Statistical Association 100, pp. 577–590.

[3]

Staniswalis and Lee (1998), Nonparametric Regression Analysis of Longitudinal Data, Journal of the American Statistical Association, 93, pp. 1403–1418.

[4]

Tsybakov, A.B. (2008), Introduction to Nonparametric Estimation. Springer Series in Statistics.

Examples

>>> kl = KarhunenLoeve(
...     basis_name='bsplines',
...     n_functions=5,
...     random_state=42
... )
>>> kl.new(n_obs=100)
>>> kl.add_noise(0.01)
>>> kl.noisy_data.covariance(smooth=True)
Functional data object with 1 observations on a 2-dimensional support.
class FDApy.representation.functional_data.IrregularFunctionalData(argvals: IrregularArgvals, values: IrregularValues)#

Bases: FunctionalData

A class for defining Irregular Functional Data.

Parameters:
argvals: IrregularArgvals

The sampling points of the functional data. Each entry of the dictionary represents an input dimension. Then, each dimension is a dictionary where entries are the different observations. So, the observation \(i\) for the dimension \(j\) is a np.ndarray with shape \((m^i_j,)\) for \(0 \leq i \leq n\) and \(0 \leq j \leq p\).

values: IrregularValues

The values of the functional data. Each entry of the dictionary is an observation of the process. And, an observation is represented by a np.ndarray of shape \((n, m_1, \dots, m_p)\). It should not contain any missing values.

Examples

For 1-dimensional irregular data:

>>> argvals = IrregularArgvals({
...     0: DenseArgvals({'input_dim_0': np.array([0, 1, 2, 3, 4])}),
...     1: DenseArgvals({'input_dim_0': np.array([0, 2, 4])}),
...     2: DenseArgvals({'input_dim_0': np.array([2, 4])})
... })
>>> values = IrregularValues({
...     0: np.array([1, 2, 3, 4, 5]),
...     1: np.array([2, 5, 6]),
...     2: np.array([4, 7])
... })
>>> IrregularFunctionalData(argvals, values)

For 2-dimensional irregular data:

>>> argvals = IrregularArgvals({
...     0: DenseArgvals({
...         'input_dim_0': np.array([1, 2, 3, 4]),
...         'input_dim_1': np.array([5, 6, 7])
...     }),
...     1: DenseArgvals({
...         'input_dim_0': np.array([2, 4]),
...         'input_dim_1': np.array([1, 2, 3])
...     }),
...     2: DenseArgvals({
...         'input_dim_0': np.array([4, 5, 6]),
...         'input_dim_1': np.array([8, 9])
...     })
... })
>>> values = IrregularValues({
...     0: np.array([[1, 2, 3], [4, 1, 2], [3, 4, 1], [2, 3, 4]]),
...     1: np.array([[1, 2, 3], [1, 2, 3]]),
...     2: np.array([[8, 9], [8, 9], [8, 9]])
... })
>>> IrregularFunctionalData(argvals, values)
Attributes:
argvals

Getter for argvals.

argvals_stand

Getter for argvals_stand.

n_dimension

Get the number of input dimension of the functional data.

n_obs

Get the number of observations of the functional data.

n_points

Get the number of sampling points.

values

Getter for values.

Methods

center([mean, smooth])

Center the data.

concatenate(*fdata)

Concatenate IrregularFunctionalData objects.

covariance([points, smooth])

Compute an estimate of the covariance function.

inner_product([method, smooth, noise_variance])

Compute the inner product matrix of the data.

mean([points, smooth])

Compute an estimate of the mean.

noise_variance([order])

Estimate the variance of the noise.

norm([squared, method, use_argvals_stand])

Norm of each observation of the data.

normalize([weights, method, use_argvals_stand])

Normalize the data.

smooth([points, kernel_name, bandwidth, degree])

Smooth the data.

to_long()

Convert the data to long format.

static concatenate(*fdata: IrregularFunctionalData) IrregularFunctionalData#

Concatenate IrregularFunctionalData objects.

Returns:
IrregularFunctionalData

The concatenated objects.

property argvals: Type[Argvals]#

Getter for argvals.

property values: Type[Values]#

Getter for values.

to_long() DataFrame#

Convert the data to long format.

This function transform a IrregularFunctionalData object into pandas DataFrame. It uses the long format to represent the IrregularFunctionalData object as a dataframe. This is a helper function as it might be easier for some computation, e.g., smoothing of the mean and covariance functions to have a long format.

Returns:
pd.DataFrame

The data in a long format.

Examples

For one-dimensional functional data:

>>> argvals = IrregularArgvals({
...     0: DenseArgvals({'input_dim_0': np.array([0, 1, 2, 3, 4])}),
...     1: DenseArgvals({'input_dim_0': np.array([0, 2, 4])}),
...     2: DenseArgvals({'input_dim_0': np.array([2, 4])})
... })
>>> values = IrregularValues({
...     0: np.array([1, 2, 3, 4, 5]),
...     1: np.array([2, 5, 6]),
...     2: np.array([4, 7])
... })
>>> fdata = IrregularFunctionalData(argvals, values)
>>> fdata.to_long()
   input_dim_0  id  values
0            0   0       1
1            1   0       2
2            2   0       3
3            3   0       4
4            4   0       5
5            0   1       2
6            2   1       5
7            4   1       6
8            2   2       4
9            4   2       7
noise_variance(order: int = 2) float#

Estimate the variance of the noise.

This function estimates the variance of the noise. The noise is estimated for each individual curve using the methodology in [1]. As the curves are assumed to be generated by the same process, the estimation of the variance of the noise is the mean over the set of curves.

TODO: Add multidimensional estimation.

Parameters:
order: int, default=2

Order of the difference sequence. The order has to be between 1 and 10. See [1] for more information.

Returns:
float

The estimation of the variance of the noise.

References

[1] (1,2)

Hall, P., Kay, J.W. and Titterington, D.M. (1990). Asymptotically Optimal Difference-Based Estimation of Variance in Nonparametric Regression. Biometrika 77, 521–528.

Examples

>>> kl = KarhunenLoeve(
...     basis_name='bsplines',
...     n_functions=5,
...     random_state=42
... )
>>> kl.new(n_obs=100)
>>> kl.sparsify(0.5)
>>> kl.sparse_data.noise_variance(order=2)
0.006671248206782777
smooth(points: DenseArgvals | None = None, kernel_name: str = 'epanechnikov', bandwidth: float | None = None, degree: int = 1) DenseFunctionalData#

Smooth the data.

This function smooths each curves individually. Based on [1], it fits a local smoother to the data (the argument degree controls the degree of the local fits).

Parameters:
points: Optional[DenseArgvals], default=None

Points at which the curves are estimated. The default is None, meaning we use the argvals as estimation points.

kernel_name: str, default=”epanechnikov”

Kernel name used as weight (gaussian, epanechnikov, tricube, bisquare).

bandwidth: float, default=None

Strictly positive. Control the size of the associated neighborhood. If bandwidth == None, it is assumed that the curves are twice differentiable and the bandwidth is set to \(n^{-1/5}\) where \(n\) is the number of sampling points per curve [2]. Be careful that it will not work if the curves are not sampled on \([0, 1]\).

degree: int, default=1

Degree of the local polynomial to fit. If degree = 0, we fit the local constant estimator (equivalent to the Nadaraya-Watson estimator). If degree = 1, we fit the local linear estimator. If degree = 2, we fit the local quadratic estimator.

Returns:
DenseFunctionalData

A smoothed version of the data.

References

[1]

Zhang, J.-T. and Chen J. (2007), Statistical Inferences for Functional Data, The Annals of Statistics, Vol. 35, No. 3.

[2]

Tsybakov, A.B. (2008), Introduction to Nonparametric Estimation. Springer Series in Statistics.

Examples

For one-dimensional functional data:

>>> argvals = IrregularArgvals({
...     0: DenseArgvals({'input_dim_0': np.array([0, 1, 2, 3, 4])}),
...     1: DenseArgvals({'input_dim_0': np.array([0, 2, 4])}),
...     2: DenseArgvals({'input_dim_0': np.array([2, 4])})
... })
>>> values = IrregularValues({
...     0: np.array([1, 2, 3, 4, 5]),
...     1: np.array([2, 5, 6]),
...     2: np.array([4, 7])
... })
>>> fdata = IrregularFunctionalData(argvals, values)
>>> fdata.smooth()
Functional data object with 3 observations on a 1-dimensional support.
mean(points: DenseArgvals | None = None, smooth: bool = True, **kwargs) DenseFunctionalData#

Compute an estimate of the mean.

This function computes an estimate of the mean curve of a IrregularFunctionalData object. The curves are not sampled on a common grid. We implement the methodology from [1].

Parameters:
points: Optional[DenseArgvals], default=None

The sampling points at which the mean is estimated. If None, the DenseArgvals of the DenseFunctionalData is used. If smooth is False, the DenseArgvals of the DenseFunctionalData is used.

smooth: bool, default=True

Not used in this context. The mean curve is always smoothed for IrregularFunctionalData.

**kwargs:
kernel_name: str, default=’epanechnikov’

Name of the kernel used for local polynomial smoothing.

degree: int, default=1

Degree used for local polynomial smoothing.

bandwidth: float

Bandwidth used for local polynomial smoothing. The default bandwitdth is set to be the number of sampling points to the power \(-1/5\) [2].

Returns:
DenseFunctionalData

An estimate of the mean as a DenseFunctionalData object.

References

[1]

Cai, T.T., Yuan, M., (2011), Optimal estimation of the mean function based on discretely sampled functional data: Phase transition. The Annals of Statistics 39, 2330-2355.

[2]

Tsybakov, A.B. (2008), Introduction to Nonparametric Estimation. Springer Series in Statistics.

Examples

For one-dimensional functional data:

>>> argvals = IrregularArgvals({
...     0: DenseArgvals({'input_dim_0': np.array([0, 1, 2, 3, 4])}),
...     1: DenseArgvals({'input_dim_0': np.array([0, 2, 4])}),
...     2: DenseArgvals({'input_dim_0': np.array([2, 4])})
... })
>>> values = IrregularValues({
...     0: np.array([1, 2, 3, 4, 5]),
...     1: np.array([2, 5, 6]),
...     2: np.array([4, 7])
... })
>>> fdata = IrregularFunctionalData(argvals, values)
>>> fdata.mean()
Functional data object with 1 observations on a 1-dimensional support.
center(mean: DenseFunctionalData | None = None, smooth: bool = True, **kwargs) IrregularFunctionalData#

Center the data.

Parameters:
mean: Optional[DenseFunctionalData], default=None

A precomputed mean as a DenseFunctionalData object.

smooth: bool, default=True

Should the mean be smoothed?

**kwargs:
kernel_name: str, default=’epanechnikov’

Name of the kernel used for local polynomial smoothing.

degree: int, default=1

Degree used for local polynomial smoothing.

bandwidth: float

Bandwidth used for local polynomial smoothing. The default bandwitdth is set to be the number of sampling points to the power \(-1/5\).

Returns:
IrregularFunctionalData

The centered version of the data.

Examples

>>> kl = KarhunenLoeve(
...     basis_name='bsplines',
...     n_functions=5,
...     random_state=42
... )
>>> kl.new(n_obs=10)
>>> kl.add_noise_and_sparsify(0.01, 0.95)
>>> kl.sparse_data.center(smooth=True)
Functional data object with 10 observations on a 1-dimensional support.
norm(squared: bool = False, method: str = 'trapz', use_argvals_stand: bool = False) ndarray[Any, dtype[float64]]#

Norm of each observation of the data.

For each observation in the data, it computes its norm [1] defined as

\[\lvert\lvert f \rvert\rvert = \left(\int_{\mathcal{T}} f(t)^2dt\right)^{1\2}, t \in \mathcal{T},\]
Parameters:
squared: bool, default=False

If True, the function calculates the squared norm, otherwise the result is not squared.

method: str, {‘simpson’, ‘trapz’}, default = ‘trapz’

The method used to integrated.

use_argvals_stand: bool, default=False

Use standardized argvals to compute the normalization of the data.

Returns:
npt.NDArray[np.float64], shape=(n_obs,)

The norm of each observations.

References

[1]

Ramsey, J. O. and Silverman, B. W. (2005), Functional Data Analysis, Springer Science, Chapter 2.

Examples

>>> kl = KarhunenLoeve(
...     basis_name='bsplines',
...     n_functions=5,
...     random_state=42
... )
>>> kl.new(n_obs=10)
>>> kl.sparsify(percentage=0.5, epsilon=0.05)
>>> kl.sparse_data.norm()
array([
    0.53419879, 0.40750272, 0.67092435, 0.26762124, 0.27425138,
    0.37419987, 0.65775515, 0.54579643, 0.25830787, 0.49324345
])
normalize(weights: float = 0.0, method: str = 'trapz', use_argvals_stand: bool = False, **kwargs) Tuple[FunctionalData, float]#

Normalize the data.

The normalization is performed by divising each functional datum by \(w_j = \int_{T} Var(X(t))dt\).

Parameters:
weights: float, default=0.0

The weights used to normalize the data. If weights = 0.0, the weights are estimated by integrating the variance function [1].

method: str, {‘simpson’, ‘trapz’}, default = ‘trapz’

The method used to integrated.

use_argvals_stand: bool, default=False

Use standardized argvals to compute the normalization of the data.

**kwargs:

Keyword parameters for the smoothing of the observations.

Returns:
Tuple[IrregularFunctionalData, float]

The normalized data and its weight.

References

[1]

Happ and Greven (2018), Multivariate Functional Principal Component Analysis for Data Observed on Different (Dimensional) Domains. Journal of the American Statistical Association, 113, pp. 649–659.

Examples

>>> kl = KarhunenLoeve(
...     basis_name='bsplines',
...     n_functions=5,
...     random_state=42
... )
>>> kl.new(n_obs=10)
>>> kl.sparsify(percentage=0.5, epsilon=0.05)
>>> kl.sparse_data.normalize()
(Functional data object with 10 observations on a 1-dimensional
support., DenseValues(0.16802008))
inner_product(method: str = 'trapz', smooth: bool = True, noise_variance: float | None = None, **kwargs) ndarray[Any, dtype[float64]]#

Compute the inner product matrix of the data.

The inner product matrix is a n_obs by n_obs matrix where each entry is defined as

\[\langle x, y \rangle = \int_{\mathcal{T}} x(t)y(t)dt, t \in \mathcal{T},\]

where \(\mathcal{T}\) is a one- or multi-dimensional domain.

Parameters:
method: str, {‘simpson’, ‘trapz’}, default = ‘trapz’

The method used to integrated.

smooth: bool, default=True

Should the mean be smoothed?

noise_variance: Optional[float], default=None

An estimation of the variance of the noise. If None, an estimation is computed using the methodology in [1].

**kwargs:
kernel_name: str, default=’epanechnikov’

Name of the kernel used for local polynomial smoothing.

degree: int, default=1

Degree used for local polynomial smoothing.

bandwidth: float

Bandwidth used for local polynomial smoothing. The default bandwitdth is set to be the number of sampling points to the power \(-1/5\).

Returns:
npt.NDArray[np.float64], shape=(n_obs, n_obs)

Inner product matrix of the data.

Raises:
NotImplementedError

Not implement for higher-dimensional data.

References

[1]

Benko, M., Härdle, W., Kneip, A., (2009), Common functional principal components. The Annals of Statistics 37, 1-34.

Examples

For one-dimensional functional data:

>>> kl = KarhunenLoeve(
...     basis_name='bsplines', n_functions=5, random_state=5
... )
>>> kl.new(n_obs=3)
>>> kl.sparsify(percentage=0.8, epsilon=0.05)
>>> kl.sparse_data.inner_product(noise_variance=0)
array([
    [ 0.15749721,  0.01983093, -0.09607059],
    [ 0.01983093,  0.17937531, -0.24773228],
    [-0.09607059, -0.24773228,  0.41648575]
])
covariance(points: DenseArgvals | None = None, smooth: bool = True, **kwargs) IrregularFunctionalData#

Compute an estimate of the covariance function.

This function computes an estimate of the covariance surface of a IrregularFunctionalData object. As the curves are not sampled on a common grid, we consider the method in [1].

Parameters:
points: Optional[DenseArgvals], default=None

The sampling points at which the covariance is estimated. If None, the concatenation of the IrregularArgvals of the IrregularFunctionalData is used.

mean: Optional[DenseFunctionalData], default=None

An estimate of the mean of self. If None, an estimate is computed.

smooth: bool, default=True

Not used here. Smoothing is always performed for IrregularFunctionalData.

**kwargs:
kernel_name: str, default=’epanechnikov’

Name of the kernel used for local polynomial smoothing.

degree: int, default=1

Degree used for local polynomial smoothing.

bandwidth: float

Bandwidth used for local polynomial smoothing. The default bandwitdth is set to be the number of sampling points to the power \(-1/5\) [3].

Returns:
DenseFunctionalData

An estimate of the covariance as a two-dimensional DenseFunctionalData object.

Raises:
NotImplementedError

Not implement for higher-dimensional data.

References

[1]

Yao, F., Müller, H.-G., Wang, J.-L. (2005). Functional Data Analysis for Sparse Longitudinal Data. Journal of the American Statistical Association 100, pp. 577–590.

[2]

Staniswalis and Lee (1998), Nonparametric Regression Analysis of Longitudinal Data, Journal of the American Statistical Association, 93, pp. 1403–1418.

[3]

Tsybakov, A.B. (2008), Introduction to Nonparametric Estimation. Springer Series in Statistics.

Examples

>>> kl = KarhunenLoeve(
...     basis_name='bsplines',
...     n_functions=5,
...     random_state=42
... )
>>> kl.new(n_obs=100)
>>> kl.sparsify(percentage=0.5, epsilon=0.05)
>>> kl.sparse_data.covariance()
Functional data object with 1 observations on a 2-dimensional support.
class FDApy.representation.functional_data.MultivariateFunctionalData(initlist: List[Type[FunctionalData]])#

Bases: UserList[Type[FunctionalData]]

A class for defining Multivariate Functional Data.

An instance of MultivariateFunctionalData is a list containing objects of the class DenseFunctionalData or IrregularFunctionalData.

Parameters:
initlist: List[Type[FunctionalData]]

The list containing the elements of the MultivariateFunctionalData.

Notes

Be careful that we will not check if all the elements have the same type. It is possible to create MultivariateFunctionalData containing both Dense and Iregular functional data. However, only this two types are allowed to be in the list. The number of observations has to be the same for each element of the list.

Examples

>>> argvals = DenseArgvals({'input_dim_0': np.array([1, 2, 3, 4, 5])})
>>> values = DenseValues(np.array([
...     [1, 2, 3, 4, 5],
...     [6, 7, 8, 9, 10],
...     [11, 12, 13, 14, 15]
... ]))
>>> fdata_dense = DenseFunctionalData(argvals, values)
>>> argvals = IrregularArgvals({
...     0: DenseArgvals({'input_dim_0': np.array([0, 1, 2, 3, 4])}),
...     1: DenseArgvals({'input_dim_0': np.array([0, 2, 4])}),
...     2: DenseArgvals({'input_dim_0': np.array([2, 4])})
... })
>>> values = IrregularValues({
...     0: np.array([1, 2, 3, 4, 5]),
...     1: np.array([2, 5, 6]),
...     2: np.array([4, 7])
... })
>>> fdata_irregular = IrregularFunctionalData(argvals, values)
>>> MultivariateFunctionalData([fdata_dense, fdata_irregular])
Attributes:
n_dimension

Get the number of input dimension of the functional data.

n_functional

Get the number of functional data with self.

n_obs

Get the number of observations of the functional data.

n_points

Get the mean number of sampling points.

Methods

append(item)

Add an item to self.

center([mean, smooth])

Center the data.

clear()

Remove all items from the list.

concatenate(*fdata)

Concatenate MultivariateFunctionalData objects.

count(value)

covariance([points, smooth])

Compute an estimate of the covariance.

extend(other)

Extend the list of FunctionalData by appending from iterable.

index(value, [start, [stop]])

Raises ValueError if the value is not present.

inner_product([method, smooth, noise_variance])

Compute the inner product matrix of the data.

insert(i, item)

Insert an item item at a given position i.

mean([points, smooth])

Compute an estimate of the mean.

noise_variance([order])

Estimate the variance of the noise.

norm([squared, method, use_argvals_stand])

Norm of each observation of the data.

normalize([weights, method, use_argvals_stand])

Normalize the data.

pop([i])

Remove the item at the given position in the list, and return it.

remove(item)

Remove the first item from self where value is item.

reverse()

Reserve the elements of the list in place.

smooth([points, kernel_name, bandwidth, degree])

Smooth the data.

to_long()

Convert the data to long format.

copy

sort

static concatenate(*fdata: MultivariateFunctionalData) MultivariateFunctionalData#

Concatenate MultivariateFunctionalData objects.

Parameters:
data: MultivariateFunctionalData

The data to concatenate with self.

Returns:
MultivariateFunctionalData

The concatenation of self and data.

Raises:
ValueError

When all fdata do not have the same number of elements.

property n_obs: int#

Get the number of observations of the functional data.

Returns:
int

Number of observations within the functional data.

property n_functional: int#

Get the number of functional data with self.

Returns:
int

Number of functions in the list.

property n_dimension: List[int]#

Get the number of input dimension of the functional data.

Returns:
List[int]

List containing the dimension of each component in the functional data.

property n_points: List[Dict[str, int]]#

Get the mean number of sampling points.

Returns:
List[Union[Tuple[int, …], Dict[int, Tuple[int, …]]]]

A list containing the number of sampling points along each axis for each function.

append(item: Type[FunctionalData]) None#

Add an item to self.

Parameters:
item: Type[FunctionalData]

Item to add.

extend(other: Iterable[Type[FunctionalData]]) None#

Extend the list of FunctionalData by appending from iterable.

insert(i: int, item: Type[FunctionalData]) None#

Insert an item item at a given position i.

remove(item: Type[FunctionalData]) None#

Remove the first item from self where value is item.

pop(i: int = -1) Type[FunctionalData]#

Remove the item at the given position in the list, and return it.

clear() None#

Remove all items from the list.

reverse() None#

Reserve the elements of the list in place.

to_long() List[DataFrame]#

Convert the data to long format.

This function transform a MultivariateFunctionalData object into a list of pandas DataFrame. It uses the long format to represent each element of the MultivariateFunctionalData object as a dataframe. This is a helper function as it might be easier for some computation.

Returns:
List[pd.DataFrame]

The data in a long format.

Examples

>>> argvals = DenseArgvals({'input_dim_0': np.array([1, 2, 3, 4, 5])})
>>> values = DenseValues(np.array([
...     [1, 2, 3, 4, 5],
...     [6, 7, 8, 9, 10],
...     [11, 12, 13, 14, 15]
... ]))
>>> fdata_dense = DenseFunctionalData(argvals, values)
>>> argvals = IrregularArgvals({
...     0: DenseArgvals({'input_dim_0': np.array([0, 1, 2, 3, 4])}),
...     1: DenseArgvals({'input_dim_0': np.array([0, 2, 4])}),
...     2: DenseArgvals({'input_dim_0': np.array([2, 4])})
... })
>>> values = IrregularValues({
...     0: np.array([1, 2, 3, 4, 5]),
...     1: np.array([2, 5, 6]),
...     2: np.array([4, 7])
... })
>>> fdata_irregular = IrregularFunctionalData(argvals, values)
>>> fdata = MultivariateFunctionalData([fdata_dense, fdata_irregular])
>>> fdata.to_long()
[    input_dim_0  id  values
0             1   0       1
1             2   0       2
2             3   0       3
3             4   0       4
4             5   0       5
5             1   1       6
6             2   1       7
7             3   1       8
8             4   1       9
9             5   1      10
10            1   2      11
11            2   2      12
12            3   2      13
13            4   2      14
14            5   2      15,
   input_dim_0  id  values
0            0   0       5
1            1   0       4
2            2   0       3
3            3   0       2
4            4   0       1
5            0   1       5
6            2   1       3
7            4   1       1
8            2   2       5
9            4   2       3]
noise_variance(order: int = 2) float#

Estimate the variance of the noise.

This function estimates the variance of the noise. The noise is estimated for each individual curve using the methodology in [1]. As the curves are assumed to be generated by the same process, the estimation of the variance of the noise is the mean over the set of curves.

Parameters:
order: int, default=2

Order of the difference sequence. The order has to be between 1 and 10. See [1] for more information.

Returns:
float

The estimation of the variance of the noise.

References

[1] (1,2)

Hall, P., Kay, J.W. and Titterington, D.M. (1990). Asymptotically Optimal Difference-Based Estimation of Variance in Nonparametric Regression. Biometrika 77, 521–528.

Examples

>>> kl = KarhunenLoeve(
...     basis_name='bsplines',
...     n_functions=5,
...     random_state=42
... )
>>> kl.new(n_obs=100)
>>> kl.add_noise(0.05)
>>> kl.sparsify(0.5)
>>> fdata = MultivariateFunctionalData([kl.noisy_data, kl.sparse_data])
>>> fdata.noise_variance
[0.051922438333740877, 0.006671248206782777]
smooth(points: List[DenseArgvals] | None = None, kernel_name: List[str] | None = None, bandwidth: List[float] | None = None, degree: List[int] | None = None)#

Smooth the data.

This function smooths each curves individually. It fits a local smoother to the data (the argument degree controls the degree of the local fits). All the paraneters have to be passed as a list of the same length of the MultivariateFunctionalData.

Parameters:
points: Optional[List[DenseArgvals]], default=None

Points at which the curves are estimated. The default is None, meaning we use the argvals as estimation points.

kernel_name: Optional[List[str]], default=”epanechnikov”

Kernel name used as weight (gaussian, epanechnikov, tricube, bisquare).

bandwidth: Optional[List[float]], default=None

Strictly positive. Control the size of the associated neighborhood. If bandwidth == None, it is assumed that the curves are twice differentiable and the bandwidth is set to \(n^{-1/5}\) [2] where \(n\) is the number of sampling points per curve. Be careful that it will not work if the curves are not sampled on \([0, 1]\).

degree: Optional[List[int]], default=1

Degree of the local polynomial to fit. If degree=0, we fit the local constant estimator (equivalent to the Nadaraya-Watson estimator). If degree=1, we fit the local linear estimator. If degree=2, we fit the local quadratic estimator.

Returns:
MultivariateFunctionalData

Smoothed data.

References

[1]

Zhang, J.-T. and Chen J. (2007), Statistical Inferences for Functional Data, The Annals of Statistics, Vol. 35, No. 3.

[2]

Tsybakov, A.B. (2008), Introduction to Nonparametric Estimation. Springer Series in Statistics.

Examples

>>> kl = KarhunenLoeve(
...     basis_name='bsplines', n_functions=5, random_state=42
... )
>>> kl.new(n_obs=50)
>>> kl.add_noise_and_sparsify(0.05, 0.5)
>>> fdata_1 = kl.data
>>> fdata_2 = kl.noisy_data
>>> fdata = MultivariateFunctionalData([fdata_1, fdata_2])
>>> points = DenseArgvals({'input_dim_0': np.linspace(0, 1, 11)})
>>> fdata_smooth = fdata.smooth(
...     points=[points, points],
...     kernel_name=['epanechnikov', 'epanechnikov'],
...     bandwidth=[0.05, 0.1],
...     degree=[1, 2]
... )
Multivariate functional data object with 2 functions of 50 observations
mean(points: List[DenseArgvals] | None = None, smooth: bool = True, **kwargs) MultivariateFunctionalData#

Compute an estimate of the mean.

This function computes an estimate of the mean curve of a MultivariateFunctionalData object.

Parameters:
points: Optional[List[DenseArgvals]], default=None

Points at which the mean is estimated. The default is None, meaning we use the argvals as estimation points.

smooth: bool, default=True

Should the mean be smoothed?

**kwargs:
kernel_name: str, default=’epanechnikov’

Name of the kernel used for local polynomial smoothing.

degree: int, default=1

Degree used for local polynomial smoothing.

bandwidth: float

Bandwidth used for local polynomial smoothing. The default bandwitdth is set to be the number of sampling points to the power \(-1/5\).s

Returns:
MultivariateFunctionalData

An estimate of the mean as a MultivariateFunctionalData object.

References

[1]

Happ and Greven (2018), Multivariate Functional Principal Component Analysis for Data Observed on Different (Dimensional) Domains. Journal of the American Statistical Association, 113, pp. 649–659.

Examples

>>> kl = KarhunenLoeve(
...     basis_name='bsplines', n_functions=5, random_state=42
... )
>>> kl.new(n_obs=50)
>>> kl.add_noise_and_sparsify(0.05, 0.5)
>>> fdata_1 = kl.data
>>> fdata_2 = kl.noisy_data
>>> fdata = MultivariateFunctionalData([fdata_1, fdata_2])
>>> points = DenseArgvals({'input_dim_0': np.linspace(0, 1, 11)})
>>> fdata.mean(points=points)
Multivariate functional data object with 2 functions of 1 observations.
center(mean: MultivariateFunctionalData | None = None, smooth: bool = True, **kwargs) MultivariateFunctionalData#

Center the data.

Parameters:
mean: Optional[MultivariateFunctionalData], default=None

A precomputed mean as a MultivariateFunctionalData object.

smooth: bool, default=True

Should the mean be smoothed?

**kwargs:
kernel_name: str, default=’epanechnikov’

Name of the kernel used for local polynomial smoothing.

degree: int, default=1

Degree used for local polynomial smoothing.

bandwidth: float

Bandwidth used for local polynomial smoothing. The default bandwitdth is set to be the number of sampling points to the power \(-1/5\).

Returns:
MultivariateFunctionalData

The centered version of the data.

Examples

>>> kl = KarhunenLoeve(
...     basis_name=name, n_functions=n_functions, random_state=42
... )
>>> kl.new(n_obs=10)
>>> kl.add_noise_and_sparsify(0.05, 0.5)
>>> fdata_1 = kl.data
>>> fdata_2 = kl.sparse_data
>>> fdata = MultivariateFunctionalData([fdata_1, fdata_2])
>>> fdata.center(smooth=True)
Functional data object with 10 observations on a 1-dimensional support.
norm(squared: bool = False, method: str = 'trapz', use_argvals_stand: bool = False) ndarray[Any, dtype[float64]]#

Norm of each observation of the data.

For each observation in the data, it computes its norm defined as

\[\lvert\lvert\lvert f \rvert\rvert\rvert = \left(\sum_{p = 1}^P \int_{\mathcal{T}}\{f(t)_p\}^2dt\right)^{1\2}, t \in \mathcal{T},\]
Parameters:
squared: bool, default=False

If True, the function calculates the squared norm, otherwise it returns the norm.

method: str, {‘simpson’, ‘trapz’}, default = ‘trapz’

The method used to integrated.

use_argvals_stand: bool, default=False

Use standardized argvals to compute the normalization of the data.

Returns:
npt.NDArray[np.float64], shape=(n_obs,)

The norm of each observations.

Examples

>>> kl = KarhunenLoeve(
...     basis_name=name, n_functions=n_functions, random_state=42
... )
>>> kl.new(n_obs=4)
>>> kl.add_noise_and_sparsify(0.05, 0.5)
>>> fdata_1 = kl.data
>>> fdata_2 = kl.sparse_data
>>> fdata = MultivariateFunctionalData([fdata_1, fdata_2])
>>> fdata.norm()
array([1.05384959, 0.84700578, 1.37439764, 0.59235447])
normalize(weights: ndarray[Any, dtype[float64]] | None = None, method: str = 'trapz', use_argvals_stand: bool = False, **kwargs) Tuple[MultivariateFunctionalData, ndarray[Any, dtype[float64]]]#

Normalize the data.

The normalization is performed by divising each functional datum by \(w_j = \int_{T} Var(X(t))dt\).

Parameters:
weights: Optional[npt.NDArray[np.float64]], default=None

The weights used to normalize the data. If weights = None, the weights are estimated by integrating the variance function [1].

method: str, {‘simpson’, ‘trapz’}, default = ‘trapz’

The method used to integrated.

use_argvals_stand: bool, default=False

Use standardized argvals to compute the normalization of the data.

**kwargs:

Keyword parameters for the smoothing of the observations.

Returns:
Tuple[MultivariateFunctionalData, npt.NDArray[np.float64]]

The normalized data.

References

[1]

Happ and Greven (2018), Multivariate Functional Principal Component Analysis for Data Observed on Different (Dimensional) Domains. Journal of the American Statistical Association, 113, pp. 649–659.

Examples

>>> kl = KarhunenLoeve(
...     basis_name=name, n_functions=n_functions, random_state=42
... )
>>> kl.new(n_obs=4)
>>> kl.add_noise_and_sparsify(0.05, 0.5)
>>> fdata_1 = kl.data
>>> fdata_2 = kl.sparse_data
>>> fdata = MultivariateFunctionalData([fdata_1, fdata_2])
>>> fdata.normalize()
(Multivariate functional data object with 2 functions of 4
observations., array([0.20365764, 0.19388443]))
inner_product(method: str = 'trapz', smooth: bool = True, noise_variance: ndarray[Any, dtype[float64]] | None = None, **kwargs) ndarray[Any, dtype[float64]]#

Compute the inner product matrix of the data.

The inner product matrix is a n_obs by n_obs matrix where each entry is defined as

\[\langle\langle x, y \rangle\rangle = \sum_{p = 1}^P \int_{\mathcal{T}_k} x^{(p)}(t)y^{(p)}(t)dt, t \in \mathcal{T},\]

where \(\mathcal{T}\) is a one- or multi-dimensional domain.

Parameters:
method: str, {‘simpson’, ‘trapz’}, default = ‘trapz’

The method used to integrated.

smooth: bool, default=True

Should the mean be smoothed?

noise_variance: Optional[npt.NDArray[np.float64]], default=None

An estimation of the variance of the noise. If None, an estimation is computed using the methodology in [1].

**kwargs:
kernel_name: str, default=’epanechnikov’

Name of the kernel used for local polynomial smoothing.

degree: int, default=1

Degree used for local polynomial smoothing.

bandwidth: float

Bandwidth used for local polynomial smoothing. The default bandwitdth is set to be the number of sampling points to the power \(-1/5\).

Returns:
npt.NDArray[np.float64], shape=(n_obs, n_obs)

Inner product matrix of the data.

References

[1]

Benko, M., Härdle, W. and Kneip, A. (2009). Common functional principal components. The Annals of Statistics 37, 1–34.

Examples

>>> kl = KarhunenLoeve(
...     basis_name=name, n_functions=n_functions, random_state=42
... )
>>> kl.new(n_obs=4)
>>> kl.add_noise_and_sparsify(0.05, 0.5)
>>> fdata_1 = kl.data
>>> fdata_2 = kl.sparse_data
>>> fdata = MultivariateFunctionalData([fdata_1, fdata_2])
>>> fdata.inner_product(noise_variance=0)
array([
    [ 0.39261306,  0.06899153, -0.14614219, -0.0836462 ],
    [ 0.06899153,  0.32580074, -0.4890299 ,  0.07577286],
    [-0.14614219, -0.4890299 ,  0.94953678, -0.09322892],
    [-0.0836462 ,  0.07577286, -0.09322892,  0.17157688]
])
covariance(points: List[DenseArgvals] | None = None, smooth: bool = True, **kwargs) MultivariateFunctionalData#

Compute an estimate of the covariance.

This function computes an estimate of the covariance surface of a MultivariateFunctionalData object.

Parameters:
points: Optional[List[DenseArgvals]], default=None

Points at which the mean is estimated. The default is None, meaning we use the argvals as estimation points.

smooth: bool, default=True

Should the mean be smoothed?

**kwargs:
kernel_name: str, default=’epanechnikov’

Name of the kernel used for local polynomial smoothing.

degree: int, default=1

Degree used for local polynomial smoothing.

bandwidth: float

Bandwidth used for local polynomial smoothing. The default bandwitdth is set to be the number of sampling points to the power \(-1/5\).

Returns:
MultivariateFunctionalData

An estimate of the covariance as a two-dimensional MultivariateFunctionalData object with same argvals as self.

Examples

>>> kl = KarhunenLoeve(
...     basis_name='bsplines', n_functions=5, random_state=42
... )
>>> kl.new(n_obs=50)
>>> kl.add_noise_and_sparsify(0.05, 0.5)
>>> fdata_1 = kl.data
>>> fdata_2 = kl.noisy_data
>>> fdata = MultivariateFunctionalData([fdata_1, fdata_2])
>>> points = DenseArgvals({'input_dim_0': np.linspace(0, 1, 11)})
>>> fdata.covariance(points=[points, points])
Multivariate functional data object with 2 functions of 1 observations.

Basis#

class FDApy.representation.basis.Basis(name: str, n_functions: int = 5, dimension: str = '1D', argvals: ndarray[Any, dtype[float64]] | None = None, is_normalized: bool = False, add_intercept: bool = True, **kwargs)#

Bases: DenseFunctionalData

Define univariate orthonormal basis.

Parameters:
name: str, {‘legendre’, ‘wiener’, ‘fourier’, ‘bsplines’}

Denotes the basis of functions to use.

n_functions: int

Number of functions in the basis.

dimension: str, {‘1D’, ‘2D’}, default=’1D’

Dimension of the basis to simulate. If ‘2D’, the basis is simulated as the tensor product of the one dimensional basis of functions by itself. The number of functions in the 2D basis will be \(n_function^2\).

argvals: Optional[npt.NDArray[np.float64]]

The sampling points of the functional data. Each entry of the dictionary represents an input dimension. The shape of the \(j\) th dimension is \((m_j,)\) for \(0 \leq j \leq p\).

is_normalized: bool, default=False

Should we normalize the basis function?

add_intercept: bool, default=True

Should the constant functions be into the basis?

**kwargs
degree: int, default=3

Degree of the B-splines. The default gives cubic splines.

Attributes:
argvals

Getter for argvals.

argvals_stand

Getter for argvals_stand.

dimension

Getter for dimension.

is_normalized

Getter for is_normalized.

n_dimension

Get the number of input dimension of the functional data.

n_obs

Get the number of observations of the functional data.

n_points

Get the number of sampling points.

name

Getter for name.

values

Getter for values.

Methods

center([mean, smooth])

Center the data.

concatenate(*fdata)

Concatenate DenseFunctional objects.

covariance([points, smooth])

Compute an estimate of the covariance function.

inner_product([method, smooth, noise_variance])

Compute the inner product matrix of the data.

mean([points, smooth])

Compute an estimate of the mean.

noise_variance([order])

Estimate the variance of the noise.

norm([squared, method, use_argvals_stand])

Norm of each observation of the data.

normalize([weights, method, use_argvals_stand])

Normalize the data.

smooth([points, kernel_name, bandwidth, degree])

Smooth the data.

to_long()

Convert the data to long format.

property name: str#

Getter for name.

property is_normalized: bool#

Getter for is_normalized.

property dimension: str#

Getter for dimension.

class FDApy.representation.basis.MultivariateBasis(simulation_type: str, n_components: int, name: str | List[str], n_functions: int = 5, dimension: List[str] | None = None, argvals: ndarray[Any, dtype[float64]] | None = None, is_normalized: bool = False, **kwargs)#

Bases: MultivariateFunctionalData

Define multivariate orthonormal basis.

Parameters:
simulation_type: str, {‘split’, ‘weighted’}

Type of the simulation.

n_components: int

Number of components to generate.

name: Union[str, List[str]]

Name of the basis to use. One of {‘legendre’, ‘wiener’, ‘fourier’, ‘bsplines’}.

n_functions: int

Number of functions in the basis.

dimension: Optional[List[str]], {‘1D’, ‘2D’}, default=None

Dimension of the basis to simulate. If ‘2D’, the basis is simulated as the tensor product of the one dimensional basis of functions by itself. The number of functions in the 2D basis will be \(n_function^2\).

argvals: Optional[Dict[str, npt.NDArray[np.float64]]]

The sampling points of the functional data. Each entry of the dictionary represents an input dimension. The shape of the \(j\) th dimension is \((m_j,)\) for \(0 \leq j \leq p\).

is_normalized: bool, default=False

Should we normalize the basis function?

**kwargs:
rchoice: Callable, default=np.random.choice

Method used to generate binomial distribution.

runif: Callable, default=np.random.uniform

Method used to generate uniform distribution.

degree: int, default=3

Degree of the B-splines. The default gives cubic splines.

Attributes:
dimension

Getter for dimension.

is_normalized

Getter for is_normalized.

n_dimension

Get the number of input dimension of the functional data.

n_functional

Get the number of functional data with self.

n_obs

Get the number of observations of the functional data.

n_points

Get the mean number of sampling points.

name

Getter for name.

simulation_type

Getter for simulation_type.

Methods

append(item)

Add an item to self.

center([mean, smooth])

Center the data.

clear()

Remove all items from the list.

concatenate(*fdata)

Concatenate MultivariateFunctionalData objects.

count(value)

covariance([points, smooth])

Compute an estimate of the covariance.

extend(other)

Extend the list of FunctionalData by appending from iterable.

index(value, [start, [stop]])

Raises ValueError if the value is not present.

inner_product([method, smooth, noise_variance])

Compute the inner product matrix of the data.

insert(i, item)

Insert an item item at a given position i.

mean([points, smooth])

Compute an estimate of the mean.

noise_variance([order])

Estimate the variance of the noise.

norm([squared, method, use_argvals_stand])

Norm of each observation of the data.

normalize([weights, method, use_argvals_stand])

Normalize the data.

pop([i])

Remove the item at the given position in the list, and return it.

remove(item)

Remove the first item from self where value is item.

reverse()

Reserve the elements of the list in place.

smooth([points, kernel_name, bandwidth, degree])

Smooth the data.

to_long()

Convert the data to long format.

copy

sort

property simulation_type: str#

Getter for simulation_type.

property name: str | List[str]#

Getter for name.

property is_normalized: bool#

Getter for is_normalized.

property dimension: List[str]#

Getter for dimension.