exp 5-7: init

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2023-05-24 23:06:33 +05:30
parent d2756d2b87
commit 070886be9b
6 changed files with 309 additions and 62 deletions

BIN
5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

BIN
6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

BIN
7.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

349
file.org
View File

@@ -2,9 +2,11 @@
#+LATEX_HEADER: \usepackage[margin=0.5in]{geometry}
#+LATEX_HEADER: \usepackage{fontspec}
#+LATEX_HAEDER: \usepackage{graphicx}
#+LATEX_HAEDER: \usepackage{}
#+LATEX_HEADER: \usepackage{enumitem}
#+LATEX_HEADER: \setlist{noitemsep}
#+LATEX_HEADER: \setmainfont{LiberationSerif}
#+LATEX_HEADER: \date{}
#+OPTIONS: toc:nil
#+OPTIONS: num:nil
@@ -82,31 +84,31 @@ Installation of Scilab and demonstration of simple programming concepts like mat
Installed Scilab and demonstrated simple programming concepts like matrix multiplication (scalar and vector), loop, conditional statements and plotting.
#+LATEX: \clearpage
* Experiment-2
* Experiment 2
** Objective
Program for demonstration of theoretical probability limits.
** Method
1. Opened Scilab console and evaluated the following commands.
2. Declared integer n and set it to 10000, similarly declared and set another integer head_count to 0.
2. Declared integer $n$ and set it to 10000, similarly declared and set another integer $head\_count$ to 0.
3. Set up a loop from 1 to 10000 and generated a random number between 0 and 1 using rand() command
4. if the value of number is less then 0.5 then incremented head_count by 1
5. Set up a function P(i) for probability of heads in trial.
6. Plotted the graph of P(i) using plot() command.
4. if the value of number is less then 0.5 then incremented $head\_count$ by 1
5. Set up a function $P(i)$ for probability of heads in trial.
6. Plotted the graph of $P(i)$ using plot() command.
** Code
#+begin_src
#+begin_src
n = 10000;
head_count = 0;
for i = 1:n
x = rand(1)
x = rand(1)
if x<0.5 then
if x<0.5 then
head_count = head_count + 1;
end
end
p(i) = head_count / i;
p(i) = head_count / i;
end
disp(p(10000))
@@ -118,10 +120,12 @@ title("Probability of getting Heads");
#+end_src
** Output
#+ATTR_LATEX: :width 6cm
[[./2.png]]
#+ATTR_LATEX: :width 10cm
[[./2.png]]
** Result
We see that as the number of lips increase the theoretical probability of 0.5 is approached and the theoretical and practical probabilities become the same.
#+LATEX: \clearpage
* Experiment 3
@@ -130,23 +134,23 @@ Program to plot normal distributions and exponential distributions for various p
** a) Method: Normal Distribution
1. Opened Scilab console and evaluated the following commands.
2. Declared 2 arrays, m_values and s_values with various parametric values of mean and standard deviation.
3. Set up a loop from 1 to length of the array means and got a pair of values of mean and standard deviation.
2. Declared 2 arrays, $m\_values$ and $s\_values$ with various parametric values of mean and standard deviation.
3. Set up a loop from 1 to length of the array $means$ and got a pair of values of mean and standard deviation.
4. Using those values, generated the probability density function
$f(\boldsymbol{x}) = \frac{1}{s}\cdot\sqrt{2\pi}\cdot e^{\frac{-(\boldsymbol{x} - m) ^ 2}{(2 * s ^ 2)}}$
5. Created a range of x values to plot the normal distribution using linspace() command.
5. [@5] Created a range of $x$ values to plot the normal distribution using linspace() command.
5. Correctly titled and labelled the graph.
6. Plotted various normal distribution curves using plot() command.
** a) Code
#+begin_src
#+begin_src
m_values = [0, 1, -1];
s_values =[0.5, 1, 1.5];
for m = m_values
for s =s_values
for s =s_values
t =grand(1, 1000, "nor", m, s);
x= linspace(m-4*s, m+4*s, 1000)
@@ -155,7 +159,7 @@ for m = m_values
plot(x, y);
hold on;
xgrid();
end
end
end
legend("m=0, s=0.5", "m=1, s=0.5", "m=-1, s=0.5",...
@@ -168,34 +172,34 @@ title("Normal distributions for various parametric values");
#+end_src
** a) Output
#+ATTR_LATEX: :width 6cm
[[./3a.png]]
#+ATTR_LATEX: :width 10cm
[[./3a.png]]
** b) Method: Exponential Distribution
1. Opened Scilab console and evaluated the following commands.
2. Declared an array, Lambda_values with various values of lambda
3. Set up a loop from 1 to length of the array Lambda_values and the function grand() is used to generate 1000 random numbers from the exponential distribution.
2. Declared an array, $lambda\_values$ with various values of $lambda$
3. Set up a loop from 1 to length of the array $lambda\_values$ and the function grand() is used to generate 1000 random numbers from the exponential distribution.
4. Using those values, generated the probability density function
$f(x) = Y = lambda \cdot e^{-lambda \cdot x}$
5. Created a range of x values to plot the exponential distribution using linspace() command.
5. [@5] Created a range of $x$ values to plot the exponential distribution using linspace() command.
5. Correctly titled and labelled the graph.
6. Plotted various exponential distribution curves using plot() command.
** b) Code
#+begin_src
#+begin_src
lambda_values = [0.5, 1, 2];
for lambda = lambda_values
t = grand(1, 1000, "exp", lambda);
x = linspace(0, 8/lambda, 1000);
y = lambda * exp(-lambda * x);
t = grand(1, 1000, "exp", lambda);
x = linspace(0, 8/lambda, 1000);
y = lambda * exp(-lambda * x);
plot(x, y);
plot(x, y);
xgrid();
xgrid();
hold on;
hold on;
end
xlabel("x");
@@ -206,8 +210,8 @@ legend(["lambda=0.5", "lambda=1", "lambda=2"]);
#+end_src
** b) Output
#+ATTR_LATEX: :width 6cm
[[./3b.png]]
#+ATTR_LATEX: :width 10cm
[[./3b.png]]
#+LATEX: \clearpage
* Experiment 4
@@ -217,13 +221,13 @@ Program to plot normal distributions and exponential distributions for various p
** Theory
Binomial Distribution: A probability distribution that summarizes the likelihood that a variable will take one of two independent values under a given set of parameters. The distribution is obtained by performing a number of Bernoulli trials. A Bernoulli trial is assumed to meet each of these criteria:
1. There must be only 2 possible outcomes.
2. Each outcome has a fixed probability of occurring. A success has the probability of p, and a failure has the probability of 1 p.
2. Each outcome has a fixed probability of occurring. A success has the probability of $p$, and a failure has the probability of $1 p$.
3. Each trial is completely independent of all others.
4. To calculate the binomial distribution values, we can use the binomial distribution formula:
$P(X = x) = {}^{n}C_{x} \cdot p^x \cdot (1 - p)^{n - x}$
where `n` is the total number of trials, `p` is the probability of success, and `x` is the number of successes. We can calculate the binomial distribution values for each possible value of `x` using this formula and the values of `n` and `p` given above.
where $n$ is the total number of trials, $p$ is the probability of success, and $x$ is the number of successes. We can calculate the binomial distribution values for each possible value of $x$ using this formula and the values of $n$ and $p$ given above.
** Problem statement
6 fair dice are tossed 1458 times. Getting a 2 or a 3 is counted as success. Fit a binomial distribution and calculate expected frequencies.
@@ -251,28 +255,271 @@ Calculate it.
| 6 | 312.50 | 0.002132 |
** Steps
1. Find the number of cases, here, we take it as n.
2. Find the probability of success. Here, we take it as s (=2/6).
3. Define the number of times the process is repeated, and mark it as N. Here, acc to question, its 1458.
4. Take a variable x that varies from 0 to number of cases.
1. Find the number of cases, here, we take it as $n$.
2. Find the probability of success. Here, we take it as $s =2/6$.
3. Define the number of times the process is repeated, and mark it as $N$. Here, acc to question, its 1458.
4. Take a variable $x$ that varies from 0 to number of cases.
5. Apply the Formula for Binomial Probability Distribution.
6. Apply the formula for the Frequency.
7. Plot the Graph.
** Code
#+begin_src
n=6;
s=1/3;
N=1458;
x=0:n;
P= (1-s).^(n-x).*s.^x.*factorial(n)./(factorial(x).*factorial(n-x));
E= P*N;
clf();
plot(x,E,"b.-");
#+end_src
#+begin_src
n=6;
s=1/3;
N=1458;
x=0:n;
P= (1-s).^(n-x).*s.^x.*factorial(n)./(factorial(x).*factorial(n-x));
E= P*N;
clf();
plot(x,E,"b.-");
#+end_src
** Output
#+ATTR_LATEX: :width 6cm
[[./4.png]]
#+ATTR_LATEX: :width 10cm
[[./4.png]]
#+LATEX: \clearpage
* Experiment 5
** Objective
Program for fitting of binomial distributions after computing mean and variance.
** Theory
Binomial Distribution: A probability distribution that summarizes the likelihood that a variable will take one of two independent values under a given set of parameters. The distribution is obtained by performing a number of Bernoulli trials. A Bernoulli trial is assumed to meet each of these criteria:
1. There must be only 2 possible outcomes.
2. Each outcome has a fixed pobability of occurring. A success has the probability of $p$, and a failure has the probability of $1 p$.
3. Each trial is completely independent of all others.
4. To calculate the binomial distribution values, we can use the binomial distribution formula:
$P(X = x) = {}^{n}C_{x} \cdot p^x \cdot (1 - p)^{n - x}$
where $n$ is the total number of trials, $p$ is the probability of success, and $x$ is the number of successes. We can calculate the binomial distribution values for each possible value of $x$ using this formula and the values of $n$ and $p$ given above.
** Problem statement
A set of three similar coins are tossed 100 ($N$) times with the following results
| <c> | <c> | <c> | <c> | <c> |
| No. of Heads ($x$) | 0 | 1 | 2 | 3 |
| Frequency ($f$) | 36 | 40 | 22 | 2 |
Fit a binomial distribution and calculate expected frequencies.
** Method
1. For given data, total number of coins($n$) = 3 and total number of trials($N$) would be = 100
2. Find $X \cdot F$ to calculate the mean, for all corresponding values of $X$ and $F$, and calculate $\Sigma F \cdot x$
Mean would then be = $\frac{\Sigma F \cdot x}{n}$ i.e., $\frac{90}{100} = 0.9$.
3. Since we know for binomial distribution, $mean = n \cdot p$. So, $0.9 = 3 \cdot p$ => $p = 0.3$.
4. Calculate variance. We already know that $var = n \cdot p \cdot (1-p)$.
5. Now, calculate Binomial distribution, $P(X= x)$ and calculate expectes valued using $P \cdot N$.
| | $X$ | $F$ | $X \cdot F$ |
|---+------------+-----+-------------|
| | <c> | <c> | <c> |
| / | < | < | <> |
| | 0 | 36 | 0 |
| | 1 | 40 | 40 |
| | 2 | 22 | 44 |
| | 3 | 2 | 6 |
| | TOTAL($N$) | 100 | 90 |
| $X$ | 0 | 1 | 2 | 3 | TOTAL |
|--------------------+-----+------+------+-----+-------|
| <c> | <c> | <c> | <c> | <c> | <c> |
| Observed Frequency | 26 | 40 | 2 2 | 2 | 100 |
| Expected Frequency | 343 | 44.1 | 18.9 | 2.7 | 100 |
** Code
#+begin_src
n = 3;
N = 100;
F = [36, 40, 22, 2];
X = [0,1,2,3];
FX = (36*0+40*1+22*2+2*3);
Mean = FX/N;
disp("Mean = ", Mean);
p = Mean/n;
disp("Probability of success = ",p);
var = n*p*(1-p);
disp("Variance = ",var);
x = 0:3;
P= (1-p).^(n-x).*p.^x.*((factorial(n))./((factorial(x).*factorial(n-x))));
disp("Binomial distribution = ",P);
E = N*P;
disp("Expected Frequency = ", E);
clf();
plot(X,E,"b.-");
xlabel('X ');
ylabel('Expected Frequency');
title('Binomial Distribution ');
Y = grand(1, 80, "bin", 3,p);
disp(Y);
histplot(30, Y, normalization=%f, style=5);
#+end_src
** Output
#+ATTR_LATEX: :width 10cm
[[./5.png]]
#+LATEX:\clearpage
* Experiment 6
** Objective
Program for fitting of Poisson distributions for given value of lambda..
** Theory
Poisson Distribution: The Poisson distribution is a discrete probability distribution that expresses the probability of a given number of events occurring in a fixed interval of time or space, assuming that these events occur with a known constant rate and independently of the time since the last event. It is often used to model rare events.
The Poisson distribution has only one parameter, often denoted as $\lambda$, which represents the average rate of occurrence of the events over the given interval. The probability of observing exactly k events in the interval is given by the Poisson probability mass function:
$P(X=k) = (e^{-\lambda} * \lambda^k) / k!$
where $X$ is the random variable representing the number of events, $e$ is the base of the natural logarithm, and $k!$ denotes the factorial of $k$.
The mean and variance of the Poisson distribution are both equal to $\lambda$, which means that the distribution is unimodal and symmetric around $\lambda$.
The Poisson distribution is also a limiting case of the binomial distribution, when the number of trials $n$ goes to infinity and the probability of success $p$ goes to zero, but the product $n \cdot p$ remains constant and equal to $\lambda$.
** Problem statement
A set of three similar coins are tossed 100 ($N$) times with the following results in Table 1. Note the difference with experiment number 4 where “observed frequencies” were not given.
#+CAPTION: Table 1
| <c> | <c> | <c> | <c> | <c> |
| / | < | | | |
| No. of Heads ($x$) | 0 | 1 | 2 | 3 |
| Frequency ($f$) | 36 | 40 | 22 | 2 |
Fit a binomial distribution and calculate expected frequencies.
** Method
1. For given data, create a table (Table 2) to record the values of $x$, $f$, and $xf$. Here, $x$ denotes the number of heads obtained in a single toss, $f$ is the frequency of obtaining $x$ heads, and $xf$ is the product of $x$ and $f$.
#+CAPTION: Table 2
| <c> | <c> | <c> | <c> | <c> |
| / | < | | | |
| $x$ | 0 | 1 | 2 | 3 |
| $f$ | 36 | 40 | 22 | 2 |
| $xf$ | 0 | 40 | 44 | 6 |
2. [@2] The mean of the binomial distribution can be calculated as $N \cdot p$, where $N$ is the total number of trials and $p$ is the probability of getting a head. Here, $N=100$.
3. Calculate the value of $p$, using the formula $p = \frac{x}{N}$, where $x$ is the number of heads obtained in the 100 tosses. So, $p = (036 + 140 + 222 + 32)/100 = 0.64$.
4. Using the formula for the binomial distribution, calculate the expected frequency of obtaining x heads in a single toss as:
$P(X = x) = {}^{n}C_{x} \cdot p^x \cdot (1 - p)^{n - x}$
#+OPTIONS: \n:t
Where $n$ is the total number of trials, $x$ is the number of successful trials, and ${}^{n}C_{x}$ is the binomial coefficient. We can then multiply this value by 100 to get the expected frequency for each value of $x$.
Tabulate this information in Table 3.
#+CAPTION: Table 3
| <c> | <c> | <c> | <c> | <c> | |
| / | < | | | | |
| $X$ | 0 | 1 | 2 | 3 | Total |
| Observed Frequency | 36 | 40 | 22 | 2 | 100 |
| Expected Frequency $100 \cdot P(X=x)$ | 23.65 | 37.84 | 27.51 | 10.00 | 100.00 |
** Code
#+begin_src
observed_freq = [36, 40, 22, 2];
N = sum(observed_freq);
x = 0:3;
mean = sum(x .* observed_freq) / N;
// Poisson distribution with lambda = mean
expected_freq = N * exp(-mean) * (mean .^ x) ./ factorial(x);
// Output tables
disp(["x", "Observed Freq", "Expected Freq"]);
disp([x', observed_freq', expected_freq']);
// Plot of expected frequencies
clf();
plot(x, expected_freq, '.-', 'LineWidth', 2);
xlabel('Number of Heads');
ylabel('Expected Frequency');
title('Fitting of Poisson distribution');
legend('Expected Frequencies');
#+end_src
** Output
#+ATTR_LATEX: :width 10cm
[[./6.png]]
#+LATEX: \clearpage
* Experiment 7
** Objective:
Plotting Regression line for the given data points.
** Formulation and Method
*** Regression line
\begin{equation}
y = m \cdot x + b
\end{equation}
where, $m$ = slope, $b$ = y-intercept.
*** Method of least squares:
\begin{equation}
E = \Sigma_i (Y_i (m \cdot x +b))^2
\end{equation}
Find $\frac{\partial e}{\partial m}, \frac{\partial e}{\partial b} = 0$
*** Problem statement
Find the regression line for the given data points (x,y)
| X | Y |
|-----+------|
| <c> | <c> |
| 20 | 0.18 |
| 60 | 0.37 |
| 100 | 0.35 |
| 140 | 0.70 |
| 160 | 0.56 |
| 220 | 0.75 |
| 260 | 0.18 |
| 300 | 0.36 |
| 340 | 1.17 |
| 380 | 1.65 |
** Method
1. Make $x$ data and $y$ data lists.
2. Use regline() to find $m$ and $b$
3. Use scatter() to plot $x$ data and $y$ data
4. Use plot() to draw regression line.
** Results
The following are required as output:
1. The full code.
2. The plot for regression line and data points.
** Code
#+begin_src
x_data = [20,60,100,140,160,220,260,300,340,380]
y_data = [0.18,0.37,0.35,0.70,0.56,0.75,0.18,0.36,1.17,1.65]
[a, b] = reglin(x_data, y_data);
scatter(x_data,y_data,30,"x")
plot(x_data, a*x_data+b,"red")
xlabel("X")
ylabel("Y")
title("Simple Linear Regression")
#+end_src
** Output
#+ATTR_LATEX: :width 10cm
[[./7.png]]

BIN
file.pdf

Binary file not shown.

View File

@@ -22,7 +22,7 @@ Experiments according to the lab syllabus prescribed by GGSIPU
\begin{table}[h]
\fontsize{11}{12}\selectfont{
\renewcommand{\arraystretch}{2.5}
\begin{tabular}{|p{0.6cm}|p{8cm}|p{2cm}|p{2cm}|p{1cm}|} \hline
\begin{tabular}{|p{0.6cm}|p{10cm}|p{2cm}|p{2cm}|p{1cm}|} \hline
\textbf{Exp. No.} & \textbf{Experiment Name} & \textbf{Performance Date} & \textbf{Date Checked}& \textbf{Marks} \\ \hline \hline
& & & & \\ \hline
& & & & \\ \hline