jeudi 1 mars 2018

Python: How can I fix my drawn "random" values s.t. function calls are consistent?

I have:

np.random.seed(123)
var_v = 0.007 ** 2
T = 100
rho   = 0.9

def v_t(var_v, T):
    v_t_ = np.zeros([T,1])
    v_t_[1:T] = (var_v ** 0.5) * np.random.randn(len(v_t_) - 1, 1)
    return v_t_

def s_t(rho, T):
    v_t_ = v_t(var_v, T)
    s_t_ = np.zeros([T,1])
    s_t_[0] = 0
    for t in range(1,T):
        s_t_[t] = rho *s_t_[t-1] + v_t_[t]
    return s_t_

However every time I call one of the values, i.e.

s_t(rho, T) "or" v_t(var_v, T)

the right value is shown. But directly afterwards, when I call the other value, the value is wrong. After I clear my namespace, when I call the functions in the mirrored sequence, the same holds true.

I suspect that this is because new values are drawn by np.random.randn. How can I easily fix the drawn values s.t. I get the right values calling s_t and v_t?




Aucun commentaire:

Enregistrer un commentaire