Skip to content

Commit

Permalink
add process discriminator
Browse files Browse the repository at this point in the history
  • Loading branch information
imanolperez committed Apr 12, 2019
1 parent 5c5f748 commit 78055c8
Show file tree
Hide file tree
Showing 9 changed files with 498 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": "/usr/bin/python3"
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# market_simulator
Market simulator
# Market simulator
Project with Blanka.
187 changes: 187 additions & 0 deletions notebooks/.ipynb_checkpoints/process_discriminator.py-checkpoint.ipynb

Large diffs are not rendered by default.

Binary file added notebooks/__pycache__/base.cpython-36.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions notebooks/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import sys

sys.path.insert(0, "../src")
187 changes: 187 additions & 0 deletions notebooks/process_discriminator.py.ipynb

Large diffs are not rendered by default.

Empty file added src/__init__.py
Empty file.
Binary file not shown.
116 changes: 116 additions & 0 deletions src/process_discriminator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import numpy as np
from esig import tosig
from tqdm import tqdm
from scipy.optimize import brentq
from joblib import Parallel, delayed
import ast

def psi(x, M=4, a=1):
x = x ** 2

if x <= M:
return x

return M + M ** (1 + a) * (M ** (-a)) / a

def norm(x):
return np.linalg.norm(x)

def phi(x, order):
x = np.array(x)

a = x ** 2
a[0] -= psi(norm(x))

f = lambda x: np.dot(a, [x ** (2 * m) for m in range(len(a))])

return brentq(f, 0, 5)

def get_keys(dim, order):
s = tosig.sigkeys(dim, order)

tuples = []

for t in s.split():
if len(t) > 2:
t = t.replace(")", ",)")

tuples.append(ast.literal_eval(t))

return tuples

def Phi(X, order, normalise=True):
sig = tosig.stream2sig(np.array(X), order)

if not normalise:
return sig

keys = get_keys(np.shape(X)[1], order)

phi_x = phi(tuple(sig), order)

Lambda = np.array([phi_x ** len(t) for t in keys])

return Lambda * sig

def k(X, Y):
return np.dot(X, Y)


def T(set1, set2, order, verbose=True, normalise=True):
m = len(set1)
n = len(set2)

X = Parallel(n_jobs=-1)(delayed(Phi)(path, order, normalise) for path in tqdm(set1, desc="Computing signatures of population 1", disable=(not verbose)))
Y = Parallel(n_jobs=-1)(delayed(Phi)(path, order, normalise) for path in tqdm(set2, desc="Computing signatures of population 2", disable=(not verbose)))

XX = np.dot(X, np.transpose(X))
YY = np.dot(Y, np.transpose(Y))
XY = np.dot(X, np.transpose(Y))

TU = 0.
TU += XX.sum() / (m * m)
TU += YY.sum() / (n * n)
TU -= 2 * XY.sum() / (m * n)


return TU

def c_alpha(m, alpha):
K = 1.
return (2 * K / m) * (1 + np.sqrt(-2 * np.log(alpha))) ** 2

def test(set1, set2, order, confidence_level=0.99):
"""Statistical test to determine if two sets of paths come
from the same distribution.
Parameters
----------
set1 : list of array
Set of paths.
set2 : list of array
Set of paths.
order : int
Signature order.
confidence_level : float, optional
Confidence level of the statistical test. Must be in [0, 1].
Default is 0.99, i.e. 99%.
Returns
-------
bool
True if the hypothesis is rejected and hence the sets come from
different distributions, False otherwise.
"""

assert len(set1) == len(set2), "Same size samples accepted for now."

assert confidence_level >= 0. and confidence_level <= 1., "Confidence level must be in [0, 1]."

m = len(set1)

TU = T(set1, set2, order)
c = c_alpha(m, confidence_level)

return TU > c

0 comments on commit 78055c8

Please sign in to comment.