Exercise HW 7.8, proposed solution

Abierto
#227 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
1/5
Tiempo estimado
Menos de una hora
Aptitud para principiantes
35/100
Tipo de issue
Documentación
Claridad
Necesita aclaración
Estado de actividad
Activo
Stack tecnológico
matlab
Área
content

Línea de trabajo

Read the proposed HW 7.8 solutions in the issue body, especially the corrcdf explanations and Monte Carlo code. The issue asks for feedback on the explanation and on interval inputs, but does not specify a change or acceptance criteria, so clarify the intended contribution before editing.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Hi everyone,

I wanted to share my proposed solutions for HW 7.8. While working through the exercises, I noticed a couple of common pitfalls regarding the behavior of corrcdf and the Monte Carlo simulation. I put together a quick theoretical recap to help other students avoid the same confusion.

You can find the complete MATLAB code in the next comment.

🧠 Theoretical Notes & Common Pitfalls

**1. How corrcdf actually works **
corrcdf(x, rho, n) computes the cumulative probability (P(r \le x)), where (r) is the sample correlation.

  • Interval Probabilities: If an exercise asks for the probability that (r) lies between 0.3 and 0.4, you shouldn't use a for loop. Simply subtract the two CDFs: corrcdf(0.4, rho, n) - corrcdf(0.3, rho, n).
  • Dimension Mismatch Trap: Be careful with matrix pre-allocation. If rho is a vector (e.g., -0.6:0.1:0.9), corrcdf returns a vector of the same length. Pre-allocating your output matrix based on n (which might be a scalar) will throw the "Unable to perform assignment" error.

2. Exercise 4: The Statistical Theory (t-Student)
When the population correlation is zero ((\rho = 0)), the exact distribution of the sample correlation is known. The statistic (t = r \sqrt{\frac{n-2}{1-r^2}}) follows a Student's t-distribution with (n-2) degrees of freedom.

  • This is why the exercise asks to compute the probability both with and without corrcdf.
  • Without corrcdf: you manually calculate the (t)-statistic and use 1 - tcdf(t, n-2).
  • With corrcdf: the function does exactly the same math under the hood, returning (1 - \text{corrcdf}(0.4, 0, 20)).
    (Note: Fisher's z-transformation is generally used to approximate the distribution when (\rho \neq 0), which is why it is relevant for Ex. 1-3, but for (\rho=0) the t-Student is exact).

3. Exercise 5: Monte Carlo Simulation
This exercise is purely empirical. It asks to simulate 100,000 independent bivariate normal samples (with (n=20) and (\rho=0)) to estimate how many have (r > 0.4).

  • We can use mvnrnd to generate the samples (using a covariance matrix with (\rho=0)) and corr to compute the sample correlation (also the solution with only corr one is suggested by Copilot in first place)
  • By calculating the proportion mean(r_sim > 0.4), we obtain a Monte Carlo estimate. This simulated probability will converge perfectly to the exact analytical probability calculated in Exercise 4, verifying the theory.
📌 A quick not

I don't really know what is the specific reason why corrcdf doesn't natively support interval inputs (e.g., corrcdf([0.3, 0.4], rho, n) to directly compute the probability between the bounds), or is the CDF(b) - CDF(a) subtraction the standard intended workflow

in any case, thanks for any feedback!

HW 7.8

1

n=6;
p=-0.6:0.1:0.9;
prob=corrcdf(0.4,p,n)-corrcdf(0.3,p,n);


2

figure;
plot(p, prob);
xlabel('Correlation coefficient');
ylabel('Probability');
grid on;
%as we can see, near the real value of p the probability is higher than the
%other values, because from the theoretical part of the book is explained:
%we "know" Rho: the higher is the number of our population, more "precise"
%will be our plot in the real value of the correlation

3
p=0.1;
n=6:2:200;
r=corrcdf(0.4,p,n)-corrcdf(0.3,p,n);
disp(r)


4
n=20;
p=0;
%with corrcdf
disp(1-corrcdf(0.4,p,n));
%without corrcdf
tstud=0.4*sqrt(n-2) / sqrt(1-0.4^2);
probabilityWithoutCorrcdf = 1 - tcdf(tstud, n - 2);
disp(probabilityWithoutCorrcdf);


5
%Finally, by simulating 100,000 independent bivariate normal samples, estimate how many have r > 0.4.
%copilot-driven solution with corr
sampleCount = 100000;
sampleSize = 20;
correlationMatrix = [1 0; 0 1];
sampleCorrelations = zeros(sampleCount, 1);
for sampleIndex = 1:sampleCount
    samples = randn(sampleSize, 2) * chol(correlationMatrix);
    sampleCorrelations(sampleIndex) = corr(samples(:, 1), samples(:, 2));
end
estimatedProbability = mean(sampleCorrelations > 0.4);
disp(estimatedProbability);
%alternative solution with mvrnd
n_sim = 100000;   
n = 20;           
rho = 0;          

Sigma = [1, rho; rho, 1];
r_sim = zeros(n_sim, 1);


for i = 1:n_sim
   
    R = mvnrnd([0 0], Sigma, n);
   
    r_sim(i) = corr(R(:,1), R(:,2));
end


prob = mean(r_sim > 0.4);
fprintf('extimated probability with mvnrnd: %f\n', prob);

Lenguaje dominante
MATLAB
Estrellas
30
Forks
1
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de UniprJRC/DSwithMATLAB

Todos los issues de UniprJRC/DSwithMATLAB

Issues similares

Más issues de MATLAB

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.