Description Usage Arguments Value See Also Examples
Function to estimate the volatility, σ, and drift, μ. E.g.,
the window can be over a given number of months. See
vignette("Distance-to-default", package = "DtD")
for details.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
S |
numeric vector with observed stock prices. |
D |
numeric vector or scalar with debt due in |
T. |
numeric vector or scalar with time to maturity. |
r |
numeric vector or scalar with risk free rates. |
time |
numeric vector with the observation times. |
dt |
numeric scalar with time increments between observations. |
vol_start |
numeric scalar with starting value for σ. |
method |
string to specify which estimation method to use. |
tol |
numeric scalar with tolerance to |
eps |
numeric scalar with convergence threshold. |
grp |
integer vector with the group identifier (e.g., units of months). |
width |
integer scalar with the units of |
min_obs |
integer scalar for the minimum number of observation required in each window. |
Matrix with the grp
, number of observation in the window, parameter
estimates, and 'n_iter'
as in BS_fit
, and whether the
estimation method was successful.
An error
attribute is added in case other code than
optim
fails. It is a list of lists with the grp
index
where the method failed and the output from try
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | # Simulate data
set.seed(55770945)
n <- 21L * 3L * 12L # 21 trading days for 3 years w/ 12 months
sims <- BS_sim(
vol = .1, mu = .05, dt = .1, V_0 = 100, T. = 1,
D = runif(n, 80, 90), r = runif(n, 0, .01))
sims$month <- (1:nrow(sims) - 1L) %/% 21L + 1L
# throw out some months
sims <- subset(sims, !month %in% 15:24)
# assign parameters
grp <- sims$month
width <- 12L # window w/ 12 month width
min_obs <- 21L * 3L # require 3 months of data
# estimate results with R loop which is slightly simpler then the
# implementation
grps <- unique(grp)
out <- matrix(
NA_real_, nrow = length(grps), ncol = 6,
dimnames = list(NULL, c("mu", "vol", "n_iter", "success", "n_obs", "grp")))
for(g in grps){
idx <- which(grps == g)
keep <- which(grp %in% (g - width + 1L):g)
out[idx, c("n_obs", "grp")] <- c(length(keep), g)
if(length(keep) < min_obs)
next
res <- with(
sims[keep, ],
BS_fit(S = S, D = D, T. = T, r = r, time = time, method = "iterative",
vol_start = 1))
out[idx, c("mu", "vol", "n_iter", "success")] <- rep(
do.call(c, res[c("ests", "n_iter", "success")]), each = length(idx))
}
# we get the same with the R function
out_func <- with(sims, BS_fit_rolling(
S = S, D = D, T. = T, r = r, time = time, method = "iterative",
grp = month, width = width, min_obs = min_obs))
all.equal(out[, names(out) != "n_iter"],
out_func[, names(out_func) != "n_iter"])
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.