Skip to content

Instantly share code, notes, and snippets.

@tvladeck
tvladeck / beta_nbd_ll.R
Created February 17, 2016 23:48
log likelihood of beta nbd model
beta.nbd.ll <- function(x, n_x, r, alpha, a, b)
{
# params:
# x: vector of counts
# n_x: number of observations for each count
# r, alpha: nbd params
# a, b: beta params
# output:
# the log-likelihood of the model
probs <- sapply(x, function(y) beta.nbd.density(y, r, alpha, a, b))
@tvladeck
tvladeck / beta_nbd.R
Created February 17, 2016 20:23
NBD with t distributed beta
# NBD with t distributed beta
beta.nbd.density <- function(x, r, alpha, a, b)
{
# params:
# x: count we are evaluating density at
# r: nbd r parameter
# alpha: nbd alpha parameter
# a: beta alpha parameter
# b: beta beta parameter
@tvladeck
tvladeck / kmm_scree.R
Created December 23, 2015 18:46
determining optimal # of clusters in k means
mydata <- d
wss <- (nrow(mydata)-1)*sum(apply(mydata,2,var))
for (i in 2:15) wss[i] <- sum(kmeans(mydata,
centers=i)$withinss)
plot(1:15, wss, type="b", xlab="Number of Clusters",
ylab="Within groups sum of squares")
@tvladeck
tvladeck / spectral.R
Last active December 19, 2015 16:26
spectral clustering for random forest proximity / similarity matrix
spectral <- function(proximity, k = 2)
{
D <- diag(apply(proximity, 1, sum))
U <- D - proximity
# L <- diag(nrow(my.data)) - solve(D) %*% A # simple Laplacian
# round(L[1:12,1:12],1)
# matrix power operator: computes M^power (M must be diagonalizable)
"%^%" <- function(M, power)
tooltips.keys.each do |key|
define_method(key) { tooltip tooltips[key][:display], t[key][:tip] }
end
- method: energy_cost_savings
display: Energy Cost Savings
tip: This is the tooltip
irb(main):012:0> account.logo.url
=> "/uploads/account/logo/1/me_climbing_mountains.jpg"
Showing /Users/thomasvladeck/Git/bh_pro/app/views/proposals/_cover_sheet.xlsx.axlsx where line #6 raised:
No such file or directory - /uploads/account/logo/1/me_climbing_mountains.jpg
Extracted source (around line #6):
3
4
5
6
7
8
filepath = "#{Rails.root.join('tmp')}/new"
File.open(filepath, "wb") do |f|
f.write open(@account.logo.url).read
end
@tvladeck
tvladeck / convert_to_annual.rb
Created November 15, 2012 05:36
converting to annual
irb(main):002:0> def convert_to_annual(ary)
irb(main):003:1> len = ary.length
irb(main):004:1> rem = len % 12
irb(main):005:1> idx = len / 12
irb(main):006:1> ret = idx.times.map { |i| ary[i..(i+11)].reduce(:+) }
irb(main):007:1> ret << ary[(len - rem)..(len)].reduce(:+)
irb(main):008:1> ret
irb(main):009:1> end