Coding STEM Uncategorized

Sample Size in Python

This is part of a 3-part series on the topic. Please read the posts in the order for maximum clarity and context:

1. Sample Size and Margin of Errors. Everything you need to know and ++
2. Sample Size (Contd.)
3. Sample size in Python (This one)

Ok, after reading the firstĀ  2 posts, you have an idea of where we’re headed. For my own experimentation and making things quicker and easier, I converted the statistical method into a Python program. Here I share some basics on how it’s done.

The Code:

The actual arithmetic part of most useful statistical functions are actually not too difficult, most people struggle (or err) around the conceptual and modeling part of it. So, with that out of the way in the 2 previous posts on this topic, we can automate the actual calculation in a program and Python is a good candidate for it.

Because the associative table of z-scores and confidence levels are widely available (and used), I took a decent range of the table and create a lookup table using the dictionary object in Python, so I can use key:value pair easily to do forward and reverse lookup at will.

The first line below does just that. In the following lines, I define a function that actually outputs the Sample Size based on the 2 parameters passed to the function: z-score, and error-margin (EM).

The z-score is actually sent to the function above from the main driver code, which simply leverages the lookup dictionary object. The rest of the code is is just to get user-input and do some basic input validation:

The Output:

The program asks for only 2 inputs: confidence level, and error margin that’s acceptable, and it outputs the minimum required sample size.

Hope you had a chance to read all 3 parts on this topic and found it interesting.


Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top