Exercise HW 7.8, proposed solution
Nobody has claimed this yet.
Assessment
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Newbie friendliness
- 35/100
Research direction
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.
Written by the indexing model from the issue text.
Description
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);
- Dominant language
- MATLAB
- Stars
- 30
- Forks
- 1
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from UniprJRC/DSwithMATLAB
-
Difficulty 1/5 Under an hour Newbie friendliness 75/100
UniprJRC/DSwithMATLAB#243 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 75/100
UniprJRC/DSwithMATLAB#242 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
UniprJRC/DSwithMATLAB#241 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 83/100
UniprJRC/DSwithMATLAB#240 ·
-
Suggestion for the next edition
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
UniprJRC/DSwithMATLAB#238 · 2 comments ·
All issues in UniprJRC/DSwithMATLAB
Similar issues
-
bug priority: p3 size: xs
Difficulty 1/5 Under an hour Newbie friendliness 75/100
-
Ax/Atb trim the caller's GpuIds in place, so blocksize-1 solvers run on one GPU after the first anglePossibly taken @AnderBiguri claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 63/100
mathworks/MATLAB-Coding-Guidelines#7 · 3 comments ·