0
$\begingroup$

For no practical reasons whatsoever, I designed a simple Monte Carlo simulation in python.

import random
import numpy as np

class Part:
    def __init__(self,exp_death=80):
        self.age = 0
        self.exp_death = exp_death
        self.alive = True
        
    def survive(self,days=1):
        self.age += days
        event = random.uniform(0,1)
        surv = 1/(1 + np.exp(-  (self.exp_death - self.age)))
        if event >= surv:
            self.alive = False
            
    def test(self):
        while self.alive:
            self.survive()

data = []
for i in range(100):
    p = Part()
    p.test()
    data.append(p.age)

And now I can take a look at the mean age of death and standard deviation:

print(np.average(data), np.std(data)) 
>>>
79.74 1.616292052817188

The variance is extremely tight around the expected death. I'm curious, simply for the sake of learning, how could I increase or decrease the variance in this MC simulation? In other words, how could create a variance knob to control in these simulations?

Again, this has no practical motivation; I'm just curious.

$\endgroup$

1 Answer 1

0
$\begingroup$

A simple way is to add a scale coefficient to this line:

surv = 1/(1 + np.exp(-scale * (self.exp_death - self.age)))

A smaller value will increase the variance (relative to the implicit value of 1 in your simulation).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.