Skip to content

Instantly share code, notes, and snippets.

View agarie's full-sized avatar

Carlos Agarie agarie

View GitHub Profile
@agarie
agarie / matthews.rb
Created December 10, 2013 18:20
Matthews correlation coefficient. I'll never have to reimplement this!
def matthews(tp, tn, fp, fn)
den = tp * tn - fp * fn
num = (tp + fp) * (tp + fn) * (tn + fp) * (tn + fn)
den / Math.sqrt(num)
end
@agarie
agarie / fix_encoding.rb
Created December 18, 2013 03:47
Fixes issues when dealing with ISO-8859-1 ("LATIN-1") encoded files, which are very common with government data and such.
def fix_encoding(fn)
q = File.read(fn)
File.open(fn, 'w') { |f| f << q.encode("UTF-8", "ISO-8859-1") }
end
@agarie
agarie / gist:8048725
Created December 20, 2013 00:41
Ruby webserver one-liner.
ruby -rwebrick -e 'WEBrick::HTTPServer.new(:Port => 3000, :DocumentRoot => Dir.pwd).start'
@agarie
agarie / poly.rb
Created July 14, 2014 20:12
DSL for symbolic integration of polynomials
def polynomial(&block)
varX = Variable::Local.new('x')
x = Polynomial.valueOf(Real::ONE,varX)
yield x
end
# Creating a polynomial fx = 1(x^5) + 6(x^2).
fx = polynomial do |x|
x.pow(5).plus(x.pow(2).times(Real.valueOf(6)))
end
@agarie
agarie / icc.js
Created November 6, 2014 18:04
Item Characteristic Curve plotting with d3js.
var N = 100,
a = 2.0,
b = 0.0,
c = 0.2;
var defaultScale = { mu: 0, sigma: 1 },
enemScale = { mu: 500, sigma: 100 };
// @return: The probability of hit at `theta`, or P(right | theta, a, b, c).
var prob = function (a, b, c, theta) {
return c + (1 - c) / (1 + Math.exp(- a * (theta - b)));
@agarie
agarie / ngrams.rb
Created April 14, 2015 18:19
Generate {uni,bi,tri}grams from a token list.
class Counter
def initialize
@counts = Hash.new 0
end
def <<(key)
@counts[key] += 1
end
end
@agarie
agarie / create_has_library.rb
Created May 23, 2015 04:08
A function for inquiring if a library is available. Automatically requires the library if it is available.
# Create a method `has_<library>?` on Module that requires the library and
# return a boolean indicating if the library is available.
#
# @param library [String] The library name.
# @return [Boolean] Whether the library is available or not.
def create_has_library(library) #:nodoc:
define_singleton_method("has_#{library}?") do
cv = "@@#{library}"
unless class_variable_defined? cv
begin
@agarie
agarie / rng.lua
Created June 12, 2015 00:38
RNGs for some distributions written in Lua, mostly as a first exercise in the language.
-- Some RNGs for getting to play with Lua.
--
-- Carlos Agarie <[email protected]>
--
-- Public domain.
-- N(mean; std^2).
function gauss(mean, std)
if std <= 0.0 then error("standard deviation must be positive!") end
@agarie
agarie / xmonad.md
Last active September 23, 2021 09:05
XMonad cheatsheet, resources, etc.

xmonad

Shortcuts

  • Mod + Shift + Enter -> Open console
  • Mod + Space -> Change tiling mode
  • Mod + j & Mod + k -> Move focus between windows
  • Mod + Shift + c -> Close the focused window
  • Mod + . & Mod + , -> Control the number of windows displayed in the master pane on the left
  • Mod + Enter -> Move the focused window to the master pane on the left
@agarie
agarie / faces_dataset_32x30.list
Last active November 19, 2015 17:58
Code used to convert the PGM images from the dataset used in one of Tom Mitchell's homework assignments (https://www.cs.cmu.edu/~tom/faces.html) to CSV files.
./faces/kawamura/kawamura_straight_happy_open_4.pgm
./faces/phoebe/phoebe_up_sad_open_4.pgm
./faces/saavik/saavik_left_sad_sunglasses_4.pgm
./faces/sz24/sz24_right_angry_open_4.pgm
./faces/tammo/tammo_left_angry_sunglasses_4.pgm
./faces/bpm/bpm_up_neutral_open_4.pgm
./faces/kawamura/kawamura_up_angry_open_4.pgm
./faces/choon/choon_right_happy_sunglasses_4.pgm
./faces/saavik/saavik_right_sad_sunglasses_4.pgm
./faces/ch4f/ch4f_straight_angry_open_4.pgm