3
$\begingroup$

I have the following problem.

I have to generate 100 random values and make a list.

From this list I will randomly select 60 values and calculate MeanCI <- This procedure must be repeated 100 times.

I have to calculate the ratio between intervals MeanCI in which there is zero and which do not.

My code:

vstupnidata = RandomReal[NormalDistribution[0, 1], 100];

Needs["HypothesisTesting`"]
For[i = 1, i <= 100, i++,
 {vyb60 = MeanCI[RandomChoice[vstupnidata, 60]],
  Print[If[vyb60[[1]] < 0 \[And] vyb60[[2]] > 0, 1, 0
    ]]}
 ]

Now, my program writes 1 when there is 0 and 0 when it is not. I am not able to determine their number.

Could you help me please? Jan

$\endgroup$
5
  • $\begingroup$ Note that you have to use RandomVariate instead of RandomReal to generate samples from a normal distribution. 'RandomReal' will assume a uniform distribution. $\endgroup$ – gwr Nov 20 '15 at 18:48
  • $\begingroup$ @gwr That's not true. RandomReal[NormalDistribution[], 100] works just fine. Look at a histogram. $\endgroup$ – Pillsy Nov 20 '15 at 19:21
  • $\begingroup$ @Pillsy That may well be the case but Mathematica is certainly not following its own documentation in that case. Using RandomVariate her to me seems like the "proper" thing to do. I somehow am a bit "old fashioned" in sticking to what is documented... ;-) $\endgroup$ – gwr Nov 20 '15 at 19:28
  • 1
    $\begingroup$ Interesting post with regard to RandomReal vs. RandomVariate here. $\endgroup$ – gwr Nov 20 '15 at 19:36
  • $\begingroup$ @Jan, delete your answer. =)) you may now upvote. $\endgroup$ – garej Nov 20 '15 at 21:19
5
$\begingroup$

Here is your code rewritten to create the list of zeros and ones in a list, and then to count the number of ones:

vstupnidata = RandomReal[NormalDistribution[0, 1], 100];
Needs["HypothesisTesting`"];
zeroOne = Table[vyb60 = MeanCI[RandomChoice[vstupnidata, 60]]; 
  If[vyb60[[1]] < 0 \[And] vyb60[[2]] > 0, 1, 0], {i, 1, 100}]
Total[zeroOne]

86
$\endgroup$
2
  • $\begingroup$ Why introduce i? Also see my comment on using RandomReal in this case... $\endgroup$ – gwr Nov 20 '15 at 19:24
  • $\begingroup$ @gwr - I was just trying to leave the code as similar to the OPs as possible. There are lots of ways one might optimize... $\endgroup$ – bill s Nov 20 '15 at 20:12
5
$\begingroup$

Lets create a "popuplation" of random numbers that are following a normal distribution and make the confidence level transparent:

Needs["HypothesisTesting`"];
pop = RandomVariate[ NormalDistribution[], 100];
SetOptions[ MeanCI, ConfidenceLevel -> 0.95 ]; (* or whatever is needed *)

Now we draw samples which means we are sampling without replacement and sampling with replacement (you might look up RandomChoice for sampling with replacement):

samples = Table[ RandomSample[ pop, 60 ], {100} ];
choices = Table[ RandomChoice[ pop, 60 ], {100} ];

Then we build confidence intervalcs for the mean of the population:

ciListSamples = Map[ MeanCI, samples ];
ciListChoices = Map[ MeanCI, choices ];

Finally we count the intervals that contain $0$:

Count[ #, {a_, b_} /; a < 0 && b > 0 ]& /@ {ciListSamples, ciListChoices}

{94,89}

So we see, that in our test we implicitly assumed sampling without replacement, since the empirical result is closer to our confidence level. Being a Bayesian by conviction I cannot help but to remind about the meaning of a frequentist confidence interval which needs repeated sampling to be meaningful.

$\endgroup$
0
$\begingroup$

You could do this as follows:

ata = RandomVariate[NormalDistribution[0, 1], 100];
rc = RandomChoice[data, {100, 60}];
Needs["HypothesisTesting`"];
Count[Times @@@ (Sign /@ (MeanCI /@ rc)), -1]

Doing it 10 times: yielded (using Table): {69, 77, 96, 69, 69, 88, 93, 92, 94, 93}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.