Python Forum
How to do 100 runs simulation based on the current codes?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to do 100 runs simulation based on the current codes?
#1
The current codes are already on the website: https://github.com/xunzheng/notears

linear.py allows me to run one-time simulation. https://github.com/xunzheng/notears/blob.../linear.py

Currently, we fix the n, d, s0, graph_type, sem_type = 100, 20, 20, 'ER', 'gauss'.
And I can get a result of SHD. This is one-time run / simulation. (Don't get confused with the above n. The n denotes the number of observations in one simulated data set.) Now what I want is doing 100 simulations, i.e., repeat 100 times of linear.py file. Then I take the average of SHD to get my final desired number.

I also want to compute the run-time of the whole simulation.

But I don't know how to fulfill my goal.
Reply
#2
Take all that stuff that is under "if __name__ == '__main__': and put it in a function called main. Now you can runt it as many times as you want.

You might want to modify the code to append result to your save files. or maybe you return the values instead of writing them to a file. Guess that depends on how you need to process the results, and how they are formatted.
dududada likes this post
Reply
#3
Thank you for your hint. That's helpful. Now I can update the code:

`
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

    for _ in range(num_experiments):
        run_experiment()
`

So the above code can give me each three rounds of the loop. But I want to get the average value of shd of the whole loop, rather than seeing the outcome of each round of the loop.
deanhystad write Sep-03-2023, 02:12 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#4
(Sep-02-2023, 10:38 PM)deanhystad Wrote: Take all that stuff that is under "if __name__ == '__main__': and put it in a function called main. Now you can runt it as many times as you want.

You might want to modify the code to append result to your save files. or maybe you return the values instead of writing them to a file. Guess that depends on how you need to process the results, and how they are formatted.

Thank you for your hint. I have a follow-up question.
Reply
#5
What do you want to average, acc?
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=',')
    return utils.count_accuracy(B_true, W_est != 0)
 
if __name__ == '__main__':
    acc = [run_experiment() for _ in range(num_experiments)]
    print("Average acc =", sum(acc) / len(acc))
You should not use assert.
Reply
#6
(Sep-03-2023, 02:17 AM)deanhystad Wrote: What do you want to average, acc?
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=',')
    return utils.count_accuracy(B_true, W_est != 0)
 
if __name__ == '__main__':
    acc = [run_experiment() for _ in range(num_experiments)]
    print("Average acc =", sum(acc) / len(acc))
You should not use assert.

Yes, I want to get the average value of acc. The current acc gives me a list like this: {'fdr': 0.0, 'tpr': 0.95, 'fpr': 0.0, 'shd': 1, 'nnz': 19}
Reply
#7
That is a dictionary, not a list. Do you know what those fields mean? What is the math to compute the average?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Another program runs bho68 7 1,207 Nov-08-2022, 08:16 PM
Last Post: bho68
  Importing a function from another file runs the old lines also dedesssse 6 2,571 Jul-06-2021, 07:04 PM
Last Post: deanhystad
  How to create an Excel app that runs Python? felipe0216 3 2,285 May-31-2020, 01:19 AM
Last Post: ibutun
  Python Program Runs in Pycharm but not in Terminal Vbhardwaj2383 2 3,310 Apr-06-2020, 04:41 PM
Last Post: Vbhardwaj2383
  How to execute code WHILE a function runs t4keheart 4 2,693 Jan-27-2020, 01:47 PM
Last Post: t4keheart
  scheduled job only runs once Stan2292 4 2,891 Aug-19-2019, 08:18 AM
Last Post: fishhook
  Stock Rate of Change (based on open and current price) Not Working firebird 1 2,376 Jul-29-2019, 07:10 AM
Last Post: perfringo
  Help with simulation Geeseball 0 2,071 Oct-18-2018, 12:19 PM
Last Post: Geeseball
  Scheduler runs but then fails marciokoko 2 4,110 Jan-19-2017, 12:02 AM
Last Post: marciokoko

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020