Learning finance concepts using go-finance

Introduction

I have been interested in accounting and finance for years. I learned how to make income statements and balance sheets before I wrote my first hello world program. Most developers’ knowledge of finance tends to be limited to around knowing how stock options or RSUs work but I think it is important to be familiar with some basic concepts.

Valuations

When you hear Apple is worth ~two trillion dollars or that the market cap of Facebook is ~seven hundred and ninety billion dollars - what does that actually mean? It means that the company is valued at that much i.e. the price per share times the number of shares outstanding is that much. But why though? Who decides that? Where is this share price coming from? Wall street dude bros will be quick to say market forces and they are correct to an extent.

Let’s look at it from a more core financial perspective - this will also help nail down the golden rule of finance.

Golden rule of finance

The price of an asset is the net present value of its future cash flows.

So when you hear that a company is worth X number of dollars - what it means is that company is expected to earn X dollars in its entirety in today’s dollar value [present value].

What does earn mean?

Earn means the net cashflows of the company plus the terminal value. We need to cap our valuation calculation to a certain timeframe and can not calculate it till the end of time realistically - terminal value is either how much we expect the company to bring in cashflow in perpetuity till the end of time or how much we expect it will sell for.

What does present value mean?

A dollar today is worth more than the same dollar next year. Present value means accounting for the time value of money i.e. using the current value of a dollar that is expected to come in the future.

How do I calculate the present value?

To calculate the present value of money - you first need a discount factor to discount future cash flows into today’s dollar value. Discount factor is the interest rate or the rate of return on the investment. The formula is Present value = Future value/(1 + discount factor)^n – where n is the number of periods such as years.

Putting it all together

If a company is expected to bring in $1000 in year 1, $2000 in year 2, and be sold for $3000 in year 3 - the cash flow table would look something like the following

Year 1 Year 2 Year 3 [Terminal value]
$1000 $2000 $3000

If the discount factor is 5% - then the present vavlue of its future cash flows is

1
2
3
Present Value = 1000/1.05 + 2000/1.05^2 + 3000/1.05^3
Present Value = 952.38 + 1814.106 + 2591.51
Present Value = 5358

We have discounted the future cash flows to get the present value. The company is expected to earn $5358 over three years in today’s dollar value. If there are 1000 shares - then we expect each share to be priced at $5.358. If the market price is higher/lower that means the company is over/under valued or there is something missing in our analysis.

Show me the code

Generally these analyses are not done by hand. Undergrad finance students are still expected to draft the cash flow table for learning purposes but for the rest of us we have computers that can do a lot of the manual work.

A really useful library I stumbled upon is Go-finance. It contains many utility functions around the time value of money, interest rates, depreciation etc. Let’s walk through some of the functions provided.

Rates

Effective vs nominal rate?

Nominal rate is what is quoted on the product i.e. a credit card that charges 21% interest. Effective rate is different from the nominal rate as it takes into account the compounding effect. If the credit card company charges 21% interest compounded monthly - the effective interest rate charged by the company is more than 21% because you are paying interest on the interest i.e. compounding interest. What is the effective interest rate charged?

1
2
// func EffectiveRate(nominal float64, numPeriods int) (float64, error)
r, _ := fin.EffectiveRate(0.21, 12) // 0.23143 or 23.14%

Cashflow

Net present value

Doing the example above using go-finance would look something like

1
2
// func NetPresentValue(rate float64, values []float64) float64
r := fin.NetPresentValue(0.05, []float64{1000, 2000, 3000}) // 5357.95

Internal rate of return

Up til now we have only looked at positive cash flows but it is possible to have negative cash flows as well such as during pandemic years or in the initial investment year. Internal rate of return is the discount factor that will lead to the present value of future cash flows to be equal to zero i.e. what is the minimum rate of return required to break even on this investment?

If a company is expected to bring invest $5000 in year 1 and have cashflows of $5000 in year 2, and be sold for $1000 in year 3 - the cash flow table would look something like the following

Year 1 Year 2 Year 3 [Terminal value]
($5000) $5000 $1000
1
2
3
4
// func InternalRateOfReturn(values []float64, guess float64) (float64, error)
r, _ := fin.InternalRateOfReturn([]float64{-5000, 5000, 1000}, 0.3) // 0.1708 - 17.08%
// func NetPresentValue(rate float64, values []float64) float64
fin.NetPresentValue(r, []float64{-5000, 5000, 1000}) // -3.410605131648481e-13 ~= 0

Time value money

Future value

What is the future value of $100 invested every year end for 24 years at 5% return?

1
2
// func FutureValue(rate float64, numPeriods int, pmt float64, pv float64, paymentType int) (fv float64, err error)
fin.FutureValue(0.05, 24, -100, 0, fin.PayEnd) // 4450.199

We use negative because it is a cash outflow.

Payment

How much do I need to invest every year for 40 years at 7% return to have 1 million dollars at the end?

1
2
// func Payment(rate float64, numPeriods int, pv float64, fv float64, paymentType int) (pmt float64, err error)
fin.Payment(0.07, 40, 0, 1000000, fin.PayEnd) // 5009.1388

Bonds

TBillPrice

A treasury bill is a short-term debt backed by the government. T-Bills generally do not offer regular interest payments - instead they are typically sold at a price lower than the face value. When the T-Bill matures - the face value is paid out and the difference is considered to be the interest earned.

To calculate the price of a T-Bill that has a rate of 5% and matures 3 months from today

1
2
// func TBillPrice(settlement int64, maturity int64, discount float64) (float64, error)
fin.TBillPrice(1606860585, 1614636282, 0.05) // 98.75

TBillYield

To get the yield of a T-Bill given its price we can do the opposite

1
2
// func TBillYield(settlement int64, maturity int64, price float64) (float64, error)
fin.TBillYield(1606860585, 1614636282, 98.75004870756173) // 0.0506

Depreciation

Depreciation is an accounting concept to write-off the fall in the value of an asset, such as a car. It allows for expensing a certain portion of the asset’s value every year. Assets such as land are never depreciated.

The two common ways of calculating depreciation are straight-line and declining. Straight line is the easier one, where the cost of the asset is evenly distributed among the expected years of usage minus any salvage value i.e. Depreciation per year = (Purchase Price of Asset - Salvage Value) / Estimated years of life.

Declining on the other hand is considered an accelerated method of depreciation as the depreciation per year declines with the asset’s age.

Luckily, both these methods are provided by the go-finance library.

What is the depreciation per year for a car that costs $30,000 with a salvage value of $5000 and an estimated life of 5 years using Straight line method?

1
2
// func DepreciationStraightLine(cost float64, salvage float64, life int) (float64, error)
fin.DepreciationStraightLine(30000, 5000, 5) // 5000

What is the depreciation for the first year for a car that costs $30,000 with a salvage value of $5000 and an estimated life of 5 years using declining method?

1
2
// func DepreciationFixedDeclining(cost float64, salvage float64, life int, period int, month int) (float64, error)
fin.DepreciationFixedDeclining(30000, 5000, 5, 1, 12) // 9030

Side note on depreciation: Depreciation for tax purposes is different from depreciation for accounting purposes [shown above]. For tax, accounting depreciation is added back to profits and a different class of allowance is calculated and deducted known as Capital Cost Allowance or CCA.

Conclusion

It is important to venture out and be informed about domains other than tech. Go-finance gives a good tech-y way to get into learning finance concepts. Being knowledgable in other domains will change the way you approach software and always remember present value of future cash flows.