Exercise HW 7.8, proposed solution

Aperta
#227 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
1/5
Tempo stimato
Meno di un'ora
Idoneità per principianti
35/100
Tipo di issue
Documentazione
Chiarezza
Da chiarire
Stato di attività
Attiva
Stack tecnologico
matlab
Ambito
content

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
MATLAB
Stelle
30
Fork
1
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di UniprJRC/DSwithMATLAB

Tutte le issue di UniprJRC/DSwithMATLAB

Issue simili

Altre issue su MATLAB

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.