---
# System prepended metadata

title: Coco's Programming Test
tags: [Coco]

---

# Coco's Programming Test

###### tags: `Coco`

#### Instructions

1. The test is 60 minutes long.
2. You can use Python or R to complete this test.
3. To access the programming environment, log onto the Sloan Remote Lab using your MIT ID: https://sloan-remote.mit.edu/.
4. Save all your code as a single file in the folder PLT. Name the file in the following format: `MITID_LastName_FirstName.txt`
5. Do not communicate with other students during the test.
6. No materials outside the programming environment are allowed. You are allowed to use the help files within the programming environment.

#### Problem 1 (Simulating a random walk)
Consider the following model:
$$x_t = x_{t-1} + \sigma\epsilon_t$$

In this model $\sigma$ is a constant, and $\epsilon_t$ (for $t = 1,2,...,T$) are independent and identically distributed, taking the value 1 and −1 with probability 0.5.

1. Write a function that simulates a path of $x_t$ for $T$ periods.
    * The function should take the following parameter values as input: $T, \sigma, x_0$
    * The function should produce the following output: the time series of simulated values of $x_t$
    
2. Use the function you have written above to do the following:
    * Simulate a time series of $x$ using the following parameter values: $T = 60, \sigma = 0.1, x_0 =1$
    * Plot the time series, add proper labels and title, and save it as a PDF file under the name "MITID path.pdf".
    
```python=
import random

# 1. write function that simulate a path of x_t for T periods

def simulate(T, sigma, x0):
    """
        input:
            T: number of periods
            sigma: a constant
            x0: initial condition
        output:
            t: timestamp
            x: simulated x values
    """
    x = [x0]
    t = [0]
    for i in range(T):
        x.append(x[-1] + sigma * simulateHelper())
        t.append(t[-1] + 1)
    return t, x
        
def simulateHelper():
    """
        return 1 or -1 with equal probability 0.5
    """
    if random.random() < 0.5:
        return 1
    else:
        return -1

# 2.1 Simulate a time series of 𝑥 using the given parameter values
t, x = simulate(60, 0.1, 1)

# 2.2 Plot the time series, add proper labels and title
import matplotlib.pyplot as plt
f = plt.figure()
f.suptitle('Random Walk Simulation')
plt.plot(t, x)
plt.xlabel('time')
plt.ylabel('value')
plt.show() # needed if not interactive mode

# save it as a PDF file under the name MITID_path.pdf
f.savefig("MITID_path.pdf", bbox_inches='tight')
```

R Solution
```R
randomwalk <- function(T,sigma,x0) {
  x=c(x0)
  t=c(0)
  for (i in 1:T) {
    x<-append(x,x[length(x)]+sigma*random1(),after=length(x))
    t<-append(t,t[length(t)]+1,after=length(t))
  }
  a=list(x,t)
  return(a)
}

random1 <- function() {
  if (runif(1,0,1)>=0.5) {
    return(1)
  }
  else {
    return(-1)
  }
}

b<-randomwalk(60,0.1,1)
pdf("MITID_path.pdf")
plot(unlist(b[2]),unlist(b[1]), type="l", xlab="time", ylab="value", main="Random Walk")
dev.off() 
```

#### Problem 2 (Working with market data)

Copy the data file CPIAUCSL.csv for Consumer Price Index from the common working directory and perform the following tasks.

```python=
# First compute the monthly inflation, defined using the formula

import pandas

df = pandas.read_csv("CPIAUCSL.csv")
size = len(df)
for i in range(size):
    if i != 0:
        df.loc[i, 'inflation'] = df.loc[i, 'CPIAUCSL']
        df.loc[i, 'inflation'] -= df.loc[i-1, 'CPIAUCSL']
        df.loc[i, 'inflation'] /= df.loc[i-1, 'CPIAUCSL']
        
# Split the sample into halves

half_index = size / 2
df1, df2 = df.loc[:half_index], df.loc[half_index+1:]

# Compute mean and standard deviation in both samples
mean1, std1 = df1['inflation'].mean(), df1['inflation'].std()
mean2, std2 = df2['inflation'].mean(), df2['inflation'].std()

# Plot the monthly inflation time series
import matplotlib.pyplot as plt
f = plt.figure()
f.suptitle('Monthly Inflation Time Series')
plt.plot(df["DATE"], df["inflation"], label='monthly')

# Add to the plot two horizontal lines to mark the average inflation computed above
plt.plot(df["DATE"], [mean1] * size, label='average 1')
plt.plot(df["DATE"], [mean2] * size, label='average 2')

# Label the axes properly
plt.xlabel('Date')
plt.ylabel('Monthly Inflation Rates')

# Save the plot as a PDF under the name "MITID_inflation.pdf"
plt.legend(loc='upper right')
f.savefig("MITID_inflation.pdf", bbox_inches='tight')
```

<div style="text-align: center">
<img src="https://i.imgur.com/3iJY4RS.png">
</div>

R solution:
```r
mydata<-read.csv("/Users/cocoz/Downloads/CPIAUCSL.csv",header = TRUE)
x<-diff(mydata$CPIAUCSL,lag = 1,differences = 1)
x<-c(0,x)
mydata$diff<-x
mydata$diff<-mydata$diff/mydata$CPIAUCSL

print(nrow(mydata))
half<-nrow(mydata)/2

mydata1<-mydata[1:half, ]
mydata2<-mydata[(half+1):nrow(mydata), ]
summary(mydata1$diff)
summary(mydata2$diff)


mean1<-mean(mydata1$diff)
mean2<-mean(mydata2$diff)
pdf("MITID_path.pdf")
plot(mydata$DATE, mydata$diff,type="l",xlab="Date",ylab="monthly inflation rate")
lines(mydata$DATE, mydata$diff,xlab="Date",ylab="monthly inflation rate")
lines(mydata$DATE, rep(mean(mydata1$diff),856),col="blue")
lines(mydata$DATE, rep(mean(mydata2$diff),856),col="red")
dev.off()
```
```python=
for i in stocks:
    nb = nbbo[nbbo.symbol == i]
    trade = trades[trades.symbol == i]
    l = max(nb.time[nb.time < morning])
    u = min(nb.time[nb.time > end])
    nb = nb[(nb.time >= l) & (nb.time <= u)]
    nb.time = nb.time * (10 ** 6)
    trade = trade[(trade.time >= morning * (10 ** 6)) & (trade.time <= stop * (10 ** 6))]

    addrows = pd.DataFrame({'time': trade.time.values})
    nb = nb.append(addrows, ignore_index=True, sort=True)
    nb = nb.sort_values(by=['time'])

    nb.set_index('time', inplace=True)
    trade.set_index('time', inplace=True)

    df = pd.merge_asof(trade, nb, on='time', by='symbol')

    df0 = pd.concat([df0, df])
df0.set_index('symbol', inplace=True)
df0['mid'] = (df0.bid + df0.ask) / 2
df0['PerShareTC'] = abs(df0.Price - df0.mid) / df0.mid * (10 ** 4) 
df0['TotalTC'] = df0.PerShareTC * df0.Shares
vwac = pd.DataFrame(index=stocks, columns=['Buyer', 'Seller'])
for j in stocks:
    df = df0.loc[j]
    vwac_B = sum(df[df.BuySell == 'B'].TotalTC) / sum(df[df.BuySell == 'B'].Shares)
    vwac_S = sum(df[df.BuySell == 'S'].TotalTC) / sum(df[df.BuySell == 'S'].Shares)
    vwac.loc[j] = [vwac_B, vwac_S]
    
    
mktcap['ln_mktcap'] = np.log(mktcap.mktcap)
buy_regression = sm.OLS(vwac.Buyer.astype(float), sm.add_constant(mktcap.ln_mktcap)).fit()
buy_regression.summary()
sell_regression = sm.OLS(vwac.Seller.astype(float), sm.add_constant(mktcap.ln_mktcap)).fit()
sell_regression.summary()


```
