Files
katrin-plasma/docs/masters/creiling/LaTeX/backup_python_scripts/Random Sampling.ipynb
T

48 KiB

In [1]:
import numpy as np
import os
import sys


from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
import tikzplotlib
In [2]:
import matplotlib.pyplot as plt
%matplotlib inline

import seaborn as sns

NEJM = sns.color_palette(palette = ['#bc3c29',
 '#0072bf',
 '#e18727',
 '#20854e',
 '#7876b1',
 '#6f99ad',
 '#ffdc91',
 '#ee4c97'])
sns.set(
    context='paper', 
    style='white', 
    palette=NEJM, 
    font='sans-serif', 
    font_scale=1, 
    color_codes=False, 
    rc = {'figure.dpi' : 100,
        'text.usetex' : True,
        'axes.labelsize' : 10,
        'xtick.labelsize' : 10, 
        'ytick.labelsize' : 10,
        'legend.fontsize' : 10,
        'xtick.bottom' : True, 
        'xtick.minor.bottom' : False,
        'ytick.left' : True,
        'ytick.minor.left' : False
        })
In [3]:
style_path = "/Users/ChristianReiling/Documents/Masterarbeit/mcelectron-analysis/science.mplstyle"
latex_path = "/Users/ChristianReiling/Documents/Masterarbeit/docs/LaTeX/pics"
plt.style.use(style_path)
In [4]:
import scipy.integrate
def f(x):
    sx = 1
    sy = 1
    dx = 0.5
    dy = 1
    x = (x - dx)/sx
    y = -sy * pow(x/0.5, 2) + sy*dy
    insym = 0.1*x + 0.1
    y = y*insym
    return y
f = np.vectorize(f)
In [5]:
n = 5
x = np.linspace(0,1, 100)
y = f(x)
norm = scipy.integrate.trapz(y,x)
y = y/norm
y[y<0] = 0
In [6]:
cum_int = scipy.integrate.cumtrapz(y,x)
boarders = np.linspace(0,1,n+1)
x_i = [np.argmax(cum_int>b) for b in boarders[1:-1]]
x_i = [0] + x_i + [99]
x_labels = ['$x_' + str(i) + '$' for i in np.arange(n+1)]
In [7]:
xs = x[x_i]
ys =1/(np.array(xs[1:]) - np.array(xs[:-1]))/(n)
In [8]:
fig, ax = plt.subplots(figsize=[10/2.54, 7/2.54])
ax.plot(x,y, color = 'black', linewidth = 2)
ax.set_xlim (0,1)
ax.set_ylim (0,1.9)

ax.set_xlabel(r'$x$', size = 12)
ax.set_ylabel(r'$p(x)$', size = 12)
ax.set_xticks(xs)
ax.set_xticklabels(x_labels, size = 12)
ax.set_yticks([])
ax.set_yticklabels([])
ax.grid(False)
#plt.x
for i in np.arange(n):
    ax.bar(xs[i], ys[i], width=(xs[i+1]-xs[i]), \
           align = 'edge', color = 'grey', edgecolor = 'black', linewidth = 1, alpha = 0.5)
fig.tight_layout()

fig.show()
fig.savefig(latex_path + "/monte/inversion.pdf", bbox_inches='tight')
fig.savefig(latex_path + "/monte/inversion.pgf", bbox_inches='tight')
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/figure.py:445: UserWarning: Matplotlib is currently using module://ipykernel.pylab.backend_inline, which is a non-GUI backend, so cannot show the figure.
  % get_backend())
In [9]:
fig, ax = plt.subplots(figsize=[12/2.54,8/2.54])
ax.plot(x,y, color = 'black', linewidth = 2)
ax.fill_between(x,y, color = 'grey', alpha = 0.5)

ax.vlines(0, 0, max(y), linestyles='solid', linewidth=1.2, linestyle = '--' )
ax.vlines(1, 0, max(y), linestyles='solid', linewidth=1.2, linestyle = '--' )
ax.hlines( max(y), 0, 1, linestyles='solid', linewidth=1.2, linestyle = '--' )

ax.text(0.5, 0.3, 'accept', fontsize = 14)
ax.text(0.08, 1.3, 'reject', fontsize = 14)

ax.set_xlim (-0.1,1.1)
ax.set_ylim (0,1.9)

ax.set_xlabel(r'$x$', size = 12)
ax.set_ylabel(r'$p(x)$', size = 12)
ax.set_xticks([0,1])
ax.set_xticklabels(['$a$', '$b$'], size = 12)
ax.set_yticks([0, max(y)])
ax.set_yticklabels(['0', '$p_\mathrm{max}$'], size = 12)
ax.grid(False)

fig.tight_layout()
fig.show()
fig.savefig(latex_path + "/monte/rejection.pdf", bbox_inches='tight')
fig.savefig(latex_path + "/monte/rejection.pgf", bbox_inches='tight')
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/figure.py:445: UserWarning: Matplotlib is currently using module://ipykernel.pylab.backend_inline, which is a non-GUI backend, so cannot show the figure.
  % get_backend())
In [10]:
bs = ys/ys.sum()
bs_stacked = bs.cumsum()
In [11]:
fig, ax = plt.subplots(figsize=[12/2.54,8/2.54])
ax.bar(np.arange(n), bs_stacked, width = 1, \
       align = 'edge', color = 'grey', edgecolor = 'black', linewidth = 1, alpha = 0.5)

ax.set_xlim (0-0.2,n+0.2)
ax.set_ylim (0,1.2)

ax.set_xlabel(r'$x$', size = 12)
ax.set_ylabel(r'$P(x)$', size = 12)
ax.set_xticks(np.arange(n+1))
ax.set_xticklabels(x_labels, size = 12)
ax.set_yticks([0, 0.5, 1])
ax.set_yticklabels(['0', '0.5', '1'], size = 12)
ax.grid(False)

fig.tight_layout()
fig.show()
fig.savefig(latex_path + "/monte/numeva.pdf", bbox_inches='tight')
fig.savefig(latex_path + "/monte/numeva.pgf", bbox_inches='tight')

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/figure.py:445: UserWarning: Matplotlib is currently using module://ipykernel.pylab.backend_inline, which is a non-GUI backend, so cannot show the figure.
  % get_backend())
In [ ]:
In [ ]: