Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kernel Density Estimate #32

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed infoFlow/__init__.py
Empty file.
86 changes: 86 additions & 0 deletions infoflow/entropy_kde.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import numpy as np
from infoflow import multidimkde as mdkde
from math import log


def kerneldensityestimation(source, target, timelagx, timelagy, n, bw_coeff):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to PEP8, function names should be in the form kernel_density_estimation. That also goes for timelag_x and timelag_y.

"""
:param source: source time series, 1-D np.ndarray
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameters
----------
source : 1-D ndarray
    Source time series
etc.

:param target: target time series, 1-D np.ndarray
:param timelagx: time lag in x from present. Integer.
:param timelagy: time lag in y from present. Integer.
:param n: number of equally spaced points along each dimension where probabilities are to be estimated. Integer.
:param bw_coeff: multiplier that adjusts Gaussian bandwidth. Integer.
:return: transfer entropy in bits. float.
"""

""" All code written here is based on the transfer entropy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Place this into the docstring above.

"Use protected under GNU" is incorrect. You can say something like "licensed under the GNU GPL v3 license".

toolbox, Copyright 2011 Joon Lee. Use protected under GNU."""

# fixing block lengths to 1
l, k = 1, 1

sourcearr = source.flatten()
targetarr = target.flatten()

# populating source_pat, target_pat and target_t
source_pat = []
target_pat = []
target_t = []

for i in range(max(l+timelagx, k+timelagy)-1, min(len(sourcearr), len(targetarr))):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You almost never want to loop over arrays in this fashion.

Read more about numpy array operations here: http://www.scipy-lectures.org/intro/numpy/index.html

source_pat.append(sourcearr[i - l - timelagx+1: i - timelagx+1][0])
target_pat.append(targetarr[i - k - timelagy+1: i - timelagy+1][0])
target_t.append(targetarr[i])

# reassigning to numpy arrays
source_pat = np.array(source_pat).reshape(len(source_pat), 1)
target_pat = np.array(target_pat).reshape(len(target_pat), 1)
target_t = np.array(target_t).reshape(len(target_t), 1)

# establishing values
source_pat_i = np.linspace(np.amin(source_pat) - 0.1*(np.amax(source_pat) - np.amin(source_pat)),
np.amax(source_pat) + 0.1*(np.amax(source_pat) - np.amin(source_pat)), n)

target_pat_i = np.linspace(np.amin(target_pat) - 0.1*(np.amax(target_pat) - np.amin(target_pat)),
np.amax(target_pat) + 0.1*(np.amax(target_pat) - np.amin(target_pat)), n)

target_t_i = np.linspace(np.amin(target_t)-0.1*(np.amax(target_t)-np.amin(target_t)),
np.amax(target_t)+0.1*(np.amax(target_t)-np.amin(target_t)), n)

# reshaping the resultant arrays
source_pat_i = np.array(source_pat_i)
target_pat_i = np.array(target_pat_i)
target_t_i = np.array(target_t_i)

# initializing the probability density function matrix
pdf = np.zeros(shape=(len(target_t_i), len(source_pat_i), len(target_pat_i)))

# bookkeeping
source_pat = source_pat.transpose()
target_pat = target_pat.transpose()
target_t = target_t.transpose()

# populating the pdf matrix
x = np.vstack((source_pat, target_pat, target_t)).transpose()
for i in range(len(source_pat_i)):
for j in range(len(target_pat_i)):
for k in range(len(target_t_i)):
xi = np.hstack((source_pat_i[i], target_pat_i[j], target_t_i[k]))
ans = mdkde.multidimensionalkde(x, xi, bw_coeff)
pdf[k, i, j] = ans[0]

# normalizing probabilities
pdf = pdf/np.sum(pdf)

# computing transfer entropy
transferentropy = 0
for i in range(len(source_pat_i)):
for j in range(len(target_pat_i)):
for k in range(len(target_t_i)):
a = pdf[k, i, j]
b = np.sum(pdf[:, i, j])
c = np.sum(pdf[k, :, j])
d = np.sum(sum(pdf[:, :, j]))
transferentropy += a * log(((a * d) / (b * c)), 2)
return transferentropy
32 changes: 32 additions & 0 deletions infoflow/multidimkde.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import numpy as np
from math import pi, exp, sqrt


def multidimensionalkde(x, xi, bw_coeff):
"""
:param x: np.ndarray, rows are observations and columns are dimensions.
:param xi: np.ndarray, coordinates at which joint probabilities are to be estimated.
:param bw_coeff: multiplier that adjusts the rule of thumb bandwidth. Integer.
:return: joint probability at xi
"""

# assertions to ensure input is in right format.
assert isinstance(x, np.ndarray)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests should be in the form

if not isinstance(x, np.ndarray):
    raise ValueError("x must be ndarray")

assert isinstance(xi, np.ndarray)

n, d = np.shape(x)
# pick the bandwidth for each dimension
h = np.zeros(shape=(1, d))
for i in range(d):
h[0, i] = float(bw_coeff * 1.06 * n**(-1/5) *np.std(x[:, i],ddof=1))
# estimating probability at xi
p = np.zeros(shape=(np.shape(xi)[0], 1))
for k in range(len(p)):
for i in range(n):
prod = 1
for j in range(d):
u = float((xi[j] - x[i, j])/h[0][j])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You never want to do this type of for-loop on a numpy array.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you recommend vectorizing this?

prod = float(prod * 1/sqrt(2*pi) * exp(-0.5*(u**2)) / h[0][j])
p[k] += prod
p = p/n
return p
1 change: 1 addition & 0 deletions infoflow/tests/input_kde/entropykdeinputx.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10.4211260079593 9.15959175116347 8.42791637961942 9.79937565892561 11.0935185821076 9.73725099429120 10.2243469322360 9.14123254116025 9.27978556360681 10.4625464979946 11.0532504912095 8.37120765662389 8.31299790562039 11.3632685729923 10.0351674383055 9.78837437910691 10.3025585944617 9.97080241324564 10.3538933865610 10.6079548361543 8.94148954012898 9.47833605650219 9.92537602561508 9.50144983292374 10.4229167707908 8.94229569585545 10.1630719263382 8.31144638274103 8.70392723628035 10.9626524057649 10.7513587129864 10.6985933029311 10.1934766837466 8.55865025366214 9.77089142787289 11.3586402663943 7.86245295803992 10.3451863308292 7.46527397614331 9.68710681121817 10.3649792158121 8.40812696263984 10.8562622100752 9.23978685343992 10.5440053605012 10.4683979739141 9.73608537929506 10.5512564821944 10.0484117707993 8.04072569129428 10.6735654913598 10.1828715200493 9.96968448112454 10.2489827369610 10.0531253810500 7.67165861107142 11.7197788310825 9.57947501239318 9.29625795552166 11.3998640005889 10.1373742414350 9.01100801303883 10.9049803732133 10.1033344160733 9.32410854469947 11.6280330977918 9.85747779740347 9.69796324352113 8.86402817842106 10.1171639601262 11.5280148529030 10.8757601216499 9.80653805805204 9.73017365115883 10.6196452989191 11.7917319631748 9.91330802847372 9.75905627047967 10.2835667875428 8.71599998475651 10.9803639476732 9.45264445714630 9.43835681862873 7.91040504493523 9.04374138942974 10.2119536137048 10.7971784055176 11.1546396794403 8.70620162412084 9.66870517734535 9.87619320128848 10.3404378591972 10.4166902669334 10.3604459390833 9.52905000082007 9.78773022225650 9.19197187636095 9.33759915410536 11.8571358650712 8.78386760180735 9.74288266411103 8.80846479929184 10.7973171856024 8.71773764110380 8.14439559714581 8.34305489301146 10.6670110210607 12.4146862141603 9.61705667616662 10.9746111393636 11.2613670851036 11.2255057655008 11.8794812760294 9.66125009841665 10.9770718448007 9.61885979100899 8.20394806363581 9.75222917260762 10.3807775239919 10.9185911679075 10.8563781321093 10.2231245103995 11.1730192468860 9.72786807833919 8.73372705809844 9.02992561801242 8.86095513474243 11.1177126097330 9.82991216438658 8.61585951453221 9.65034485444263 10.1196333015828 9.67688510208878 11.6637580508535 11.5434488090031 9.48286063156920 10.1694907154755 10.5045025708633 10.1803562858932 9.56685897211484 10.5537178614578 9.05063141544852 12.9054894272364 9.75302003682373 10.2941874860983 9.20946963016349 8.55666581778405 11.1430581055165 10.0936766601589 10.3670319086311 8.14386564486635 10.4535441133938 11.7223379926330 10.5927084785974 11.2144544492494 11.6879516780366 11.4556534338735 8.65122029311518 9.47934281204521 8.40963475436907 10.6278446404594 12.0162482623612 11.4696135507092 10.8674026057028 8.97336288741732 8.95834919952158 9.78664488766869 9.89131930266141 10.7660388327426 12.2657015270320 9.88205821947244 9.96323461539321 11.2232922459353 11.1769593227852 8.28437478168918 9.58148155918462 9.60348889503716 8.92353335913221 9.29170798438943 10.0571438414249 11.2758327997102 10.7733479413764 10.8705727179738 9.18109123035243 10.6157976774387 11.4801845404507 8.71623561375216 12.2037500514460 11.3266086901697 8.79914325310611 10.4582994211230 9.23599690280768 9.55299920735131 9.30433656337055 9.55012430373347 9.46276994556222 9.37910504539161 9.60009122456007 10.4950921294591 11.8306052926597 8.41056780343131 9.86289137162470
1 change: 1 addition & 0 deletions infoflow/tests/input_kde/entropykdeinputx1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10.4211260079593 9.15959175116347 8.42791637961942 9.79937565892561 11.0935185821076 9.73725099429120 10.2243469322360 9.14123254116025 9.27978556360681 10.4625464979946 11.0532504912095 8.37120765662389 8.31299790562039 11.3632685729923 10.0351674383055 9.78837437910691 10.3025585944617 9.97080241324564 10.3538933865610 10.6079548361543 8.94148954012898 9.47833605650219 9.92537602561508 9.50144983292374 10.4229167707908 8.94229569585545 10.1630719263382 8.31144638274103 8.70392723628035 10.9626524057649 10.7513587129864 10.6985933029311 10.1934766837466 8.55865025366214 9.77089142787289 11.3586402663943 7.86245295803992 10.3451863308292 7.46527397614331 9.68710681121817 10.3649792158121 8.40812696263984 10.8562622100752 9.23978685343992 10.5440053605012 10.4683979739141 9.73608537929506 10.5512564821944 10.0484117707993 8.04072569129428 10.6735654913598 10.1828715200493 9.96968448112454 10.2489827369610 10.0531253810500 7.67165861107142 11.7197788310825 9.57947501239318 9.29625795552166 11.3998640005889 10.1373742414350 9.01100801303883 10.9049803732133 10.1033344160733 9.32410854469947 11.6280330977918 9.85747779740347 9.69796324352113 8.86402817842106 10.1171639601262 11.5280148529030 10.8757601216499 9.80653805805204 9.73017365115883 10.6196452989191 11.7917319631748 9.91330802847372 9.75905627047967 10.2835667875428 8.71599998475651 10.9803639476732 9.45264445714630 9.43835681862873 7.91040504493523 9.04374138942974 10.2119536137048 10.7971784055176 11.1546396794403 8.70620162412084 9.66870517734535 9.87619320128848 10.3404378591972 10.4166902669334 10.3604459390833 9.52905000082007 9.78773022225650 9.19197187636095 9.33759915410536 11.8571358650712 8.78386760180735 9.74288266411103 8.80846479929184 10.7973171856024 8.71773764110380 8.14439559714581 8.34305489301146 10.6670110210607 12.4146862141603 9.61705667616662 10.9746111393636 11.2613670851036 11.2255057655008 11.8794812760294 9.66125009841665 10.9770718448007 9.61885979100899 8.20394806363581 9.75222917260762 10.3807775239919 10.9185911679075 10.8563781321093 10.2231245103995 11.1730192468860 9.72786807833919 8.73372705809844 9.02992561801242 8.86095513474243 11.1177126097330 9.82991216438658 8.61585951453221 9.65034485444263 10.1196333015828 9.67688510208878 11.6637580508535 11.5434488090031 9.48286063156920 10.1694907154755 10.5045025708633 10.1803562858932 9.56685897211484 10.5537178614578 9.05063141544852 12.9054894272364 9.75302003682373 10.2941874860983 9.20946963016349 8.55666581778405 11.1430581055165 10.0936766601589 10.3670319086311 8.14386564486635 10.4535441133938 11.7223379926330 10.5927084785974 11.2144544492494 11.6879516780366 11.4556534338735 8.65122029311518 9.47934281204521 8.40963475436907 10.6278446404594 12.0162482623612 11.4696135507092 10.8674026057028 8.97336288741732 8.95834919952158 9.78664488766869 9.89131930266141 10.7660388327426 12.2657015270320 9.88205821947244 9.96323461539321 11.2232922459353 11.1769593227852 8.28437478168918 9.58148155918462 9.60348889503716 8.92353335913221 9.29170798438943 10.0571438414249 11.2758327997102 10.7733479413764 10.8705727179738 9.18109123035243 10.6157976774387 11.4801845404507 8.71623561375216 12.2037500514460 11.3266086901697 8.79914325310611 10.4582994211230 9.23599690280768 9.55299920735131 9.30433656337055 9.55012430373347 9.46276994556222 9.37910504539161 9.60009122456007 10.4950921294591 11.8306052926597 8.41056780343131 9.86289137162470
1 change: 1 addition & 0 deletions infoflow/tests/input_kde/entropykdeinputx10.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10.8699833127185 9.68443275893966 10.5474476160265 9.67150769870410 10.7441432241123 9.54103668641083 9.63948659269747 8.87017541640272 8.52449047797456 10.5632632713485 11.1222725620246 10.1693053869160 9.29632394105644 9.08850773421474 9.04290421198221 12.0122190052164 9.90985980443958 8.84689788899307 8.88867199607254 9.28736211049221 9.75906286068963 12.0711034949049 10.0237853657269 8.85450478825283 9.28631148731868 9.05245702052238 11.3083260817942 11.9225930224378 11.3635869955078 9.78374948750941 11.2840706560335 10.0387666785327 9.86887022046866 8.76961125969829 9.66746858109348 9.17092316882893 9.62967933942374 9.46861769610689 9.58077693275956 8.44574988870922 10.5462090906056 8.65154081077131 10.4052637417213 9.40559083878890 9.06690749411444 8.14427566538484 9.73898601602242 10.8774108836587 11.7096320388117 10.7186783456309 8.74706404508343 9.82932986439422 11.3610074119434 10.6392839511152 9.41261046561218 10.5054843441130 10.8199808053511 9.62344252530183 9.28095789013691 9.27298536814906 10.5082888888033 10.3242291915240 9.52174214619540 8.38617504517682 10.0890008381056 9.77536566753334 9.97716400632260 11.4930037953760 10.1230906889496 9.24997610419842 12.2525291832571 9.25516781841040 9.52884670833557 10.9540907070798 8.43651427795226 10.0381509943223 9.89605596658276 12.0771950491055 11.2850531721867 10.4428686324097 8.91364072221276 10.5478482165796 9.43442588280808 9.39819642439597 8.19561808019623 7.67083396995431 10.4020385330024 10.5473996340275 9.49313824486275 10.6522344887757 10.3631325429750 8.62523297109888 8.71135231636371 11.3145773341740 10.6427378694596 9.68452882670857 8.99675469680888 10.3312895113314 10.1723869235317 8.02483359973832 10.9645923912353 9.81285845854804 10.1280154269215 8.56214362722571 11.7281196180565 10.8134092911562 9.11178740562848 9.77150123035137 9.29994387378397 10.4798597047718 10.7740608040303 10.3319113132463 10.5207036542453 10.5506744709158 8.98411503293251 10.6865626905703 9.04885478322149 10.6280760162404 8.90290646349454 11.0863169187603 10.3839231939128 11.0239483519199 9.27226853949843 10.7962177976519 9.21947268315388 9.58929940494356 9.41243469949015 10.5439225430818 11.3974015747748 9.75220803488193 9.89411617013360 10.4888048558715 9.30453936229872 11.3832378372143 10.9727075619210 10.9032900875427 9.63238273389541 8.41996793972577 9.22065755692087 10.3025956484214 9.38350566235906 10.3223236263488 9.27343905631594 9.77293689428016 9.76923820976849 10.4758022466870 8.45789723750968 11.6985410782802 9.47610182866091 10.4343925508904 11.7175294151061 10.0356719075508 11.2214347847136 9.75811363819631 11.0376248731612 10.4131337752811 10.0757996564907 9.62930982077582 10.5059506235190 8.69429413441244 9.06679752181153 10.7464278847451 10.7566364162748 10.4964114398382 9.71242931446934 9.83630448601838 8.47298726801289 8.50357123688345 10.4700811650934 8.96711185957138 10.2816354604784 8.57760356987811 9.62884685481851 9.64703590273127 10.6296940322822 9.97453773122864 10.0339425819693 8.90393445181706 8.80722389874830 8.72291861053948 11.1723380983660 10.4304715913696 10.1669071845318 9.11065465102903 8.73348342541021 11.0315023257774 10.6366611800350 8.88094079691397 11.5910836813201 8.37013420154003 10.3667578176358 10.7998002537185 10.3338328754108 9.19997558276628 10.6557606255287 9.76975641100687 10.2326991152455 9.76774810371417 10.5360980475171 9.53935634159052 8.69901163811486 10.9110337031062
1 change: 1 addition & 0 deletions infoflow/tests/input_kde/entropykdeinputx11.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10.2650031217267 10.5393115623259 10.8366592712663 10.2164277098032 9.04901772325981 12.1126742321417 10.8436693924080 10.4795521298182 10.4971440300976 10.5271959376006 10.7796104939838 10.5808655031939 9.51191407787242 9.26226263615811 8.45726174110138 9.09730438741823 9.21893699600892 11.5561929912459 10.9255724113922 10.6846825452640 11.3523240255352 10.6925082663783 9.57444653100827 8.87366361850511 10.4887045089911 11.5412581064639 8.38452310045086 9.00050241847607 8.34456668599759 8.92953389834645 8.63163435892398 11.6538783805602 9.22816270553699 9.04306317851690 10.7629283350275 11.1923289450496 8.87757966917845 9.27172037043561 9.34918668150888 9.62576903482679 9.98313270529759 9.72775552138250 9.94952966209813 10.1374396069950 10.0064854509544 10.0684736940324 10.7807754310207 9.12232186286621 10.3791617626664 10.1789540774375 8.23276752791774 10.3010362086974 9.36936508193022 9.96495460508096 9.53207524551425 11.1099303764913 9.08566426037886 8.96633059282538 9.39459697716774 9.57051161614223 10.5377656978986 9.64372298026826 8.12033654047367 11.2162242764593 9.69552005140730 9.97535008731363 9.56129311727412 10.3552091610814 9.68149666588918 9.21703852164318 9.51680953635896 12.3770536905890 9.87602543525270 9.59732529013975 9.17746668857288 10.3430333595844 9.65640792089456 10.5487171474989 10.3510012421435 9.85305463059498 9.16161055080809 10.7998992237352 10.7278979420852 9.74942929698285 8.74054439167245 11.7191661485678 11.3168931351180 11.1087712835346 10.0264204707143 11.2541625884703 11.0445974713625 10.2726444185476 9.97452198459473 9.47284229143707 9.53200113846465 8.66335522275122 11.7751530256925 10.8460272340523 10.4070539345584 8.32511230626856 11.1963916550441 9.81359318078762 10.2701931973531 11.1225525354986 10.8093985581362 11.3803302825028 8.44522527388516 7.58279387368937 9.29068407631297 10.2570453056043 8.77075621697050 9.56402014637945 9.77187506522339 10.9949597738548 9.58363660575203 9.57854500323970 10.2094758629416 10.4887785527478 9.18167318521141 9.36675850845245 8.99733783389463 8.55375499586773 10.7145818253566 9.90743176130331 10.5572222737203 10.3933374140840 11.8074585008597 9.59673319208464 9.69826906539365 11.2723290683466 10.9769312033130 7.67419555594442 10.9853633196110 11.6049313128488 10.1431598526012 9.76167706133087 10.1627545790438 8.95682965027942 10.3988065161239 8.83781249979439 9.78115796949206 10.6337276582304 10.4332792119797 7.76849180933676 9.91104784025682 9.31371856804519 9.49266030300822 9.90622695004614 9.34907041338394 9.71246732115887 9.35324053035200 9.51179710383298 10.8226413568566 9.85160274244869 9.38587939003002 9.72412115069804 10.5568309499475 10.6461467501458 10.7434163751031 10.7281193221701 9.49791099280693 11.1046604560937 7.66755989607798 9.55645711874518 10.2140099385349 10.9868096990598 9.84767826270591 9.84855367338394 10.4743073190621 11.6517755548257 9.36818812649911 9.87625683982133 10.4058196271714 10.4931253336097 10.1214738829226 10.7200024866163 8.92410424402206 9.90966428059849 9.41205080880640 10.9855735117227 10.2140306439153 8.71991849272080 10.5175863520236 10.0873662573558 10.5916882520516 9.30785722085465 9.00398131220727 10.6912702687135 10.0675704833073 9.63129305145649 8.88358794328132 9.25245009605866 11.0504712536495 10.0624971987128 9.61169225414984 8.69676599419327 10.8141922535247 8.74098973684215 10.3093092627024 9.35400027716601 9.83051484763655 9.33005003109133
Loading