### Install MOGPTK Source: https://games-uchile.github.io/mogptk/index.html Command to install the MOGPTK package via pip. ```bash pip install mogptk ``` -------------------------------- ### SM_LMC Training Output Example Source: https://games-uchile.github.io/mogptk/examples.html?q=example_currency_exchange Example output from training an SM_LMC model, showing optimization progress and duration. ```text Trial 1 of 3 Starting optimization using Adam ‣ Model: Exact ‣ Kernel: LinearModelOfCoregionalizationKernel['SpectralKernel', 'SpectralKernel', 'SpectralKernel'] ‣ Likelihood: GaussianLikelihood ‣ Channels: 10 ‣ Parameters: 46 ‣ Training points: 1079 ‣ Iterations: 1000 0/1000 0:00:14 loss= 589.172 (warmup) 2/1000 0:01:52 loss= 565.024 68/1000 0:02:00 loss= 214.408 170/1000 0:02:10 loss= 124.878 272/1000 0:02:20 loss= 109.457 375/1000 0:02:30 loss= 105.879 477/1000 0:02:40 loss= 104.072 577/1000 0:02:50 loss= 103.038 677/1000 0:03:00 loss= 102.352 777/1000 0:03:10 loss= 101.886 878/1000 0:03:20 loss= 101.505 980/1000 0:03:30 loss= 101.196 1000/1000 0:03:32 loss= 101.139 Optimization finished in 3 minutes 32 seconds ================================================== Trial 2 of 3 Starting optimization using Adam ‣ Model: Exact ‣ Kernel: LinearModelOfCoregionalizationKernel['SpectralKernel', 'SpectralKernel', 'SpectralKernel'] ‣ Likelihood: GaussianLikelihood ‣ Channels: 10 ‣ Parameters: 46 ‣ Training points: 1089 ‣ Iterations: 1000 0/1000 0:00:15 loss= 608.623 (warmup) 2/1000 0:01:53 loss= 587.874 67/1000 0:02:00 loss= 226.184 163/1000 0:02:10 loss= 145.649 260/1000 0:02:20 loss= 94.631 357/1000 0:02:30 loss= -20.3752 453/1000 0:02:40 loss= -45.6881 551/1000 0:02:50 loss= -47.8884 649/1000 0:03:00 loss= -48.8847 745/1000 0:03:10 loss= -50.3853 841/1000 0:03:20 loss= -103.072 938/1000 0:03:30 loss= -115.846 1000/1000 0:03:36 loss= -117.359 Optimization finished in 3 minutes 36 seconds ================================================== Trial 3 of 3 Starting optimization using Adam ‣ Model: Exact ‣ Kernel: LinearModelOfCoregionalizationKernel['SpectralKernel', 'SpectralKernel', 'SpectralKernel'] ‣ Likelihood: GaussianLikelihood ‣ Channels: 10 ‣ Parameters: 46 ‣ Training points: 1092 ‣ Iterations: 1000 0/1000 0:00:14 loss= 593.533 (warmup) 2/1000 0:01:52 loss= 572.904 64/1000 0:02:00 loss= 218.689 150/1000 0:02:10 loss= 145.44 238/1000 0:02:20 loss= 121.193 323/1000 0:02:30 loss= 116.266 408/1000 0:02:40 loss= 114.072 493/1000 0:02:50 loss= 112.8 577/1000 0:03:00 loss= 111.967 662/1000 0:03:10 loss= 111.359 745/1000 0:03:20 loss= 110.914 828/1000 0:03:30 loss= 110.567 922/1000 0:03:40 loss= 110.265 1000/1000 0:03:48 loss= 110.071 Optimization finished in 3 minutes 48 seconds ================================================== ``` -------------------------------- ### Train the Model Source: https://games-uchile.github.io/mogptk/model.html Examples for training the model using different optimizers and parameters. ```python >>> model.train() ``` ```python >>> model.train(method='lbfgs', tolerance_grad=1e-10, tolerance_change=1e-12) ``` ```python >>> model.train(method='adam', lr=0.5) ``` -------------------------------- ### Initialize Environment Source: https://games-uchile.github.io/mogptk/examples.html?q=06_Custom_Kernels_and_Mean_Functions Import necessary libraries and set the random seed for reproducibility. ```python import mogptk import torch import numpy as np torch.manual_seed(1); ``` -------------------------------- ### Initialize SM Model and Train Source: https://games-uchile.github.io/mogptk/models/sm.html Demonstrates how to create an SM model, initialize its parameters, train it, and make predictions. Requires a DataSet object. ```python import numpy as np import mogptk t = np.linspace(0, 10, 100) y = np.sin(0.5 * t) data = mogptk.Data(t, y) model = mogptk.SM(data, Q=1) model.init_parameters() model.train() model.predict() data.plot() ``` -------------------------------- ### Initialize MOGPTK Environment Source: https://games-uchile.github.io/mogptk/examples.html?q=01_Data_Loading Import necessary libraries and set the random seed for reproducibility. ```python import mogptk import torch import pandas as pd torch.manual_seed(1); ``` -------------------------------- ### Example Usage of LoadSplitData Source: https://games-uchile.github.io/mogptk/data.html This example demonstrates how to call the `LoadSplitData` function with sample training and testing data, assigning a name to the dataset. ```python >>> LoadSplitData(x_train, x_test, y_train, y_test, name='Sine wave') ``` -------------------------------- ### Import Dependencies Source: https://games-uchile.github.io/mogptk/examples.html?q=08_Multi_Likelihood_Classification Initializes the environment with necessary libraries for data generation and MOGPTK modeling. ```python import numpy as np import torch import mogptk from sklearn.datasets import make_classification, make_regression torch.manual_seed(1); ``` -------------------------------- ### Filter Data by Range Source: https://games-uchile.github.io/mogptk/data.html Filters the dataset to include only observations within a specified start and end range. Can be used with numerical or date-based ranges. Ensure the input format for start and end matches the data's X-axis. ```python def filter(self, start, end, dim=None): """ Filter the data range to be between `start` and `end` in the X axis. Args: start (float, str, list): Start of interval. end (float, str, list): End of interval (not included). dim (int): Input dimension to apply to, if not specified applies to all input dimensions. Examples: >>> data.filter(3, 8) >>> data.filter('2016-01-15', '2016-06-15') """ start = self._normalize_x_val(start) end = self._normalize_x_val(end) if dim is not None: ind = np.logical_and(self.X[:,dim] >= start[dim], self.X[:,dim] < end[dim]) else: ind = np.logical_and(self.X[:,0] >= start[0], self.X[:,0] < end[0]) for i in range(1,self.get_input_dims()): ind = np.logical_and(ind, np.logical_and(self.X[:,i] >= start[i], self.X[:,i] < end[i])) self.X = self.X[ind,:] self.Y = self.Y[ind] if self.Y_err is not None: self.Y_err = self.Y_err[ind] self.mask = self.mask[ind] ``` -------------------------------- ### Initialize MOGPTK model Source: https://games-uchile.github.io/mogptk/examples.html?q=00_Quick_Start Create a model instance with a specified number of components. ```python # create model, uncomment for different kernels model = mogptk.MOSM(dataset, Q=2) # model = mogptk.CSM(dataset, Q=2) # model = mogptk.SM_LMC(dataset, Q=2) # model = mogptk.CONV(dataset, Q=2) ``` -------------------------------- ### GET /get_prediction_data Source: https://games-uchile.github.io/mogptk/data.html Returns the prediction points. ```APIDOC ## GET /get_prediction_data ### Description Returns the prediction points. ### Response #### Success Response (200) - **X_pred** (numpy.ndarray) - X prediction of shape (data_points,input_dims). ``` -------------------------------- ### GET /get_name Source: https://games-uchile.github.io/mogptk/data.html Returns the name of the channel. ```APIDOC ## GET /get_name ### Description Return the name of the channel. ### Response #### Success Response (200) - **name** (str) - The name of the channel. ``` -------------------------------- ### Initialize and Train SM Model Source: https://games-uchile.github.io/mogptk/models/sm.html Demonstrates the basic workflow of creating a dataset, initializing an SM model, training it, and generating predictions. ```python >>> import numpy as np >>> import mogptk >>> >>> t = np.linspace(0, 10, 100) >>> y = np.sin(0.5 * t) >>> >>> data = mogptk.Data(t, y) >>> model = mogptk.SM(data, Q=1) >>> model.init_parameters() >>> model.train() >>> model.predict() >>> data.plot() ``` -------------------------------- ### Importing Dependencies Source: https://games-uchile.github.io/mogptk/examples.html?q=example_eeg Initializes the environment with necessary libraries and sets a random seed for reproducibility. ```python import numpy as np import pandas as pd import torch import mogptk torch.manual_seed(1); ``` -------------------------------- ### Filter Data by Range Source: https://games-uchile.github.io/mogptk/data.html Filters the dataset to include only data points within a specified start and end range on the X-axis. Can apply to a specific input dimension or all dimensions. Accepts numerical or string date formats for start and end. ```python data.filter(3, 8) ``` ```python data.filter('2016-01-15', '2016-06-15') ``` -------------------------------- ### GET /get_train_data Source: https://games-uchile.github.io/mogptk/data.html Returns the observations used for training. ```APIDOC ## GET /get_train_data ### Description Returns the observations used for training. ### Parameters #### Query Parameters - **float64** (boolean) - Optional - Return as float64s. - **transformed** (boolean) - Optional - Return transformed data. ### Response #### Success Response (200) - **X** (numpy.ndarray) - X data of shape (data_points,input_dims). - **Y** (numpy.ndarray) - Y data of shape (data_points,). ``` -------------------------------- ### Initialize SM Parameters Source: https://games-uchile.github.io/mogptk/models/sm.html Method to estimate kernel parameters using IPS, LS, or BNSE estimation strategies. ```python def init_parameters(self, method='LS', iters=500): """ Estimate kernel parameters from the data set. The initialization can be done using three methods: - BNSE estimates the PSD via Bayesian non-parametris spectral estimation (Tobar 2018) and then selecting the greater Q peaks in the estimated spectrum, and use the peak's position, magnitude and width to initialize the mean, magnitude and variance of the kernel respectively. - LS is similar to BNSE but uses Lomb-Scargle to estimate the spectrum, which is much faster but may give poorer results. - IPS uses independent parameter sampling from the PhD thesis of Andrew Wilson 2014. It takes the inverse of the lengthscales drawn from a truncated Gaussian Normal(0, max_dist^2), the means drawn from a Unif(0, 0.5 / minimum distance between two points), and the mixture weights by taking the standard variation of the Y values divided by the number of mixtures. In all cases the noise is initialized with 1/30 of the variance of each channel. Args: method (str): Method of estimation, such as IPS, LS, or BNSE. iters (str): Number of iterations for initialization. """ input_dims = self.dataset.get_input_dims() output_dims = self.dataset.get_output_dims() if method.lower() not in ['ips', 'ls', 'bnse']: raise ValueError("valid methods of estimation are IPS, LS, and BNSE") if method.lower() == 'ips': for j in range(output_dims): nyquist = self.dataset[j].get_nyquist_estimation() x = self.dataset[j].X[self.dataset[j].mask,:] y = self.dataset[j].Y_transformer.forward(self.dataset[j].Y[self.dataset[j].mask], x) x_range = np.max(x, axis=0) - np.min(x, axis=0) weights = [2.0*y.std()/self.Q] * self.Q means = nyquist * torch.rand(self.Q, input_dims[j]) variances = 1.0 / (torch.abs(torch.randn(self.Q, input_dims[j])) * x_range) self.gpr.kernel[j].magnitude.assign(weights) self.gpr.kernel[j].mean.assign(means) self.gpr.kernel[j].variance.assign(variances) return elif method.lower() == 'ls': amplitudes, means, variances = self.dataset.get_ls_estimation(self.Q) if len(amplitudes) == 0: logger.warning('LS could not find peaks for SM') return ``` -------------------------------- ### GET /model/num_parameters Source: https://games-uchile.github.io/mogptk/model.html Returns the count of trainable parameters. ```APIDOC ## GET /model/num_parameters ### Description Returns the number of trainable parameters in the model. ### Response #### Success Response (200) - **count** (int) - Number of parameters. ``` -------------------------------- ### POST /model/sample Source: https://games-uchile.github.io/mogptk/model.html Generates samples from the kernel at input X. ```APIDOC ## POST /model/sample ### Description Draws samples from the kernel at input X. Can sample from the prior or posterior distribution. ### Parameters #### Request Body - **X** (list, dict) - Optional - Input data for sampling. - **n** (int) - Optional - Number of samples to generate. - **prior** (boolean) - Optional - If true, samples from the prior instead of the posterior. - **transformed** (boolean) - Optional - Whether to return transformed data. ### Response #### Success Response (200) - **samples** (numpy.ndarray, list) - Samples for each channel. ``` -------------------------------- ### Initialize and Train a CONV Model Source: https://games-uchile.github.io/mogptk/models/conv.html Demonstrates the standard workflow for creating a dataset, initializing a CONV model, training it, and generating predictions. ```python >>> import numpy as np >>> import mogptk >>> >>> t = np.linspace(0, 10, 100) >>> y1 = np.sin(0.5 * t) >>> y2 = 2.0 * np.sin(0.2 * t) >>> >>> dataset = mogptk.DataSet(t, [y1, y2]) >>> model = mogptk.CONV(dataset, Q=2) >>> model.init_parameters() >>> model.train() >>> model.predict() >>> dataset.plot() ``` -------------------------------- ### GET /model/loss Source: https://games-uchile.github.io/mogptk/model.html Returns the current loss of the model. ```APIDOC ## GET /model/loss ### Description Returns the loss of the kernel and its data and parameters. ### Response #### Success Response (200) - **loss** (float) - The current loss value. ``` -------------------------------- ### Initialize Model and Kernel Source: https://games-uchile.github.io/mogptk/examples.html?q=06_Custom_Kernels_and_Mean_Functions Set up the model with the custom mean function and an IndependentMultiOutputKernel, then assign initial values to kernel parameters. ```python mean = Mean() kernel = mogptk.gpr.PeriodicKernel(input_dims=1) mo_kernel = mogptk.gpr.IndependentMultiOutputKernel(kernel) model = mogptk.Model(data, mo_kernel, mean=mean, name="Periodic") # initialize kernel parameters to reasonable values kernel.lengthscale.assign(1.0) kernel.period.assign(1.0) ``` -------------------------------- ### Get All Data Source: https://games-uchile.github.io/mogptk/data.html Returns all observations, both training and testing. ```APIDOC ## Get All Data ### Description Returns all observations, train and test. ### Method `get_data` ### Parameters #### Query Parameters - **transformed** (boolean) - Optional - Return transformed data. ### Returns - **numpy.ndarray**: X data of shape (data_points, input_dims). - **numpy.ndarray**: Y data of shape (data_points,). ### Examples ```python x, y = data.get_data() ``` ``` -------------------------------- ### Get Channel Name Source: https://games-uchile.github.io/mogptk/data.html Retrieves the name of the channel. ```APIDOC ## Get Channel Name ### Description Returns the name of the channel. ### Method `get_name` ### Returns - **str**: The name of the channel. ### Examples ```python data.get_name() 'A' ``` ``` -------------------------------- ### Initialize and Train CONV Model Source: https://games-uchile.github.io/mogptk/models/conv.html Demonstrates initializing a CONV model with a dataset, initializing its parameters, training it, and making predictions. Requires numpy and mogptk imports. ```python import numpy as np import mogptk t = np.linspace(0, 10, 100) y1 = np.sin(0.5 * t) y2 = 2.0 * np.sin(0.2 * t) dataset = mogptk.DataSet(t, [y1, y2]) model = mogptk.CONV(dataset, Q=2) model.init_parameters() model.train() model.predict() dataset.plot() ``` -------------------------------- ### GET /dataset/names Source: https://games-uchile.github.io/mogptk/dataset.html Retrieves the names of the channels in the dataset. ```APIDOC ## GET /dataset/names ### Description Return the names of the channels. ### Response #### Success Response (200) - **names** (list) - List of channel names. ``` -------------------------------- ### SM Model Initialization and Usage Source: https://games-uchile.github.io/mogptk/models/sm.html Demonstrates how to initialize and use the SM model with a DataSet, including parameter initialization and training. ```APIDOC ## Class: SM ### Description Independent Spectral Mixture kernels per channel. The spectral mixture kernel is proposed by [1]. The parameters will be randomly instantiated, use `init_parameters()` to initialize the parameters to reasonable values for the current data set. ### Args - **dataset** (mogptk.dataset.DataSet): `DataSet` object of data for all channels. - **Q** (int): Number of components. - **inference**: Gaussian process inference model to use, such as `mogptk.Exact`. - **mean** (mogptk.gpr.mean.Mean): The mean class. - **name** (str): Name of the model. ### Attributes - **dataset**: The associated mogptk.dataset.DataSet. - **gpr**: The mogptk.gpr.model.Model. - **times** (numpy.ndarray): Training times of shape (iters,). - **losses** (numpy.ndarray): Losses of shape (iters,). - **errors** (numpy.ndarray): Errors of shape (iters,). ### Examples ```python import numpy as np import mogptk t = np.linspace(0, 10, 100) y = np.sin(0.5 * t) data = mogptk.Data(t, y) model = mogptk.SM(data, Q=1) model.init_parameters() model.train() model.predict() data.plot() ``` [1] A.G. Wilson and R.P. Adams, "Gaussian Process Kernels for Pattern Discovery and Extrapolation", International Conference on Machine Learning 30, 2013 ## Method: init_parameters ### Description Estimate kernel parameters from the data set. The initialization can be done using three methods: - BNSE estimates the PSD via Bayesian non-parametris spectral estimation (Tobar 2018) and then selecting the greater Q peaks in the estimated spectrum, and use the peak's position, magnitude and width to initialize the mean, magnitude and variance of the kernel respectively. - LS is similar to BNSE but uses Lomb-Scargle to estimate the spectrum, which is much faster but may give poorer results. - IPS uses independent parameter sampling from the PhD thesis of Andrew Wilson 2014. It takes the inverse of the lengthscales drawn from a truncated Gaussian Normal(0, max_dist^2), the means drawn from a Unif(0, 0.5 / minimum distance between two points), and the mixture weights by taking the standard variation of the Y values divided by the number of mixtures. In all cases the noise is initialized with 1/30 of the variance of each channel. ### Args - **method** (str): Method of estimation, such as IPS, LS, or BNSE. - **iters** (str): Number of iterations for initialization. ### Raises - **ValueError**: If the method is not one of 'IPS', 'LS', or 'BNSE'. ### Returns - None: If LS method cannot find peaks. ``` -------------------------------- ### GET /get_input_dims Source: https://games-uchile.github.io/mogptk/data.html Returns the number of input dimensions for the data. ```APIDOC ## GET /get_input_dims ### Description Returns the number of input dimensions. ### Returns - **int** - Number of input dimensions. ``` -------------------------------- ### Initialize MultiOutputHarmonizableSpectralKernel Source: https://games-uchile.github.io/mogptk/gpr/multioutput.html Initializes the MultiOutputHarmonizableSpectralKernel with specified dimensions and parameters. It sets up internal parameters like weight, mean, variance, lengthscale, center, delay, and phase. ```python class MultiOutputHarmonizableSpectralKernel(MultiOutputKernel): """ Multi-output harmonizable spectral kernel (MOHSM) where each channel and cross-channel is modelled with a spectral kernel as proposed by [1]. You can add the mixture kernel with `MixtureKernel(MultiOutputHarmonizableSpectralKernel(...), Q=3)`. $$ K_{ij}(x,x') = \alpha_{ij} \exp\left(-\frac{1}{2}(\tau+\theta_{ij})^T\Sigma_{ij}(\tau+\theta_{ij})\right) \cos((\tau+\theta_{ij})^T\mu_{ij} + \phi) \exp\left(-\frac{l_{ij}}{2}|\bar{x}-c|\right) $$ \alpha_{ij} = w_{ij}(2\pi)^n\sqrt{\left(|\Sigma_{ij}|\right)} $$ w_{ij} = w_iw_j\exp\left(-\frac{1}{4}(\mu_i-\mu_j)^T(\Sigma_i+\Sigma_j)^{-1}(\mu_i-\mu_j)\right) $$ \mu_{ij} = (\Sigma_i+\Sigma_j)^{-1}(\Sigma_i\mu_j + \Sigma_j\mu_i) $$ \Sigma_{ij} = 2\Sigma_i(\Sigma_i+\Sigma_j)^{-1}\Sigma_j with \(\theta_{ij} = \theta_i-\theta_j\), \(\phi_{ij} = \phi_i - \phi_j\), \(\tau = |x-x'|\), \(\bar{x} = |x+x'|/2\), \(w\) the weight, \(\mu\) the mean, \(\Sigma\) the variance, \(l\) the lengthscale, \(c\) the center, \(\theta\) the delay, and \(\phi\) the phase. Args: output_dims (int): Number of output dimensions. input_dims (int): Number of input dimensions. active_dims (list of int): Indices of active dimensions of shape (input_dims,). Attributes: weight (mogptk.gpr.parameter.Parameter): Weight \(w\) of shape (output_dims,). mean (mogptk.gpr.parameter.Parameter): Mean \(\mu\) of shape (output_dims,input_dims). variance (mogptk.gpr.parameter.Parameter): Variance \(\Sigma\) of shape (output_dims,input_dims). lengthscale (mogptk.gpr.parameter.Parameter): Lengthscale \(l\) of shape (output_dims,). center (mogptk.gpr.parameter.Parameter): Center \(c\) of shape (input_dims,). delay (mogptk.gpr.parameter.Parameter): Delay \(\theta\) of shape (output_dims,input_dims). phase (mogptk.gpr.parameter.Parameter): Phase \(\phi\) in hertz of shape (output_dims,). [1] M. Altamirano, "Nonstationary Multi-Output Gaussian Processes via Harmonizable Spectral Mixtures, 2021 """ def __init__(self, output_dims, input_dims=1, active_dims=None): super().__init__(output_dims, input_dims, active_dims) # TODO: incorporate mixtures? # TODO: allow different input_dims per channel weight = torch.ones(output_dims) mean = torch.zeros(output_dims, input_dims) variance = torch.ones(output_dims, input_dims) lengthscale = torch.ones(output_dims) center = torch.zeros(input_dims) delay = torch.zeros(output_dims, input_dims) phase = torch.zeros(output_dims) self.weight = Parameter(weight, lower=config.positive_minimum) self.mean = Parameter(mean, lower=config.positive_minimum) self.variance = Parameter(variance, lower=config.positive_minimum) self.lengthscale = Parameter(lengthscale, lower=config.positive_minimum) self.center = Parameter(center) self.delay = Parameter(delay) self.phase = Parameter(phase) if output_dims == 1: self.delay.train = False self.phase.train = False self.twopi = np.power(2.0*np.pi, float(self.input_dims)) ``` -------------------------------- ### Get Data Source: https://games-uchile.github.io/mogptk/data.html Retrieves all observations, both training and testing data. ```APIDOC ## GET /get_data ### Description Returns all observations, train and test. ### Method GET ### Endpoint /get_data ### Parameters #### Query Parameters - **transformed** (boolean) - Optional - Return transformed data. Defaults to False. ### Request Example ```json { "transformed": true } ``` ### Response #### Success Response (200) - **X** (numpy.ndarray) - X data of shape (data_points, input_dims). - **Y** (numpy.ndarray) - Y data of shape (data_points,). #### Response Example ```json { "X": [[...], [...]], "Y": [...] } ``` ``` -------------------------------- ### GET /model/predict_y Source: https://games-uchile.github.io/mogptk/gpr/model.html Retrieves the predictive posterior for values of y. ```APIDOC ## GET /model/predict_y ### Description Get the predictive posterior for values of y. ### Parameters #### Query Parameters - **X** (torch.tensor) - Required - Input of shape (data_points, input_dims). - **ci** (list of float) - Optional - Confidence interval [lower, upper]. - **sigma** (float) - Optional - Number of standard deviations. - **n** (int) - Optional - Number of samples for quantile estimation. ### Response #### Success Response (200) - **mean** (torch.tensor) - Mean of the predictive posterior. ``` -------------------------------- ### Import necessary libraries Source: https://games-uchile.github.io/mogptk/examples.html?q=example_mauna_loa Imports the required libraries for the example: mogptk, numpy, and torch. Sets a manual seed for reproducibility. ```python import mogptk import numpy as np import torch torch.manual_seed(1); ``` -------------------------------- ### Kernel Initialization Source: https://games-uchile.github.io/mogptk/gpr/multioutput.html Constructor for the MultiOutputHarmonizableSpectralKernel, setting up parameters like weight, mean, variance, and lengthscale. ```python def __init__(self, output_dims, input_dims=1, active_dims=None): super().__init__(output_dims, input_dims, active_dims) # TODO: incorporate mixtures? # TODO: allow different input_dims per channel weight = torch.ones(output_dims) mean = torch.zeros(output_dims, input_dims) variance = torch.ones(output_dims, input_dims) lengthscale = torch.ones(output_dims) center = torch.zeros(input_dims) delay = torch.zeros(output_dims, input_dims) phase = torch.zeros(output_dims) self.weight = Parameter(weight, lower=config.positive_minimum) self.mean = Parameter(mean, lower=config.positive_minimum) self.variance = Parameter(variance, lower=config.positive_minimum) self.lengthscale = Parameter(lengthscale, lower=config.positive_minimum) self.center = Parameter(center) self.delay = Parameter(delay) self.phase = Parameter(phase) if output_dims == 1: self.delay.train = False self.phase.train = False self.twopi = np.power(2.0*np.pi, float(self.input_dims)) ``` -------------------------------- ### GET /model/predict_f Source: https://games-uchile.github.io/mogptk/gpr/model.html Retrieves the predictive posterior for values of f. ```APIDOC ## GET /model/predict_f ### Description Get the predictive posterior for values of f. ### Parameters #### Query Parameters - **X** (torch.tensor) - Required - Input of shape (data_points, input_dims). - **full** (bool) - Optional - Return the full variance matrix if true. ### Response #### Success Response (200) - **mean** (torch.tensor) - Mean of the predictive posterior. - **variance** (torch.tensor) - Variance of the predictive posterior. ``` -------------------------------- ### CONV Class Initialization and Usage Source: https://games-uchile.github.io/mogptk/models/conv.html Demonstrates how to initialize and use the CONV class for multi-output Gaussian process modeling. ```APIDOC ## CONV Class CONV is the Convolutional Gaussian kernel with `Q` components. ### Description Initializes a CONV model, which represents a Convolutional Gaussian kernel. The parameters are randomly instantiated and should be initialized using `init_parameters()`. ### Args - **dataset** (`DataSet`) - Required - `DataSet` object containing the data for all channels. - **Q** (`int`) - Optional - Defaults to 1. The number of components for the kernel. - **inference** - Optional - Gaussian process inference model to use (e.g., `mogptk.Exact`). - **mean** (`Mean`) - Optional - The mean class to be used. - **name** (`str`) - Optional - Defaults to 'CONV'. The name of the model. ### Attributes - **dataset** (`DataSet`) - The associated `mogptk.dataset.DataSet`. - **gpr** (`mogptk.gpr.model.Model`) - The underlying Gaussian Process Regression model. ### Example ```python import numpy as np import mogptk t = np.linspace(0, 10, 100) y1 = np.sin(0.5 * t) y2 = 2.0 * np.sin(0.2 * t) dataset = mogptk.DataSet(t, [y1, y2]) model = mogptk.CONV(dataset, Q=2) model.init_parameters() model.train() model.predict() dataset.plot() ``` ### Reference [1] M.A. Álvarez and N.D. Lawrence, "Sparse Convolved Multiple Output Gaussian Processes", Advances in Neural Information Processing Systems 21, 2009. ``` -------------------------------- ### GET /model/kernel Source: https://games-uchile.github.io/mogptk/gpr/model.html Evaluates the kernel matrix for given inputs. ```APIDOC ## GET /model/kernel ### Description Evaluates the kernel at X1 and X2 and returns the NumPy representation. ### Parameters #### Query Parameters - **X1** (torch.tensor) - Required - Input of shape (data_points1, input_dims). - **X2** (torch.tensor) - Optional - Input of shape (data_points2, input_dims). ### Response #### Success Response (200) - **kernel_matrix** (numpy.ndarray) - Kernel matrix of shape (data_points1, data_points2). ``` -------------------------------- ### Initialize and Train SM_LMC Model Source: https://games-uchile.github.io/mogptk/models/sm_lmc.html Example of creating a DataSet, initializing an SM_LMC model, training it, and making predictions. Ensure the DataSet is correctly configured with time and multiple output channels. ```python import numpy as np import mogptk t = np.linspace(0, 10, 100) y1 = np.sin(0.5 * t) y2 = 2.0 * np.sin(0.2 * t) dataset = mogptk.DataSet(t, [y1, y2]) model = mogptk.SM_LMC(dataset, Q=2) model.init_parameters() model.train() model.predict() dataset.plot() ``` -------------------------------- ### GET /model/log_prior Source: https://games-uchile.github.io/mogptk/gpr/model.html Calculates the log prior of the model parameters. ```APIDOC ## GET /model/log_prior ### Description Returns the log prior of the model parameters given by log p(θ). ### Response #### Success Response (200) - **log_prior** (torch.tensor) - The sum of the log priors of all model parameters. ``` -------------------------------- ### Apply Data Transformations Source: https://games-uchile.github.io/mogptk/examples.html?q=00_Quick_Start Demonstrates initializing a dataset, applying exponential transformations, and using the log-transformation for specific channels. ```python dataset = mogptk.DataSet() dataset.append(mogptk.Data(t, np.exp(y1), name='Signal 1')) dataset.append(mogptk.Data(t, np.exp(y2), name='Signal 2')) dataset.append(mogptk.Data(t, np.exp(y3), name='Signal 3')) dataset.append(mogptk.Data(t, y4, name='4')) for data in dataset: data.remove_randomly(pct=0.3) dataset[0].remove_range(start=2.0) # apply transformation for data in dataset[:-1]: data.transform(mogptk.TransformLog) ``` -------------------------------- ### GET /model/log_marginal_likelihood Source: https://games-uchile.github.io/mogptk/gpr/model.html Calculates the log marginal likelihood of the model. ```APIDOC ## GET /model/log_marginal_likelihood ### Description Returns the log marginal likelihood of the model given by log p(y). ### Response #### Success Response (200) - **log_marginal_likelihood** (torch.tensor) - The calculated log marginal likelihood. ``` -------------------------------- ### GET /dataset/prediction_data Source: https://games-uchile.github.io/mogptk/dataset.html Returns the prediction X range for all channels. ```APIDOC ## GET /dataset/prediction_data ### Description Returns the prediction X range for all channels. ### Response #### Success Response (200) - **x** (list) - X prediction of shape (data_points,input_dims) per channel. ``` -------------------------------- ### Initialize and Train CSM Model Source: https://games-uchile.github.io/mogptk/models/csm.html Demonstrates the workflow for creating a dataset, initializing a CSM model, and performing training and prediction. ```python >>> import numpy as np >>> import mogptk >>> >>> t = np.linspace(0, 10, 100) >>> y1 = np.sin(0.5 * t) >>> y2 = 2.0 * np.sin(0.2 * t) >>> >>> dataset = mogptk.DataSet(t, [y1, y2]) >>> model = mogptk.CSM(dataset, Q=2) >>> model.init_parameters() >>> model.train() >>> model.predict() >>> dataset.plot() ``` -------------------------------- ### GET /dataset/nyquist_estimation Source: https://games-uchile.github.io/mogptk/dataset.html Estimates the Nyquist frequency per channel. ```APIDOC ## GET /dataset/nyquist_estimation ### Description Estimate the Nyquist frequency by taking 0.5/(minimum distance of points) per channel. ### Response #### Success Response (200) - **freqs** (list) - Nyquist frequency array of shape (input_dims) per channel. ``` -------------------------------- ### MultiOutputHarmonizableSpectralKernel Initialization Source: https://games-uchile.github.io/mogptk/gpr/multioutput.html Initializes the MultiOutputHarmonizableSpectralKernel with specified output and input dimensions. ```APIDOC ## MultiOutputHarmonizableSpectralKernel.__init__ ### Description Initializes the internal Module state and sets up the kernel parameters including weight, mean, variance, lengthscale, center, delay, and phase. ### Parameters - **output_dims** (int) - Required - Number of output dimensions. - **input_dims** (int) - Optional - Number of input dimensions (default: 1). - **active_dims** (list of int) - Optional - Indices of active dimensions. ``` -------------------------------- ### Get All Data Source: https://games-uchile.github.io/mogptk/dataset.html Retrieves all observations (both training and testing) for all channels. ```APIDOC ## GET /dataset/get_data ### Description Retrieves all observations, including training and testing data, for all channels. ### Method GET ### Endpoint /dataset/get_data ### Parameters #### Query Parameters - **transformed** (boolean) - Optional - If true, returns transformed data. ### Response #### Success Response (200) - **X_data** (list) - A list of X data arrays, one for each channel. Shape: (data_points, input_dims). - **Y_data** (list) - A list of Y data arrays, one for each channel. Shape: (data_points,). #### Response Example ```json { "X_data": [[...], [...]], "Y_data": [[...], [...]] } ``` ``` -------------------------------- ### SM_LMC Model Initialization Source: https://games-uchile.github.io/mogptk/models/sm_lmc.html Demonstrates initializing the SM_LMC model with a dataset and specifying the number of components (Q) and subcomponents (Rq). The kernel parameters are then initialized using a specified method. ```python import torch import numpy as np from mogptk.dataset import DataSet from mogptk.model import Model, Exact, logger from mogptk.gpr import LinearModelOfCoregionalizationKernel, SpectralKernel, GaussianLikelihood class SM_LMC(Model): def __init__(self, dataset, Q=1, Rq=1, inference=Exact(), mean=None, name="SM-LMC"): if not isinstance(dataset, DataSet): dataset = DataSet(dataset) output_dims = dataset.get_output_dims() input_dims = dataset.get_input_dims()[0] for input_dim in dataset.get_input_dims()[1:]: if input_dim != input_dims: raise ValueError("input dimensions for all channels must match") spectral = [SpectralKernel(input_dims) for q in range(Q)] kernel = LinearModelOfCoregionalizationKernel(spectral, output_dims=output_dims, input_dims=input_dims, Q=Q, Rq=Rq) kernel.weight.assign(torch.rand(output_dims,Q,Rq)) for q in range(Q): kernel[q].magnitude.assign(torch.rand(1)) kernel[q].mean.assign(torch.rand(input_dims)) kernel[q].variance.assign(torch.rand(input_dims)) super().__init__(dataset, kernel, inference, mean, name) self.Q = Q self.Rq = Rq nyquist = np.amin(self.dataset.get_nyquist_estimation(), axis=0) for q in range(Q): self.gpr.kernel[q].magnitude.assign(1.0, train=False) # handled by LMCKernel self.gpr.kernel[q].mean.assign(upper=np.maximum(self.gpr.kernel[q].mean.lower.detach().cpu().numpy(), nyquist)) ``` -------------------------------- ### Configure MOGPTK settings Source: https://games-uchile.github.io/mogptk/gpr/config.html Initializes the global configuration object for device and precision settings. ```python import torch class Config: dtype = torch.float64 if torch.cuda.is_available(): device = torch.device('cuda', torch.cuda.current_device()) else: device = torch.device('cpu') positive_minimum = 1e-8 config = Config() def use_half_precision(): """ Use half precision (float16) for all tensors. This may be much faster on GPUs, but has reduced precision and may more often cause numerical instability. Only recommended on GPUs. """ if config.device.type == 'cpu': print('WARNING: half precision not recommended on CPU') config.dtype = torch.float16 def use_single_precision(): """ Use single precision (float32) for all tensors. This may be faster on GPUs, but has reduced precision and may more often cause numerical instability. """ config.dtype = torch.float32 def use_double_precision(): """ Use double precision (float64) for all tensors. This is the recommended precision for numerical stability, but can be significantly slower. """ config.dtype = torch.float64 def use_cpu(n=None): """ Use the CPU instead of the GPU for tensor calculations. This is the default if no GPU is available. If you have more than one CPU, you can use a specific CPU by setting `n`. """ if n is None: config.device = torch.device('cpu') else: config.device = torch.device('cpu', n) def use_gpu(n=None): """ Use the GPU instead of the CPU for tensor calculations. This is the default if a GPU is available. If you have more than one GPU, you can use a specific GPU by setting `n`. """ if not torch.cuda.is_available(): logger.error("CUDA is not available") elif n is not None and (not isinstance(n, int) or n < 0 or torch.cuda.device_count() <= n): logger.error("CUDA GPU '%s' is not available" % (n,)) elif n is None: config.device = torch.device('cuda', torch.cuda.current_device()) else: config.device = torch.device('cuda', n) def print_gpu_information(): """ Print information about whether CUDA is supported, and if so which GPU is being used. """ if not torch.cuda.is_available(): print("CUDA is not available") return print("CUDA is available:") current = None if config.device.type == 'cuda': current = config.device.index for n in range(torch.cuda.device_count()): print("%2d %s%s" % (n, torch.cuda.get_device_name(n), " (selected)" if n == current else "")) def set_positive_minimum(val): """ Set the positive minimum for kernel parameters. This is usually slightly larger than zero to avoid numerical instabilities. Default is at 1e-8. """ config.positive_minimum = val ``` -------------------------------- ### remove_range Source: https://games-uchile.github.io/mogptk/data.html Removes observations within a specified interval [start, end]. ```APIDOC ## remove_range ### Description Removes observations in the interval [start, end]. ### Parameters #### Request Body - **start** (float, str) - Optional - Start of interval (inclusive). Defaults to the first value in observations. - **end** (float, str) - Optional - End of interval (inclusive). Defaults to the last value in observations. - **dim** (int) - Optional - Input dimension to apply to, if not specified applies to all input dimensions. ``` -------------------------------- ### GET /model/parameters Source: https://games-uchile.github.io/mogptk/model.html Retrieves all parameters associated with the model's kernel. ```APIDOC ## GET /model/parameters ### Description Returns a list of all parameters of the kernel. ### Method GET ### Response #### Success Response (200) - **parameters** (list) - A list of mogptk.gpr.parameter.Parameter objects. ``` -------------------------------- ### Initialize and train a MOSM model Source: https://games-uchile.github.io/mogptk/models/mosm.html Demonstrates the standard workflow for creating a dataset, initializing a MOSM model, training it, and generating predictions. ```python import numpy as np import mogptk t = np.linspace(0, 10, 100) y1 = np.sin(0.5 * t) y2 = 2.0 * np.sin(0.2 * t) dataset = mogptk.DataSet(t, [y1, y2]) model = mogptk.MOSM(dataset, Q=2) model.init_parameters() model.train() model.predict() dataset.plot() ``` -------------------------------- ### GET /data/copy Source: https://games-uchile.github.io/mogptk/data.html Creates a deep copy of the current Data object. ```APIDOC ## GET /data/copy ### Description Make a deep copy of the Data object. ### Method GET ### Response #### Success Response (200) - **data** (mogptk.data.Data) - A deep copy of the original data object. ``` -------------------------------- ### Initialize Inducing Points - Density Method Source: https://games-uchile.github.io/mogptk/gpr/model.html Initializes inducing points based on the density of the input data using Gaussian Kernel Density Estimation. Points are resampled from the estimated density. ```python def _init_density(N, X): kernel = gaussian_kde(X.T.detach().cpu().numpy(), bw_method='scott') Z = torch.tensor(kernel.resample(N).T, device=config.device, dtype=config.dtype) return Z ``` -------------------------------- ### GET /model/plot_correlation Source: https://games-uchile.github.io/mogptk/model.html Visualizes the correlation matrix between different channels in the dataset. ```APIDOC ## GET /model/plot_correlation ### Description Plot the correlation matrix between each channel. ### Parameters #### Query Parameters - **title** (str) - Optional - Figure title. - **figsize** (tuple) - Optional - Figure size. ```