# the number of values problem

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[] < 0 \[And] vyb60[] > 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

• Note that you have to use RandomVariate instead of RandomReal to generate samples from a normal distribution. 'RandomReal' will assume a uniform distribution. – gwr Nov 20 '15 at 18:48
• @gwr That's not true. RandomReal[NormalDistribution[], 100] works just fine. Look at a histogram. – Pillsy Nov 20 '15 at 19:21
• @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... ;-) – gwr Nov 20 '15 at 19:28
• Interesting post with regard to RandomReal vs. RandomVariate here. – gwr Nov 20 '15 at 19:36
• @Jan, delete your answer. =)) you may now upvote. – garej Nov 20 '15 at 21:19

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[] < 0 \[And] vyb60[] > 0, 1, 0], {i, 1, 100}]
Total[zeroOne]

86

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

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.

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}