<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Exercise from Think Stats, 2nd Edition (thinkstats2.com)<br>
Allen Downey
Read the pregnancy file.
Step1:  Select live births, then make a CDF of <tt>totalwgt_lb</tt>.
Step2:  Display the CDF.
Step3:  Find out how much you weighed at birth, if you can, and compute CDF(x).
Step4:  If you are a first child, look up your birthweight in the CDF of first children; otherwise use the CDF of other children.
Step5:  Compute the percentile rank of your birthweight
Step6:  Compute the median birth weight by looking up the value associated with p=0.5.
Step7:  Compute the interquartile range (IQR) by computing percentiles corresponding to 25 and 75.
Step8:  Make a random selection from <tt>cdf</tt>.
Step9:  Draw a random sample from <tt>cdf</tt>.
Step10:  Draw a random sample from <tt>cdf</tt>, then compute the percentile rank for each value, and plot the distribution of the percentile ranks.
Step11:  Generate 1000 random values using <tt>random.random()</tt> and plot their PMF.
Step12:  Assuming that the PMF doesn't work very well, try plotting the CDF instead.
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
import nsfg
preg = nsfg.ReadFemPreg()
Explanation: Exercise from Think Stats, 2nd Edition (thinkstats2.com)<br>
Allen Downey
Read the pregnancy file.
End of explanation
import thinkstats2
live = preg[preg.outcome == 1]
firsts = live[live.birthord == 1]
others = live[live.birthord != 1]
cdf = thinkstats2.Cdf(live.totalwgt_lb)
Explanation: Select live births, then make a CDF of <tt>totalwgt_lb</tt>.
End of explanation
import thinkplot
thinkplot.Cdf(cdf, label='totalwgt_lb')
thinkplot.Show(loc='lower right')
Explanation: Display the CDF.
End of explanation
cdf.Prob(8.4)
Explanation: Find out how much you weighed at birth, if you can, and compute CDF(x).
End of explanation
other_cdf = thinkstats2.Cdf(others.totalwgt_lb)
other_cdf.Prob(8.4)
Explanation: If you are a first child, look up your birthweight in the CDF of first children; otherwise use the CDF of other children.
End of explanation
cdf.PercentileRank(8.4)
Explanation: Compute the percentile rank of your birthweight
End of explanation
cdf.Value(0.5)
Explanation: Compute the median birth weight by looking up the value associated with p=0.5.
End of explanation
cdf.Percentile(25), cdf.Percentile(75)
Explanation: Compute the interquartile range (IQR) by computing percentiles corresponding to 25 and 75.
End of explanation
cdf.Random()
Explanation: Make a random selection from <tt>cdf</tt>.
End of explanation
cdf.Sample(10)
Explanation: Draw a random sample from <tt>cdf</tt>.
End of explanation
t = [cdf.PercentileRank(x) for x in cdf.Sample(1000)]
cdf2 = thinkstats2.Cdf(t)
thinkplot.Cdf(cdf2)
thinkplot.Show(legend=False)
Explanation: Draw a random sample from <tt>cdf</tt>, then compute the percentile rank for each value, and plot the distribution of the percentile ranks.
End of explanation
import random
t = [random.random() for _ in range(1000)]
pmf = thinkstats2.Pmf(t)
thinkplot.Pmf(pmf, linewidth=0.1)
thinkplot.Show()
Explanation: Generate 1000 random values using <tt>random.random()</tt> and plot their PMF.
End of explanation
cdf = thinkstats2.Cdf(t)
thinkplot.Cdf(cdf)
thinkplot.Show()
import scipy.stats
scipy.stats.norm.cdf(0)
Explanation: Assuming that the PMF doesn't work very well, try plotting the CDF instead.
End of explanation
<END_TASK>