In [1]:
```get_ipython().ast_node_interactivity = 'all'
import matplotlib.pyplot as plt
import numpy as np```
In [2]:
```vals = np.random.normal(0, 1, 10_000_000)

np.mean(vals), np.std(vals)```
Out [2]:
`(0.00010162346483672586, 0.9999363897125003)`
In [3]:
```# PDF

hist = np.histogram(vals, bins=100)

Xs = hist[1][:-1]
Ys = hist[0]

plt.plot(hist[1][:-1], hist[0])```
Out [3]:
`[]`
Out:
In [4]:
```# Approximate the PDF with a polynomial

pdf_poly = np.polyfit(Xs, Ys, 20)

pdf = lambda x: np.polyval(pdf_poly, x)

plt.plot(Xs, pdf(Xs))```
Out [4]:
`[]`
Out:
In [5]:
```# CDF
cdf_vals = []
s = 0.0

for x in Xs:
s += pdf(x)
cdf_vals.append(s)

cdf_vals = np.array(cdf_vals) / s

plt.plot(Xs, cdf_vals)```
Out [5]:
`[]`
Out:
In [17]:
```# Approximate the CDF with a polynomial

cdf_poly = np.polyfit(Xs, cdf_vals, 20)

cdf = lambda x: np.polyval(cdf_poly, x)

plt.plot(Xs, cdf(Xs))```
Out [17]:
`[]`
Out:
In [48]:
```# Approximate the inverse CDF with a polynomial
y = cdf(Xs)

# Learn the inverse function
import sklearn.neural_network
from sklearn.preprocessing import MinMaxScaler

inputscaler = MinMaxScaler()
outputscaler = MinMaxScaler()

m = sklearn.neural_network.MLPRegressor(hidden_layer_sizes=(100, 100, 100), max_iter=1000000, activation='logistic')

m.fit(inputscaler.fit_transform(y.reshape(-1, 1)), outputscaler.fit_transform(Xs.reshape(-1, 1)))

def inv_cdf(x):
inp = inputscaler.transform(x.reshape(-1, 1))
res = m.predict(inp).reshape(-1, 1)
return outputscaler.inverse_transform(res)

plt.plot(cdf(Xs), Xs)

xs = np.linspace(0, 1, 500)

_ = plt.plot(xs, inv_cdf(xs))```
Out:
```/usr/lib/python3.11/site-packages/sklearn/neural_network/_multilayer_perceptron.py:1624: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
```
Out [48]:
```﻿#sk-container-id-26{--sklearn-color-text: black;--sklearn-color-line: gray;--sklearn-color-unfitted-level-0: #fff5e6;--sklearn-color-unfitted-level-1: #f6e4d2;--sklearn-color-unfitted-level-2: #ffe0b3;--sklearn-color-unfitted-level-3: chocolate;--sklearn-color-fitted-level-0: #f0f8ff;--sklearn-color-fitted-level-1: #d4ebff;--sklearn-color-fitted-level-2: #b3dbfd;--sklearn-color-fitted-level-3: cornflowerblue;--sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));--sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, white)));--sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));--sklearn-color-icon: #696969}@media (prefers-color-scheme: dark){#sk-container-id-26{--sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));--sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, #111)));--sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));--sklearn-color-icon: #878787}}#sk-container-id-26{color:var(--sklearn-color-text)}#sk-container-id-26 pre{padding:0}#sk-container-id-26 input.sk-hidden--visually{border:0;clip:rect(1px 1px 1px 1px);clip:rect(1px, 1px, 1px, 1px);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px}#sk-container-id-26 div.sk-dashed-wrapped{border:1px dashed var(--sklearn-color-line);margin:0 0.4em 0.5em 0.4em;box-sizing:border-box;padding-bottom:0.4em;background-color:var(--sklearn-color-background)}#sk-container-id-26 div.sk-container{display:inline-block !important;position:relative}#sk-container-id-26 div.sk-text-repr-fallback{display:none}div.sk-parallel-item,div.sk-serial,div.sk-item{background-image:linear-gradient(var(--sklearn-color-text-on-default-background), var(--sklearn-color-text-on-default-background));background-size:2px 100%;background-repeat:no-repeat;background-position:center center}#sk-container-id-26 div.sk-parallel-item::after{content:"";width:100%;border-bottom:2px solid var(--sklearn-color-text-on-default-background);flex-grow:1}#sk-container-id-26 div.sk-parallel{display:flex;align-items:stretch;justify-content:center;background-color:var(--sklearn-color-background);position:relative}#sk-container-id-26 div.sk-parallel-item{display:flex;flex-direction:column}#sk-container-id-26 div.sk-parallel-item:first-child::after{align-self:flex-end;width:50%}#sk-container-id-26 div.sk-parallel-item:last-child::after{align-self:flex-start;width:50%}#sk-container-id-26 div.sk-parallel-item:only-child::after{width:0}#sk-container-id-26 div.sk-serial{display:flex;flex-direction:column;align-items:center;background-color:var(--sklearn-color-background);padding-right:1em;padding-left:1em}#sk-container-id-26 div.sk-toggleable{background-color:var(--sklearn-color-background)}#sk-container-id-26 label.sk-toggleable__label{cursor:pointer;display:block;width:100%;margin-bottom:0;padding:0.5em;box-sizing:border-box;text-align:center}#sk-container-id-26 label.sk-toggleable__label-arrow:before{content:"▸";float:left;margin-right:0.25em;color:var(--sklearn-color-icon)}#sk-container-id-26 label.sk-toggleable__label-arrow:hover:before{color:var(--sklearn-color-text)}#sk-container-id-26 div.sk-toggleable__content{max-height:0;max-width:0;overflow:hidden;text-align:left;background-color:var(--sklearn-color-unfitted-level-0)}#sk-container-id-26 div.sk-toggleable__content.fitted{background-color:var(--sklearn-color-fitted-level-0)}#sk-container-id-26 div.sk-toggleable__content pre{margin:0.2em;border-radius:0.25em;color:var(--sklearn-color-text);background-color:var(--sklearn-color-unfitted-level-0)}#sk-container-id-26 div.sk-toggleable__content.fitted pre{background-color:var(--sklearn-color-fitted-level-0)}#sk-container-id-26 input.sk-toggleable__control:checked~div.sk-toggleable__content{max-height:200px;max-width:100%;overflow:auto}#sk-container-id-26 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before{content:"▾"}#sk-container-id-26 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label{color:var(--sklearn-color-text);background-color:var(--sklearn-color-unfitted-level-2)}#sk-container-id-26 div.sk-label.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label{background-color:var(--sklearn-color-fitted-level-2)}#sk-container-id-26 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label{background-color:var(--sklearn-color-unfitted-level-2)}#sk-container-id-26 div.sk-estimator.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label{background-color:var(--sklearn-color-fitted-level-2)}#sk-container-id-26 div.sk-label label.sk-toggleable__label,#sk-container-id-26 div.sk-label label{color:var(--sklearn-color-text-on-default-background)}#sk-container-id-26 div.sk-label:hover label.sk-toggleable__label{color:var(--sklearn-color-text);background-color:var(--sklearn-color-unfitted-level-2)}#sk-container-id-26 div.sk-label.fitted:hover label.sk-toggleable__label.fitted{color:var(--sklearn-color-text);background-color:var(--sklearn-color-fitted-level-2)}#sk-container-id-26 div.sk-label label{font-family:monospace;font-weight:bold;display:inline-block;line-height:1.2em}#sk-container-id-26 div.sk-label-container{text-align:center}#sk-container-id-26 div.sk-estimator{font-family:monospace;border:1px dotted var(--sklearn-color-border-box);border-radius:0.25em;box-sizing:border-box;margin-bottom:0.5em;background-color:var(--sklearn-color-unfitted-level-0)}#sk-container-id-26 div.sk-estimator.fitted{background-color:var(--sklearn-color-fitted-level-0)}#sk-container-id-26 div.sk-estimator:hover{background-color:var(--sklearn-color-unfitted-level-2)}#sk-container-id-26 div.sk-estimator.fitted:hover{background-color:var(--sklearn-color-fitted-level-2)}.sk-estimator-doc-link,a:link.sk-estimator-doc-link,a:visited.sk-estimator-doc-link{float:right;font-size:smaller;line-height:1em;font-family:monospace;background-color:var(--sklearn-color-background);border-radius:1em;height:1em;width:1em;text-decoration:none !important;margin-left:1ex;border:var(--sklearn-color-unfitted-level-1) 1pt solid;color:var(--sklearn-color-unfitted-level-1)}.sk-estimator-doc-link.fitted,a:link.sk-estimator-doc-link.fitted,a:visited.sk-estimator-doc-link.fitted{border:var(--sklearn-color-fitted-level-1) 1pt solid;color:var(--sklearn-color-fitted-level-1)}div.sk-estimator:hover .sk-estimator-doc-link:hover,.sk-estimator-doc-link:hover,div.sk-label-container:hover .sk-estimator-doc-link:hover,.sk-estimator-doc-link:hover{background-color:var(--sklearn-color-unfitted-level-3);color:var(--sklearn-color-background);text-decoration:none}div.sk-estimator.fitted:hover .sk-estimator-doc-link.fitted:hover,.sk-estimator-doc-link.fitted:hover,div.sk-label-container:hover .sk-estimator-doc-link.fitted:hover,.sk-estimator-doc-link.fitted:hover{background-color:var(--sklearn-color-fitted-level-3);color:var(--sklearn-color-background);text-decoration:none}.sk-estimator-doc-link span{display:none;z-index:9999;position:relative;font-weight:normal;right:.2ex;padding:.5ex;margin:.5ex;width:min-content;min-width:20ex;max-width:50ex;color:var(--sklearn-color-text);box-shadow:2pt 2pt 4pt #999;background:var(--sklearn-color-unfitted-level-0);border:0.5pt solid var(--sklearn-color-unfitted-level-3)}.sk-estimator-doc-link.fitted span{background:var(--sklearn-color-fitted-level-0);border:var(--sklearn-color-fitted-level-3)}.sk-estimator-doc-link:hover span{display:block}#sk-container-id-26 a.estimator_doc_link{float:right;font-size:1rem;line-height:1em;font-family:monospace;background-color:var(--sklearn-color-background);border-radius:1rem;height:1rem;width:1rem;text-decoration:none;color:var(--sklearn-color-unfitted-level-1);border:var(--sklearn-color-unfitted-level-1) 1pt solid}#sk-container-id-26 a.estimator_doc_link.fitted{border:var(--sklearn-color-fitted-level-1) 1pt solid;color:var(--sklearn-color-fitted-level-1)}#sk-container-id-26 a.estimator_doc_link:hover{background-color:var(--sklearn-color-unfitted-level-3);color:var(--sklearn-color-background);text-decoration:none}#sk-container-id-26 a.estimator_doc_link.fitted:hover{background-color:var(--sklearn-color-fitted-level-3)}

MLPRegressor(activation='logistic', hidden_layer_sizes=(100, 100, 100),
max_iter=1000000)

MLPRegressor?Documentation for MLPRegressoriFittedMLPRegressor(activation='logistic', hidden_layer_sizes=(100, 100, 100),
max_iter=1000000)
```
Out [48]:
`[]`
Out:
In [45]:
```# Sample from the distribution
xs = np.random.uniform(0, 1, 5000)
samples = inv_cdf(xs)

_ = plt.hist(samples, bins=100)```
Out: