Skip to content

Instantly share code, notes, and snippets.

@rrguntaka
rrguntaka / spam.rb
Created October 25, 2011 23:41 — forked from mschuetz/spam.rb
simple bayesian spam classifier using laplace smoothing. quickly thrown together but i'll try to polish this tomorrow.
def sum(arr)
res = 0
arr.each{|e| res+=e}
res
end
def mult(arr)
res = 1
arr.each{|e| res*=e}
res
@rrguntaka
rrguntaka / gist:1352994
Created November 9, 2011 20:57
Split Ruby array
a.each_slice(n).to_a
#Source: http://snippets.dzone.com/posts/show/3486
@rrguntaka
rrguntaka / parse_topics.rb
Created November 9, 2011 22:56
Parse tab limited, two level text file
require "rubygems"
require "sequel"
DB = Sequel.connect('jdbc:sqlite:store.db') #
all_tables = DB[:sqlite_master]
topics = [];goals = [];prev_ind = 0;second_ind = 0
Dir.glob("*.txt").each do |f|
IO.readlines(f).each do |line|
@rrguntaka
rrguntaka / quote_parser.rb
Created November 10, 2011 05:36
Parse Google historical prices csv file
require "rubygems"
require "sequel"
require "date"
DB = Sequel.connect('jdbc:sqlite:stockinfo.db')
DB.create_table? :quote do
primary_key :id
String :tick
DateTime :qdate
Float :open
@rrguntaka
rrguntaka / utils.clj
Created November 15, 2011 20:55
Harmonic Mean
def har_mean(a)
a.size / a.map{|v| 1/v.to_f}.reduce(:+)
end
puts har_mean([6,9,12])
@rrguntaka
rrguntaka / logic.rb
Created November 15, 2011 21:30
Propositional Logic - AI Class - Truth Table
def implies(a,b)
!(a & !b)
end
def str(a)
a.to_s[0].upcase
end
def printTable
@rrguntaka
rrguntaka / hmm.rb
Created November 17, 2011 16:59
Hidden Markov Model - 2 States
# The model here is a two state HMM (States are A and B).
# This is a simple network having 4 possible actions,
# two for each state (going to the other or staying at the same)
# pa and pb are probabilities of having next state as A
# from state A and B respectively
class Hmm
def initialize(pa,pb)
@pa = pa; @pb = pb
end
@rrguntaka
rrguntaka / Matrix.java
Created November 29, 2011 18:07
StdDev of a portfolio
public class Matrix {
Double[][] data;
public Matrix(int m, int n) {
data = new Double[n][m];
}
Matrix(Double[][] data) {
this.data = data;
}
@rrguntaka
rrguntaka / update_with_select.sql
Created December 7, 2011 20:59
Update with Select (Oracle). This query is to update a set of columns with the data from a subselect by matching id
UPDATE table_to_be_updated t1
SET (t1.col1, t1.col2) =
(SELECT (t2.col1, t2.col2) subquery_or_table t2
WHERE(t2.id1, t2.id2) in (t1.id1,t1.id2))
WHERE (t1.id1, t1.id2) IN
(select t2.id1, t2.id2 from subquery_or_table t2)
CREATE TABLE InvRec(
rcpt_nr integer primary key,
purchase_date datetime not null,
qty_on_hand integer not null
check (qty_on_hand>= 0),
unit_price decimal(12,4) not null
);
insert into InvRec values(1,'2011-01-01',15,10.00);