Finalise Application Code
Now that we have modelled our operational income and filled in all the relevant values into our Research Grid we can model our fixed costs and set up the code to find the final bank balance at the end of every time step which for this model is at the end of every month.
To model the Fixed costs and find the final bank balance we will need to exit the Research Grid and go back to the Build screen and our code editor.
Once there, copy the code below over the “return ()” statement at the bottom of the main.r file, ensuring that the closing bracket has not been copied over:
# Initialize model run variables
date <- as.Date(start_day)
time_step <- months(1)
account_balance <- start_capital
n <- as.integer(periods)
monthly_incomes <- data.frame(date = as.Date(n, origin = "1900-01-01"), monthly_incomes = numeric(n), stringsAsFactors=FALSE)
monthly_balances <- data.frame(date = as.Date(n, origin = "1900-01-01"), monthly_balances = numeric(n), stringsAsFactors=FALSE)
# Iterate over time periods
for (i in 1:n) {
# Calculate operational income
profit_per_cup <- price_per_cup - cost_per_cup
demand <- 3500 - 100 * price_per_cup**2 - rnorm(1, 0, demand_noise)
income <- demand * profit_per_cup * as.numeric(fract_of_month_operating[['Sheet1']][1,i])
# Calculate operational expenses
fix_costs <- jacks_salary + shop_rent
# Calculate cash flow and account balance
cash_flow <- income - fix_costs
account_balance <- account_balance + cash_flow
# Store values for output reporting
monthly_incomes$date[i] <- date
monthly_incomes$monthly_incomes[i] <- income
monthly_balances$date[i] <- date
monthly_balances$monthly_balances[i] <- account_balance
# Advance model time
date <- date + time_step
}
# The akumen() function must return a dictionary including keys relating to outputs.
ret <- list()
ret[["monthly_incomes"]] <- monthly_incomes
ret[["monthly_balances"]] <- monthly_balances
return(ret)
The above lines of code not only Calculate the income and the costs, but also model the distribution noise, create timesteps in the model and work out and write the final bank balances to the Results tab. This is now a workable model of John and Jack’s coffee shop which we can use for finding answers to their two questions:
- What is the best price for a cup of coffee?
- When is the coffee shop making its return on investment?
The completed main.r file should appear as follows:
# Load Libraries
library(lubridate)
akumen <- function (start_day, periods,
cost_per_cup, price_per_cup, demand_noise, fract_of_month_operating,
shop_rent, jacks_salary, start_capital,
...) {
# Parameters:
# - Input: start_day [datetime]
# - Input: periods [float]
# Operational Income:
# - Input: cost_per_cup [float]
# - Input: price_per_cup [float]
# - Input: demand_noise [float]
# - Input: fract_of_month_operating [tabular] (xlsx)
# Fix costs and Accounting:
# - Input: shop_rent [float]
# - Input: jacks_salary [float]
# - Input: start_capital [float]
# Output data:
# - Output: monthly_incomes [file] (monthly_incomes.csv)
# - Output: monthly_balances [file] (monthly_balances.csv)
# Initialize model run variables
date <- as.Date(start_day)
time_step <- months(1)
account_balance <- start_capital
n <- as.integer(periods)
monthly_incomes <- data.frame(date = as.Date(n, origin = "1900-01-01"), monthly_incomes = numeric(n), stringsAsFactors=FALSE)
monthly_balances <- data.frame(date = as.Date(n, origin = "1900-01-01"), monthly_balances = numeric(n), stringsAsFactors=FALSE)
# Iterate over time periods
for (i in 1:n) {
# Calculate operational income
profit_per_cup <- price_per_cup - cost_per_cup
demand <- 3500 - 100 * price_per_cup**2 - rnorm(1, 0, demand_noise)
income <- demand * profit_per_cup * as.numeric(fract_of_month_operating[['Sheet1']][1,i])
# Calculate operational expenses
fix_costs <- jacks_salary + shop_rent
# Calculate cash flow and account balance
cash_flow <- income - fix_costs
account_balance <- account_balance + cash_flow
# Store values for output reporting
monthly_incomes$date[i] <- date
monthly_incomes$monthly_incomes[i] <- income
monthly_balances$date[i] <- date
monthly_balances$monthly_balances[i] <- account_balance
# Advance model time
date <- date + time_step
}
# The akumen() function must return a dictionary including keys relating to outputs.
ret <- list()
ret[["monthly_incomes"]] <- monthly_incomes
ret[["monthly_balances"]] <- monthly_balances
return(ret)
}
Akumen’s R integration supports most functionality of R and there are no differences in syntax or semantic.
You have now completed an R Application in Akumen. The next part of the tutorial is using the Research Grid to find the best price for a cup of coffee to maximize the return. Proceed to research to learn how to research the coffee shop findings, or click here to build the application in a Driver Model or here to build the application in Python.