<jupyter_start><jupyter_text>Inverse Transform SamplingThis notebook will conceptualize how inverse transform sampling works<jupyter_code>import numpy as np
from matplotlib import pyplot as plt
import numpy.random as ra

%matplotlib inline<jupyter_output><empty_output><jupyter_text>Below is a spectrum which follows an `almost` bell-curve type distribution (anyway, the specific type of distribution is not important here).<jupyter_code>spectrum = [[1, 2, 3, 4, 5, 6],[2000, 4040, 6500, 6000, 4020, 2070]]
energies = np.array(spectrum[0])
fluxes = np.array(spectrum[1])
spectrum<jupyter_output><empty_output><jupyter_text>Below, first we compute probabilities of flux. Afterwards, we compute the cumulative probability.<jupyter_code>prob = fluxes/float(sum(fluxes))
cum_prob = np.cumsum(prob)
cum_prob<jupyter_output><empty_output><jupyter_text>We draw ten thousand numbers from uniform random distribution.<jupyter_code>N = 10000
R = ra.uniform(0, 1, N)
R[1:10]<jupyter_output><empty_output><jupyter_text>We assign energies to events corresponding to the random number drawn._Note: The command below finds bin interval using a single command. I am not sure though that it's very readble. Wouldwe want to split that in multiple lines and maybe use explicit loops to make it more readable? Or is it fine as it is?Comments?_<jupyter_code>gen_energies = [int(energies[np.argwhere(cum_prob == min(cum_prob[(cum_prob - r) > 0]))]) for r in R]
gen_energies[1:10]<jupyter_output><empty_output><jupyter_text>Histogram energies to get shape approximation.<jupyter_code>gen_energies = ((np.array(gen_energies) - 1) / 1).astype(int)
times = np.arange(1, 6, 1)
lc = np.bincount(gen_energies, minlength=len(times))
lc
plot1, = plt.plot(lc/float(sum(lc)), 'r--', label='Assigned energies')
plot2, = plt.plot(prob,'g',label='Original Spectrum')
plt.xlabel('Energies')
plt.ylabel('Probability')
plt.legend(handles=[plot1,plot2])
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11463664278403275, 'fraction_numerical': 0.028147389969293755, 'mean_word_length': 7.354700854700854, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '486629', 'license': 'MIT', 'n_tokens_mistral': 722, 'n_tokens_neox': 654, 'n_words': 183, 'path': 'Simulator/Concepts/Inverse Transform Sampling.ipynb', 'repo_name': 'jdswinbank/notebooks', 'size': '1954'}