Generating random variable of certain std and mean (Simple Task)
Show older comments
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 Comments
Cris LaPierre
on 19 Nov 2022
"Somehow the value D and the Q are incorrect "
You do not appear to be calculating Q
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ä
on 19 Nov 2022
Cris LaPierre
on 19 Nov 2022
Edited: Cris LaPierre
on 19 Nov 2022
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ä
on 19 Nov 2022
Cris LaPierre
on 19 Nov 2022
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ä
on 19 Nov 2022
John D'Errico
on 19 Nov 2022
Edited: John D'Errico
on 19 Nov 2022
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
on 19 Nov 2022
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ä
on 19 Nov 2022
Elias Seppä
on 19 Nov 2022
John D'Errico
on 19 Nov 2022
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!
Accepted Answer
More Answers (0)
Categories
Find more on Hypothesis Tests in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!