# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# Your name here.  
# Your Woskshop section here.

# # Homework 4: File I/O, Statistics

# **Submit this notebook to bCourses to receive a grade for this Homework.**
#
# Please complete homework activities in code cells in this iPython notebook. Be sure to comment your code well so that anyone who reads it can follow it and use it. Enter your name in the cell at the top of the notebook. When you are ready to submit it, you should download it as a python notebook (click "File", "Download as", "Notebook (.ipynb)") and upload it on bCourses under the Assignments tab. Please upload the PDF file also.
#
# For questions that ask you to make an interpretation, please write a sentence or two explaining your response. You can use "Markdown" language to create a cell (like this one) which is ordinary text instead of using code. To do this, create a new cell, and then look at the bar above which has a picture of a floppy disk. On the right side is a drop down menu that allows you change whether a cell is "Markdown" or "Code".

# ## Problem 1: Central Limit Theorem

# Here we will verify the Central Limit Theorem and reproduced a plot I showed in class (https://en.wikipedia.org/wiki/Central_limit_theorem#/media/File:Dice_sum_central_limit_theorem.svg)

# 1. Write a function that returns $n$ integer random numbers, uniformly disributed between 1 and 6, inclusively. This represents $n$ throws of a fair 6-sided die. The value that comes up at each throw will be called the "score".
# 1. Generate a distribution of 1000 dice throws and plot it as a  histogram normalized to unit area. Compute the mean $\mu_1$ and standard deviation $\sigma_1$ of this distribution. Compare your numerical result to the analytical calculation. 
# 1. Generate 1000 sets of throws of $N=2,3,4,5,10$ dice, computing the total sum of dice scores for each set. For each value of $N$, plot the distribution of total scores, and compute the mean $\mu_N$ and standard deviation $\sigma_N$ of each distribution. This should be similar to the plot at the link above.
# 1. Plot the standard deviation $\sigma_N$ as a function of $N$. Does it follow the Central Limit Theorem? 



# ## Problem 2: Parity-Violating Asymmetry

# The data sample for this problem comes from the <a href="http://www.slac.stanford.edu/exp/e158">E158</a> experiment at SLAC (a national lab near that Junior university across the Bay). E158 measured a parity-violating asymmetry in Møller (electron-electron) scattering. This was a fixed-target experiment, which scattered longitudinally-polarized electrons off atomic (unpolarized) electrons in the 1.5m liquid hydrogen target. The data below contains a snapshot of 10,000 "events" from this experiment (overall, the experiment collected almost 400 million such events over the course of about 4 months). Each event actually records a pair of pulses: one for the right-handed electron (spin pointing along momentum) and one for the left-handed electron. For each event, we record 4 variables:
#
# * Counter: event index
# * Asymmetry: "raw" cross section asymmetry $A_{raw}$ from one of the detector channels (there are 50 of these overall). The cross section asymmetry is defined as 
# $A_{raw} = \frac{\sigma_R-\sigma_L}{\sigma_R+\sigma_L}$
# The asymmetry is recorded in units of PPM (parts per million). It is called "raw" because corrections due to the difference in beam properties at the target are not yet applied.
# * DeltaX: difference in beam position $\Delta X=X_R-X_L$ at the target in X direction in microns (with the convention that the beam is traveling along Z)
# * DeltaY: difference in beam position $\Delta Y=Y_R-Y_L$ at the target in Y direction in microns
#
# The data sample is provided in plain text format as the file <tt>asymdata.txt</tt>. Questions for this analysis:
#
# 1. Read the data from the file, and plot distributions of $A_{raw}$, $\Delta X$, and $\Delta Y$. 
# 1. Compute the mean of the raw asymmetry distribution and its statistical uncertainty.
# 1. Plot $A_{raw}$ vs $\Delta X$, $A_{raw}$ vs $\Delta Y$, and $\Delta X$ vs $\Delta Y$ as scatter plots. 
# 1. Compute the correlation coefficients <tt>Corr(Asym,DeltaX)</tt>, <tt>Corr(Asym,DeltaY)</tt>, and <tt>Corr(DeltaX,DeltaY)</tt>. See lecture notes, Workshop05.ipynb and Workshop05_optional.ipynb or https://en.wikipedia.org/wiki/Pearson_correlation_coefficient for additional help understanding correlation coefficients. Which variables are approximately independent of each other ?



# ## Problem 3: Gamma-ray peak
#
# [Some of you may recognize this problem from Advanced Lab's Error Analysis Exercise. That's not an accident.]
#
# You are given a dataset (`peak.dat`) from a gamma-ray experiment consisting of ~1000 gamma-ray hits. Each line in the file corresponds to one recorded gamma-ray event, and stores the the measured energy of the gamma-ray (in MeV). We will assume that the energies are randomly distributed about a common mean, and that each event is uncorrelated to others. Read the dataset from the enclosed file and:
# 1. Produce a histogram of the distribution of energies. Choose the number of bins wisely, i.e. so that the width of each bin is smaller than the width of the peak, and at the same time so that the number of entries in the most populated bin is relatively large. Since this plot represents randomly-collected data, plotting error bars would be appropriate.
# 1. Compute the mean and standard deviation of the distribution of energies and their statistical uncertainties. Assume the distribution is Gaussian and see the lecture notes for the formulas for the mean and variance of the sample and the formulas for the errors on these quantities. 
# 1. Compute the fraction of events contained within $\pm 1\sigma$ of the mean, $\pm 2\sigma$ of the mean, and $\pm 3\sigma$ of the mean (where $\sigma$ is the standard deviation you computed in Part 2). Compare these fractions with the quantiles of the Gaussian distribution (see lecture notes) ? 
#



StarcoderdataJupyterScriptsDedupFiltered