Title: | Portfolio Risk Analysis |
---|---|
Description: | Risk Attribution of a portfolio with Volatility Risk Analysis. |
Authors: | Sourish Das [aut, cre], Tamal Kanti Panja [aut] |
Maintainer: | Sourish Das <[email protected]> |
License: | GPL-2 | GPL-3 |
Version: | 1.1.0 |
Built: | 2025-02-13 03:11:27 UTC |
Source: | https://github.com/cran/PortRisk |
Risk Attribution of a portfolio with Volatility Risk Analysis.
Package: | PortRisk |
Type: | Package |
Version: | 1.1.0 |
Date: | 2015-10-31 |
License: | GPL-2 | GPL-3 |
Depends: | R (>= 3.0.0) |
Imports: | zoo , tseries , MCMCpack , copula , MASS
|
This package includes functions to compute the volatility risk attributes such as Volatility, Portfolio Volatility, MCTR, CCTR etc.
Sourish Das [aut, cre], Tamal Kanti Panja [aut].
Maintainer: Sourish Das <[email protected]>
Access data from a zoo
type daily returns table and returns as a zoo
object. Basically, it returns a table of daily returns of a given list of company ticker names for a time period given as the input.
access(tickers, start, end, data)
access(tickers, start, end, data)
tickers |
A character vector of ticker names of companies in the portfolio. |
start |
Start date in the format "yyyy-mm-dd". |
end |
End date in the format "yyyy-mm-dd". |
data |
A |
Returns a zoo
series as a table of daily returns corresponding to the company ticker names in tickers
for the given time period. Basically, it picks up a block from a large table of daily returns of the stocks corresponding to the dates.
data(SnP500Returns) # list all the ticker names in a character vector tckk <- colnames(SnP500Returns) # access the data corresponding to the first 3 ticker names # for the time period January 1, 2013 - January 10, 2013 access(tickers = tckk[1:3], start = "2013-01-01", end = "2013-01-10", data = SnP500Returns)
data(SnP500Returns) # list all the ticker names in a character vector tckk <- colnames(SnP500Returns) # access the data corresponding to the first 3 ticker names # for the time period January 1, 2013 - January 10, 2013 access(tickers = tckk[1:3], start = "2013-01-01", end = "2013-01-10", data = SnP500Returns)
portvol
computes portfolio volatility of a given portfolio for specific weight and time period. mctr
& cctr
computes the Marginal Contribution to Total Risk (MCTR) & Conditional Contribution to Total Risk (CCTR) for the given portfolio.
portvol(tickers, weights = rep(1,length(tickers)), start, end, data) mctr(tickers, weights = rep(1,length(tickers)), start, end, data) cctr(tickers, weights = rep(1,length(tickers)), start, end, data)
portvol(tickers, weights = rep(1,length(tickers)), start, end, data) mctr(tickers, weights = rep(1,length(tickers)), start, end, data) cctr(tickers, weights = rep(1,length(tickers)), start, end, data)
tickers |
A character vector of ticker names of companies in the portfolio. |
weights |
A numeric vector of weights assigned to the stocks corresponding to the ticker names in |
start |
Start date in the format "yyyy-mm-dd". |
end |
End date in the format "yyyy-mm-dd". |
data |
A |
As any portfolio can be considered as bag of -many risky assets, it is important to figureout how these assets contributes to total volatility risk of the portfolio. We consider an investment period and suppose
denote return to source
for the same period, where
. The portfolio return over the period is
where is the portfolio exposure to the asset
, i.e., portfolio weight, such that
and
. Portfolio manager determines the size of
at the beginning of the investment period. Portfolio volatility is defined as
where and
being the variance-covariance matrix of the assets in the portfolio. The weights (
) are the main switches of portfolio's total volatility. Therefore, it is important for a manager to quantify, the sensitivity of the portfolio's volatility with respect to small change in
. This can be achieved by differentiating the portfolio volatility with respect to
,
where is know as 'Marginal Contribution to Total Risk' (MCTR). Note that MCTR of asset
is
The CCTR (aka. Conditional Contribution to Total Risk) is the amount that an asset add to total portfolio volatility. In other words, is the CCTR of asset
, i.e.,
Therefore portfolio volatility can be viewed as weighted average of MCTR.
portvol |
A numeric value. Volatility of a given portfolio in percentage. |
mctr |
A named numeric vector of Marginal Contribution to Total Risk (MCTR) in percentage with names being the ticker names. |
cctr |
A named numeric vector of Conditional Contribution to Total Risk (CCTR) in percentage with names being the ticker names. |
data(SnP500Returns) # consider the portfolio containing the first 4 stocks pf <- colnames(SnP500Returns)[1:4] st <- "2013-01-01" # start date en <- "2013-01-31" # end date # suppose the amount of investments in the above stocks are # $1,000, $2,000, $3,000 & $1,000 respectively wt <- c(1000,2000,3000,1000) # weights # portfolio volatility for the portfolio 'pf' with equal (default) weights pv1 <- portvol(pf, start = st, end = en, data = SnP500Returns) # portfolio volatility for the portfolio 'pf' with weights as 'wt' pv2 <- portvol(pf, weights = wt, start = st, end = en, data = SnP500Returns) # similarly, # mctr for the portfolio 'pf' with weights as 'wt' mc <- mctr(pf, weights = wt, start = st, end = en, data = SnP500Returns) # cctr for the portfolio 'pf' with weights as 'wt' cc <- cctr(pf, weights = wt, start = st, end = en, data = SnP500Returns) sum(cc) == pv2 # note that, sum of the cctr values is the portfolio volatility
data(SnP500Returns) # consider the portfolio containing the first 4 stocks pf <- colnames(SnP500Returns)[1:4] st <- "2013-01-01" # start date en <- "2013-01-31" # end date # suppose the amount of investments in the above stocks are # $1,000, $2,000, $3,000 & $1,000 respectively wt <- c(1000,2000,3000,1000) # weights # portfolio volatility for the portfolio 'pf' with equal (default) weights pv1 <- portvol(pf, start = st, end = en, data = SnP500Returns) # portfolio volatility for the portfolio 'pf' with weights as 'wt' pv2 <- portvol(pf, weights = wt, start = st, end = en, data = SnP500Returns) # similarly, # mctr for the portfolio 'pf' with weights as 'wt' mc <- mctr(pf, weights = wt, start = st, end = en, data = SnP500Returns) # cctr for the portfolio 'pf' with weights as 'wt' cc <- cctr(pf, weights = wt, start = st, end = en, data = SnP500Returns) sum(cc) == pv2 # note that, sum of the cctr values is the portfolio volatility
portvol.Bayes
computes portfolio volatility of a given portfolio for specific weight and time period. mctr.Bayes
& cctr.Bayes
computes the Marginal Contribution to Total Risk (MCTR) & Conditional Contribution to Total Risk (CCTR) for the given portfolio.
portvol.Bayes(tickers, weights = rep(1,length(tickers)), start, end, data, sim.size = 1000) mctr.Bayes(tickers, weights = rep(1,length(tickers)), start, end, data, sim.size = 1000) cctr.Bayes(tickers, weights = rep(1,length(tickers)), start, end, data, sim.size = 1000)
portvol.Bayes(tickers, weights = rep(1,length(tickers)), start, end, data, sim.size = 1000) mctr.Bayes(tickers, weights = rep(1,length(tickers)), start, end, data, sim.size = 1000) cctr.Bayes(tickers, weights = rep(1,length(tickers)), start, end, data, sim.size = 1000)
tickers |
A character vector of ticker names of companies in the portfolio. |
weights |
A numeric vector of weights assigned to the stocks corresponding to the ticker names in |
start |
Start date in the format "yyyy-mm-dd". |
end |
End date in the format "yyyy-mm-dd". |
data |
A |
sim.size |
Simulation size, default 1000. |
As any portfolio can be considered as bag of -many risky assets, it is important to figureout how these assets contributes to total volatility risk of the portfolio. We consider an investment period and suppose
denote return to source
for the same period, where
. The portfolio return over the period is
where is the portfolio exposure to the asset
, i.e., portfolio weight, such that
and
. Portfolio manager determines the size of
at the beginning of the investment period. Portfolio volatility is defined as
where and
being the variance-covariance matrix of the assets in the portfolio.
is the sample portfolio-covariance matrix. If
and prior distribution on is
Then posterior distribution is
For more detail, see portvol
, mctr
, cctr
portvol |
A numeric value. Volatility of a given portfolio in percentage. |
mctr |
A named numeric vector of Marginal Contribution to Total Risk (MCTR) in percentage with names being the ticker names. |
cctr |
A named numeric vector of Conditional Contribution to Total Risk (CCTR) in percentage with names being the ticker names. |
data(SnP500Returns) # consider the portfolio containing the first 4 stocks pf <- colnames(SnP500Returns)[1:4] st <- "2013-01-01" # start date en <- "2013-01-31" # end date # suppose the amount of investments in the above stocks are # $1,000, $2,000, $3,000 & $1,000 respectively wt <- c(1000,2000,3000,1000) # weights # portfolio volatility for the portfolio 'pf' with equal (default) weights pv1 <- portvol(pf, start = st, end = en, data = SnP500Returns) # portfolio volatility for the portfolio 'pf' with weights as 'wt' pv2 <- portvol(pf, weights = wt, start = st, end = en, data = SnP500Returns) # similarly, # mctr for the portfolio 'pf' with weights as 'wt' mc <- mctr(pf, weights = wt, start = st, end = en, data = SnP500Returns) # cctr for the portfolio 'pf' with weights as 'wt' cc <- cctr(pf, weights = wt, start = st, end = en, data = SnP500Returns) sum(cc) == pv2 # note that, sum of the cctr values is the portfolio volatility
data(SnP500Returns) # consider the portfolio containing the first 4 stocks pf <- colnames(SnP500Returns)[1:4] st <- "2013-01-01" # start date en <- "2013-01-31" # end date # suppose the amount of investments in the above stocks are # $1,000, $2,000, $3,000 & $1,000 respectively wt <- c(1000,2000,3000,1000) # weights # portfolio volatility for the portfolio 'pf' with equal (default) weights pv1 <- portvol(pf, start = st, end = en, data = SnP500Returns) # portfolio volatility for the portfolio 'pf' with weights as 'wt' pv2 <- portvol(pf, weights = wt, start = st, end = en, data = SnP500Returns) # similarly, # mctr for the portfolio 'pf' with weights as 'wt' mc <- mctr(pf, weights = wt, start = st, end = en, data = SnP500Returns) # cctr for the portfolio 'pf' with weights as 'wt' cc <- cctr(pf, weights = wt, start = st, end = en, data = SnP500Returns) sum(cc) == pv2 # note that, sum of the cctr values is the portfolio volatility
Combined representation of the risk attributes MCTR, CCTR, Portfolio Volatility, Portfolio Value at Risk (VaR) and individual Volatility of the stocks in a given portfolio for a Markowitz's Optimized weights using t-Copula.
risk.attrib.Copula(tickers, data, start, end, sim.size=1000, df=10)
risk.attrib.Copula(tickers, data, start, end, sim.size=1000, df=10)
tickers |
A character vector of ticker names of companies in the portfolio. |
data |
A |
start |
Start date in the format "yyyy-mm-dd". |
end |
End date in the format "yyyy-mm-dd". |
sim.size |
Simulation size. Default at 1000. |
df |
Degrees of freedom for t-Copula. Default set at 10. |
It calculate portfolio Value at Risk after fitting t-Copula with empirical distribution on marginals. It simulate returns from the fitted t-Copula and uses Markowitz's Optimized weight.
Returns a list of following objects:
Volatility |
Data frame caontaining Markowitz's optimized weights, individual stock's volatility, MCTR, CCTR for the given tickers. |
Portfolio Volatility |
Portfolio Volatility |
Portfilio VaR |
Portfolio Value at Risk |
volatility
,
portvol
,
mctr
,
cctr
,
zoo
# load the data 'SnP500Returns' data(SnP500Returns) # consider the portfolio containing the stocks of the companies # Apple, IBM, Intel, Microsoft pf <- c("AAPL","IBM","INTC","MSFT") # risk attribution for the portfolio 'pf' # for the time period January 1, 2013 - January 10, 2013 st<-"2013-01-01" ed<-"2013-10-10" risk.attrib.Copula(tickers = pf, data = SnP500Returns, start = st, end = ed, sim.size=1000, df=10)
# load the data 'SnP500Returns' data(SnP500Returns) # consider the portfolio containing the stocks of the companies # Apple, IBM, Intel, Microsoft pf <- c("AAPL","IBM","INTC","MSFT") # risk attribution for the portfolio 'pf' # for the time period January 1, 2013 - January 10, 2013 st<-"2013-01-01" ed<-"2013-10-10" risk.attrib.Copula(tickers = pf, data = SnP500Returns, start = st, end = ed, sim.size=1000, df=10)
Combined representation of the risk attributes MCTR, CCTR, CCTR percentage, Portfolio Volatility and individual Volatility of the stocks in a given portfolio for a given weight and time period.
risk.attribution(tickers, weights = rep(1,length(tickers)), start, end, data, CompanyList = NULL)
risk.attribution(tickers, weights = rep(1,length(tickers)), start, end, data, CompanyList = NULL)
tickers |
A character vector of ticker names of companies in the portfolio. |
weights |
A numeric vector of weights assigned to the stocks corresponding to the ticker names in |
start |
Start date in the format "yyyy-mm-dd". |
end |
End date in the format "yyyy-mm-dd". |
data |
A |
CompanyList |
A dataframe containing all the Company names corresponding to the ticker names as its |
For details of the risk attributes refer to the corresponding functions. See volatility
for individual volatility of the stocks and portvol
for portfolio volatility, MCTR & CCTR.
CCTR percentage for a stock in the portfolio is defined as the percentage of the portfolio volatility contributed by that stock for the given weight. i.e.,
where is the portfolio volatility.
Returns a dataframe with rownames
as the ticker names as given in the input tickers
with the last row corresponding to the portfolio values. The result contains the following columns:
Company Name |
Optional. Available only if the dataframe with the company names corresponding to the ticker names as |
Weight |
Standardized value of the weights assigned to the stocks in the portfolio. Value of this column corresponding to portfolio is the sum of the weights (i.e. 1). |
MCTR |
Marginal Contribution to Total Risk (MCTR) in percentage. MCTR corresponding to the portfolio will be shown as |
CCTR |
Conditional Contribution to Total Risk (CCTR) in percentage. CCTR corresponding to the portfolio is the sum of the CCTR values, which is the portfolio volatility. |
CCTR(%) |
Percentage of the portfolio volatility contributed by the stock for the given weight. Clearly, CCTR percentage corresponding to the portfolio is 100. |
Volatility |
Individual volatility of the stocks in percentage. Note that, the value of this column corresponding to the portfolio is not the sum of this column. It is the portfolio volatility. |
In the result or output (see example), both the values of the last row (Portfolio) corresponding to the columns CCTR and Volatility are same (Portfolio Volatility). It should also be noted that, Portfolio Volatility is the sum of CCTR values corresponding to all the stocks but not the sum of individual Volatility of the stocks.
volatility
,
portvol
,
mctr
,
cctr
,
zoo
# load the data 'SnP500Returns' data(SnP500Returns) # consider the portfolio containing the stocks of the companies # Apple, IBM, Intel, Microsoft pf <- c("AAPL","IBM","INTC","MSFT") # suppose the amount of investments in the above stocks are # $10,000, $40,000, $20,000 & $30,000 respectively wt <- c(10000,40000,20000,30000) # weights # risk attribution for the portfolio 'pf' with weights 'wt' # for the time period January 1, 2013 - January 31, 2013 risk.attribution(tickers = pf, weights = wt, start = "2013-01-01", end = "2013-01-31", data = SnP500Returns) # to attach the company names corresponding to the ticker names # load the dataset containing the company names data(SnP500List) risk.attribution(tickers = pf, weights = wt, start = "2013-01-01", end = "2013-01-31", data = SnP500Returns, CompanyList = SnP500List)
# load the data 'SnP500Returns' data(SnP500Returns) # consider the portfolio containing the stocks of the companies # Apple, IBM, Intel, Microsoft pf <- c("AAPL","IBM","INTC","MSFT") # suppose the amount of investments in the above stocks are # $10,000, $40,000, $20,000 & $30,000 respectively wt <- c(10000,40000,20000,30000) # weights # risk attribution for the portfolio 'pf' with weights 'wt' # for the time period January 1, 2013 - January 31, 2013 risk.attribution(tickers = pf, weights = wt, start = "2013-01-01", end = "2013-01-31", data = SnP500Returns) # to attach the company names corresponding to the ticker names # load the dataset containing the company names data(SnP500List) risk.attribution(tickers = pf, weights = wt, start = "2013-01-01", end = "2013-01-31", data = SnP500Returns, CompanyList = SnP500List)
List of company names corresponding to the ticker names of the stocks listed in S&P500 List in the year 2013.
data(SnP500List)
data(SnP500List)
A data frame with 500 observations on the following variable.
Company
Names of companies as character string corresponding to their ticker names as rowname.
data(SnP500List) head(SnP500List)
data(SnP500List) head(SnP500List)
Daily log returns corresponding to the ticker names of the stocks of the companies listed in S&P500 List in the year 2013.
data(SnP500Returns)
data(SnP500Returns)
The format is zoo
series from 2013-01-02 to 2013-12-31.
rownames
are the dates in the format "yyyy-mm-dd" and colnames
are the ticker names of the stocks.
Yahoo Finance <http://finance.yahoo.com>
access
to pick a block from this large zoo
series.
data(SnP500Returns)
data(SnP500Returns)
Volatility of one or more stock(s) for a given time period.
volatility(tickers, start, end, data)
volatility(tickers, start, end, data)
tickers |
List of ticker names of companies. A character vector. |
start |
Start date in the format "yyyy-mm-dd". |
end |
End date in the format "yyyy-mm-dd". |
data |
A |
Volatility of a given stock for a time period is defined as the standard deviation of the returns of that stock in that time period.
A named numeric vector of volatility in percentage with names being the ticker names of the stocks given as input in tickers
.
data(SnP500Returns) tckk <- colnames(SnP500Returns) # volatility of the stock of the company Apple # for the time period January 1, 2013 - January 31, 2013 volatility("AAPL", start = "2013-01-01", end = "2013-01-31", data = SnP500Returns) # volatility of the first three stocks in SnP500Returns # for the time period January 1, 2013 - January 31, 2013 volatility(tickers = tckk[1:3], start = "2013-01-01", end = "2013-01-31", data = SnP500Returns)
data(SnP500Returns) tckk <- colnames(SnP500Returns) # volatility of the stock of the company Apple # for the time period January 1, 2013 - January 31, 2013 volatility("AAPL", start = "2013-01-01", end = "2013-01-31", data = SnP500Returns) # volatility of the first three stocks in SnP500Returns # for the time period January 1, 2013 - January 31, 2013 volatility(tickers = tckk[1:3], start = "2013-01-01", end = "2013-01-31", data = SnP500Returns)