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 in the build screen put the following lines of code at the top of the model:

from pandas import DataFrame 
from dateutil import parser, relativedelta 
from random import gauss 

These lines of code will allow the model import the necessary libraries to complete the calculations needed in our next few lines of code and allow us to model the noise demand that we have already defined as part of our operational income.

Once these lines are in replace the return {} at the bottom of the model with the following code:

    # Initialize model run variables 
    fract_of_month_operating = fract_of_month_operating.get('Sheet1') 
    date = parser.parse(start_day) 
    time_step = relativedelta.relativedelta(months=1) 
    account_balance = start_capital 
    monthly_incomes = [] 
    monthly_balances = []   

    # Iterate over time periods 
    for i in range(int(periods)):        
        # Calculate operational income 
        profit_per_cup = price_per_cup - cost_per_cup 
        demand = 3500 - 100 * price_per_cup**2 - gauss(0, demand_noise) 
        income = demand * profit_per_cup * fract_of_month_operating.iloc[0, i%12]      

        # 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.append([date, income]) 
        monthly_balances.append([date, account_balance])         

        # Advance model time 
        date = date + time_step 
       
    # Prepare output data 
    monthly_incomes = DataFrame(monthly_incomes, columns=['date', 'monthly_incomes']) 
    monthly_balances = DataFrame(monthly_balances, columns=['date', 'monthly_balances']) 

    return {
        'monthly_incomes': monthly_incomes, 
        'monthly_balances': monthly_balances 
    } 

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.

You have now completed a Python 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 R code.