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

Update stereonet_math.py #1

Merged
merged 1 commit into from
Mar 24, 2021
Merged
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
47 changes: 38 additions & 9 deletions mplstereonet/stereonet_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,23 @@ def mean_vector(lons, lats):
the degree of clustering in the data.
"""
xyz = sph2cart(lons, lats)
xyz = np.vstack(xyz).T
mean_vec = xyz.mean(axis=0)
r_value = np.linalg.norm(mean_vec)
xyz = np.vstack(xyz)
xbar = xyz.mean(axis=1)
r_value = np.linalg.norm(xbar)
xobar = xbar/r_value
xobar = xobar.reshape(3,1)
num = xyz.shape[1]

# Equation 9.2.10 from Directional Statistics (Jupp and Mardia, 2000, pp.165)
T = np.zeros((3,3))
for i in range(num):
T += (xyz[:,i].reshape(3,1)).dot((xyz[:,i].reshape(3,1)).T)
T = T/num

# Mean vector of distribution is one of the eigenvectors of the scatter matrix T
eigs = np.linalg.eig(T)
mean_vec = eigs[1][:,0]
r_value = np.sqrt(eigs[0].max())
mean_vec = cart2sph(*mean_vec)
return mean_vec, r_value

Expand Down Expand Up @@ -417,22 +431,37 @@ def fisher_stats(lons, lats, conf=95):

"""
xyz = sph2cart(lons, lats)
xyz = np.vstack(xyz).T
mean_vec = xyz.mean(axis=0)
r_value = np.linalg.norm(mean_vec)
num = xyz.shape[0]
mean_vec = cart2sph(*mean_vec)
xyz = np.vstack(xyz)

xbar = xyz.mean(axis=1)
r_value = np.linalg.norm(xbar)
xobar = xbar/r_value
xobar = xobar.reshape(3,1)
num = xyz.shape[1]

# Equation 9.2.10 from Directional Statistics (Jupp and Mardia, 2000, pp.165)
T = np.zeros((3,3))
for i in range(num):
T += (xyz[:,i].reshape(3,1)).dot((xyz[:,i].reshape(3,1)).T)
T = T/num

# Mean vector of distribution is one of the eigenvectors of the scatter matrix T
eigs = np.linalg.eig(T)
mean_vec = eigs[1][:,0]

if num > 1:
p = (100.0 - conf) / 100.0
vector_sum = xyz.sum(axis=0)
r_value = np.sqrt(eigs[0].max())
vector_sum = mean_vec*num*r_value
result_vect = np.sqrt(np.sum(np.square(vector_sum)))
fract1 = (num - result_vect) / result_vect
fract3 = 1.0 / (num - 1.0)
angle = np.arccos(1 - fract1 * ((1 / p) ** fract3 - 1))
angle = np.degrees(angle)
kappa = (num - 1.0) / (num - result_vect)
mean_vec = cart2sph(*mean_vec)
return mean_vec, (r_value, angle, kappa)

else:
return None, None

Expand Down