Using base R, I've conducted a simple profit sensitivity analysis, i.e. measuring the impact on profit if there is a change in price, variable cost per sale, unit sales or fixed costs assuming that there are no interaction effects between the dependent variables, e.g. a change in price does not change unit sales.
I'd like to have feedback on my code, which is 'just for fun'. Particularly I'm interested in:
- Making the loops slicker
- Assigning the matrices full of zeroes with fewer bytes if I can
# Aim: conduct a profit sensitivity analysis
# Define parameters
Param = c(10,5,100000,10000) # I.e. a business with a price, variable cost, unit sales and fixed cost of these values
Max_Change = 0.1
Changes = seq(-Max_Change, Max_Change, by=0.01)
# Define profit function
Profit = function(Price, Var_Cost, Units, Fixed_Cost){
Profit = (Price - Var_Cost)*Units - Fixed_Cost
return(Profit)
}
# Calculating base and updated profit
Start_Profit = do.call("Profit", as.list(Param))
New_Price = matrix(0,length(Changes),4)
New_Var_Cost = matrix(0,length(Changes),4)
New_Unit_Sales = matrix(0,length(Changes),4)
New_Fixed_Costs = matrix(0,length(Changes),4)
for(i in 1:length(Changes))
{
New_Price[i,] = c((1+Changes[i])*Param[1], Param[2], Param[3], Param[4])
New_Var_Cost[i,] = c(Param[1], (1+Changes[i])*Param[2], Param[3], Param[4])
New_Unit_Sales[i,] = c(Param[1], Param[2], (1+Changes[i])*Param[3], Param[4])
New_Fixed_Costs[i,] = c(Param[1], Param[2], Param[3], (1+Changes[i])*Param[4])
}
Profit_New_Price = matrix(0,length(Changes),1)
Profit_New_Var_Cost = matrix(0,length(Changes),1)
Profit_New_Unit_Sales = matrix(0,length(Changes),1)
Profit_New_Fixed_Costs = matrix(0,length(Changes),1)
for(i in 1:length(Changes))
{
Profit_New_Price[i,] = do.call("Profit", as.list(New_Price[i,]))
Profit_New_Var_Cost[i,] = do.call("Profit", as.list(New_Var_Cost[i,]))
Profit_New_Unit_Sales[i,] = do.call("Profit", as.list(New_Unit_Sales[i,]))
Profit_New_Fixed_Costs[i,] = do.call("Profit", as.list(New_Fixed_Costs[i,]))
}
Diff_Profit_New_Price = Profit_New_Price-Start_Profit
Diff_Profit_New_Var_Cost = Profit_New_Var_Cost-Start_Profit
Diff_Profit_New_Unit_Sales = Profit_New_Unit_Sales-Start_Profit
Diff_Profit_New_Fixed_Costs = Profit_New_Fixed_Costs-Start_Profit
# Plot the profit sensitivities
options(scipen=999) # Remove scientific notation from the chart axes
msg=paste("Starting Business Conditions: Price = £",
format(Param[1],big.mark=",",scientific=FALSE),
", Variable Cost = £",
format(Param[2],big.mark=",",scientific=FALSE),
", Unit Sales = ",
format(Param[3],big.mark=",",scientific=FALSE),
", Fixed Costs = £",
format(Param[4],big.mark=",",scientific=FALSE),
", Giving a Profit of £",
format(Start_Profit,big.mark=",",scientific=FALSE))
plot(100*Changes,Diff_Profit_New_Price,
xlab="Change in Dependent Variable (%)",
ylab="Change in Profit (£)",
main="Profit Sensitivity Analysis of Changing One Variable at a Time, Assuming no Interaction Among Dependent Variables",
type="l",
sub=msg)
lines(100*Changes, Diff_Profit_New_Var_Cost, col="red")
lines(100*Changes, Diff_Profit_New_Unit_Sales, col="blue")
lines(100*Changes, Diff_Profit_New_Fixed_Costs, col="green")
# Building legend
Leg_Names = c("Price Change", "Variable Cost Change", "Unit Sales Change", "Fixed Costs Change")
legend("bottomright",
legend=Leg_Names,
col=c("black","red","blue","green"),
pch=15,
bty="n")