-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
###################### | ||
## type declaration ## | ||
###################### | ||
|
||
@doc doc""" | ||
PITMatrix to allow reusage of probability integral transforms. | ||
Original data is stored at the main diagonal. Values at main diagonal | ||
must be copula data between 0 and 1. | ||
"""-> | ||
type PITMatrix | ||
data::Array{Float64, 3} | ||
function PITMatrix(x::Array{Float64, 3}) | ||
if !(size(x, 1) == size(x, 2)) | ||
error("First two dimensions must be of equal size") | ||
end | ||
for ii=1:size(x, 1) | ||
if any(x[ii, ii, :][:] .> 1) | ||
error("Copula data values must not be larger than 1.") | ||
end | ||
if any(x[ii, ii, :][:] .< 0) | ||
error("Copula data values must not be smaller than 0.") | ||
end | ||
end | ||
return new(x) | ||
end | ||
end | ||
|
||
################## | ||
## constructors ## | ||
################## | ||
|
||
@doc doc""" | ||
Initialize PITMatrix with single observation per variable. | ||
"""-> | ||
function PITMatrix(x::Array{Float64, 1}) | ||
d = length(x) | ||
pitMatr = fill(NaN, d, d, 1) | ||
for ii=1:d | ||
pitMatr[ii, ii] = x[ii] | ||
end | ||
return PITMatrix(pitMatr) | ||
end | ||
|
||
@doc doc""" | ||
Initialize PITMatrix with multiple observations per variable. | ||
"""-> | ||
function PITMatrix(x::Array{Float64, 2}) | ||
n, d = size(x) | ||
pitMatr = fill(NaN, d, d, n) | ||
for ii=1:d | ||
pitMatr[ii, ii, :] = x[:, ii] | ||
end | ||
return PITMatrix(pitMatr) | ||
end | ||
|
||
############## | ||
## getindex ## | ||
############## | ||
|
||
import Base.getindex | ||
function getindex(pitMatr::PITMatrix, ind1::Int, ind2::Int) | ||
return dg(pitMatr)[ind1, ind2, :][:] | ||
end | ||
|
||
########################## | ||
## additional functions ## | ||
########################## | ||
|
||
function dg(pitMatr::PITMatrix) | ||
return pitMatr.data | ||
end | ||
|
||
import Base.isequal | ||
function isequal(pitMatr1::PITMatrix, pitMatr2::PITMatrix) | ||
return isequal(dg(pitMatr1), dg(pitMatr2)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module TestPITMatrix | ||
|
||
using Copulas | ||
using Base.Test | ||
|
||
## single observation | ||
kk = PITMatrix([0.1, 0.3]) | ||
|
||
kk2 = [0.1 NaN; | ||
NaN 0.3] | ||
expOut = PITMatrix(reshape(kk2, 2, 2, 1)) | ||
|
||
@test isequal(kk, expOut) | ||
|
||
## multiple observations | ||
kk = PITMatrix([0.1 0.3; | ||
0.4 0.7; | ||
0.6 0.4]) | ||
|
||
@test size(dg(kk)) == (2, 2, 3) | ||
@test kk[1, 1] == [0.1, 0.4, 0.6] | ||
@test isnan(dg(kk)[1, 2]) | ||
|
||
## throw error during construction | ||
@test_throws Exception PITMatrix([0.1 0.3; | ||
0.4 1.2; | ||
0.6 0.4]) | ||
|
||
@test_throws Exception PITMatrix([0.1 0.3; | ||
0.4 -0.2; | ||
0.6 0.4]) | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters