Search code examples
pythondirected-acyclic-graphs

How to compute the average value of the simulation (loop)?


def run_experiment():
    from notears import utils
    # utils.set_random_seed(1) this line cannot be used to ensure different outcomes in each round of the loop

    n, d, s0, graph_type, sem_type = 1000, 20, 20, 'ER', 'gauss'
    B_true = utils.simulate_dag(d, s0, graph_type)
    W_true = utils.simulate_parameter(B_true)
    np.savetxt('W_true.csv', W_true, delimiter=',')

    X = utils.simulate_linear_sem(W_true, n, sem_type)
    np.savetxt('X.csv', X, delimiter=',')

    W_est = notears_linear(X, lambda1=0.1, loss_type='l2')
    assert utils.is_dag(W_est)
    np.savetxt('W_est.csv', W_est, delimiter=',')
    acc = utils.count_accuracy(B_true, W_est != 0)
    print(acc)

if __name__ == '__main__':
    num_experiments = 3
    start = timer()
    for _ in range(num_experiments):
        run_experiment()
    end = timer()
    print(end - start)

The above simulation gives me each rounds of the loop. But what I want is the average value of outcomes, rather than see the outcomes of individual rounds. How to update the code?

The code is from https://github.com/xunzheng/notears/blob/master/notears/linear.py

(I changed a little bit to add simulation part and computing time part.)


Solution

  • Try returning the value from your run_experiment() function and then recording it in a list:

    def run_experiment():
        from notears import utils
        # utils.set_random_seed(1) this line cannot be used to ensure different outcomes in each round of the loop
    
        n, d, s0, graph_type, sem_type = 1000, 20, 20, 'ER', 'gauss'
        B_true = utils.simulate_dag(d, s0, graph_type)
        W_true = utils.simulate_parameter(B_true)
        np.savetxt('W_true.csv', W_true, delimiter=',')
    
        X = utils.simulate_linear_sem(W_true, n, sem_type)
        np.savetxt('X.csv', X, delimiter=',')
    
        W_est = notears_linear(X, lambda1=0.1, loss_type='l2')
        assert utils.is_dag(W_est)
        np.savetxt('W_est.csv', W_est, delimiter=',')
        acc = utils.count_accuracy(B_true, W_est != 0)
        return acc # <==== Add return statement
    
    if __name__ == '__main__':
        num_experiments = 3
        start = timer()
        results = []
        for _ in range(num_experiments):
            results.append(run_experiment()) # <==== Store results
        end = timer()
        print(end - start)
        print(sum(results) / len(results)) # <==== Calculate mean