Generating random variable of certain std and mean (Simple Task)

Hi,
Below is a school task. Somehow the value D and the Q are incorrect according to the automatic test software that is checking if my code is correct.
Does anyone know, what I'm doing wrong, this should be pretty simple task.
So the task is:
1) Create artificial data samples (, ) from two normal distributions with mean
values (, ) and standard deviations (, ) (use Matlab's function
randn to generate the samples)
2) From these samples calculate the sample means , and their difference .
Calculate also the sample variances , and their quotient .
My code:
y = 4.2.*randn(1, 20) + 7.2 %First data sample.
y2 = 6.1.*randn(1, 18) + 8.5 %Second data sample.
m1 = mean(y) %
m2 = mean(y2) %
D = m1 - m2 % Difference: .
var1 = (1/(20-1)).*sum((y - m1).^2);
var2 = (1/(20-1)).*sum((y2 - m2).^2);
Q = var1 / var2

12 件のコメント

Cris LaPierre
Cris LaPierre 2022 年 11 月 19 日
"Somehow the value D and the Q are incorrect "
You do not appear to be calculating Q
Torsten
Torsten 2022 年 11 月 19 日
編集済み: Torsten 2022 年 11 月 19 日
Looks fine to me.
Maybe you are asked to fix the seed of the random number generator for randn ?
Otherwise I think it's difficult for an automatic software checker to decide whether your code is correct because the results will differ depending on what seed you chose.
Elias Seppä
Elias Seppä 2022 年 11 月 19 日
Hi Cris,
Now it should be there, Q = var1 / var2.
Calculating the Q might be wrong, but at least the D should be correct I think.
Cris LaPierre
Cris LaPierre 2022 年 11 月 19 日
編集済み: Cris LaPierre 2022 年 11 月 19 日
What is the full error message you are getting from the grader? If this is a problem in MATLAB Grader, share all the feedback (especially the red text at the top).
In MATLAB Grader, the reference and learner workspaces share the same random number generator seed, so would both generate the same random numbers (assuming the samples are created in the same order in both scripts)
Elias Seppä
Elias Seppä 2022 年 11 月 19 日
Here's a JPG file where you can see the error messages from the grader for D and Q.
Cris LaPierre
Cris LaPierre 2022 年 11 月 19 日
This is a problem in MATLAB Grader. Please share a screenshot of the full page so that we can see the code you have written and submitted, along with the corresponding assessment test results.
Elias Seppä
Elias Seppä 2022 年 11 月 19 日
Here's screenshots of the Exercise and the script that i have inserted into MATLAB Grader.
As you can see, there is also a part c) in this exercise but I have not included that code in the matlab grader as of now, because I'm first trying to get this D and Q correct.
John D'Errico
John D'Errico 2022 年 11 月 19 日
編集済み: John D'Errico 2022 年 11 月 19 日
Looking at the tests checked in grader, it looks as if you were supposed to perform this 10000 times, getting 10000 results in D, because the length of D is supposed to be 10000. If that is not true, then the tests shown will fail.
I think you need to show us the complete assignment text for this problem.
Cris LaPierre
Cris LaPierre 2022 年 11 月 19 日
I see you are no longer getting an error that Q does not exist. Just that t is incorrect. As John D'Errico said, this is because the test is grading the final case - 10000 samples.
Elias Seppä
Elias Seppä 2022 年 11 月 19 日
So I modified my code.
Now, it calculates the Difference (D), 10 000 times.
And also the Q, 10 000 times.
Now, the matlab grader gives me the correct answer for Q but D it still incorrect.
Elias Seppä
Elias Seppä 2022 年 11 月 19 日
Oh, I had calculated the mean wrong.
Now, it is all correct.
Thank you all very much for the help, I wouldn't have known that the Matlab Grader is expecting me to calculate this 10 000 times without the help.
John D'Errico
John D'Errico 2022 年 11 月 19 日
Good. In the end, we figured it out, as a group effort.
In hindsight, as long as you can check the tests that are being used, you could have seen that, but it may not have been obvious what was happening even so. My guess is the problem statement was poor. which does happen. Onward and upwards!

サインインしてコメントする。

 採用された回答

Cris LaPierre
Cris LaPierre 2022 年 11 月 19 日

2 投票

My suspicion is that the grader is expecting column vectors but you have created row vectors.
Try this
y = 4.2.*randn(20, 1) + 7.2 %First data sample.
y2 = 6.1.*randn(18, 1) + 8.5 %Second data sample.

2 件のコメント

Elias Seppä
Elias Seppä 2022 年 11 月 19 日
Unfortunately, this still didn't work :(
Cris LaPierre
Cris LaPierre 2022 年 11 月 19 日
編集済み: Cris LaPierre 2022 年 11 月 19 日
There is some inconsistency between the problem statement you have shared and the pretest code. You say you are supposed to create samples with n=20 and 18, but the pretest code is checking that D has 10000 elements. This suggests to me you are supposed to create 10000 sample data sets.
y = 4.2.*randn(20, 10000) + 7.2; %First data sample.
y2 = 6.1.*randn(18, 10000) + 8.5; %Second data sample.
m1 = mean(y); %
m2 = mean(y2); %
D = m1 - m2; % Difference:
length(D)
ans = 10000

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by