-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bigsimr.jl
117 lines (96 loc) · 2.4 KB
/
Bigsimr.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
module Bigsimr
include("submodules/BigsimrBase.jl")
using Distributions: UnivariateDistribution as UD
using Distributions: ContinuousUnivariateDistribution as CUD
using Distributions: DiscreteUnivariateDistribution as DUD
using LinearAlgebra: issymmetric, isposdef, cholesky, diagm, diag
using LinearAlgebra: Diagonal, Symmetric
using NearestCorrelationMatrix
using NearestCorrelationMatrix.Internals: isprecorrelation
using PearsonCorrelationMatch
using Reexport
using SharedArrays
using Statistics: cor
using StatsBase: corspearman, corkendall
using .BigsimrBase:
clampcor,
make_symmetric!,
set_diag1!,
norm2margin,
randn_shared,
rmvn_shared,
idx_subsets2
using Statistics: Statistics
import .BigsimrBase: _cor
@reexport using .BigsimrBase
@reexport using PearsonCorrelationMatch: pearson_bounds, pearson_match
@reexport using NearestCorrelationMatrix: nearest_cor, nearest_cor!
@reexport using NearestCorrelationMatrix: Newton, AlternatingProjections, DirectProjection
export
# Correlation Methods
cor,
cor_fast,
cor_bounds,
cor_convert,
cor_constrain,
cor_constrain!,
cov2cor,
cov2cor!,
is_correlation,
# Random Correlation Generation
cor_randPSD,
cor_randPD,
# Nearest Correlation Matrix
cor_nearPSD,
cor_nearPSD!,
cor_nearPD,
cor_nearPD!,
cor_fastPD,
cor_fastPD!,
# Random Vector Generation
rmvn,
rvec
include("cor.jl")
include("cor_utils.jl")
include("cor_gen.jl")
include("nearest_cor.jl")
include("rand.jl")
include("deprecated.jl")
"""
is_correlation(X)
Check if the given matrix passes all the checks required to be a valid correlation matrix.
# Criteria
A matrix is a valid correlation matrix if:
- Square
- Symmetric
- Diagonal elements are equal to `1`
- Off diagonal elements are between `-1` and `1`
- Is positive definite
# Examples
```julia-repl
julia> x = rand(3, 3)
3×3 Matrix{Float64}:
0.834446 0.183285 0.837872
0.637295 0.270709 0.458703
0.626566 0.736907 0.61903
julia> is_correlation(x)
false
julia> x = cor_randPD(3)
3×3 Matrix{Float64}:
1.0 0.190911 0.449104
0.190911 1.0 0.636305
0.449104 0.636305 1.0
julia> is_correlation(x)
true
julia> r_negdef = [
1.00 0.82 0.56 0.44
0.82 1.00 0.28 0.85
0.56 0.28 1.00 0.22
0.44 0.85 0.22 1.00
];
julia> is_correlation(r_negdef)
false
```
"""
is_correlation(X) = isprecorrelation(X) && isposdef(X)
end