MinimumVarianceAnalysis
A Julia package for minimum or maximum variance analysis (MVA).
API Reference
LinearAlgebra.issuccess — Method
issuccess(F::MVAResult; r0 = 5)Whether the normal is well determined: $λ_i - λ_j > (r_0 - 1) V_j$, with $(i, j) = (2, 3)$ for MVAB and $(1, 2)$ for MVAE, and $V_j$ the larger of the variances of B and of background along $𝐱_j$. Without background this is $λ_i/λ_j > r_0$.
MinimumVarianceAnalysis.mva — Function
mva(V, F=V; dim=nothing, kwargs...)
mva(V, F::MVAResult; dim=nothing)Transform time series V into the variance frame (LMN) of reference field F, or of an mva_eigen result F.
MinimumVarianceAnalysis.mva_eigen — Method
mva_eigen(B; dim = nothing, field = :B, background = nothing, check = false) -> F::MVAResultPerform minimum variance analysis of the magnetic field B or maximum variance analysis of the electric field E when field=:E.
check=true warns unless issuccess(F).
Notes
For a one-dimensional current layer, the tangential electric field components are approximately constant across the boundary, while the normal component exhibits the largest variation. Therefore, the eigenvector corresponding to the maximum eigenvalue $λ_1$ (first column of F.vectors) gives an estimate of the boundary normal direction.
MinimumVarianceAnalysis.normal_component_error — Method
normal_component_error(F::MVAResult; M = F.n)Statistical error of the mean field's normal component, $⟨𝐁⟩·𝐱₃$ for MVAB or $⟨𝐄⟩·𝐱₁$ for MVAE:
$|Δ⟨𝐅·𝐱_n⟩| = \sqrt{\frac{s_n^2 + σ_n^2}{M-1} + ∑_{k ≠ n} (Δφ_{nk} ⟨𝐅⟩·𝐱_k)^2}$
with the angular uncertainty of eigenvector $n$ toward $k$
$|Δφ_{nk}| = \sqrt{\frac{σ_n^2 s_k^2 + σ_k^2 s_n^2 + (1 + n/n_b)(σ_n^2 σ_k^2 + σ_{nk}^4)}{(M-1)(λ_n - λ_k)^2}}$
with signal variance $s_k^2$ along eigenvector $k$, noise covariance $σ_{nk}^2$ ($σ_k^2 ≡ σ_{kk}^2$) in the eigenbasis, and $1 + n/n_b$ the sampling error of a background of $n_b$ samples against the $n$ of B, assuming equal correlation times. Without background, $σ_{nk}^2 = λ_3 δ_{nk}$, $n_b = ∞$ and $s_k^2 = λ_k - λ_3$, which gives Sonnerup & Scheible (1998) $|Δφ_{nk}| = \sqrt{\frac{λ_3}{M-1} \frac{λ_n + λ_k - λ_3}{(λ_n - λ_k)^2}}$.
Error estimates for MVA:
MinimumVarianceAnalysis.normal_component_error — Function
normal_component_error(F::MVAResult; M = F.n)Statistical error of the mean field's normal component, $⟨𝐁⟩·𝐱₃$ for MVAB or $⟨𝐄⟩·𝐱₁$ for MVAE:
$|Δ⟨𝐅·𝐱_n⟩| = \sqrt{\frac{s_n^2 + σ_n^2}{M-1} + ∑_{k ≠ n} (Δφ_{nk} ⟨𝐅⟩·𝐱_k)^2}$
with the angular uncertainty of eigenvector $n$ toward $k$
$|Δφ_{nk}| = \sqrt{\frac{σ_n^2 s_k^2 + σ_k^2 s_n^2 + (1 + n/n_b)(σ_n^2 σ_k^2 + σ_{nk}^4)}{(M-1)(λ_n - λ_k)^2}}$
with signal variance $s_k^2$ along eigenvector $k$, noise covariance $σ_{nk}^2$ ($σ_k^2 ≡ σ_{kk}^2$) in the eigenbasis, and $1 + n/n_b$ the sampling error of a background of $n_b$ samples against the $n$ of B, assuming equal correlation times. Without background, $σ_{nk}^2 = λ_3 δ_{nk}$, $n_b = ∞$ and $s_k^2 = λ_k - λ_3$, which gives Sonnerup & Scheible (1998) $|Δφ_{nk}| = \sqrt{\frac{λ_3}{M-1} \frac{λ_n + λ_k - λ_3}{(λ_n - λ_k)^2}}$.
Validation with PySPEDAS
References: mva_eigen, test_minvar.py - PySPEDAS
using MinimumVarianceAnalysis
using PySPEDAS
using PySPEDAS.PythonCall
@py import pyspedas.cotrans_tools.tests.test_minvar: TestMinvar
@py import pyspedas.cotrans_tools.minvar_matrix_make: minvar_matrix_make
isapprox_eigenvector(v1, v2) = isapprox(v1, v2) || isapprox(v1, -v2)
pytest = TestMinvar()
pytest.setUpClass()
thb_fgs_gsm = get_data("idl_thb_fgs_gsm_mvaclipped1")
jl_mva_eigen = mva_eigen(thb_fgs_gsm)
jl_mva_mat = jl_mva_eigen.vectors
jl_mva_vals = jl_mva_eigen.values
py_mva_vals = PyArray(pytest.vals.y) |> vec
py_mva_mat = PyArray(pytest.mat.y[0])'
@assert isapprox(jl_mva_vals, py_mva_vals)
@assert all(isapprox_eigenvector.(eachcol(jl_mva_mat), eachcol(py_mva_mat))) CondaPkg Found dependencies: /home/runner/.julia/packages/CondaPkg/Pc7ZA/CondaPkg.toml
CondaPkg Found dependencies: /home/runner/.julia/packages/PySPEDAS/3mgxR/CondaPkg.toml
CondaPkg Found dependencies: /home/runner/.julia/packages/PythonCall/trYos/CondaPkg.toml
CondaPkg Initialising pixi
│ /home/runner/.julia/artifacts/8429d049f6c01106c62971e9540e146dc068df25/bin/pixi
│ init
│ --format pixi
└ /home/runner/work/MinimumVarianceAnalysis.jl/MinimumVarianceAnalysis.jl/docs/.CondaPkg
✔ Created /home/runner/work/MinimumVarianceAnalysis.jl/MinimumVarianceAnalysis.jl/docs/.CondaPkg/pixi.toml
CondaPkg Wrote /home/runner/work/MinimumVarianceAnalysis.jl/MinimumVarianceAnalysis.jl/docs/.CondaPkg/pixi.toml
│ [dependencies]
│ python-gil = "*"
│ openssl = ">=3, <3.6"
│ uv = ">=0.4"
│ certifi = "*"
│ netcdf4 = "*"
│
│ [dependencies.python]
│ version = ">=3.10,<3.15, >=3.10,!=3.14.0,!=3.14.1,<4"
│ build = "*cp*"
│ channel = "conda-forge"
│
│ [dependencies.libpython]
│ version = "*"
│ channel = "conda-forge"
│
│ [pypi-dependencies]
│ pyspedas = ">=2.2.2"
│
│ [workspace]
│ name = ".CondaPkg"
│ description = "automatically generated by CondaPkg.jl"
│ platforms = ["linux-64"]
│ channel-priority = "strict"
└ channels = ["conda-forge"]
CondaPkg Installing packages
│ /home/runner/.julia/artifacts/8429d049f6c01106c62971e9540e146dc068df25/bin/pixi
│ install
└ --manifest-path /home/runner/work/MinimumVarianceAnalysis.jl/MinimumVarianceAnalysis.jl/docs/.CondaPkg/pixi.toml
✔ The default environment has been installed.
05-Nov-26 23:10:16: Downloading https://github.com/spedas/test_data/raw/refs/heads/main/cotrans_tools/mva_python_validate.tplot to _testing_output/cotrans_tools/cotrans_tools/mva_python_validate.tplot
05-Nov-26 23:10:16: Download of _testing_output/cotrans_tools/cotrans_tools/mva_python_validate.tplot complete, 2.282 MB in 0.7 sec (3.075 MB/sec) (transfer_normal)
05-Nov-26 23:10:16: del_data: No valid tplot variables found, returning
05-Nov-26 23:10:16: store_data: Data array for variable thb_fgs_gsm_mvaclipped1_mva_mat has 3 dimensions, but only 1 v_n keys plus time. Adding empty v_n keys.Since eigenvectors are only unique up to sign; therefore, the test checks if each Julia eigenvector is approximately equal to the corresponding Python eigenvector or its negative.
Benchmark
using Chairmarks
@b mva_eigen(thb_fgs_gsm), minvar_matrix_make("idl_thb_fgs_gsm_mvaclipped1")(781.000 ns (1 allocs: 96 bytes), 2.156 ms (3 allocs: 48 bytes))Julia demonstrates a performance advantage of approximately 1000 times over Python, with significantly reduced memory allocations. Moreover, Julia's implementation is generalized for N-dimensional data.