Central limit theorems (CLT) are a class of theorems in probability theory stating that the sum of a sufficiently large number of weakly dependent random variables that have approximately the same scale (none of the terms dominates, does not make a decisive contribution to the sum), has a distribution close to normal. Since many random variables in applications are formed under the influence of several weakly dependent random factors, their distribution is considered normal. In this case, the condition must be observed that none of the factors is dominant. Central limit theorems in these cases justify the application of the normal distribution.
Following below is python code to be run by flask framework
(.env) boris@boris-All-Series:~/flaskmatplotlib$ cat flaskMatplotlib7.py
import io
from flask import Response
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
from flask import Flask
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
plt.rcParams["figure.figsize"] = [14.50, 10.50]
plt.rcParams["figure.autolayout"] = True
app = Flask(__name__)
@app.route('/print-plot')
def plot_png():
fig = Figure()
axes = fig.add_subplot(1, 1, 1)
axes.set_title('CLT via flask&&matplotlib, Refresh Screen a couple of times',fontsize = 15, color = 'black')
n = 10000
avg = []
for i in range(1,n):
a = np.random.randint(1,7,10)
avg.append(np.average(a))
# Normalise this histogram too
axes.hist(avg[0:], 20, density=True, stacked=True)
avg1 = []
for i in range(1,n):
a = np.random.randint(1,7,10)
avg1.append(np.average(a))
# Normalise this histogram too
axes.hist(avg1[0:], 20, density=True, stacked=True)
avg2 = []
for i in range(1,n):
a = np.random.randint(1,7,10)
avg2.append(np.average(a))
# Normalise this histogram too
axes.hist(avg2[0:], 20, density=True, stacked=True)
avg3 = []
for i in range(1,n):
a = np.random.randint(1,7,10)
avg3.append(np.average(a))
# Normalise this histogram too
axes.hist(avg3[0:], 20, density=True, stacked=True)
zscore = stats.zscore((avg[0:]+avg1[0:]+avg2[0:]+avg3[0:]))
mu, sigma = np.mean(avg+avg1+avg2+avg3), np.std(avg+avg1+avg2+avg3)
s = np.random.normal(mu, sigma, 10000)
# Create the bins and histogram
count, bins, ignored = axes.hist(s, 20, density=True, stacked=True)
# Use scipy.stats implementation of the normal pdf
# Plot the distribution curve
x = np.linspace(1.5, 5.5, num=100)
axes.plot(x, stats.norm.pdf(x, mu, sigma))
output = io.BytesIO()
FigureCanvas(fig).print_png(output)
return Response(output.getvalue(), mimetype='image/png')
$ export FLASK_ENV=development
$ export FLASK_DEBUG=1
$ export FLASK_APP=flaskMatplotlib7.py
(.env) boris@boris-All-Series:~/flaskmatplotlib$ flask run
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Serving Flask app 'flaskMatplotlib7.py'
* Debug mode: on
* Running on http://127.0.0.1:5000 (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 452-793-879
Another version of python code. Just looping when creating histograms.
(.env) boris@boris-All-Series:~/flaskmatplotlib$ cat flaskMatplotlib10.py
import io
from flask import Response
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
from flask import Flask
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
import warnings
warnings.filterwarnings('ignore')
plt.rcParams["figure.figsize"] = [15.50, 11.50]
plt.rcParams["figure.autolayout"] = True
app = Flask(__name__)
@app.route('/print-plot')
def plot_png():
fig = Figure()
axes = fig.add_subplot(1, 1, 1)
axes.set_title('Verification CLT via flask&&matplotlib, Refresh Screen a couple of times',fontsize = 15, color = 'black')
n = 10000
longlst = []
sigmalst = []
lst = ["avg","avg1","avg2","avg3"]
for avs in lst:
avs = []
for i in range(1,n):
a = np.random.randint(1,7,10)
avs.append(np.average(a))
# Normalise this histogram too
axes.hist(avs[0:], 20, density=True, stacked=True)
longlst = longlst + avs[0:]
sigmalst = sigmalst + avs[:]
zscore = stats.zscore(longlst)
mu, sigma = np.mean(sigmalst), np.std(sigmalst)
s = np.random.normal(mu, sigma, 10000)
# Create the bins and histogram
count, bins, ignored = axes.hist(s, 20, density=True, stacked=True)
# Use scipy.stats implementation of the normal pdf
# Plot the distribution curve
x = np.linspace(1.5, 6.5, num=100)
axes.plot(x, stats.norm.pdf(x, mu, sigma))
output = io.BytesIO()
FigureCanvas(fig).print_png(output)
return Response(output.getvalue(), mimetype='image/png')
(.env) boris@boris-All-Series:~/flaskmatplotlib$ flask run
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Serving Flask app 'flaskMatplotlib10.py'
* Debug mode: on
* Running on http://127.0.0.1:5000 (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 255-673-976