Answer To: yield_data.csv "Index" "ZC025YR" "ZC050YR" "ZC075YR" "ZC100YR" "ZC125YR" "ZC150YR" "ZC175YR"...
Neha answered on Jul 06 2021
Question 1
A) Yield Curve
library(cashflow)
library(holdings)
library(yield_data)
library(MSCI World Index Total Return CAD)
yield_curve <- list("DTB3", "DGS2", "DGS5", "DGS10", "DGS30") %>%
map(
~getSymbols(.x, auto.assign=FALSE, src="True")
) %>%
do.call(merge,.)
yield_curve["1980::"] %>%
data.matrix() %>%
t() %>%
plot_ly(
x=as.Date(index(yield_curve["1980::"])),
y=c(0.25,2,5,10,30),
z=.,
type="surface"
) %>%
plotly::layout(
scene=list(
xaxis=list(title="date"),
yaxis=list(title="term"),
zaxis=list(title="yield")
)
)
yield_curve_tidy <- yield_curve %>%
data.frame() %>%
add_rownames(var="date") %>%
gather(symbol,yield,-date) %>%
mutate(term=c(0.25,2,5,10,30)[match(symbol,colnames(yield_curve))])
yield_curve_tidy[which(!is.na(yield_curve_tidy$yield)),] %>%
group_by(symbol) %>%
plot_ly(
x = ~date, y = ~term, z = ~yield,
type="scatter3d",
mode="markers",
size=3,
color=~yield
)
Standard Deviation
const standardDeviation = values => {
const avg = arr => arr.reduce((acc, cur) => (acc + cur)) / arr.length
const mean = avg(values)
const squareDiffs = values.map(val => {
const diff = val - mean
const sqrDiff = diff * diff
return sqrDiff
})
const avgSquareDiff = avg(squareDiffs)
const stdDev = Math.sqrt(avgSquareDiff)
return stdDev
}
Variance Covariance
covariance = function(x, y)
mean(x*y) - mean(x) * mean(y)
true_variance = function(x)
covariance(x, x)
bootstrap = function(x, s=sample_size, it=iteration)
matrix(sample(x, s*it, replace=T), nrow=s, ncol=it)
confidence = .05
q = c(1 - confidence/2, confidence/2)
sample_size = 100
iteration = 1E3
x = rnorm(sample_size)
monte_carlo = matrix(rnorm(sample_size*iteration), nrow=sample_size, ncol=iteration)
plot(1:iteration, cumsum(variance(monte_carlo) - 1) / 1:iteration, type='l')
lines(1:iteration, cumsum(variance(bootstrap(x)) - true_variance(x)) / 1:iteration, type='l', col='green')
abline(h=-1/sample_size, col='red')
sample_size = 1E3
iteration = 1E3
x = rnorm(sample_size, mean=5, sd= sqrt(2))
bootstraped_mean = mean(x)
bootstraped_var = true_variance(x)
estimated_variance = function(matrix)
apply(matrix, 2, true_variance)
estimated_mean = function(matrix)
apply(matrix, 2, mean)
bx = bootstrap(x)
IC1 = bootstraped_mean + qt(q, df=sample_size -1) * sqrt(bootstraped_var/sample_size)
IC2 = bootstraped_mean + qnorm(q) * sqrt(bootstraped_var/sample_size)
IC3 = bootstraped_mean - quantile(estimated_mean(bx) - bootstraped_mean, probs=q)
IC4 = bootstraped_var - quantile(estimated_variance(bx) - bootstraped_var, probs=q)
sample_size = 30
iteration = 1E3
sigma_2 = .5
alpha = 2
beta = 1
deg_of_free = 5
x = seq(0, 10, length=sample_size)
e = rt(sample_size, df=deg_of_free) * sqrt(sigma_2 * (deg_of_free - 2) / deg_of_free)
y = alpha + beta * x + e
mx = mean(x)
my = mean(y)
Sxx = true_variance(x)
Syy = true_variance(y)
Sxy = covariance(x, y)
beta_hat = Sxy/Sxx
alpha_hat = my - beta_hat * mx
be = bootstrap(y - alpha_hat - beta_hat * x)
by = alpha_hat + beta_hat * x + be
#plot(x, alpha_hat + beta_hat * x + e)
#abline(alpha_hat, beta_hat)
#abline(alpha, beta, col='red')
estimated_covariance = estimated_mean(by * x) - estimated_mean(by) * mx
beta_star = estimated_covariance / Sxx
alpha_star = estimated_mean(by) - beta_star * mx
ICb = beta_hat - quantile(beta_star - beta_hat , probs=q)
ICa = alpha_hat - quantile(alpha_star - alpha_hat, probs=q)
R = y - alpha_hat - beta_hat * x
T = sqrt((sample_size - 2) * Sxx) * (beta_hat - 1) / sqrt(sum( R ^ 2 ))
R_star = bootstrap(R)
T_star = sqrt((sample_size - 2) * Sxx) * (beta_star - 1) / sqrt(sum( R_star ^ 2 ))
B) Eigen Value
The result of first data matrix is equivalent to the eigenvalue matrix. The importance is used to arrange the vectors. Those vectors which are arranged to be the important ones are then further observed.
After the most important vectors are found, the economic states of the vector can be found after getting the data point of the curves we can find out the real economic status of the vectors.
.
The high value and low level value are corresponding.
There can be seen steepness in the yield curve.
C) The principal components contribute to a major section towards risk. The percent is between >=65%.
D) Since, hedging always helps in contributing to mitigate risk till a certain level. The simple duration immunization constraint has been a help.
Question 2
A) Conditional VAR...