#' Get the posterior distribution of after_tax EBITs 5Y ahead (LEGACY)
#' @description
#' This function calculates EBIT cash flow posteriors 5 years ahead
#' @param company_after_tax_ebit **Number** Current company after_tax EBIT
#' @param company_growth **Number** Current company growth rate
#' @param company_reinvestment_rate **Number** Current company reinvestment rate
#' @param sigma_ebit **Vector** Y2Y EBIT numeric differences across
#' companies, which are similar to the current firm of interest
#' @param sigma_reinvestment_rate **Vector** Y2Y CapEx numeric differences across
#' companies, which are similar to the current firm of interest
#' @param N **Number** Length of sigma_ebit and sigma_reinvestment_rate (both should be same length)
#' @return **List** Fitted STAN model and the medians (CI 95%) of relevant parameters
#' @examples
#' # get_stan_ebit_flow(company_after_tax_ebit=1444,
#' # company_growth = 0.045,
#' # company_reinvestment_rate = 0.4,
#' # sigma_ebit = c(400,299,376,420,500),
#' # sigma_reinvestment_rate = c(100,200,150,175,90),
#' # N = length(sigma_ebit))
#' @importFrom rstan stan
#' @importFrom tidyr pivot_wider
#' @importFrom dplyr select
#' @importFrom broom.mixed tidy
#' @export
get_stan_ebit_flow <- function(company_after_tax_ebit,
company_growth,
company_reinvestment_rate,
sigma_ebit,
sigma_reinvestment_rate,
N) {
### Centrilize the priors
data <- list(after_tax_ebit = company_after_tax_ebit,
growth = company_growth,
reinvestment = company_reinvestment_rate,
sigma_ebit = sigma_ebit,
sigma_rr = sigma_reinvestment_rate,
N = N)
### Describe the STAN model
ebit_flow_model <- "
data {
int <lower=1> N;
real after_tax_ebit;
real growth;
real reinvestment;
vector[N] sigma_ebit;
vector[N] sigma_rr;
}
parameters {
real ebit_flow_1;
real ebit_flow_2;
real ebit_flow_3;
real ebit_flow_4;
real ebit_flow_5;
real rr_1;
real rr_2;
real rr_3;
real rr_4;
real rr_5;
}
model {
ebit_flow_1 ~ normal(after_tax_ebit * (1+growth) ^ 1, sigma_ebit);
ebit_flow_2 ~ normal(after_tax_ebit * (1+growth) ^ 2, sigma_ebit);
ebit_flow_3 ~ normal(after_tax_ebit * (1+growth) ^ 3, sigma_ebit);
ebit_flow_4 ~ normal(after_tax_ebit * (1+growth) ^ 4, sigma_ebit);
ebit_flow_5 ~ normal(after_tax_ebit * (1+growth) ^ 5, sigma_ebit);
rr_1 ~ normal(ebit_flow_1 * reinvestment, sigma_rr);
rr_2 ~ normal(ebit_flow_2 * reinvestment, sigma_rr);
rr_3 ~ normal(ebit_flow_2 * reinvestment, sigma_rr);
rr_4 ~ normal(ebit_flow_2 * reinvestment, sigma_rr);
rr_5 ~ normal(ebit_flow_2 * reinvestment, sigma_rr);
}
"
### Fit the model
fit_model <- stan(model_code = ebit_flow_model, data = data,
iter = 5000 * 4, chains = 4, cores = 2,
seed = 110)
### Tidy the model output
tidy_ebits_flow <- tidy(fit_model, conf.int=T, conf.level=0.95) %>%
select(term, estimate) %>%
pivot_wider(names_from = term, values_from = estimate)
return(list(fitted_model = fit_model,
parameters = tidy_ebits_flow))
}
#' Get FCFF 5Y ahead (LEGACY)
#' @description
#' This function calculates FCFF 5 years ahead
#' @param df **Data Frame** EBIT and reinvestment flow 5Y ahead
#' @param wacc **Number** Firm cost
#' @return **List** FCFF NPV
#' @export
get_fcff <- function(df, wacc) {
fcff_1 <- df$ebit_flow_1 - df$rr_1
fcff_2 <- df$ebit_flow_2 - df$rr_2
fcff_3 <- df$ebit_flow_3 - df$rr_3
fcff_4 <- df$ebit_flow_4 - df$rr_4
fcff_5 <- df$ebit_flow_5 - df$rr_5
npv <- fcff_1/(1+wacc)^1+fcff_2/(1+wacc)^2+fcff_3/(1+wacc)^3+fcff_4/(1+wacc)^4+fcff_5/(1+wacc)^5
return(c(fcff_1=fcff_1,
fcff_2=fcff_2,
fcff_3=fcff_3,
fcff_4=fcff_4,
fcff_5=fcff_5,
npv=npv))
}
#' Get the posterior distribution of NPV (LEGACY)
#' @description
#' This function calculates NPV posterior
#' @param df **Data Frame** NPV value
#' @param wacc **Number** Company default rate
#' @return **Tibble** NPV posterior
#' @examples
#' #get_stan_npv(npv=3374, default_rate=0.15)
#' @importFrom rstan stan
#' @importFrom broom.mixed tidy
#' @export
get_stan_npv <- function(npv_df, default_rate) {
### Centralize the priors
npv_data <- list(npv = as.numeric(npv_df[6]),
default_rate = default_rate)
### Describe the STAN model
npv_flow_model <- "
data {
real npv;
real default_rate;
}
parameters {
real npv_var;
}
model {
npv_var ~ normal(npv, npv*default_rate);
}
"
### Fit the model
npv_model <- stan(model_code = npv_flow_model, data = npv_data,
iter = 5000 * 4, chains = 4, cores = 2,
seed = 110)
tidy_npv <- tidy(npv_model, conf.int=T, conf.level=0.95)
}
#' Get the posterior distribution of terminal year EBIT (LEGACY)
#' @description
#' This function calculates terminal year EBIT posterior
#' @param ebit_flow_5 EBIT Y5 value
#' @param roic **Number** Company ROIC
#' @param growth **Number** Company growth
#' @param sigma_ebit **Vector** Y2Y EBIT numeric differences across
#' @param N **Number** Length of sigma_ebit and sigma_reinvestment_rate, both should be same length
#' companies, which similar with the current firm of interest
#' @return **Tibble** Terminal EBIT posterior
#' @examples
#' # get_terminal_ebit(N=5, growth_roic_reinv_sigma=c(0.2,0.5,0.25,0.3,0.4),
#' # ebit_flow_5=as.numeric(tidy_ebits_flows$parameters[5]),
#' # sigma_ebit = c(400,299,376,420,500),
#' # roic=0.25,
#' # growth=0.045)
#' @importFrom rstan stan
#' @importFrom broom.mixed tidy
#' @export
get_stan_terminal_ebit <- function(N, growth_roic_reinv_sigma,
ebit_flow_5,
sigma_ebit,
roic,
growth) {
### Centralize the priors
terminal_ebit_data <- list(N=N, growth_roic_reinv_sigma=growth_roic_reinv_sigma,
ebit_flow_5=ebit_flow_5,
sigma_ebit = sigma_ebit,
roic=roic,
growth=growth)
### Describe the STAN model
terminal_ebit_model <- "
data {
int <lower=1> N;
vector[N] growth_roic_reinv_sigma;
real ebit_flow_5;
vector[N] sigma_ebit;
real roic;
real growth;
}
parameters {
real terminal_ebit;
real reinvestment_rate;
}
model {
terminal_ebit ~ normal(ebit_flow_5*(1+growth), sigma_ebit);
reinvestment_rate ~ normal(1-(growth/roic), growth_roic_reinv_sigma);
}
"
### Fit the model
terminal_ebit_fit <- stan(model_code = terminal_ebit_model, data = terminal_ebit_data,
iter = 5000 * 4, chains = 4, cores = 2,
seed = 110)
tidy(terminal_ebit_fit, conf.int=T, conf.level=0.95)
}
#' Calculate terminal value (LEGACY)
#' @description
#' This function calculates terminal value
#' @param terminal_ebit **Number** EBIT and reinvestment flow 5Y ahead
#' @param wacc **Number** Firm cost
#' @param growth **Number** Firm growth
#' @param reinvestment_rate **Number** Reinvestment rate
#' @return **List** FCFF NPV
#' @export
get_tv_value <- function(terminal_ebit,
reinvestment_rate,
wacc,
growth) {
tv <- terminal_ebit*reinvestment_rate/(wacc-growth)
}
#' Get the posterior distribution of terminal value
#' @description
#' This function calculates terminal value posterior
#' @param default_rate Firm default rate
#' @param tv **Number** Firm terminal value
#' @return **Tibble** Terminal EBIT posterior
#' @examples
#' # get_stan_terminal_value(tv=14677, default_rate=0.15)
#' @importFrom rstan stan
#' @importFrom broom.mixed tidy
get_stan_terminal_value <- function(tv, default_rate) {
### Centralize the priors
tv_data <- list(tv=tv,
default_rate = default_rate)
### Describe the STAN model
terminal_value_model <- "
data {
real tv;
real default_rate;
}
parameters {
real var_tv;
}
model {
var_tv ~ normal(tv, tv*default_rate);
}
"
### Fit the model
tv_model <- stan(model_code = terminal_value_model, data = tv_data,
iter = 5000 * 4, chains = 4, cores = 2,
seed = 110)
tidy_tv <- tidy(tv_model, conf.int=T, conf.level=0.95)
}
#' Calculate equity value (LEGACY)
#' @description
#' This function calculates equity value
#' @param npv **Number** FCFF net present value
#' @param wacc **Number** Firm cost
#' @param cash **Number** Firm cash
#' @param debt **Number** Firm debt
#' @return **Number** Equity value
#' @examples
#' # equity_value <- get_equity_value(npv = 150,
#' # tv = 175,
#' # wacc = 0.15,
#' # cash = 300,
#' # debt = 750)
#' @export
get_equity_value <- function(npv, tv, wacc, cash, debt) {
value_operating_assets <- npv + tv / (1+wacc)^5
value_equity <- value_operating_assets + cash - debt
}
#' Calculate value per share posterior (LEGACY)
#'@description
#' This function calculates value per share posterior
#' @param equity_value *Number* Firm equity
#' @param ind_stock_price_sd **Vector** Industry stock price standard deviation
#' @param share_outstanding **Number** Number of shares outstanding in millions
#' @param N **Number** Length of ind_stock_price_sd vector
#' @return **Tibble** Value per share posterior
#' @examples
#' # get_stan_value_per_share(N=5, equity_value=700, share_outstanding, ind_stock_price_sd)
#' @importFrom rstan stan
#' @importFrom broom.mixed tidy
#' @export
get_stan_value_per_share <- function(equity_value, ind_stock_price_sd,
share_outstanding, N) {
### Centralize the priors
assets_data <- list(N=5, equity_value=equity_value,
share_outstanding=share_outstanding,
ind_stock_price_sd = ind_stock_price_sd)
### Describe the STAN model
operating_assests_model <- "
data {
int <lower=1> N;
real equity_value;
real share_outstanding;
vector[N] ind_stock_price_sd;
}
parameters {
real value_per_share;
}
model {
value_per_share ~ normal(equity_value / share_outstanding, ind_stock_price_sd);
}
"
### Fit the model
asset_model <- stan(model_code = operating_assests_model, data = assets_data,
iter = 5000 * 4, chains = 4, cores = 2,
seed = 110)
tidy_asset <- tidy(asset_model, conf.int=T, conf.level=0.95)
return(list(model=asset_model, df=tidy_asset))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.