Skip to content

Instantly share code, notes, and snippets.

@krishnanraman
krishnanraman / election.csv
Created February 19, 2025 21:10
election.csv
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 3 columns, instead of 2 in line 1.
Alabama 2,265,090 Republican
Alaska 338,177 Republican
Arizona 3,390,161 Republican
Arkansas 1,182,676 Republican
California 15,865,475 Democratic
Colorado 3,192,745 Democratic
Connecticut 1,759,010 Democratic
Delaware 512,912 Democratic
District of Columbia 325,869 Democratic
Florida 10,893,752 Republican
@krishnanraman
krishnanraman / qol.csv
Last active June 6, 2024 17:31
oxford qol
Place Economics HumanCapital QOL Environment Governance
New York United States 1 4 278 353 184
London United Kingdom 7 1 292 197 72
San Jose United States 3 28 38 53 184
Tokyo Japan 10 2 218 277 26
Paris France 9 6 65 409 161
Seattle United States 4 40 56 40 184
Los Angeles United States 2 19 280 79 184
San Francisco United States 5 34 101 62 184
Melbourne Australia 16 11 185 30 18
# Find x such that x+1, 2x+3,3x+5 and 4x+7 are prime.
library(DescTools)
# This works
subset(expand.grid(x=1:1000),IsPrime(x+1) & IsPrime(2*x+3) & IsPrime(3*x+5) & IsPrime(4*x+7))
# Alternate solution
which(mapply(function(x) IsPrime(x+1) & IsPrime(2*x+3) & IsPrime(3*x+5) & IsPrime(4*x+7), 1:1000))
dim(subset(expand.grid(a=0:80,b=seq(2,80,2),c=c(0,2,5)), a+b+c == 80))
@krishnanraman
krishnanraman / aime4.R
Created February 3, 2024 00:44
AIME 2024 4
x<-0:9
n<-4
N<-100000
atleasttwomatch<- 0
allfourmatch<-0
for(i in 1:N) {
a<-sample(x,n)
b<-sample(x,n)
s<-length(a[is.element(a, b)])
if (s > 1) {
x<-seq(1,20)
n<-100000
mx<-c()
for (i in 1:n) {
mx<-c(mx,max(sample(x,10,replace=F)))
}
summary(mx)
Min. 1st Qu. Median Mean 3rd Qu. Max.
> x<-runif(100,0,2)
> y<-1+x+x^2+x^3
> summary(lm(y~x))
Call:
lm(formula = y ~ x)
Residuals:
Min 1Q Median 3Q Max
-1.334 -1.018 -0.213 0.677 2.742
@krishnanraman
krishnanraman / test.R
Created December 6, 2020 02:42
hierarchical model Stan
rm(list=ls())
library(rstan)
rstan_options(auto_write = FALSE)
options(mc.cores = parallel::detectCores())
N=1000
data = list(N=N,x1<-rnorm(N,5,2),x2<-rnorm(N,7,2),x3<-rnorm(N,9,2),y<-x1+x2+x3)
fit <- suppressMessages(stan(file = '~/Desktop/695/test.stan', data = data, iter=11000, warmup=1000, chains=2, seed=483892929, refresh=11000))
print(fit)
plot(fit)
rm(list =ls())
dev.off()
for(j in 1:4) {
n=1000
x=numeric(n)
y=numeric(n)
x[1] = rnorm(1,0,0.2)
y[1] = rnorm(1,0,0.2)
a=0.51
@krishnanraman
krishnanraman / gradientdescent.R
Created November 28, 2020 01:48
gradient descent for regression
# Goal: Find unknown scalar w to minimize L(w)
# L(w) = sum((y[i] - w*x[i])^2)
# (x[i], y[i]), i=1..n dataset for linear regression
#
# Repeat Iterative Procedure below until convergence:
# w[i+1] = w[i] - alpha * gradient(L(w), w=w[i])
#
set.seed(12345)
x=seq(-5,5,0.5)
y = 2*x + rnorm(length(x),0,1)