Exercise HW 7.8, proposed solution
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 1/5
- Tiempo estimado
- Menos de una hora
- Aptitud para principiantes
- 35/100
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
forloop. 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
rhois a vector (e.g.,-0.6:0.1:0.9),corrcdfreturns a vector of the same length. Pre-allocating your output matrix based onn(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 use1 - 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
mvnrndto generate the samples (using a covariance matrix with (\rho=0)) andcorrto 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
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de UniprJRC/DSwithMATLAB
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 69/100
UniprJRC/DSwithMATLAB#232 ·
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 74/100
UniprJRC/DSwithMATLAB#229 · 2 comentarios ·
-
Dificultad 1/5 1-3 horas Aptitud para principiantes 72/100
UniprJRC/DSwithMATLAB#228 · 3 comentarios ·
-
Small typo Page 137Abierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 77/100
UniprJRC/DSwithMATLAB#198 · 1 comentario · 1 reacción ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 55/100
UniprJRC/DSwithMATLAB#231 ·
Todos los issues de UniprJRC/DSwithMATLAB
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
Los mantenedores suelen responder en 1 día
-
windows clang 20 warnings buildAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 67/100
wissem01chiha/slxio#51 ·
-
census digest: print a denominator for EDGE-FAMILY CARDINALITY VIOLATIONS (did-schema OPEN_WORK #79)AbiertoV2
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
VH-Lab/DID-matlab#399 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
VH-Lab/NDI-matlab#1183 ·
Los mantenedores suelen responder en 1 día