comments, rename variables to clarify
This commit is contained in:
parent
adad0973f9
commit
a6a06adb69
23
main.go
23
main.go
@ -7,37 +7,46 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// Random seed based on time when program runs
|
||||||
rand.Seed(time.Now().UTC().UnixNano())
|
rand.Seed(time.Now().UTC().UnixNano())
|
||||||
|
|
||||||
var balance, investedPercentage, winningPercentage float64
|
// Initialize variables
|
||||||
balance = 100
|
var startingBalance, investedPercentage, winningPercentage float64
|
||||||
|
startingBalance = 100
|
||||||
investedPercentage = 0.80
|
investedPercentage = 0.80
|
||||||
winningPercentage = 0.70
|
winningPercentage = 0.70
|
||||||
discreteCompoundingPeriods := 200
|
discreteCompoundingPeriods := 200
|
||||||
numSimulations := 16
|
numSimulations := 16
|
||||||
outputChannel := make(chan float64)
|
outputChannel := make(chan float64)
|
||||||
|
|
||||||
|
// Start simulations
|
||||||
for i := 0; i < numSimulations; i++ {
|
for i := 0; i < numSimulations; i++ {
|
||||||
go simulation(balance, investedPercentage, winningPercentage, discreteCompoundingPeriods, outputChannel)
|
go simulation(startingBalance, investedPercentage, winningPercentage, discreteCompoundingPeriods, outputChannel)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wait for simulations to finish and write output
|
||||||
for i := 0; i < numSimulations; i++ {
|
for i := 0; i < numSimulations; i++ {
|
||||||
fmt.Println(<-outputChannel)
|
fmt.Println(<-outputChannel)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// discrete period, symmetric payoff simulator
|
||||||
func simulation(startingBalance, investedPercentage, winningPercentage float64, discreteCompoundingPeriods int, outputChannel chan float64) {
|
func simulation(startingBalance, investedPercentage, winningPercentage float64, discreteCompoundingPeriods int, outputChannel chan float64) {
|
||||||
balance := startingBalance
|
balance := startingBalance
|
||||||
|
|
||||||
|
// Each iteration is one bet
|
||||||
for i := 0; i < discreteCompoundingPeriods; i++ {
|
for i := 0; i < discreteCompoundingPeriods; i++ {
|
||||||
investedAmount := balance * investedPercentage
|
// Each bet is a fixed percentage of the balance
|
||||||
|
betAmount := balance * investedPercentage
|
||||||
if rand.Float64() <= winningPercentage {
|
if rand.Float64() <= winningPercentage {
|
||||||
// you win
|
// you win
|
||||||
balance += investedAmount
|
balance += betAmount
|
||||||
} else {
|
} else {
|
||||||
// you lose
|
// you lose
|
||||||
balance -= investedAmount
|
balance -= betAmount
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Calculate and report the result on the outputChannel
|
||||||
outputChannel <- (balance / startingBalance)
|
outputChannel <- (balance / startingBalance)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user