Learning Artificial Intelligence in Banking: Part 2 – Ifs, Functions, and Loops

Banking Skills For The Next Decade

In a previous blog (HERE), we took got bankers used to basic programming as we went hands-on learning Python, one of the most popular and versatile coding languages for banking applications. By the end of the lesson, we created a tutorial on how to program your own ROI calculator. For this article, we take another step towards teaching bankers the power of machine learning as we expand on the computational power of computer programming. This article will show you how, through the power of relatively simple programs, you can completely automate statistical analysis on thousands of data points or make automated decisions by the hundreds in fractions of a second.

 

Here at CenterState, we have already implemented many Excel-based and code-based automated applications to increase efficiency. Think of coding as having the power of infinite interns that almost never make mistakes.

 

While the prospect of automation appears daunting, every single program, no matter how advanced or powerful, works on the same principals. The most basic way to unlock and demonstrate the computational power of computer programming is through understanding if-statements, functions, and loops. Let’s delve into all three to produce a balance sheet calculator. The calculator will first automatically add your balance sheet’s assets, liabilities, and stockholder’s equity, and second, validate that the accounting equation balances.

 

If-Statements

 

If-statements test logical conditions, something that is either true or false and then produces outputs based on the conditions. Just like in the English language and Excel, you can also attach a value if the if-statement is false; this would be an else statement. If-statements are extremely effective for basic decision making and using the true/false to influence other parts of your program. For example, if I, as a banker, can approve a loan only if the borrower’s DSCR is above 1.30, I would use an if-statement to check this value.

 

The if-statement would look similar to this.

 

Banking and coding

 

Functions

 

Functions are a way for a programmer to organize blocks of code into condensed and easily callable statements. They work almost exactly like Excel functions, condensing lengthy formulas into easily digestible bits. However, unlike Excel formulas which are prebuilt, you have to define your own functions. Inputs are referred to as parameters, and outputs returned as “return statements.” For example, if we wanted to create a function to divide a number by two, we would make the number a parameter and return the solution to the equation. This is how the function would look:

 

Programming in Banking

 

We can then call the function later in the body of our code like this, "divideByTwo(24)," which would return twelve.

 

Loops

 

Loops are a tool that computer programmers use when a something requires repetition. A loop will undergo multiple iterations over the same block of code automatically. For example, if you want a program to check hundreds of things in a file, or perform repeated arithmetic operations on multiple values, you would use a loop to accomplish this task.

 

Bank Programming

 

There are two basic forms of loops in every computer science language. “For-loops” are used to iterate over a specified number of elements in a sequence or over a distinctly finite number of times. For example, if we had multiply together a list of values, we would use a for-loop to iterate through this list. Likewise, if we wanted to execute a part of our program ten-times exactly, we would also use a for-loop. This is how a for-loop would look that multiplies all the numbers in a list together:

 

Bank Coding

 

Alternatively, a “while-loop,” which functions much like an if-statement, tells the computer to execute a block of code until a condition is fulfilled. If we wanted to export the historical price of a security until 2016, we would use a while-loop that would give us a quote as long as the year value of the date does not exceed 2016. This is how this while-loop would look.

 

Bank Programming

 

 

Bank Code Example

 

Putting it All Together in a Quick Balance Sheet Project

 

The above was a lot of information, equivalent to two to four lectures in a college-level course. We are impressed that you made it this far as the majority of bankers have already given up on learning the basics of code.

 

However, the following walk-through should help you to better understand these tools. In this quick project, we will add up an example balance sheet of assets, liabilities, and equity using a function and a for-loop. Then we will verify if the statement balances using an if-statement.

 

Follow this link and find a template to start with. We prebuilt a couple lists of asset, liability, and equity values. Press the arrow button to run your program at any point.

 

Python Code for Banks

 

The first thing we need to do is create a function to add values in a list together. Because of the versatility of functions, we can use the same function for all three lists. This function will add together the assets, liabilities, and equity to get the totals.

 

To do this, we need to define the function in Python using a list as a parameter. Then we must initialize a variable to zero; this will be our return value. We must then build a for-loop to add each component of the list to sum, and finally, we must return the sum. You can use print statements to check if you wrote your program correctly. Below is how we wrote our function (there are multiple correct answers).

 

Programming for Bankers

 

Second, we need to define our verifying function. In this function, we will use three parameters and an if-statement to verify if the totals in the accounting equation balance. If it does balance, we should print something to show that. If it does not, we should include an else statement to print that the equation does not balance. Below is how we wrote the function.

 

Programming for Bankers

 

We can call the function later by writing “check(assets, liabilities, equity).”

 

Below is our fully built out code with comments that should help with any confusion here: http://py3.codeskulptor.org/#user301_zwPJ3ho1jqS534p_3.py

 

Computer programming is not just the future of banking; it is the present. In the next installment of this blog series, we plan on discussing machine learning and walk you through building your own algorithm.

 

 

About the Author: This is a guest post by Dan Kim, an analyst at CenterState Bank.