%% Transform a Data Series Contained in a Financial Times Series Object
% Use boxcox to transform the data series contained in a financial time series object into another set of data series with relatively normal distributions.
% Create a financial time series object from the supplied whirlpool.dat data file.

whrl = ascii2fts('whirlpool.dat', 1, 2, []);

%
% Fill any missing values denoted with NaN's in whrl with values calculated using the linear method.

f_whrl = fillts(whrl);

%
% Transform the nonnormally distributed filled data series f_whrl into a normally distributed one using Box-Cox transformation.

bc_whrl = boxcox(f_whrl);

%
% Compare the result of the Close data series with a normal (Gaussian) probability distribution function and the nonnormally distributed f_whrl.

subplot(2, 1, 1);
hist(f_whrl.Close);
grid; title('Nonnormally Distributed Data');
subplot(2, 1, 2);
hist(bc_whrl.Close);
grid; title('Box-Cox Transformed Data');

% The bar chart on the top represents the probability distribution function of the filled data series, f_whrl, which is the original data series whrl with the missing values interpolated using the linear method. The distribution is skewed toward the left (not normally distributed). The bar chart on the bottom is less skewed to the left. If you plot a Gaussian probability distribution function (PDF) with similar mean and standard deviation, the distribution of the transformed data is very close to normal (Gaussian). When you examine the contents of the resulting object bc_whrl, you find an identical object to the original object whrl but the contents are the transformed data series.
% Copyright 2012 The MathWorks, Inc.