library(ggplot2)
u <- function(x){
x^1.5
}
df <- data.frame(
x=seq(1,220,0.1),
y=NA
)
df$y <- u(df$x)
#Variables for plot (may not match labels as not done to scale)
#Payoffs from gamble
p1 <- 0.5
x1 <- 30 #loss
p2 <- 0.5
x2 <- 200 #win
ev <- p1*x1+p2*x2 #expected value of gamble
xc <-115 #certain outcome
ce <-(p1*u(x1)+p2*u(x2))^(1/1.5) #certainty equivalent
px2 <-(ev-x1)/(x2-x1)
ggplot(mapping = aes(x, y)) +
#Plot the utility curve
geom_line(data = df) +
geom_vline(xintercept = 0, linewidth=0.25)+
geom_hline(yintercept = 0, linewidth=0.25)+
labs(x = "x", y = "U(x)")+
# Set the theme
theme_minimal()+
#remove numbers on each axis
theme(axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title=element_text(size=14,face="bold"),
axis.title.y = element_text(angle=0, vjust=0.5))+
#set limits - need to include room for labels
coord_cartesian(xlim = c(-25, 220), ylim = c(-0.25, 3000))+
#Add labels x1, U(x1) and line to curve indicating each
annotate("text", x = x1, y = 0, label = "x1", size = 4, hjust = 0.5, vjust = 1.5)+
annotate("segment", x = x1, y = 0, xend = x1, yend = u(x1), linewidth = 0.5, colour = "black", linetype="dotted")+
annotate("segment", x = 0, y = u(x1), xend = x1, yend = u(x1), linewidth = 0.5, colour = "black", linetype="dotted")+
annotate("text", x = 0, y = u(x1), label = "U(x1)", size = 4, hjust = 1.05, vjust = 0.6)+
#Add line to curve indicating utility of expected value
annotate("segment", x = xc, y = 0, xend = xc, yend = u(xc), linewidth = 0.5, colour = "black", linetype="dotted")+
annotate("segment", x = 0, y = u(xc), xend = xc, yend = u(xc), linewidth = 0.5, colour = "black", linetype="dotted")+
annotate("text", x = 0, y = u(xc), label = "U[E(X)]", size = 4, hjust = 1.05, vjust = 0.3)+
#Add expected utility line
annotate("segment", x = x1, xend = x2, y = u(x1), yend = u(x2), linewidth = 0.5, colour = "black", linetype="dotdash")+
#Add labels x2, U(x2) and line to curve indicating each
annotate("text", x = x2, y = 0, label = "x2", size = 4, hjust = 0.4, vjust = 1.5)+
annotate("segment", x = x2, y = 0, xend = x2, yend = u(x2), linewidth = 0.5, colour = "black", linetype="dotted")+
annotate("segment", x = 0, y = u(x2), xend = x2, yend = u(x2), linewidth = 0.5, colour = "black", linetype="dotted")+
annotate("text", x = 0, y = u(x2), label = "U(x2)", size = 4, hjust = 1.05, vjust = 0.45)+
#Add labels E[X], E[U(X)] and curve indicating each
annotate("text", x = ev, y = 0, label = "E[X]", size = 4, hjust = 0.4, vjust = 1.5)+
annotate("segment", x = ev, y = 0, xend = ev, yend = u(x1)+(u(x2)-u(x1))*px2, linewidth = 0.5, colour = "black", linetype="dashed")+
annotate("segment", x = 0, y = u(x1)+(u(x2)-u(x1))*px2, xend = ev, yend = u(x1)+(u(x2)-u(x1))*px2, linewidth = 0.5, colour = "black", linetype="dashed")+
annotate("text", x = 0, y = u(x1)+(u(x2)-u(x1))*px2, label = "E[U(X)]", size = 4, hjust = 1.05, vjust = 0.45)+
#Add vertical line indicating certainty equivalent and labelled "CE" plus line extending to utility curve
annotate("segment", x = ce, xend = ce, y = 0, yend = u(ce), linewidth = 0.5, colour = "black", linetype="dotted")+
annotate("text", x = ce, y = 0, label = "CE", size = 4, hjust = 0.4, vjust = 1.5)+
annotate("segment", x = 0, y = u(ce), xend = ce, yend = u(ce), linewidth = 0.5, colour = "black", linetype="dotted")