Table of Contents

Class SpectralAnalysisModel<T>

Namespace
AiDotNet.TimeSeries
Assembly
AiDotNet.dll

Implements spectral analysis for time series data, which transforms time domain signals into the frequency domain.

public class SpectralAnalysisModel<T> : TimeSeriesModelBase<T>, ITimeSeriesModel<T>, IFullModel<T, Matrix<T>, Vector<T>>, IModel<Matrix<T>, Vector<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Matrix<T>, Vector<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Matrix<T>, Vector<T>>>, IGradientComputable<T, Matrix<T>, Vector<T>>, IJitCompilable<T>

Type Parameters

T

The numeric data type used for calculations (e.g., float, double).

Inheritance
SpectralAnalysisModel<T>
Implements
IFullModel<T, Matrix<T>, Vector<T>>
IModel<Matrix<T>, Vector<T>, ModelMetadata<T>>
IParameterizable<T, Matrix<T>, Vector<T>>
ICloneable<IFullModel<T, Matrix<T>, Vector<T>>>
IGradientComputable<T, Matrix<T>, Vector<T>>
Inherited Members
Extension Methods

Remarks

Spectral analysis is a technique used to analyze the frequency content of time series data. It helps identify periodic patterns and dominant frequencies in the data by transforming it from the time domain to the frequency domain.

For Beginners: Spectral analysis is like breaking down a song into its individual notes. Just as a song is made up of different notes played at different times, time series data can contain different patterns that repeat at different frequencies.

For example, if you analyze temperature data over several years, spectral analysis might reveal:

  • A strong yearly cycle (frequency = 1/365 days) due to seasonal changes
  • A daily cycle (frequency = 1/24 hours) due to day/night temperature differences
  • Other cycles you might not notice just by looking at the raw data

This model uses the Fast Fourier Transform (FFT) algorithm to convert time data into frequency information, showing you how strong each frequency component is in your data.

Constructors

SpectralAnalysisModel(SpectralAnalysisOptions<T>?)

Initializes a new instance of the SpectralAnalysisModel class with optional configuration options.

public SpectralAnalysisModel(SpectralAnalysisOptions<T>? options = null)

Parameters

options SpectralAnalysisOptions<T>

The configuration options for spectral analysis. If null, default options are used.

Properties

SupportsJitCompilation

Gets whether this model supports JIT compilation.

public override bool SupportsJitCompilation { get; }

Property Value

bool

Returns true when the model has computed a periodogram. Spectral analysis prediction simply returns the precomputed periodogram, which can be efficiently exported as a constant tensor.

Remarks

For Beginners: JIT compilation for spectral analysis is straightforward because the "prediction" is just the precomputed periodogram. The FFT analysis is done during training, and prediction just returns the result.

Methods

CreateInstance()

Creates a new instance of the SpectralAnalysisModel with the same options.

protected override IFullModel<T, Matrix<T>, Vector<T>> CreateInstance()

Returns

IFullModel<T, Matrix<T>, Vector<T>>

A new instance of the SpectralAnalysisModel.

Remarks

For Beginners: This method creates a fresh copy of the spectral analysis model with the same configuration settings but no trained data. It's useful when you want to: - Analyze a different dataset with the same settings - Create multiple models with similar configurations - Reset the model to start fresh with the same options

DeserializeCore(BinaryReader)

Deserializes the model's core parameters from a binary reader.

protected override void DeserializeCore(BinaryReader reader)

Parameters

reader BinaryReader

The binary reader to read from.

Remarks

For Beginners: Deserialization is the process of loading a previously saved model from disk. This method reads the model's parameters from a file and reconstructs the model exactly as it was when it was saved.

This allows you to train a model once and then use it many times without retraining.

EvaluateModel(Matrix<T>, Vector<T>)

Evaluates the performance of the trained model on test data.

public override Dictionary<string, T> EvaluateModel(Matrix<T> xTest, Vector<T> yTest)

Parameters

xTest Matrix<T>

The input features matrix for testing (not used directly).

yTest Vector<T>

The actual time series data for testing.

Returns

Dictionary<string, T>

A dictionary containing evaluation metrics.

Remarks

For Beginners: This method compares the spectral analysis of your training data with the spectral analysis of your test data to see how similar they are. It calculates several metrics:

  • MSE (Mean Squared Error): The average squared difference between the training and test periodograms
  • RMSE (Root Mean Squared Error): The square root of MSE, giving an error in the same units
  • MAE (Mean Absolute Error): The average absolute difference between the periodograms
  • R2 (R-squared): How well the training periodogram explains the variation in the test periodogram
  • Peak Frequency Difference: The difference between the dominant frequencies in both datasets

Lower values for MSE, RMSE, MAE, and Peak Frequency Difference indicate better model performance. Higher values for R2 (closer to 1) indicate better model performance.

ExportComputationGraph(List<ComputationNode<T>>)

Exports the Spectral Analysis Model as a computation graph for JIT compilation.

public override ComputationNode<T> ExportComputationGraph(List<ComputationNode<T>> inputNodes)

Parameters

inputNodes List<ComputationNode<T>>

A list to which input nodes will be added (not used for spectral analysis).

Returns

ComputationNode<T>

The output computation node containing the periodogram.

Remarks

Since spectral analysis doesn't make traditional predictions but returns the computed periodogram, the computation graph simply returns the precomputed periodogram as a constant.

For Beginners: Unlike other models that compute predictions from input, spectral analysis just returns the frequency content it found in your data during training. The JIT-compiled version returns this precomputed result efficiently.

GetFrequencies()

Gets the frequency values corresponding to each point in the periodogram.

public Vector<T> GetFrequencies()

Returns

Vector<T>

A vector of frequency values.

Remarks

For Beginners: This method returns the frequencies that were analyzed in your data. Each value represents a frequency (how often a pattern repeats). For example, a frequency of 0.5 means the pattern repeats every 2 time units.

GetModelMetadata()

Gets metadata about the model, including its type, parameters, and characteristics of the spectral analysis.

public override ModelMetadata<T> GetModelMetadata()

Returns

ModelMetadata<T>

A ModelMetaData object containing information about the model.

Remarks

For Beginners: This method provides a summary of the spectral analysis, including: - The configuration settings used (FFT size, window function type, etc.) - Statistics about the frequency analysis (dominant frequencies, frequency range, etc.) - Overall metrics that describe the spectrum

This information is useful for comparing different analyses, documenting your process, or sharing your findings with others.

GetPeriodogram()

Gets the periodogram (power spectral density) values for each frequency.

public Vector<T> GetPeriodogram()

Returns

Vector<T>

A vector of periodogram values.

Remarks

For Beginners: This method returns the "strength" of each frequency in your data. Higher values mean that frequency is more prominent in your data. By looking at which frequencies have the highest values, you can identify the most important repeating patterns in your time series.

Predict(Matrix<T>)

Returns the periodogram (power spectral density) as the prediction result.

public override Vector<T> Predict(Matrix<T> input)

Parameters

input Matrix<T>

The input features matrix (not used in spectral analysis).

Returns

Vector<T>

The periodogram (power spectral density) of the trained model.

Remarks

For Beginners: Unlike most prediction models, spectral analysis doesn't predict future values. Instead, it analyzes the frequency content of your data. This method simply returns the periodogram (the strength of each frequency component) that was calculated during training.

PredictSingle(Vector<T>)

Predicts a single value based on the dominant frequency component.

public override T PredictSingle(Vector<T> input)

Parameters

input Vector<T>

The input vector containing a time index.

Returns

T

A predicted value based on the dominant frequency component.

Remarks

For Beginners: Unlike traditional forecasting models, spectral analysis doesn't really predict future values. Instead, it identifies patterns in the data. This method tries to provide a prediction by: 1. Finding the strongest frequency component in your data 2. Using that frequency to generate a value at the time index you specified

This is primarily useful for synthetic data generation or simplified predictions based on the dominant cycle in your data.

SerializeCore(BinaryWriter)

Serializes the model's core parameters to a binary writer.

protected override void SerializeCore(BinaryWriter writer)

Parameters

writer BinaryWriter

The binary writer to write to.

Remarks

For Beginners: Serialization is the process of converting the model's state into a format that can be saved to disk. This allows you to save a trained model and load it later without having to retrain it.

This method saves:

  • The spectral analysis options (like FFT size and window function settings)
  • The calculated frequencies
  • The calculated periodogram (power spectral density)

TrainCore(Matrix<T>, Vector<T>)

Core implementation of the training logic for the spectral analysis model.

protected override void TrainCore(Matrix<T> x, Vector<T> y)

Parameters

x Matrix<T>

The input features matrix (not used in spectral analysis).

y Vector<T>

The time series data to analyze.

Remarks

For Beginners: This method contains the actual implementation of the spectral analysis process. It takes your time series data and breaks it down into its frequency components, similar to how a prism breaks light into different colors. This helps identify which patterns (frequencies) are most prominent in your data.