Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
first commit
  • Loading branch information
Kailash Budhathoki committed Feb 6, 2017
1 parent 334ec76 commit 0f71398
Show file tree
Hide file tree
Showing 301 changed files with 792,933 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Expand Up @@ -87,3 +87,9 @@ ENV/

# Rope project settings
.ropeproject

# Mac folder meta
.DS_Store

# results folder
results/
53 changes: 53 additions & 0 deletions cisc.py
@@ -0,0 +1,53 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Causal inference on discrete data using stochastic complexity of multinomial.
"""
from collections import defaultdict
from sc import stochastic_complexity


__author__ = "Kailash Budhathoki"
__email__ = "kbudhath@mpi-inf.mpg.de"
__copyright__ = "Copyright (c) 2017"
__license__ = "MIT"


def marginals(X, Y):
Ys = defaultdict(list)
for i, x in enumerate(X):
Ys[x].append(Y[i])
return Ys


def cisc(X, Y):
scX = stochastic_complexity(X)
scY = stochastic_complexity(Y)

mYgX = marginals(X, Y)
mXgY = marginals(Y, X)

scYgX = sum(stochastic_complexity(Z) for Z in mYgX.itervalues())
scXgY = sum(stochastic_complexity(Z) for Z in mXgY.itervalues())

ciscXtoY = scX + scYgX
ciscYtoX = scY + scXgY

return (ciscXtoY, ciscYtoX)


if __name__ == "__main__":
import random
from test_anm import map_randomly

n = 1000
Xd = range(1, 4)
fXd = range(1, 4)
f = map_randomly(Xd, fXd)
N = range(-2, 3)

X = [random.choice(Xd) for i in xrange(n)]
Y = [f[X[i]] + random.choice(N) for i in xrange(n)]

print cisc(X, Y)

0 comments on commit 0f71398

Please sign in to comment.