Financial Time Series Toolbox | ![]() ![]() |
Data Transformation and Frequency Conversion
The data transformation and the frequency conversion functions convert a data series into a different format.
Function |
Purpose |
boxcox |
Box-Cox transformation |
diff |
Differencing |
fillts |
Fill missing values |
filter |
Filter |
lagts |
Lag time series object |
leadts |
Lead time series object |
peravg |
Periodic average |
smoothts |
Smooth data |
tsmovavg |
Moving average |
Function |
New Frequency |
convertto |
As specified |
resamplets |
As specified |
toannual |
Annual |
todaily |
Daily |
tomonthly |
Monthly |
toquarterly |
Quarterly |
tosemi |
Semiannually |
toweekly |
Weekly |
As an example look at boxcox
, the Box-Cox transformation function. This function transforms the data series contained in a financial time series object into another set of data series with relatively normal distributions.
First create a financial time series object from the supplied whirlpool.dat
data file.
Fill any missing values denoted with NaN
s in whrl
with values calculated using the linear method:
Transform the nonnormally distributed filled data series f_whrl
into a normally distributed one using Box-Cox transformation:
Compare the result of the Close
data series with a normal (Gaussian) probability distribution function as well as 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');
Figure 2-1: Box-Cox Transformation
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 towards 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. If you have the Statistics Toolbox, you can generate a Gaussian PDF with mean and standard deviation equal to those of the transformed data series and plot it as an overlay to the second bar chart. In the next figure you can see that it is an approximately normal distribution.
Figure 2-2: Overlay of Gaussian PDF
The next example uses the smoothts
function to smooth a time series.
To begin, transform ibm9599.dat
, a supplied data file, into a financial time series object:
Fill the missing data for holidays with data interpolated using the fillts
function and the Spline
fill method:
Smooth the filled data series using the default Box (rectangular window) method:
Now, plot the original and smoothed closing price series for IBM:
plot(f_ibm.CLOSE('11/01/97::02/28/98'), 'r') datetick('x', 'mmmyy') hold on plot(sm_ibm.CLOSE('11/01/97::02/28/98'), 'b') hold off datetick('x', 'mmmyy') legend('Filled', 'Smoothed') title('Filled IBM Close Price vs. Smoothed Series')
Figure 2-3: Smoothed Data Series
These examples give you an idea of what you can do with a financial time series object. This toolbox provides some MATLAB functions that have been overloaded to work directly with the these objects. The overloaded functions are those most commonly needed to work with time series data.
![]() | Operations | Demonstration Program | ![]() |