This chapter begins with some basic quantitative aspects of bonds and stocks and then move to a narrative about events from recent history. We will distinguish between the most basic securities whose prices are quoted underlying securities and those securities whose value is derived from those basic securities, derivative securities. Analysis of Derivatives that are derive their value from the value of the underlying security on which they are based have been practically calculated using appropriate R codes.
2. 2
INTRODUCTION
This chapter begins with some basic
quantitative aspects of bonds and stocks and
then move to a narrative about events from
recent history.
If we were not affected, we probably know
somebody, maybe a good friend or loved one,
who was: they lost their retirement nest egg or
their job.
As we look back on these stories, we can see
why having financial analytic skills in our
background can help us.
3. 3
INTRODUCTION
We need to understand the individual security
statistical behaviors and their interrelationships.
Indeed, just to manage our own portfolio toward
our children’s future school funding, budgeting
for an aging relative or our own eventual
retirement, requires this knowledge, especially
in these days of minimal return on savings
accounts.
4. 4
INTRODUCTION
Securities are different from most products we
encounter in that their prices fluctuate
continuously during trading hours.
Therefore, analyzing securities prices is a study
of random processes.
We will distinguish between the most basic
securities whose prices are quoted underlying
securities and those securities whose value is
derived from those basic securities, derivative
securities.
Basic securities include stocks, bonds, and spot
commodities which are priced in the present.
5. 5
INTRODUCTION
A large portion of stock market transactions use
basic securities, so establishing an analytical
foundation with these is a priority.
In all types of bonds, the purchaser is expecting
to gain value through the income provided by
the coupons and through the increase in the final
bond value when compared to the original
purchase price.
6. 6
INTRODUCTION
In the case of stocks, the stock purchaser or owner
is buying a small piece of the corporation,
determined by the number of shares.
Unlike the bond, there is an unlimited time period to
hold the stock.
The price per share in the stock market fluctuate
quite rapidly as buyers and sellers are trading shares
and determining the price by the rules of supply and
demand.
7. 7
INTRODUCTION
Both bonds and stocks also have what are known as
derivatives.
These are securities which always have a time of
maturity, also known as their expiration date.
Derivatives derive their value from the value of the
underlying security on which they are based.
The derivatives are contracts to buy or sell bonds or
stocks at a particular value by a certain date.
8. 8
BOND INVESTMENTS
one question we would like to concern ourselves with
is how to calculate a bond’s value?
If a bond’s coupon payments are every six months, the
interest rate, r, is typically fixed throughout the life of
the bond.
If one purchases and becomes the owner of a bond, we
say they are long the bond.
The bond seller is then short the bond.
The bond has a limited period of coupon payments.
In mathematical terms,
10. 10
BOND INVESTMENTS
The value of a bond is a common application of
opportunity cost.
Once the bond holder has purchased a bond at rate r, and
the bond market continues to move, if the prevailing rate
goes up, the bond holder now holds a bond which is
worth less.
If the bond holder’s rate r = 2% and the market moves to
3%, then we can calculate the value of the original and
newer zero-coupon 30-year bonds. (Use of R - Software)
11. 11
BOND INVESTMENTS
The already-purchased bond is paying less than the
current market rate.
The inverse relationship between a bond’s interest rate
r and the Bond Value is clear from this calculation.
The interest rate went up, but the bond value went
down.
12. 12
BOND INVESTMENTS
Now that we have a function to find the bond value BV(),
we can set our seed and simulate r(t) using the rnorm()
function to obtain Gaussian variates.
We then loop to simulate t from 0 to T. BV() implements
our Formula 4.3 for BVsemi, the semi-annual version, by
doubling the time to maturity.
While the prior code will simulate the interest rate and
plot the bond value, the upcoming code corresponds to
“unit test” cases for checking the results of the bond
value.
13. 13
BOND INVESTMENTS
Unit testing involves a very small number of lines of code to
demonstrate a basic expected property of a function.
Run Codes
BV(P,C,r=.06,t=0,T=20)
[1] 1000
> BV(P,C,r=.06,t=1/2,T=20)
[1] 1030
> BV(P,C,r=.06,t=1,T=20)
[1] 1060
> BV(P,C,r=.07,t=1/2,T=20)
[1] 924.4875
> BV(P,C,r=.06,t=20,T=20)
[1] 2200
> BV(P,C,r=0,t=0,T=20)
[1] 2200
RUN THESE CODES
14. 14
BOND INVESTMENTS
The final unit test case when time rates are zero (r = 0)
illustrates what payments are present when there is no
discounting.
The value of 2200 comes from the 40 payments of 30 and
the par value of 1000.
With bonds, there is one party who issues the bonds and
is expected to make payments, the bond seller and bond
issuer.
The other party, the bond buyer and bond holder, expects
to receive the payments until final maturity.
15. 15
STOCK INVESTMENTS
The stock market is one of the most dynamic places to
invest in terms of price movement, as the supply and
demand for shares of the company fluctuates on a daily,
second, or even sub-second basis.
If the investor purchases one or more shares of stock, one
becomes part owner of a company and is considered long
the stock.
If the stock appreciates in value, then the long stock
owner stands to profit or gain if they decide to sell the
stock.
16. 16
STOCK INVESTMENTS
We designate the price of a stock as being a random variable
S, or more specifically, S(t).
At the beginning of a trade, time 0, the stock prices is S(0),
and at the end of the trade, time T, the stock price is S(T).
Our time increments here are days.
The stock trade for a long position of the security where the
investor owns the security during the trade. With a long
position the investor profits when S(T) > S(0) amount S(T) −
S(0). When S(T) = S(0) there is no gain or loss and vice
versa.
18. 18
STOCK INVESTMENTS
Lets say we have a T = 45-day trade where there is a
realized gain at time T.
If the stock began the period at 100 per share and we
purchase, or “go long” 100 shares, then we have
invested 10,000 at the outset.
The following code will simulate a simple hypothetical
long stock position using the rnorm() generator to yield
Gaussian variates:
20. 20
STOCK INVESTMENTS
The stock trade for a short position where the security is
borrowed so that it can be sold to a buyer for the period of
time 0 to T.
With a short position the investor profits when S(T) < S(0)
by the amount S(0)−S(T).
When S(T) = S(0) there is no gain or loss.
When S(T) > S(0) the investor loses the amount S(T) − S(0).
Short positions are rarer than long positions and are
sometimes prohibited in investor accounts because they are
more complex operationally.
21. 21
SECURITIES DATASETS AND VISUALIZATION
We now discuss the specific technique for
analyzing stocks: that is; charting.
Charting is the most common form of
visualization.
The following R routine takes a multivariate
matrix of n × p prices and plots them in
rectangular display array with the R command for
parallel plots: par(mfrow=c(nrow,ncol)).
22. 22
SECURITIES DATASETS AND VISUALIZATION
It will be used frequently to help analysts
visualize portfolios of securities prices and log
returns.
In terms of our time-based random variable S(t),
in this case, we have p of these which can be
expressed as (S1(t), . . . , Sp(t)) and are named
prices.
24. 24
SECURITIES DATASETS AND VISUALIZATION
In the displayCharts() function, the nrow and
ncol refer to the plot display grid, not the price
data.
lab is a vector of ticker symbols.
In the unit test code the function definition, two
dummy stocks X and Y make up this vector.
25. 25
SECURITIES DATA IMPORTING
The huge stockdata dataset is a fairly robust place to start.
However, there are a lot more traded equity securities
than the 452 provided by it, and we want to be able to
control our time frame rather than be limited to from 2003
to 2008.
We especially want to get current quotes for today and
immediately prior to today to see how a portfolio is
performing lately.
26. 26
SECURITIES DATA IMPORTING
To be able to measure historical return and even calibrate
a chosen portfolio to the current market, we need
software mechanisms.
The tseries R package provides an extremely useful
function called get.hist.quote() for obtaining Yahoo!-
based historical prices:
library(tseries)
pv <- get.hist.quote('YHOO',quote="Adj",start="2011-02-
09", end="2015-02-09")
pv
27. 27
SECURITIES DATA IMPORTING
When the dataset is new there is no choice but to connect to an
outside source.
Using an online data source is somewhat convenient, but caching
it makes it available locally, without the need for a network
connection.
So we can connect to the internet using the get.hist.quote() when
we require a new dataset and cache it into flat files as it streams
in.
Reading from the cache (means Excel files stored in laptop)
makes simulation run faster and allows it to run offline, providing
flexibility.
28. 28
SECURITIES DATA IMPORTING
readExchSymbols(), reads the desired stock quotes for
given time horizon and caches the data into individual flat
files in two directories: NYSE and NASDAQ.
The R code makes use of routines displayCharts() and
splitAdjust() to read from a directory of historical prices
and display them in an array.
splitAdjust() was introduced and there are two sub-
directories: one for the NYSE stocks and another for
NASDAQ stocks.
29. 29
SECURITIES DATA IMPORTING
Each directory contains approximately 2200 cache files, one per
security. For example, for the NYSE security IBM, the file is named
cacheIBM.csv and contains multiple years of prices arranged in a
single-column format.
The following routine, createDirs(), sets up a directory for storing
stock prices for a test date range.
Typically there is one sub-directory for NYSE and one for NASDAQ
under the main directory.
If isSubDir==TRUE, it assumes the two ticker files for the NYSE
and NASDAQ need to be copied over into the sub-directories.
30. 30
SECURITIES DATA IMPORTING
The function called readSubDir(), accounts for
two primary cases:
1. A directory with a sub-directory for the NYSE
and the NASDAQ exchanges.
2. A directory with no sub-directories.
31. 31
SECURITIES DATA IMPORTING
acquirePrices() is our main routine for downloading and
caching price quotes.
Initially, acquirePrices() must download all prices for all
the securities listed in the lab vector. As price vectors get
acquired from R tseries utility get.hist.quote() for the
required date range, they are cached in CSV files for later
use.
NYSE and NASDAQ quotes are kept in separate sub-
directories.
32. END OF LECTURE 3
32
ALL THE CODES PRACTICED IN CLASS
ARE IN SEPARATE FILE UPLOADED ON THE
CLASS WEBSITE