Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Plotting results from Monte carlo simulation in Python

runs = 1000000

import matplotlib.pyplot as plt
import random
import numpy as np
from scipy.stats import norm
from scipy.stats import uniform
import seaborn as sns
import pandas 

def steadystate():
    p=0.88
    Cout=4700000000
    LambdaAER=0.72
    Vol=44.5
    Depo=0.42
    Uptime=0.1
    Effic=0.38
    Recirc=4.3
    x = random.randint(86900000,2230000000000)
    conc = ((p*Cout*LambdaAER)+(x/Vol))/(LambdaAER+Depo+(Uptime*Effic*Recirc))
    return conc

x = 0
while x < runs:
    #results = steadystate (Faster)
    results = np.array([steadystate() for _ in range(runs)])
    print(results)
    x+=1
    
ax = sns.distplot(results,
                  bins=100,
                  kde=True,
                  color='skyblue',
                  hist_kws={"linewidth": 15,'alpha':1})
ax.set(xlabel='Uniform Distribution ', ylabel='Frequency')

This was my original code which plotted the results from the while loop, however it would take fairly long to print out the plotted data. Plotted data looks something like this:Correctly plotted data

import numpy.random
runs = 1000000000

import matplotlib.pyplot as plt
import random
import numpy as np
from scipy.stats import norm
from scipy.stats import uniform
import seaborn as sns
import pandas 

#Simulation Function
def steadystate(count):  
    p=0.88
    Cout=4700000000
    LambdaAER=0.72
    Vol=44.5
    Depo=0.42
    Uptime=0.1
    Effic=0.38
    Recirc=4.3
    x = numpy.random.randint(86900000, 2230000000000, dtype=np.int64)  

    conc = ((p*Cout*LambdaAER)+(x/Vol))/(LambdaAER+Depo+(Uptime*Effic*Recirc))
    return conc

x = 0
while x < runs:
    results = steadystate(runs)
    print(results)
    x+=1
#While loop to run iterations of the simulation for x number of runs
    
#Using seaborn to plot the results in a uniform distribution
ax = sns.distplot(results,
                  bins=100,
                  kde=True,
                  color='skyblue',
                  hist_kws={"linewidth": 15,'alpha':1})
ax.set(xlabel='Uniform Distribution ', ylabel='Frequency')

This is my updated code which prints out values much quicker, however, it doesnt plot the data correctly as it just plots this:
Incorrectly plotted data

Am I using seaborns plotting wrong in this instance? Any suggestions would be greatly appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I modified a little to the part where you generates results.

import matplotlib.pyplot as plt
import random
import numpy as np
import seaborn as sns

runs = 1000000

def steadystate():
    p=0.88
    Cout=4700000000
    LambdaAER=0.72
    Vol=44.5
    Depo=0.42
    Uptime=0.1
    Effic=0.38
    Recirc=4.3
    x = random.randint(86900000,2230000000000)
    conc = ((p*Cout*LambdaAER)+(x/Vol))/(LambdaAER+Depo+(Uptime*Effic*Recirc))
    return conc

results = np.array([steadystate() for _ in range(runs)])
print(results)

ax = sns.distplot(results,
                  bins=100,
                  kde=True,
                  color='skyblue',
                  hist_kws={"linewidth": 15,'alpha':1})
ax.set(xlabel='Uniform Distribution ', ylabel='Frequency')
plt.show()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading