R code used at VAPMS Selected Applications of Mathematical Statistics, summer semester 2018/2019.

Class 6 (March 28 2019)

Homework 6: Modification of code from Class 3 to demonstrate the validity of Central Limit Theorem 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); 
}

Function for computation of iterative correlation matrix:
icorr <- function (A)
{
	n <- dim(A)[1];
	B <- matrix (c(1),n,n);
	for (i in 1:(n-1))
	{	
		for (j in (i+1):n)
		{ 
			B[i,j] <- cor (A[i,], A[j,]);
			B[j,i] <- B[i,j];
		}
	}
	return (B);
}

A <- matrix (c(1, 0.1, 0.2, 0.1, 1, -0.5, 0.2, -0.5, 1), 3, 3);

for (i in 1:10)
{
	A <- icorr (A);
	print (A);
} 
Example of a matrix with a very slow convergence:
A <- matrix (c(1, -0.73333333333, -0.2, 0.2, -0.73333333333, 1, -0.2, 0.2, -0.2, -0.2, 1, 0.33333333333, 0.2, 0.2, 0.33333333333, 1), 4, 4)

iter <- function (A, n)
{
	for (i in 1:n)
	{
		A <- icorr (A);
	}
	return (A);	
}	

iter (A, 100)
iter (A, 1000)
iter (A, 10000) 

Created: Thu Oct 29 2015
Last modified: Thu Mar 28 21:26:13 CET 2019