Statistical Analysis in Physics Practical File
Statistical Analysis in Physics Practical File
in Physics
Practical File
Dhruv Verma
22/26080
Physics (Hons.)
Sec– B
INDEX
Algorithm:
1. Import Libraries:
● Import numpy for numerical operations.
● Import matplotlib.pyplot for plotting histograms.
● Import scipy.stats for fitting normal distributions.
4. Set Parameters:
● N = 50: Sample size per iteration.
● M = 10000: Number of iterations.
● Poisson Distribution:
o Call generate_means() with np.random.poisson and parameter (5,).
o Plot the distribution of means using plot_distribution() with the label
"Poisson".
● Normal Distribution:
o Call generate_means() with np.random.normal and parameters (0, 1).
o Plot the distribution of means using plot_distribution() with the label
"Normal".
Algorithm:
1. Import Required Libraries:
● Import numpy for numerical operations.
● Import matplotlib.pyplot for visualization.
● Import norm from scipy.stats for generating normal distributions (not directly
used but relevant for continuous distributions).
● Output:
o x_edges: Bin edges for xi.
o y_edges: Bin edges for yi.
o histogram: 2D array representing joint probability density.
▪ Use extent to align the plot axes with the continuous variable ranges
from x_edges and y_edges.
● Customize Plot:
o Add a color bar to indicate probability density values.
o Set the title as "Joint Probability Distribution (Continuous)".
o Label the axes as "X" and "Y".
o Disable grid lines for a cleaner heatmap.
● Show Plot:
o Display the heatmap using plt.show().
6. Print Joint Probability Density Matrix:
● Print the joint probability density matrix (joint_prob_continuous) to observe the
numerical values of the distribution.
Code:
Output:
Experiment – 3
Aim:
Given data for two independent variables (xi, yi). Write a code to compute the
joint probability in a given sample space.
Verify the same for the data generated by a random number generator
based on a given probability distribution of a pair of independent variables.
(Discrete)
Algorithm:
1. Import Required Libraries:
● Import numpy for numerical operations.
● Import matplotlib.pyplot for plotting.
● Import randint from scipy.stats to generate discrete random variables.
Code:
Output:
Experiment – 4
Aim:
Hypothesis testing
Make a random number generator to simulate the tossing of a coin n times
with the probability for the head being q.
Write a code for a Binomial test with the Null hypothesis H0 (q = 0.5) against the
alternative hypothesis H1 (q not equal to 0.5)
Algorithm:
Step 1: Define Functions
1. Set n = 100 (number of tosses) and q = 0.55 (actual probability of heads).
Step 5: Visualization
1. Compute the binomial probability mass function (PMF) for n tosses
assuming a fair coin (q = 0.5).
2. Plot the expected distribution using plt.plot(x, pmf).
3. Mark observed_heads on the graph using plt.axvline().
4.Display the graph.
Code:
Output:
Experiment – 5
Aim:
Bayesian Inference
a) In an experiment of flipping a coin N times, M heads showed up (fraction
of heads f = M/N). Write a code to determine the posterior probability,
given the following prior for the probability of f:
i. Beta Distribution B(a, b) with given values of a and b.
ii. Gaussian Distribution with a given mean and variance.
Algorithm:
Input:
● Use conjugacy of the Beta prior with the binomial likelihood: posterior
parameters become (a + M) and (b + (N - M)).
● Plot:
o Likelihood (dashed line)
o Beta prior
o Gaussian prior (dotted line)
o Vertical line at MLE (dashed red)
● Label axes and add a legend.
Code:
Output:
Experiment – 6
Aim:
Regression Analysis and Gradient Descent:
Given a dataset (Xi, Yi). Write a code to obtain the parameters of the linear
regression equation using the method of least squares with both constant and
variable errors in the dependent variable (Y).
The data obtained in a physics lab may be used for this purpose. Also obtain the
correlation coefficient and the 90% confidence interval for the regression line.
Make a scatter plot along with error bars.
Also, overlay the regression line and show the confidence interval.
Algorithm:
Input:
● Arrays of measured values: X, Y
● Array of uncertainties in Y: sigma_Y
Output:
● Best-fit slope (m) and intercept (c) with uncertainties
● Correlation coefficient (r)
● Plot of data, fit, and 90% confidence interval
Steps:
1. Import required libraries:
o numpy, matplotlib.pyplot, scipy.stats, and scipy.optimize.curve_fit
7. Create a finer grid X_fit over the range of X for smooth plotting
8. Compute the best-fit line Y_fit using the linear model and best-fit parameters
12.Print results:
o Slope and intercept with uncertainties
o Correlation coefficient
Code:
Output:
Experiment – 7
Aim:
Markov Chain
Given that a particle may exist in one of the given energy states (Ei, i = 1, … 4) and
the transition probability matrix T, so that Tij gives the probability for the particle
to make transition from energy state Ei to state Ej. Determine the long-term
probability of a particle to be in state in the state Ef if the particle was initially in
state Ei.
Algorithm:
Input:
● A transition matrix T (square matrix, rows sum to 1)
Output:
● Stationary distribution (steady-state probabilities)
Steps:
1. Import necessary library:
o Use numpy for numerical operations
Code:
Output: