Sampling and Sampling Distributions

Inputs

<<"Combinatorica`";
<<"Statistics`DataManipulation`";
(<<"BarCharts`";<<"Histograms`";<<"PieCharts`");
sample[n_]:=Table[If[RandomReal[]≤p,1,0],{n}];
samplestat[n_]:=Plus@@sample[n];
color[i_]:=If[i==0,RGBColor[0,0,0],RGBColor[1,0,0]];
ssshow[sam_]:=Show[Graphics[Table[{color[sam[[i]]],Point[{i,0}]},{i,1,Length[sam]}],AspectRatio->0.02`,ImageSize->{400,8}]]

1. Drawing a single sample from a population

Introduction

Imagine a large jar containing red and black beads.  The problem that we want to think about is the relationship between the proportion of red beads in the jar and the proportion of red beads in a sample.

Task 1: Although we are imagining a jar of beads, this is only an analogy for more meaningful situations that you might encounter (e.g., in the news).  List some examples and draw the analogy explicitly.  (For a hint, expand this cell.)

For example: someone wants to predict the outcome of an election by means of an exit poll.  All the people who voted are analogous to all the beads in the jar.  The color of bead is analogous to the vote---e.g. color:bead::candidate voted for:voter.  The people who are questioned in the poll are analogous to the sample.

Task 2: Suppose a large population is 40% red.  Imagine that you have drawn a sample of size 20 from this population.  Describe what you think a typical sample might be like.

Task 3: Suppose that you have drawn a sample of size 20 from a population of unknown proportion red, and that our sample is 40% red.  What do you think you can deduce about the population?

The computer makes a population, and we examine a sample.

Now, we shall ask the computer to create a large population of individuals in which some fraction is red.  The computer will know that fraction, but we won't.

p=RandomReal[];Show[Graphics[Table[{color[If[RandomReal[]≤p,1,0]],Point[RandomReal[{0,1},2]]},{500}],AspectRatio->1,ImageSize->{150,150}]]

We decide how large a sample we wish to take:

samplesize= 100;

And then we  ask the computer to draw a sample of that size:

sam1=sample[samplesize];
ssshow[sam1]

The computer will count up the proportion of red in the sample:

stat1=N[Plus@@sam1/samplesize]

Task 4: Now that you have seen a sample from the population in the computer's memory, what do think the proportion red is?

Task 5: What do you think you know about the population?

We look at a few samples of various size.

Type the desired sample size in the input lines below, and then enter the cell.  The computer will create a sample, and will tell you the percent red in that sample.  Each time you enter the cell, a new sample is created and counted.

samplesize= 200;
sam1=sample[samplesize];
stat1=N[Plus@@sam1/samplesize]

Assessment Task 6: Using the ability to draw samples of many sizes, determine the population proportion as best you can.   How accurate do you think your estimate is?

We  ask the computer to reveal the proportion red in the original population:

p

2. Drawing many samples

Picking our own population and thinking about many samples.

This time, we will create a large population of individuals in which a fraction that we decide is red.  (Fill in the choice for the proportion below and enter the cell.)

p=0.4`;Show[Graphics[{PointSize[0.02`],Table[{color[If[RandomReal[]≤p,1,0]],Point[RandomReal[{0,1},2]]},{500}]},AspectRatio->1,ImageSize->{150,150}]]

We are going to look at samples from this population.  Let us stipulate how large the samples will be:

samplesize= 100;

We will ask the computer to pull out many samples of that size, and for each sample to show the proportion red in that sample.  We stipulate how many different samples we will ask the computer to make.

numberofsamples=1000;

Assessment Task 7: Instead of using a computer to do this, we could use a large bin with pieces of paper---or a jar filled with colored beads.  Describe what we would be doing if we were doing this task with physical manipulatives rather than on a computer.

Assessment Task 8: Before we ask the computer to draw all these samples, try to predict what the distribution of the sample statistic will look like.  How many samples will have a statistic of exactly p (the population proportion that you decided to work with)? What will the greatest deviation from p be?

By entering the next cell, we get the computer to create all the samples and reckon the statistic associated with each one.

manysamplestats=Sort@Table[samplestat[samplesize]/samplesize, {numberofsamples}];

N@manysamplestats

The distribution of the statistic among all the samples we made.

We ask the comuter to tabulate the information we created in the last group:

freq1={BinCounts[manysamplestats, {Ceiling[100*Min[manysamplestats]]/100-1/100, Ceiling[100*Max[manysamplestats]]/100, .01}],Range[Ceiling[100*Min[manysamplestats]]/100, Ceiling[100*Max[manysamplestats]]/100, .01]};
TableForm[{Reverse[{Prepend[freq1[[1]], "number of\nsamples"],"  ", Prepend[freq1[[2]], "sample\nstatistic"]}]}]

The computer can also display it. The chart below shows the number of samples with various proportions of red.  (Proportion red is along the bottom.)

freq1

ListPlot[Transpose[Reverse@freq1], Filling->Axis, ImageSize->{600, 400}]

Assessment Task 9:  Describe how the shape of the sampling distribution depends on the sample size.  (To do this, you will have to experiment with different sample sizes.)

3. Theoretical Sampling Distribution

amt[k_]:= (*This function simply determines the probability that a sample (of size "samplesize") from a population with proportion p red will have a percent red between k-1 and k (percent).*)    
Chop[Sum[Binomial[samplesize, i]*p^i*(1-p)^(samplesize-i),{i, Floor[1+((k-1)*samplesize)/100], k*samplesize/100}], .001]

p=.5; samplesize=100;  dddd=Transpose@{ Table[i, {i, 0, 100}], Table[amt[k], {k, 0, 100}]}; ListPlot[dddd,PlotRange->{{0,100}, {-.010, .2}}, Filling->Axis]

Do[
dddd=Transpose@{Table[i, {i, 0, 100}],Table[amt[k], {k, 0, 100}]};
Print@ListPlot[dddd, PlotRange->{{0, 100}, {-.01, .25}}, Filling->Axis], 
{p, .1, .95, .05}
]

(***********************************
Original version written November 11, 2002
************************************)