kelly-paper-experiment/main.go

53 lines
1.3 KiB
Go
Raw Permalink Normal View History

2021-05-20 22:39:13 +00:00
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
2021-05-20 22:57:57 +00:00
// Random seed based on time when program runs
2021-05-20 22:39:13 +00:00
rand.Seed(time.Now().UTC().UnixNano())
2021-05-20 22:57:57 +00:00
// Initialize variables
var startingBalance, investedPercentage, winningPercentage float64
startingBalance = 100
2021-05-20 22:39:13 +00:00
investedPercentage = 0.80
winningPercentage = 0.70
discreteCompoundingPeriods := 200
numSimulations := 16
outputChannel := make(chan float64)
2021-05-20 22:57:57 +00:00
// Start simulations
2021-05-20 22:39:13 +00:00
for i := 0; i < numSimulations; i++ {
2021-05-20 22:57:57 +00:00
go simulation(startingBalance, investedPercentage, winningPercentage, discreteCompoundingPeriods, outputChannel)
2021-05-20 22:39:13 +00:00
}
2021-05-20 22:57:57 +00:00
// Wait for simulations to finish and write output
2021-05-20 22:39:13 +00:00
for i := 0; i < numSimulations; i++ {
fmt.Println(<-outputChannel)
}
}
2021-05-20 22:57:57 +00:00
// discrete period, symmetric payoff simulator
2021-05-20 22:39:13 +00:00
func simulation(startingBalance, investedPercentage, winningPercentage float64, discreteCompoundingPeriods int, outputChannel chan float64) {
balance := startingBalance
2021-05-20 22:57:57 +00:00
// Each iteration is one bet
2021-05-20 22:39:13 +00:00
for i := 0; i < discreteCompoundingPeriods; i++ {
2021-05-20 22:57:57 +00:00
// Each bet is a fixed percentage of the balance
betAmount := balance * investedPercentage
2021-05-20 22:39:13 +00:00
if rand.Float64() <= winningPercentage {
// you win
2021-05-20 22:57:57 +00:00
balance += betAmount
2021-05-20 22:39:13 +00:00
} else {
// you lose
2021-05-20 22:57:57 +00:00
balance -= betAmount
2021-05-20 22:39:13 +00:00
}
}
2021-05-20 22:57:57 +00:00
// Calculate and report the result on the outputChannel
2021-05-20 22:39:13 +00:00
outputChannel <- (balance / startingBalance)
}