I want to pass a function which generates random normal numbers to another function, do some calculations and pass again the random function x times. At the end there should be a Dataframe with x colums with diffrent randomly generated outcomes.
My code looks like this:
JavaScript
x
15
15
1
timeframe = 10
2
nr_simulations = 10
3
mean_vec = np.random.rand(10)
4
cov_mat = np.random.rand(10,10)
5
r_n = np.zeros((timeframe, nr_simulations))
6
7
def test_function(func, timeframe, nr_simulations):
8
for i in range(0, nr_simulations):
9
r_n[:,i] = func.mean(axis=1)
10
11
12
def simulate_normal_numbers(mean_vec, cov_mat, timeframe):
13
return np.random.multivariate_normal(mean_vec, cov_mat, timeframe)
14
15
But this gives me always identical columns.
JavaScript
1
2
1
test_function(simulate_normal_numbers(mean_vec, cov_mat, timeframe), timeframe, nr_simulations)
2
Advertisement
Answer
I don’t think you can pass the function like that. You should pass the function and the argument separately
Something like
JavaScript
1
21
21
1
import numpy as np
2
timeframe = 10
3
nr_simulations = 10
4
mean_vec = np.random.rand(10)
5
6
cov_mat = np.random.rand(10,10)
7
cov_mat = np.maximum( cov_mat, cov_mat.transpose() )
8
r_n = np.zeros((timeframe, nr_simulations))
9
10
def test_function(func, timeframe, nr_simulations, arg):
11
for i in range(0, nr_simulations):
12
r_n[:,i] = func(*arg).mean(axis=1)
13
14
15
16
def simulate_normal_numbers(mean_vec, cov_mat, timeframe):
17
return np.random.multivariate_normal(mean_vec, cov_mat, timeframe)
18
19
test_function(simulate_normal_numbers , timeframe, nr_simulations,arg = (mean_vec, cov_mat, timeframe))
20
print(r_n)
21
be aware that the cov matrix should be symmetrical and positive.