Section # 01

Students must abide by UVic academic regulations and observe standards of scholarly integrity (i.e. no plagiarism or cheating). Therefore, this assignment must be taken individually and not with a friend, classmate, or group. You are also prohibited from sharing any information about the assignment with others. I affirm that I will not give or receive any aid on this assignment and that all work will be my own. Timothy Joy

In environmental economics, perfectly competitive markets are our benchmark for efficiency: surplus is maximal under certain conditions:

The point of experiment 1 is to investigate how crucial are the assumptions of price taking and perfect knowledge. If the model of perfect competition does a good job of predicting behaviour even when these assumptions are not met, we can be more confident about using the model as our efficiency benchmark.

In experiment 1 the market-maker (in our case, the webserver) only makes use of the bids and asks when determining price, not the values and costs which are private information. Furthermore, your bid or ask may be the marginal bid or ask, determining either the price paid by demanders or the price received by suppliers. In a nutshell, we compare the predictions of perfect competition to the experimental outcomes to understand whether the assumptions of price taking and perfect information are substantive or merely for analytical convenience.

Recall that in experiment 1 buyers with bids weakly higher than the marginal bid purchase the good and suppliers with asks weakly lower than the marginal ask sell the good. We investigated two different market institutions regarding the prices paid and received. In one treatment the price paid by buyers was the marginal bid and the price received by sellers was the marginal ask: The difference between the marginal bid and marginal ask was the market maker’s gain per unit. In the other treatment the prices were reversed: the price paid by buyers was the marginal ask and the price received by sellers was the marginal bid. The difference between the marginal bid and marginal ask was the market maker’s loss per unit. The treatments were imbalanced in terms of the number of subjects: in rounds 1-10 most subjects were in the gain treatment, and then in rounds 11-20 most of the subjects were in the loss treatment. This allows us to look at the how the market institution (market-maker gain vs. loss) interacts with market depth.

1 (10 marks)

Suppose that you are a demander. If you are in the treatment where the market-maker makes a loss, could your bid ever determine the price buyers pay? If you are in the treatment where the market-maker makes a gain, could your bid ever determine the price buyers pay? How does market depth influence the probability your bid will determine the price buyers pay when the marketmaker gains?

if the market maker is taking a loss at the marginal bid they will not be willing to sell any price below that. As a demander, you are not going to bid for a higher price than you can receive and since your order will not fill below the marginal bid, when a market maker is taking a loss, you cannot influence price. if the market maker is making a profit then your bid can influence price as suppliers may be willing to sell their product for lower than the marginal bid all the way up until the marginal bid is equal to their break even point.

The more market depth, the less likely you are to influence price. if there is lots of liquidity in the market then bid and ask spreads are close together and buyers and sellers are price takers as bid and ask spreads are minimally effected by one bid or ask. If there is not a lot of market depth, aka not a lot of liquidity then your bid is more likely to influence price because markets do not have a lot of suppliers and demander therefor making each bid and ask more considered by the market.

2 (10 marks)

Create a function named salutations which takes as an argument person and prints a personalized greeting. The body of your function should be something like print(paste0("hello ",person,"!")). Hint: functions are defined like this function_name<-function(argument){body}. Functions are called like this: function_name("Rich")

#paste here your salutations function and call it using your name.
function_salutations<-function(person){print(paste0("HOLA ",person,"!"))}
function_salutations("Tim")
## [1] "HOLA Tim!"

3 (0 marks)

Using dataframes supply_and_demand, bid_and_ask, and outcomes plot supply & demand, bids & asks and the marginal bid & ask for round_of_ten==10. Make the supply and demand prominent, and the bids and asks more subtle, and the marginal bid and ask as dots.

4 (10 marks)

Explain what each line of code does to create first_plot. Hint: help on what built-in functions do can by found by typing ?x in the console, where x is the name of the function. Suppose that you were interested in looking at all the rounds in round_of_ten, not just round 10. What would you modify in the code for first_plot for each different round? How many lines of code would you have if you cut/pasted/modified the code that created first_plot to create second_plot, third_plot… tenth_plot?

(first_plot <- ggplot(mapping=aes(x=q, y=variable, colour=role))+

The first line defines the plot name as “first_plot”, then The ggplot function is called. The next part uses aesthetic mapping to map the x and y variables (in this case x is q and y is variable). Colour is then assigned to be the role variable which is buyer or seller.

geom_step(data=filter(supply_and_demand, round_of_ten==10), direction=“vh”, alpha=.5, lwd=1.5)+

The second line of code draws a stepwise line plot for the data “supply_and_demand” and “round_of_ten”. The direction command gives the order of the horizontal and vertical parts of the steps where v=vertical and h=horizontal. So in this case where direction=“vh” the vertical is followed by the horizontal parts of the step. The alpha=.5 sets an alpha level for how opaque the lines are and the lwd=1.5 defines the width of the lines as 1.5. The plus command at the end tells R that the next command is coming.

geom_step(data=filter(bid_and_ask, round_of_ten==10), direction=“vh”, alpha=.5)+

As in line 2 this line plots a stepwise line with the same options but this time for the bid_and_ask data

geom_point(data=filter(outcomes, round_of_ten==10), size=4, alpha=.4)+

This line adds points for the outcomes at size=4 and with an alpha channel of 0.4. The plus command at the end again tells R that the next command is coming.

facet_grid(marketmaker~Rounds)+

The “facet_grid” line defines a matrix of panels by rows and column faceting variables, here it is using marketmaker and Rounds.

ggtitle(paste0(“Supply, Demand (thick), bids, asks (thin), Marginal bid, ask (dots) in round”,10)))

The ggtitle line calls the paste function and titles the graphs, using the name of the title, and the round in brakets

part b:you would modify the part of the code that says round_of_10==10 to ==1 or==2, all the way up to 10 and then change the number at the end of the ggtitle. if you cut and pasted the same code over and over again from round 1-10 you would have 60 lines of code

5 (10 marks)

Use the code that created first_plot to write a function plot_sd() that takes as an argument rnd_of_10. The body of plot_sd() will contain the code that created first_plot, except wherever there was a 10 you need to replace it with rnd_of_10. Hint: you can test the function by typing plot_sd(10) and it should look identical to first_plot. Use assert_that to make sure the input to your function is valid: i.e. rnd_of_10 %in% 1:10. How many lines of code would you have in total for the function, and 10 calls to that function?

#paste a copy of your plot_sd function in this code chunk.
function_plot_sd <- function(rnd_of_10){
  (first_plot <- ggplot(mapping=aes(x=q, y=variable, colour=role))+
  geom_step(data=filter(supply_and_demand, round_of_ten==rnd_of_10), direction="vh", alpha=.5, lwd=1.5)+
  geom_step(data=filter(bid_and_ask, round_of_ten==rnd_of_10), direction="vh", alpha=.5)+
  geom_point(data=filter(outcomes, round_of_ten==rnd_of_10), size=4, alpha=.4)+ 
  facet_grid(marketmaker~Rounds)+
  ggtitle(paste0("Supply, Demand (thick), bids, asks (thin), Marginal bid, ask (dots) in round ",rnd_of_10)))}
rnd_of_10=5
assert_that(rnd_of_10 %in% 1:10)

If I had 10 calls to that function i would have 17 lines of code, 7 for defining the function and 10 for call the function at each round

6 (10 marks)

Use your function plot_sd() to plot the supply, demand bids and asks in round 10.

# remove first hashtag when ready to insert second plot.
# make rnd_of_10 some integer value between 1 and 10
function_plot_sd(rnd_of_10=10) 

7 (10 marks)

Copy and paste the code that created first_plot, and rename it third_plot. Add the aesthetic frame=round_of_ten inside the aes(). i.e mapping=aes(q,variable, colour=role, frame=round_of_ten). Remove the filtering of the data used by both the geom_step() calls e.g instead of data=filter(supply_and_demand,round_of_ten==10) use data=supply_and_demand. Run the code to create third_plot by hitting control plus enter but do not look at at the plot. Instead, if third_plot ran without error, copy the following code into your assignment1.R file and hit control plus enter. How many lines of code were required to see the plots for all 10 rounds? it only took 7 lines of code for all 10 rounds to be displayed.

(third_plot <- ggplot(mapping=aes(x=q, y=variable, colour=role, frame=round_of_ten))+
  geom_step(data=supply_and_demand, direction="vh", alpha=.5, lwd=1.5)+
  geom_step(data=bid_and_ask, direction="vh", alpha=.5)+
  geom_point(data=filter(outcomes, round_of_ten==10), size=4, alpha=.4)+ 
  facet_grid(marketmaker~Rounds)+
  ggtitle(paste0("Supply, Demand (thick), bids, asks (thin), Marginal bid, ask (dots) in round ",10)))
########################
(third_plot <- ggplotly(third_plot)%>%
animation_opts(1000,transition = 100)%>%
hide_legend()%>% 
config(displayModeBar = F))

Insert your animation (third_plot) below:

8 (10 marks)

Describe the dataframe mydf. You can view the dataframe by clicking on it in the top right panel of Rstudio, and it will open in the top left panel. What are the columns, and what are the rows?

mydf is a dataframe of every single entry from every user in our experiment, it has 1440 rows and 13 columns.every row is a single entry from our experiment on friday. the entries have our 5 digit v number, our bid/ask, the marginal bid/ask from that specific level, our role, whether we profited or lossed, the time etc. every column is one specific aspect of our collected data, for example one column is the data of the exact time someone made an entry or the price we had put as our bid or ask.

9 (10 marks)

Explain what the following code snippet does. Hints: <- is the assignment function (give the result on the right the name on the left). %>% is the pipe function, read as THEN (take what is on the left THEN apply the function on the next line). For functions that have alphabetic names you can type ?name in the console to get help. e.g. ?group_by

value_and_cost <- mydf%>%
    group_by(marketmaker,round_of_ten, Rounds, role, variable)%>%
    count(variable)

This is a function that groups all the data that has similar values for the variables “marketmaker, round_of_ten, Rounds, role, variable”, and orders them in the order of the variables in that list. The output dataframe called ‘value_and_cost’ has a new column called ‘n’ that counts the number of entries that have the same values within the group assignment.

10 (10 marks)

Cut and paste the above code snippet into your assignment1.R file and save. Source the assignment1.R and click on value_and_cost in the top right panel. How can dataframe value_and_cost be used to check to make sure that function plot_sd is plotting the supply and demand correctly?

The dataframe ‘value_and_cost’ returns the count ‘n’ for the grouping variable (i.e., ‘group-by’) list. Value_and_cost does not change as its a summary of mydf, however, the ‘plot_sd’ function that creates the supply and demand figures, which is reactive to the input variable ‘rnd_of_10’. Therefore, ‘plot_sd’ can be compared to the dataframe ‘value_and_cost’ to ensure the figure is plotting the correct ‘n’ value.

11 (0 marks)

For round_of_ten==10, plot the relationship between choice and variable for all combinations of marketmaker and role, using a different colour for Rounds

12 (10 marks)

Copy and paste the code that created fourth_plot, and rename it fifth_plot. Add the aesthetic frame=round_of_ten inside the aes(). i.e mapping=aes(variable,choice,frame=round_of_ten, colour=Rounds). Remove the filtering of the data e.g. instead of data=filter(mydf, round_of_ten==10) use data=mydf. Run the code to create fifth_plot by hitting control plus enter but do not look at at the plot. Instead, if fifth_plot ran without error, copy the following code into your assignment1.R file and hit control plus enter.

(fifth_plot <- ggplot(data=mydf,aes(variable,choice, frame=round_of_ten, colour=Rounds))+
    geom_point(alpha=.25) +
    geom_line(stat="smooth", method="lm",lwd=1.5, alpha=.5)+
    geom_abline(intercept = 0, slope = 1,lty=2,alpha=.25)+
    facet_grid(role~marketmaker)+
    ggtitle(paste0("Choice vs Variable in round ",10)))
(fifth_plot <- ggplotly(fifth_plot)%>%
  animation_opts(1000,transition = 100)%>%
  hide_legend()%>% 
  config(displayModeBar = F))