# Wolfram Function Repository

Instant-use add-on functions for the Wolfram Language

Function Repository Resource:

Transform data such that its covariance matrix is the identity matrix

Contributed by:
Sander Huisman

ResourceFunction["WhiteningTransform"][ whitens |

The whitening transform is sometimes called the sphering transformation.

The number of data points should be more than the length of each vector, in other words, for *N*-dimensional data, at least *N*+1 data points are needed.

Using principal component analysis, *data* is transformed such that the covariance matrix of ResourceFunction["WhiteningTransform"][*data*] is the identity matrix.

ResourceFunction["WhiteningTransform"] takes a method option, with settings Automatic (default) and "SVD".

The Method "SVD" is generally more numerically stable, as it computes a singular value decomposition. So the covariance matrix is closer to an identity matrix. This is, in general, slower as compared to the default algorithm. Note that the different methods can cause the values to be multiplied -1 for each dimension: principal axes are only defined up to a constant.

Show some original data:

In[1]:= |

Out[2]= |

Apply the whitening transform and show the result:

In[3]:= |

Out[4]= |

Verify that the covariance matrix is an identity matrix (up to small numerical errors):

In[5]:= |

Out[5]= |

Data can also be in higher dimensions:

In[6]:= |

Out[7]= |

Apply the transform:

In[8]:= |

Out[9]= |

Verify that the covariance matrix is the identity matrix up to numerical error:

In[10]:= |

Out[10]= |

Use the default method and the "SVD" method and check how far the covariance matrices deviate from the identity matrix:

In[11]:= |

Out[14]= |

Create some 6 dimensional correlated data and visualize it:

In[15]:= |

Out[16]= |

Show the plots of 2D projections:

In[17]:= |

Out[17]= |

Whiten the data and visualize the output to verify that all off-diagonal plots are "sphered" and the diagonals are on a line:

In[18]:= |

Out[19]= |

Verify that the covariance matrix is the identity matrix:

In[20]:= |

Out[20]= |

- 1.0.0 – 26 May 2021

This work is licensed under a Creative Commons Attribution 4.0 International License