Learn FSHARP-FINANCE with Real Code Examples
Updated Nov 27, 2025
Code Sample Descriptions
1
Calculate Compound Interest
let compoundInterest principal rate periods =
principal * ((1.0 + rate) ** periods)
let amount = compoundInterest 1000.0 0.05 10.0
printfn "Compound Interest Amount: %f" amount
Compute the compound interest for a given principal, rate, and number of periods.
2
Black-Scholes Option Pricing
open System
open MathNet.Numerics.Distributions
let blackScholesCall S K r sigma T =
let d1 = (log(S/K) + (r + 0.5 * sigma*sigma) * T) / (sigma * sqrt T)
let d2 = d1 - sigma * sqrt T
S * Normal.CDF(0.0,1.0,d1) - K * exp(-r*T) * Normal.CDF(0.0,1.0,d2)
let callPrice = blackScholesCall 100.0 100.0 0.05 0.2 1.0
printfn "Call Option Price: %f" callPrice
Calculate the price of a European call option using the Black-Scholes formula in F#.
3
Simulate Stock Price Path
open System
open MathNet.Numerics.Distributions
let simulateStockPath S0 mu sigma T steps =
let dt = T / float steps
let rnd = Normal(0.0,1.0)
let rec loop S n acc =
if n > steps then List.rev acc
else let Snext = S * exp((mu - 0.5 * sigma*sigma)*dt + sigma*sqrt(dt)*rnd.Sample())
loop Snext (n+1) (Snext::acc)
loop S0 1 [S0]
let path = simulateStockPath 100.0 0.05 0.2 1.0 10
printfn "%A" path
Simulate a simple geometric Brownian motion for stock price evolution.
4
Calculate Portfolio Return
let portfolioReturn weights returns =
List.map2 (*) weights returns |> List.sum
let weights = [0.6; 0.4]
let returns = [0.02; 0.03]
let pReturn = portfolioReturn weights returns
printfn "Portfolio Return: %f" pReturn
Compute the weighted return of a portfolio from individual asset returns.
5
Compute Portfolio Variance
let covMatrix = [[0.0004;0.0002];[0.0002;0.0003]]
let weights = [0.6;0.4]
let portfolioVariance =
List.mapi (fun i row -> List.mapi (fun j x -> x * weights.[i] * weights.[j]) row |> List.sum) covMatrix |> List.sum
printfn "Portfolio Variance: %f" portfolioVariance
Calculate portfolio variance given asset covariance matrix and weights.
6
Compute Portfolio Standard Deviation
let portfolioStdDev variance = sqrt variance
let stdDev = portfolioStdDev 0.00028
printfn "Portfolio Std Dev: %f" stdDev
Compute the standard deviation of a portfolio from variance.
7
Calculate Sharpe Ratio
let sharpeRatio expectedReturn riskFreeRate stdDev = (expectedReturn - riskFreeRate)/stdDev
let sr = sharpeRatio 0.025 0.01 0.0167
printfn "Sharpe Ratio: %f" sr
Compute the Sharpe ratio of a portfolio.
8
Simulate Multiple Stock Paths
let simulateMultiplePaths S0 mu sigma T steps nPaths =
[1..nPaths] |> List.map (fun _ -> simulateStockPath S0 mu sigma T steps)
let paths = simulateMultiplePaths 100.0 0.05 0.2 1.0 10 5
printfn "%A" paths
Simulate multiple correlated stock price paths using geometric Brownian motion.
9
Calculate Forward Price
let forwardPrice S r T = S * exp(r*T)
let F = forwardPrice 100.0 0.05 1.0
printfn "Forward Price: %f" F
Compute the forward price of an asset given spot price, risk-free rate, and time to maturity.
10
Discount Cash Flows
let discountCashFlows cashFlows r =
List.mapi (fun i cf -> cf / ((1.0 + r) ** float (i+1))) cashFlows |> List.sum
let pv = discountCashFlows [100.0;100.0;100.0] 0.05
printfn "Present Value: %f" pv
Compute present value of future cash flows.