Plotting results from Monte carlo simulation in Python

Question:

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.

Asked By: bowlcutinmatrix

||

Answers:

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()
Answered By: 吴慈霆
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.