Lesson 17
Conditional Statements¶
if-elif-elseblocks
Problem: Print value if negative¶
Draw a standard normal value using np.random.standard_normal and print the value if it is negative.
Note: Rerun the cell a few time to see different output.
In [ ]:
Problem: Print different messages based on value¶
Draw a standard normal value and print "Positive" if it is positive and "Negative" if not.
In [ ]:
Problem:¶
Draw a standard t random variable with 2 degrees of freedom using np.random.standard_t(2) and print "Negative Outlier" if less than -2, "Positive Outlier" if larger than 2, and "Inlier" if between -2 and 2.
In [ ]:
In [ ]:
In [ ]:
In [ ]:
Exercise: Generate a contaminated normal¶
Generate a uniform using u = rs.sample(). Using this value and an if-else clause, generate a contaminated normal which is a draw from a $N(0,1)$ ($N(\mu,sigma^2)$) if $u<0.95$ or a draw from a $N(0,10)$ otherwise. Use rs.normal to generate the normal variable.
In [ ]: