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