Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Anirudh Jayaraman anirudhjayaraman

🏠
Working from home
View GitHub Profile
@anirudhjayaraman
anirudhjayaraman / HW02.R
Last active Jun 14, 2021
14.74x Foundation of Development Policy (Homework Assignment 02)
View HW02.R
# Set working directory to local directory where the data is kept
setwd("~/IGIDR/Development Economics - MIT/Homework Assignment 02")
# read data
migueldata = read.csv("ted_miguel_worms.csv", header = TRUE)
attach(migueldata)
# Question 6
# How many observations are there per pupil? (Enter a whole number of 0 or higher)?
length(migueldata$pupid)
length(unique(migueldata$pupid))
# Question 7
@anirudhjayaraman
anirudhjayaraman / sherlockandthebeast.py
Created Dec 12, 2015
Solution to HackerRank Problem - Sherlock and the Beast
View sherlockandthebeast.py
#!/bin/python
import sys
def combinations_sum(n):
k = n/2
combs = []
answers = []
for i in range(0,k+1):
a,b = i, n-i
@anirudhjayaraman
anirudhjayaraman / lm_linear_algebra.R
Created Nov 12, 2019
Linear regression implementation using linear algebra in R
View lm_linear_algebra.R
### Linear Regression Using lm() ----------------------------------------
data("swiss")
dat <- swiss
linear_model <- lm(Fertility ~ ., data = dat)
summary(linear_model)
# Call:
# lm(formula = Fertility ~ ., data = dat)
#
@anirudhjayaraman
anirudhjayaraman / karatsuba.py
Last active Feb 22, 2021
Karatsuba Multiplication
View karatsuba.py
def karatsuba(x,y):
"""Function to multiply 2 numbers in a more efficient manner than the grade school algorithm"""
if len(str(x)) == 1 or len(str(y)) == 1:
return x*y
else:
n = max(len(str(x)),len(str(y)))
nby2 = n / 2
a = x / 10**(nby2)
b = x % 10**(nby2)
@anirudhjayaraman
anirudhjayaraman / graphUndirected.py
Last active Feb 16, 2021
Implementing Undirected Graphs in Python
View graphUndirected.py
class Vertex:
def __init__(self, vertex):
self.name = vertex
self.neighbors = []
def add_neighbor(self, neighbor):
if isinstance(neighbor, Vertex):
if neighbor.name not in self.neighbors:
self.neighbors.append(neighbor.name)
neighbor.neighbors.append(self.name)
@anirudhjayaraman
anirudhjayaraman / quicksort.py
Last active Feb 3, 2021
Python code for the Quick Sort Algorithm
View quicksort.py
def quicksort(x):
if len(x) == 1 or len(x) == 0:
return x
else:
pivot = x[0]
i = 0
for j in range(len(x)-1):
if x[j+1] < pivot:
x[j+1],x[i+1] = x[i+1], x[j+1]
i += 1
View AirPassengers.Rmd
---
title: "ARIMA Modeling in R"
output: html_document
---
Let's start off by loading relevant R libraries!
```{r include = FALSE}
library(tseries)
library(zoo)
library(forecast)
@anirudhjayaraman
anirudhjayaraman / mergesort.py
Last active Aug 5, 2020
Merge Sort Algorithm Implimentation
View mergesort.py
# Code for the merge subroutine
def merge(a,b):
""" Function to merge two arrays """
c = []
while len(a) != 0 and len(b) != 0:
if a[0] < b[0]:
c.append(a[0])
a.remove(a[0])
else:
View rice_strucchange.R
library(xlsx)
library(forecast)
library(tseries)
library(strucchange)
## load the data from a CSV or Excel file. This example is done with an Excel sheet.
prod_df <- read.xlsx(file = 'agricultural_productivity.xls', sheetIndex = 'Sheet1', rowIndex = 8:65, colIndex = 2, header = FALSE)
colnames(prod_df) <- c('Rice')
## store rice data as time series objects
rice <- ts(prod_df$Rice, start=c(1951, 1), end=c(2008, 1), frequency=1)
View remove_missing_levels.R
remove_missing_levels <- function(fit, test_data) {
library(magrittr)
# https://stackoverflow.com/a/39495480/4185785
# drop empty factor levels in test data
test_data %>%
droplevels() %>%
as.data.frame() -> test_data