R code demonstrated at 6PAS2 Probability and Statistic 2, summer semester 2025/2026.

Class 3 (Feb 25 2026)

Example of a function computing mode of a distribution (Homework 3), procedural "non-R" style:
Mode <- function (v)
{
	max <- 0;
	for (val in unique (v))
	{
		count <- length (v[v==val]);
		if (count > max)
		{
			max <- count;
			m <- val; 
		}
		else if (count == max)
		{
			max <- count;
			m <- c(m,val);
		}	
	}
	return (m);
} 
Illustration of the Central Limit Theorem:
# num_dices - number of dices, N - number of times we are throwing them
clt <- function (N, num_dices)
{
	r <- rep (0, N);
	for (i in 1:num_dices)
	{
		r <- r + sample (1:6, N, replace=T);
	}
	r <- r/num_dices;
	hist (r, 100, freq=F, main=num_dices);
	x <- seq(min(r), max(r), length=100);
	lines (x, Norm(x, sd(r), mean(r)), type="l");
}

for (n in c(1,10,100,1000,10000))
{
	clt (1000, n);
	Sys.sleep (3); 
}
Modification of this code for arbitrary distribution:
# n - number of simultaneously performed observations
# N - number of iterates
# f - distribution function 
clt <- function (N, n, f)
{
	r <- rep (0, N);
	for (i in 1:n)
	{
		r <- r + f(N);
	}
	r <- r/n;
	hist (r, 100, freq=F, main=n);
	x <- seq (min(r), max(r), length=100);
	lines (x, Norm(x, sd(r), mean(r)), type="l");
}
Test for normal distribution:
for (n in c(1,10,100,1000,10000))
{
	clt (1000, n, rnorm);
	Sys.sleep (3); 
}
Test for binomial distribution:
b <- function (N)
{
	rbinom (N, 10, 0.5)
}
for (n in c(1,10,100,1000,10000))
{
	clt (1000, n, b);
	Sys.sleep (3); 
}

Created: Wed Sep 30 2015
Last modified: Wed Apr 1 2026 19:17:38 CEST