Exercise HW 7.8, proposed solution

Open
#227 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
35/100
Issue type
Documentation
Clarity
Needs clarification
Activity status
Active
Tech stack
matlab
Domain
content

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 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);

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from UniprJRC/DSwithMATLAB

All issues in UniprJRC/DSwithMATLAB

Similar issues

More MATLAB issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.