This example shows a practical use of financial time series objects, predicting the return of a stock from a given set of data. The data is a series of closing stock prices, a series of dividend payments from the stock, and an explanatory series (in this case a market index). Additionally, the example calculates the dividend rate from the stock data provided.

The data for this demonstration is found in the MAT-file `predict_ret_data.mat`

. The MAT-file contains six vectors:

Dates corresponding to the closing stock prices,

`sdates`

Closing stock prices,

`sdata`

Dividend dates,

`divdates`

Dividend paid,

`divdata`

Dates corresponding to the metric data,

`expdates`

Metric data,

`expdata`

```
load predict_ret_data.mat
```

It is useful to work with financial time series objects rather than with the vectors now in the workspace. By using objects, you can easily keep track of the dates. Also, you can manipulate the data series based on dates because a time series object keeps track of the administration of a time series for you. Use the object constructor

to construct three financial time series objects.`fints`

t0 = fints(sdates, sdata, {'Close'}, 'd', 'Inc'); d0 = fints(divdates, divdata, {'Dividends'}, 'u', 'Inc'); x0 = fints(expdates, expdata, {'Metric'}, 'w', 'Index');

The variables `t0`

, `d0`

, and `x0`

are financial time series objects containing the stock closing prices, dividend payments, and the explanatory data, respectively.

The price of a stock is affected by the dividend payment. On the day before the dividend payment date, the stock price reflects the amount of dividend to be paid the next day. On the dividend payment date, the stock price is decreased by the amount of dividend paid. Create a time series (`dadj1`

) that reflects this adjustment factor. Then create the series (`dadj2`

) that adjusts the prices at the day of dividend payment; this is an adjustment of `0`

. You also need to add the previous dividend payment date since the stock price data reflect the period subsequent to that day; the previous dividend date was December 31, 1998. Combining the two objects (`dadj1`

and `dadj2`

) gives the data needed to adjust the prices. However, since the stock price data is daily data and the effect of the dividend is linearly divided during the period, use the

function to make a daily time series (`fillts`

`dadj3`

) from the adjustment data. Use the dates from the stock price data to make the dates of the adjustment the same.

dadj1 = d0; dadj1.dates = dadj1.dates-1; dadj2 = d0; dadj2.Dividends = 0; dadj2 = fillts(dadj2,'linear','12/31/98'); dadj2('12/31/98') = 0; dadj3 = [dadj1; dadj2]; dadj3 = fillts(dadj3, 'linear', t0.dates);

The stock price recorded already reflects the dividend effect. To obtain the "correct" price, subtract the dividend amount from the closing prices. Put the result inside the same object `t0`

with the data series name `Spot`

. To make sure that adjustments correspond, index into the adjustment series using the dates from the stock price series `t0`

. Use the

command because `datestr`

`t0.dates`

returns the dates in serial date format. Also, since the data series name in the adjustment series `dadj3`

does not match the one in `t0`

, use the function

.`fts2mat`

t0.Spot = t0.Close - fts2mat(dadj3(datestr(t0.dates)));

Calculate the return series from the stock price data. A stock return is calculated by dividing the difference between the current closing price and the previous closing price by the previous closing price.

tret = (t0.Spot - lagts(t0.Spot, 1)) ./ lagts(t0.Spot, 1); tret = chfield(tret, 'Spot', 'Return');

Ignore any warnings you receive during this sequence. Since the operation on the first line above preserves the data series name `Spot`

, it has to be changed with the

command to reflect the contents correctly.`chfield`

The explanatory (metric) data set is a weekly data set while the stock price data is a daily data set. The frequency needs to be the same. Use

to convert the weekly series into a daily series. The constant needs to be included here to get the constant factor from the regression. Get all the dates common to the return series calculated and the explanatory (metric) data and then combine the contents of the two series that have dates in common into a new time series (`todaily`

`regts0`

). Remove the contents of the new time series that are not finite to create time series (`regts1`

).

Place the data to be regressed into a matrix using the function

. The first column of the matrix corresponds to the values of the first data series in the object, the second column to the second data series, and so on. In this case, the first column is regressed against the second and third column. Using the regression coefficients, calculate the predicted return from the stock price data. Put the result into the return time series `fts2mat`

`tret`

as the data series `PredReturn`

.

x1 = todaily(x0); x1.Const = 1; dcommon = intersect(tret.dates, x1.dates); regts0 = [tret(datestr(dcommon)), x1(datestr(dcommon))]; finite_regts0 = find(all(isfinite( fts2mat(regts0)), 2)); regts1 = regts0( finite_regts0 ); DataMatrix = fts2mat(regts1); XCoeff = DataMatrix(:, 2:3) \ DataMatrix(:, 1); RetPred = DataMatrix(:,2:3) * XCoeff; tret.PredReturn(datestr(regts1.dates)) = RetPred;

Plot the results in a single window. The top plot in the window has the actual closing stock prices and the dividend-adjusted stock prices (spot prices). The bottom plot shows the actual return of the stock and the predicted stock return through regression.

subplot(2, 1, 1); plot(t0); title('Spot and Closing Prices of Stock'); subplot(2, 1, 2); plot(tret); title('Actual and Predicted Return of Stock');

Calculate the dividend rate from the stock price data by dividing the dividend payments by the corresponding closing stock prices. Stock price data for October 2, 1999 does not exist. The `fillts`

function can overcome this situation;

allows you to insert a date and interpolate a value for the date from the existing values in the series. There are a number of interpolation methods. Use `fillts`

to create a new time series (`fillts`

`t1`

) containing the missing date from the original data series. Then set the frequency indicator to daily.

datestr(d0.dates, 2) t1 = fillts(t0,'nearest',d0.dates); t1.freq = 'd'; tdr = d0./fts2mat(t1.Close(datestr(d0.dates))) % Calculate the dividend rate

ans = 04/15/99 06/30/99 10/02/99 12/30/99 tdr = desc: Inc freq: Unknown (0) 'dates: (4)' 'Dividends: (4)' '15-Apr-1999' [ 0.0193] '30-Jun-1999' [ 0.0305] '02-Oct-1999' [ 0.0166] '30-Dec-1999' [ 0.0134]

You can find a file for this example program in the folder `matlabroot`

`/toolbox/finance/findemos`

on your MATLAB® path. The file is `predict_ret.m`

.

`ascii2fts`

| `boxcox`

| `convertto`

| `datestr`

| `diff`

| `fillts`

| `filter`

| `fints`

| `fts2mat`

| `ftsbound`

| `lagts`

| `leadts`

| `peravg`

| `resamplets`

| `smoothts`

| `toannual`

| `todaily`

| `tomonthly`

| `toquarterly`

| `tosemi`

| `toweekly`

| `tsmovavg`

Was this topic helpful?