Documentation |
On this page… |
---|
Create Financial Time Series Objects Create Closing Prices Adjustment Series Adjust Closing Prices and Make Them Spot Prices |
This example demonstrates 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.
Note You can find a file for this demonstration program in the directory matlabroot/toolbox/finance/ftsdemos on your MATLAB^{®} path. The file is named predict_ret.m. |
To perform these computations:
The data for this demonstration is found in the MAT-file predict_ret_data.mat:
load 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
Use the whos command to see the variables in your MATLAB workspace.
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 easily manipulate the data series based on dates because the object keeps track of the administration of time series for you.
Use the object constructor fints to construct three financial time series objects.
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. To see the contents of an object, type its name at the MATLAB command prompt and press Enter. For example:
d0
d0 = 'desc:' 'Inc' 'freq:' 'Unknown (0)' '' '' 'dates: (4)' 'Dividends: (4)' '04/15/99' '0.2000' '06/30/99' '0.3500' '10/02/99' '0.2000' '12/30/99' '0.1500'
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 that reflects this adjustment factor:
dadj1 = d0; dadj1.dates = dadj1.dates-1;
Now create the series that adjust 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:
dadj2 = d0; dadj2.Dividends = 0; dadj2 = fillts(dadj2,'linear','12/31/98'); dadj2('12/31/98') = 0;
Combining the two objects above 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 fillts function to make a daily time series from the adjustment data. Use the dates from the stock price data to make the dates of the adjustment the same:
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 datestr command because 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)));
Now 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 chfield command to reflect the contents correctly.
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 todaily to convert the weekly series into a daily series. The constant needs to be included here to get the constant factor from the regression:
x1 = todaily(x0); x1.Const = 1;
Get all the dates common to the return series calculated above and the explanatory (metric) data. Then combine the contents of the two series that have dates in common into a new time series:
dcommon = intersect(tret.dates, x1.dates); regts0 = [tret(datestr(dcommon)), x1(datestr(dcommon))];
Remove the contents of the new time series that are not finite:
finite_regts0 = find(all(isfinite( fts2mat(regts0)), 2)); regts1 = regts0( finite_regts0 );
Now, place the data to be regressed into a matrix using the function fts2mat. 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:
DataMatrix = fts2mat(regts1); XCoeff = DataMatrix(:, 2:3) \ DataMatrix(:, 1);
Using the regression coefficients, calculate the predicted return from the stock price data. Put the result into the return time series tret as the data series PredReturn:
RetPred = DataMatrix(:,2:3) * XCoeff; tret.PredReturn(datestr(regts1.dates)) = RetPred;
Plot the results in a single figure 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');
Closing Prices and Returns
The last part of the task is to calculate the dividend rate from the stock price data. Calculate the dividend rate by dividing the dividend payments by the corresponding closing stock prices.
First check to see if you have the stock price data on all the dividend dates:
datestr(d0.dates, 2)
ans = 04/15/99 06/30/99 10/02/99 12/30/99 t0(datestr(d0.dates)) ans = 'desc:' 'Inc' '' 'freq:' 'Daily (1)' '' '' '' '' 'dates: (3)' 'Close: (3)' 'Spot: (3)' '04/15/99' '10.3369' '10.3369' '06/30/99' '11.4707' '11.4707' '12/30/99' '11.2244' '11.2244'
Note that stock price data for October 2, 1999 does not exist. The fillts function can overcome this situation; fillts 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. For more information, see fillts.
Use fillts to create a new time series containing the missing date from the original data series. Then set the frequency indicator to daily:
t1 = fillts(t0,'nearest',d0.dates); t1.freq = 'd';
Calculate the dividend rate:
tdr = d0./fts2mat(t1.Close(datestr(d0.dates)))
tdr = 'desc:' 'Inc' 'freq:' 'Unknown (0)' '' '' 'dates: (4)' 'Dividends: (4)' '04/15/99' '0.0193' '06/30/99' '0.0305' '10/02/99' '0.0166' '12/30/99' '0.0134'