Skip to content

Commit

Permalink
pitmatrix added
Browse files Browse the repository at this point in the history
  • Loading branch information
cgroll committed May 13, 2015
1 parent b7bec2e commit 2d876e6
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/Copulas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Tawn2PC_Cpp,
TawnPC_Cpp,
tPC_Cpp,
# functions
PITMatrix,
Tree,
Vine,
Raw_html,
Expand All @@ -89,6 +90,7 @@ trees2vine,
vine2trees,
cdf,
checkSameLength,
dg,
getVineCPPId,
getCopNam,
getCopType,
Expand Down Expand Up @@ -117,6 +119,7 @@ include("PCC/Vines/rvine_matrix.jl")
include("PCC/Vines/graphviz.jl")
include("PCC/Vines/utils.jl")
include("PCC/Vines/vineConstruction.jl")
include("PCC/Vines/pitmatrix.jl")
include("PCC/PCC.jl")
include("testFuncs.jl")
include("testcases.jl")
Expand Down
76 changes: 76 additions & 0 deletions src/PCC/Vines/pitmatrix.jl
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
33 changes: 33 additions & 0 deletions test/pitmatrix_test.jl
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
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ tests = [
"pcc_test.jl",
"vineConstruction_test.jl",
"ParamPC_tests.jl",
"ParamPC_Cpp_tests.jl"
"ParamPC_Cpp_tests.jl",
"pitmatrix_test.jl"
]

println("Running copula tests:")
Expand Down

0 comments on commit 2d876e6

Please sign in to comment.