Introduction to Econometrics with R Practice exercises ) In Python.
The lottery will draw $ 6 $ out of $ 49 $ * unique * numbers.
** Instructions: ** Draw the winning number for this week.
import math
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from scipy import integrate, stats
np.random.seed(seed=123)
np.random.randint(low=1, high=50, size=6)
array([46, 3, 29, 35, 39, 18])
Consider the random variable $ X $ using the probability density function (PDF) below.
** Instructions: ** Define the above probability density function as a function f () </ tt>. Make sure that the defined function is actually a probability density function.
def f(x):
return x/4*math.exp(-x**2/8)
integrate.quad(f, 0, np.inf)
(1.0, 2.1730298600934144e-09)
In this exercise, you have to calculate the expected value and variance of the random variable $ X $ that you considered in the previous exercise.
The probability density function f () </ tt> in the previous exercise is assumed to be available in the operating environment.
** Instructions: ** Define an appropriate function ex () </ tt> that integrates to the expected value of $ X $. Calculate the expected value of $ X $. Store the result in expected_value </ tt>. Define an appropriate function ex2 () </ tt> that integrates to the expected value of $ X ^ 2 $. Calculate the variance of $ X $. Store the result in variance </ tt>.
# define the function ex
def ex(x):
return x*f(x)
# compute the expected value of X
expected_value = integrate.quad(ex, 0, np.inf)[0]
# define the function ex2
def ex2(x):
return x**2*f(x)
# compute the variance of X
variance = integrate.quad(ex2, 0, np.inf)[0] - expected_value**2
Let
** Instructions: ** $ \ phi (3) $, that is, calculate the value of the standard normal density at $ c = 3 $.
stats.norm.pdf(3)
0.0044318484119380075
Let
** Instructions: **
# compute the probability
stats.norm.cdf(1.64) - stats.norm.cdf(-1.64)
0.8989948330517925
Let
** Instructions: ** Calculate the 99% quantile of a given distribution, that is, find $ y $ such that $ y $ is $ \ Phi (\ frac {y-5} {5}) = 0.99 $.
# compute the 99% quantile of a normal distribution with mu = 5 and sigma^2 = 25.
stats.norm.ppf(0.99, 5, np.sqrt(25))
16.631739370204205
Let
** Instructions: ** Generate a $ 10 $ random number from this distribution.
# generate 10 random numbers from the given distribution.
stats.norm.rvs(loc=2, scale=np.sqrt(12), size=10, random_state=12)
array([ 3.63847098, -0.36052849, 2.83983505, -3.89152106, 4.60896331,
-3.31643067, 2.01776072, 1.58351913, -0.79546723, 11.9482742 ])
Let
** Instructions: ** Plot the corresponding probability density function. Specify the range of x values to $ [0,25] $.
# plot the PDF of a chi^2 random variable with df = 10
x = np.arange(0, 25)
plt.plot(x, stats.chi2.pdf(x, df=10))
plt.show()
Let
** Instructions: ** Calculate $ P (X_1 ^ 2 + X_2 ^ 2> 10) $.
# compute the probability
stats.chi2.sf(10/15, df=2, loc=0, scale=1)
0.7165313105737892
Let
** Instructions: ** Calculate the $ 95% $ quantile for both distributions. Do you have any discoveries?
# compute the 95% quantile of a t distribution with 10000 degrees of freedom
# qt(0.95, df = 10000)
print(stats.t.ppf(0.95, df = 10000))
# compute the 95% quantile of a standard normal distribution
print(stats.norm.ppf(0.95))
# both values are very close to each other. This is not surprising as for sufficient large degrees of freedom the t distribution can be approximated by the standard normal distribution.
1.6450060180692423
1.6448536269514722
Let
** Instructions: ** Generate a $ 1000 $ random number from this distribution and assign it to the variable x </ tt>. Calculate the sample mean of x </ tt>. Can you explain the result?
# generate 1000 random numbers from the given distribution. Assign them to the variable x.
x = stats.t.rvs(df = 1, size = 1000, random_state = 1)
# compute the sample mean of x.
np.mean(x)
# Although a t distribution with M = 1 is, as every other t distribution, symmetric around zero it actually has no expectation. This explains the highly non-zero value for the sample mean.
10.845661965991818
Let
** Instructions: ** Plot the quantile function of a given distribution.
# plot the quantile function of the given distribution
dfn = 10
dfd = 4
x = np.linspace(stats.f.ppf(0.01, dfn, dfd),
stats.f.ppf(0.99, dfn, dfd), 100)
plt.plot(stats.f.pdf(x = x, dfn = dfn, dfd = dfd))
plt.show()
Let
** Instructions: ** Integrate the probability density function to calculate $ P (1 <Y <10) $.
# compute the probability by integration
dfn = 4
dfd = 5
x = np.linspace(stats.f.ppf(0.01, dfn, dfd),
stats.f.ppf(0.99, dfn, dfd), 100)
def function(x):
return stats.f.pdf(x = x, dfn = dfn, dfd = dfd)
integrate.quad(function, 1, 10)[0]
0.4723970230052129
Recommended Posts