Skip to content

Algorithm

An explicit procedure turns permitted inputs into a result. Trace a cashflow total, state its contract, prove its progress and separate correctness from computational cost.

An algorithm is an explicit, computable procedure for obtaining a result from permitted inputs. For a task that must return an answer, its steps and stopping rule must ensure it finishes on those inputs. In quant work, that turns “calculate the cash balance” into a process whose assumptions, output and cost can be checked.

An algorithm can be described in words or pseudocode before anyone writes a program. A Python implementation adds the language's types, arithmetic and execution rules. The same high-level procedure can have several implementations, with different speed and numerical behaviour.

A small contract before the steps

Suppose a fictional ledger contains cashflows of USD 30, −10 and 5. Positive amounts are receipts; negative amounts are payments. We want the undiscounted net cashflow, USD 25.

Part of the contractWhat it means here
InputA finite list of signed, whole-dollar USD amounts; no missing values
OutputOne integer: their net total in USD
Empty inputReturn zero
Side effectsLeave the input list unchanged
Financial scopeNo currency conversion, discounting or interest accrual

The procedure is:

  1. Set a running total to zero.
  2. Visit each cashflow in the supplied order and add it to the total.
  3. After the last cashflow, return the total.
“Find the total” alone specifies the goal. These steps specify a way to reach it. A more elaborate data structure may change how inputs are stored and visited, but it does not remove the need for a precise contract.
Fictional USD cashflow procedure: begin with total 0, add 30 to reach 30, add minus 10 to reach 20, and add 5 to return 25. After each row the total equals the sum of processed cashflows.
Fictional USD cashflow procedure: begin with total 0, add 30 to reach 30, add minus 10 to reach 20, and add 5 to return 25. After each row the total equals the sum of processed cashflows.
A running-total algorithm visits each fictional USD cashflow once. Its invariant connects every intermediate state to the required output.

Why the procedure is correct

After processing kk cashflows, the running total equals the sum of exactly those first kk cashflows. This is a loop invariant: a statement that remains true as the loop advances.

It holds initially because no cashflows have been processed and the total is zero. Adding the next cashflow preserves it. At the end, all cashflows have been processed, so the invariant gives the promised total. Separately, termination follows because each iteration consumes one item from a finite list that the procedure does not extend.

These are two different checks: the invariant explains the answer; the progress argument explains why we reach it. A numerical solver also needs a stopping policy, such as a tolerance plus an iteration limit and an explicit failure result. “Repeat until accurate” leaves essential behaviour unspecified.

One Python implementation

The loop is kept explicit so that its state matches the diagram. This block was executed with Python 3.14.4.

def net_cashflow(cashflows_usd: list[int]) -> int: """Sum a finite list of whole-dollar USD cashflows without changing it.""" total_usd = 0 for cashflow_usd in cashflows_usd: total_usd += cashflow_usd return total_usd cashflows_usd = [30, -10, 5] print(net_cashflow(cashflows_usd)) print(net_cashflow([])) print(cashflows_usd) # 25 # 0 # [30, -10, 5]
The annotations document the intended inputs; they do not validate them at runtime. For this contract, Python integers keep the arithmetic exact. Fractional cashflows require an explicit unit and representation policy, such as integer cents; switching to floating-point arithmetic can introduce rounding and order sensitivity.
Python's built-in sum(cashflows_usd) also returns 25 here. A shorter call does not imply less computational work, and its internal handling of other numeric types need not match this explicit loop. The official documentation describes sum and its version-specific floating-point changes.

Correct, efficient and financially appropriate

For nn cashflows, this loop performs nn additions and keeps one running total. Under the usual fixed-cost arithmetic model, that is O(n)O(n) time and O(1)O(1) extra storage, excluding the input. These time complexity and space complexity claims describe growth, not measured seconds or bytes. Python integers can grow in bit length, so arbitrarily large amounts require a finer arithmetic-cost model.

Correctness also depends on the question. This algorithm correctly nets same-currency amounts, but it cannot price dated payments: that needs dates and discount factors. Nor does “algorithm” imply a trading strategy or artificial intelligence. A ledger total, a sorting procedure and a trading decision rule are all possible algorithms; each solves a different specified problem.

To practise the implementation, Python Syntax and Control Flow traces branches and finite loops (free with sign-in; public preview). Algorithms and Computational Thinking develops procedures, programs and failure modes (membership; public preview).

Sources

Last updated
  • programming
  • algorithms
  • correctness
  • complexity
← Back to the glossaryBack to top ↑