Skip to content

Instantly share code, notes, and snippets.

Avatar

Krishnan Raman krishnanraman

View GitHub Profile
@krishnanraman
krishnanraman / test.R
Created Dec 6, 2020
hierarchical model Stan
View test.R
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)
View gist:e0aa69ebf67f51668d6691ec68ff6bca
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 Nov 28, 2020
gradient descent for regression
View gradientdescent.R
# 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)
View 24.R
n=10
op = c('+', '-', '*', '/')
perms = c()
while(length(perms) < 24) {
s = paste(sample(op,4), collapse='')
if (length(perms[perms==s]) == 0) {
perms =c(perms,s)
print(length(perms))
}
View foo.R
rm(list=ls())
library(MASS)
mu <- c(1,0)
Sigma <- matrix(c(1,0.5,0.5,1),2,2)
n<- 1000
sumc <- c()
for(times in 1:1000) {
x<- mvrnorm(n=n,mu,Sigma)
sum<-0
for(i in 1:(n-1)) {
View gist:4b5ff446da6c55b971613fc43e4ebadc
tsum <- function(n, myn, mydf, myncp) {
mylist <- c()
for (i in 1:n) {
samp <- rt(n=myn, df = mydf, ncp=myncp)
mylist<- c(mylist,sum(samp) - min(samp))
}
return(mylist)
}
x<- tsum(1000, 250, 3, 1)
View lmfitparabola.py
import numpy as np
from lmfit import Minimizer, Parameters, report_fit
# create data to be fitted
x = np.linspace(0, 15, 301)
data = 2*x*x+ 3*x+4
# define objective function: returns the array to be minimized
View clickoptim.py
# maximize abc subject to a + b + c = 10
import numpy as np
import tensorflow as tf
tf.reset_default_graph()
abc = tf.get_variable("abc",shape=(3,1),dtype=tf.float32, initializer=tf.ones_initializer)
optimizer = tf.train.GradientDescentOptimizer(0.0001)
# grab a, b, c and the lambda l
@krishnanraman
krishnanraman / result.txt
Last active Feb 10, 2018
Get all leaves of the DecisionTree ( then construct spline thru leafnodes to build f(x)=>y )
View result.txt
// Exiting paste mode, now interpreting.
id = 8, isLeaf = true, predict = 0.0 (prob = -1.0), impurity = 0.0, split = None, stats = None
id = 9, isLeaf = true, predict = 1.4736842105263157 (prob = -1.0), impurity = 0.2493074792243767, split = None, stats = None
id = 10, isLeaf = true, predict = 3.0 (prob = -1.0), impurity = 0.16666666666666666, split = None, stats = None
id = 11, isLeaf = true, predict = 4.1 (prob = -1.0), impurity = 0.09000000000000057, split = None, stats = None
id = 12, isLeaf = true, predict = 5.0 (prob = -1.0), impurity = 0.0, split = None, stats = None
id = 13, isLeaf = true, predict = 6.444444444444445 (prob = -1.0), impurity = 0.2469135802469143, split = None, stats = None
id = 14, isLeaf = true, predict = 7.923076923076923 (prob = -1.0), impurity = 0.2248520710059158, split = None, stats = None
id = 15, isLeaf = true, predict = 9.0 (prob = -1.0), impurity = 0.0, split = None, stats = None
@krishnanraman
krishnanraman / output.txt
Created Feb 8, 2018
Create Spark DataFrame From List
View output.txt
+---+---+
| x| y|
+---+---+
| 1| 0|
| 2| 0|
| 3| 0|
| 4| 0|
| 5| 0|
| 6| 0|
| 7| 0|