-
Notifications
You must be signed in to change notification settings - Fork 12
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
Help with Methodology (vectors' angles from Kramers-Moyal coefficients) #18
Comments
I only glanced at the paper and the formula for the angle should actually be Do you have any minimal example you can share with us? Note that the bins are just the discretization, per dimension, of the underlying real-space -- that is, the space where your vectors are defined. |
Hey @athantas95, You should have access to a 2D time series of your dynamical (stochastic) system. Let's call it `y'. You should just have to use import kramersmoyal as kmc
# Y is a (2,N) time series
powers = [[0,0], [1,0], [0,1]]
kmc, edges = kmc.km(y, powers=2) Now you are interested in the import matplotlib.pyplot as plt
import numpy as np
X, Y = np.meshgrid(edges[0], edges[1])
fig, ax = plt.subplots()
ax.quiver(X, Y, kmc[1,...], kmc[2,...], units='width') Can you give that a try? |
Building on @LRydin's answer, note that a power
So to get import numpy as np
import kramersmoyal
y = np.random.randn(10000, 2) # Some fake data
D, edges = kramersmoyal.km(y, powers=[[0,0], [1,0], [0,1]], bins=[20,10])
D_x = D[1, ...]
D_y = D[2, ...] To obtain a proper velocity vector we should stack these D = np.stack((D_x, D_y)) Note that this "2d-vector" is is defined for all points of the underlying grid def angle_between(v1, v2):
norms = np.linalg.norm(v1) * np.linalg.norm(v2)
# Avoid division by zero and numerical instabilities with clamp
if norms == 0:
return 0
return np.arccos(np.clip(np.dot(v1, v2) / norms, -1.0, 1.0)) So now the paper states that you are computing the angle of this velocity vector with its neighbours. This is hard-coded for 2d but trivial to generalise for 3 dimensions. def calculate_angles(v):
_, n_x, n_y = v.shape
angles = np.zeros((n_x, n_y))
# Iterate over each point in the lattice
for x in range(n_x):
for y in range(n_y):
v_current = v[:, x, y]
# Pre-determine the valid neighbor offsets (luckily the underlying grid is box-like)
neighbor_offsets = [(dx, dy) for dx in [-1, 0, 1] for dy in [-1, 0, 1]
if 0 <= x + dx < n_x and 0 <= y + dy < n_y and not (dx == 0 and dy == 0)]
# Calculate angles with valid neighbors and SUM THEM
for dx, dy in neighbor_offsets:
v_neighbor = v[:, x + dx, y + dy]
angles[x, y] += angle_between(v_current, v_neighbor)
return angles Hence, theta_xy = calculate_angles(D) To visualise import matplotlib.pyplot as plt
X, Y = np.meshgrid(edges[0], edges[1])
plt.pcolormesh(X, Y, theta_xy.T)
plt.show() To now solve your problem, you'd need just to adapt the powers to 3d, that is |
Hi again! Thanks a lot for the very quick responses and help. I tried the first snippet by @LRydin and I guess now I understand a bit better the output of the km function. I will now try the extended code by @fmeirinhos and let you know. Thanks a ton for your help :D |
It just occurred to me that def angle_between(v1, v2):
angle = np.arctan2(v2[1], v2[0]) - np.arctan2(v1[1], v1[0])
return np.mod(angle, 2 * np.pi) Similarly, since we are adding so many angles, it's important the final result is def calculate_angles(v):
# ...
return np.mod(angles, 2*np.pi) Please take the script with a rock of salt, there might be other "problems" lingering, we thought we would just give you a head start. Good luck! |
Hello, I came across your package in my effort to replicate the methodology from paper:
https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1000061
where in the Data Analysis section they use the Kramers-Moyal coefficients to get the drift coefficients of the vectors and subsequently the angle of neighbor vectors in the phase space in order to make conclusions about the dynamics of the flow in the state space.
I don't know if you would be interested in helping me a bit with the code. I actually want to do it for a 3d space, but even in 2d i don't quite understand how to get the angles from the output of the
km
function since i get this bins x bins x bins vector.I hope I was clear. Thanks a lot in advance and apologies if my issue fall out of the scope of the issues section :)
The text was updated successfully, but these errors were encountered: