Вы находитесь на странице: 1из 7

Statistics I / Quiz 9

Michael King
Fall 2019

Introduction

In this quiz you will calculate the covariance of two jointly distributed continuous random
variables. The below formulas review some of the equations about covariance.

Cov(X, Y ) = E[(X − µX )(Y − µY )]

= E[XY ] − E[X]E[Y ]

Cov(X, Y ) = ρXY σX σY

Question 1

Let X and Y be jointly continuous random variables with density function







8



xy , 0 ≤ x ≤ 1, x ≤ y ≤ 2x




3



f (x, y) = 



0 , otherwise









Calculate the covariance of X and Y .


# Clear the environment
remove(list = ls())

# Define f
f <- function(x, y){(8 / 3) * x * y * (0 < x & x < 1) * (x <= y & y <= 2

# Source iterated_integral.R using source()


source('iterated_integral.R')

# Define the limits of integration


xl <- function(y){0}
xu <- function(y){1}
yl <- function(x){x}
yu <- function(x){2 * x}

# Calculate E[X]
EX <- iterated_integral(
f = function(x, y){x * f(x, y)},
xl = xl,
xu = xu,
yl = yl,
yu = yu,
dx = 2)$value
EX
[1] 0.8
# Calculate E[Y]
EY <- iterated_integral(
f = function(x, y){y * f(x, y)},
xl = xl,
xu = xu,
yl = yl,
yu = yu,
dx = 2)$value
EY
[1] 1.244444
# Calculate E[XY]
EXY <- iterated_integral(
f = function(x, y){x * y * f(x, y)},
xl = xl,
xu = xu,
yl = yl,
yu = yu,
dx = 2)$value
EXY
[1] 1.037037
# Calculate the Covariance
covariance <- EXY - EX * EY
covariance
[1] 0.04148148
Question 2

In this part of the quiz you will explor the gapminder data set. Filter the data to only
include data from 2007 . Group the data by continent and compute the average life
expectancy. Arrange the data in descending order of life expectancy and report the results
as a bar graph.
# Clear the environment
rm(list = ls())

# Load the package


library(gapminder)

# View the column names of the data saet


names(gapminder)
[1] "country" "continent" "year" "lifeExp" "pop" "gdpPerc
# View the dimensions of the data set
dim(gapminder)
[1] 1704 6
# Convert pop to numeric from integer
gapminder$pop <- as.numeric(gapminder$pop)

# Filter, group and arrange the data


M <-
gapminder %>%
filter(year == 2007) %>%
group_by(continent) %>%
summarize(avg_life = sum(pop * lifeExp) / sum(pop)) %>%
arrange(desc(avg_life))
# View M
kable(M)

continent avg_life
Oceania 81.06215
Europe 77.89057
Americas 75.35668
Asia 69.44386
Africa 54.56441

# To change plot order of bars, change levels in underlying factor


M$continent <- factor(M$continent, levels = M$continent)

# Create the bar plot


G <-
ggplot() +
geom_col(
data = M,
mapping = aes(x = continent, y = avg_life, fill = (avg_life / 80)),
# fill = 'darkred',
alpha = 0.5,
width = 0.6,
color = 'white') +
labs(
title = 'Life Epectancy by Continent for 2007',
subtitle = NULL,
caption = 'Quiz 10',
x = NULL,
y = 'Life Expectancy') +
theme_classic() +
coord_flip() +
theme(
legend.position = 'none',
title = element_text(
face = 'italic',
family = 'serif',
color = 'black',
size = 13,
angle = 0),
axis.text = element_text(
face = 'italic',
family = 'serif',
color = 'black',
size = 13,
angle = 0),
axis.ticks = element_blank())

G
Life Epectancy by Continent for 2007

Africa

Asia

Americas

Europe

Oceania

0 20 40 60 80
Life Expectancy
Quiz 10
Question 3

Let X and Y be continuous random variables with joint density function






8




xy , 0 ≤ x ≤ 1, x ≤ y ≤ 2x



f (x, y) = 

3






0 , otherwise


Calculate the covariance of X and Y .


include_graphics('quiz_9.jpg')

Вам также может понравиться