Package 'PortRisk'

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

Help Index


Portfolio Risk Analysis

Description

Risk Attribution of a portfolio with Volatility Risk Analysis.

Details

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.

Author(s)

Sourish Das [aut, cre], Tamal Kanti Panja [aut].

Maintainer: Sourish Das <[email protected]>


Access Daily Stock Returns by Dates

Description

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.

Usage

access(tickers, start, end, data)

Arguments

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 zoo object whose rownames are dates and colnames are ticker names of the companies. Values of the table corresponds to the daily returns of the stocks of corresponding ticker names.

Value

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.

See Also

zoo

Examples

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)

Portfolio Volatility and Contribution to Total Volatility Risk (MCTR & CCTR)

Description

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.

Usage

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)

Arguments

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 tickers. The sum of the weights need not to be 1 or 100 (in percentage). By default, equal weights to all the stocks are assigned (i.e., by rep(1, length(tickers))).

start

Start date in the format "yyyy-mm-dd".

end

End date in the format "yyyy-mm-dd".

data

A zoo object whose rownames are dates and colnames are ticker names of the companies. Values of the table corresponds to the daily returns of the stocks of corresponding ticker names.

Details

As any portfolio can be considered as bag of pp-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 rjr_j denote return to source jj for the same period, where j=1,2,,pj = 1, 2,\ldots, p. The portfolio return over the period is

Rp=j=1pwjrjR_p = \sum_{j=1}^{p} w_j r_j

where wjw_j is the portfolio exposure to the asset jj, i.e., portfolio weight, such that wj0w_j \ge 0 and j=1pwj=1\sum_{j=1}^{p} w_j = 1. Portfolio manager determines the size of wjw_j at the beginning of the investment period. Portfolio volatility is defined as

σ=wTΣw\sigma = \sqrt{w^T \Sigma w}

where w=(w1,w2,,wp)w = (w_1, w_2,\ldots, w_p) and Σ\Sigma being the variance-covariance matrix of the assets in the portfolio. The weights (wjw_j) 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 ww. This can be achieved by differentiating the portfolio volatility with respect to ww,

σw=1σΣw=ρ\frac{\partial \sigma}{\partial w} = \frac{1}{\sigma} \Sigma w = \rho

where ρ=(ρ1,ρ2,,ρp)\rho = (\rho_1, \rho_2,\ldots, \rho_p) is know as 'Marginal Contribution to Total Risk' (MCTR). Note that MCTR of asset ii is

ρi=1σj=1pσijwj.\rho_i = \frac{1}{\sigma} \sum_{j=1}^{p} \sigma_{ij} w_j.

The CCTR (aka. Conditional Contribution to Total Risk) is the amount that an asset add to total portfolio volatility. In other words, ξi=wiρi\xi_i = w_i \rho_i is the CCTR of asset ii, i.e.,

σ=i=1pwiρi.\sigma = \sum_{i=1}^{p} w_i \rho_i.

Therefore portfolio volatility can be viewed as weighted average of MCTR.

Value

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.

See Also

zoo

Examples

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

Portfolio Volatility and Contribution to Total Volatility Risk (MCTR & CCTR): Bayesian Approach

Description

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.

Usage

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)

Arguments

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 tickers. The sum of the weights need not to be 1 or 100 (in percentage). By default, equal weights to all the stocks are assigned (i.e., by rep(1, length(tickers))).

start

Start date in the format "yyyy-mm-dd".

end

End date in the format "yyyy-mm-dd".

data

A zoo object whose rownames are dates and colnames are ticker names of the companies. Values of the table corresponds to the daily returns of the stocks of corresponding ticker names.

sim.size

Simulation size, default 1000.

Details

As any portfolio can be considered as bag of pp-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 rjr_j denote return to source jj for the same period, where j=1,2,,pj = 1, 2,\ldots, p. The portfolio return over the period is

Rp=j=1pwjrjR_p = \sum_{j=1}^{p} w_j r_j

where wjw_j is the portfolio exposure to the asset jj, i.e., portfolio weight, such that wj0w_j \ge 0 and j=1pwj=1\sum_{j=1}^{p} w_j = 1. Portfolio manager determines the size of wjw_j at the beginning of the investment period. Portfolio volatility is defined as

σ=wTΣw\sigma = \sqrt{w^T \Sigma w}

where w=(w1,w2,,wp)w = (w_1, w_2,\ldots, w_p) and Σ\Sigma being the variance-covariance matrix of the assets in the portfolio. SS is the sample portfolio-covariance matrix. If

SWishart(n1,Σ)S \sim Wishart(n-1,\Sigma)

and prior distribution on Σ\Sigma is

ΣInvWishart(n0,Ψ)\Sigma \sim Inv-Wishart(n_0,\Psi)

Then posterior distribution is

ΣSInvWishart(n0+n1,Ψ+S)\Sigma | S \sim Inv-Wishart(n_0+n-1,\Psi+S)

For more detail, see portvol, mctr, cctr

Value

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.

See Also

zoo

Examples

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

Risk Attribution of a Portfolio with t-Copula

Description

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.

Usage

risk.attrib.Copula(tickers, data, start, end, sim.size=1000, df=10)

Arguments

tickers

A character vector of ticker names of companies in the portfolio.

data

A zoo object whose rownames are dates and colnames are ticker names of the companies. Values of the table corresponds to the daily returns of the stocks of corresponding ticker names.

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.

Details

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.

Value

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

See Also

volatility, portvol, mctr, cctr, zoo

Examples

# 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)

Risk Attribution of a Portfolio

Description

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.

Usage

risk.attribution(tickers, weights = rep(1,length(tickers)),
                  start, end, data, CompanyList = NULL)

Arguments

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 tickers. The sum of the weights need not to be 1 or 100 (in percentage). By default, equal weights to all the stocks are assigned (i.e., by rep(1, length(tickers))).

start

Start date in the format "yyyy-mm-dd".

end

End date in the format "yyyy-mm-dd".

data

A zoo object whose rownames are dates and colnames are ticker names of the companies. Values of the table corresponds to the daily returns of the stocks of corresponding ticker names.

CompanyList

A dataframe containing all the Company names corresponding to the ticker names as its rownames. The input for this argument is optional.

Details

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.,

CCTR(%)=CCTRσ100CCTR(\%) = \frac{CCTR}{\sigma}*100

where σ\sigma is the portfolio volatility.

Value

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 rownames is supplied as input in risk.attribution for the argument CompanyList.

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 NA, since it is meaningless.

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.

Note

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.

See Also

volatility, portvol, mctr, cctr, zoo

Examples

# 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 S&P500 Stocks in 2013

Description

List of company names corresponding to the ticker names of the stocks listed in S&P500 List in the year 2013.

Usage

data(SnP500List)

Format

A data frame with 500 observations on the following variable.

Company

Names of companies as character string corresponding to their ticker names as rowname.

Examples

data(SnP500List)
head(SnP500List)

Daily Returns of S&P500 Stocks in 2013

Description

Daily log returns corresponding to the ticker names of the stocks of the companies listed in S&P500 List in the year 2013.

Usage

data(SnP500Returns)

Format

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.

Source

Yahoo Finance <http://finance.yahoo.com>

See Also

access to pick a block from this large zoo series.

Examples

data(SnP500Returns)

Individual Volatility of Stock(s)

Description

Volatility of one or more stock(s) for a given time period.

Usage

volatility(tickers, start, end, data)

Arguments

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 zoo object whose rownames are dates and colnames are ticker names of the companies. Values of the table corresponds to the daily returns of the stocks of corresponding ticker names.

Details

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.

Value

A named numeric vector of volatility in percentage with names being the ticker names of the stocks given as input in tickers.

See Also

zoo

Examples

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)