{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO: Recompiling stale cache file /home/dzea/.julia/lib/v0.5/PairwiseListMatrices.ji for module PairwiseListMatrices.\n",
"WARNING: Method definition require(Symbol) in module Base at loading.jl:345 overwritten in module Main at /home/dzea/.julia/v0.5/Requires/src/require.jl:12.\n"
]
}
],
"source": [
"using PairwiseListMatrices\n",
"using BenchmarkTools\n",
"using Base.Test\n",
"using Gadfly"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"const SAMPLES = collect(5:50:2000)\n",
"const TIME = zeros(Float64, length(SAMPLES)*2)\n",
"const NAMES = vcat([ [\"pairwiselistmatrix\", \"full\"] for i in 1:length(SAMPLES) ]...)\n",
"const XS = vcat([ [x, x] for x in SAMPLES ]...);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## MEAN"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
],
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Plot(...)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k = 0\n",
"for sample in SAMPLES\n",
" list = PairwiseListMatrix(rand(div(sample*(sample-1),2)))\n",
" mat = full(list)\n",
" @test_approx_eq mean(list) mean(mat)\n",
" k += 1\n",
" TIME[k] = @elapsed mean(list)\n",
" k += 1\n",
" TIME[k] = @elapsed mean(mat)\n",
"end\n",
"\n",
"plot(x=XS, y=TIME, color=NAMES, Geom.point, Geom.smooth)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
],
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Plot(...)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k = 0\n",
"for sample in SAMPLES\n",
" list = PairwiseListMatrix(rand(div(sample*(sample-1),2)))\n",
" mat = full(list)\n",
" @test_approx_eq mean(list, 1) mean(mat, 1)\n",
" k += 1\n",
" TIME[k] = @elapsed mean(list, 1)\n",
" k += 1\n",
" TIME[k] = @elapsed mean(mat, 1)\n",
"end\n",
"\n",
"plot(x=XS, y=TIME, color=NAMES, Geom.point, Geom.smooth)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## SUM"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
],
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Plot(...)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k = 0\n",
"for sample in SAMPLES\n",
" list = PairwiseListMatrix(rand(div(sample*(sample-1),2)))\n",
" mat = full(list)\n",
" @test_approx_eq sum(list) sum(mat)\n",
" k += 1\n",
" TIME[k] = @elapsed sum(list)\n",
" k += 1\n",
" TIME[k] = @elapsed sum(mat)\n",
"end\n",
"\n",
"plot(x=XS, y=TIME, color=NAMES, Geom.point, Geom.smooth)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
],
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Plot(...)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k = 0\n",
"for sample in SAMPLES\n",
" list = PairwiseListMatrix(rand(div(sample*(sample-1),2)))\n",
" mat = full(list)\n",
" @test_approx_eq sum(list, 1) sum(mat, 1)\n",
" k += 1\n",
" TIME[k] = @elapsed sum(list, 1)\n",
" k += 1\n",
" TIME[k] = @elapsed sum(mat, 1)\n",
"end\n",
"\n",
"plot(x=XS, y=TIME, color=NAMES, Geom.point, Geom.smooth)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Mean without diagonal: mean_nodiag"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"mean_nodiag (generic function with 4 methods)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import PairwiseListMatrices: mean_nodiag\n",
"\n",
"mean_nodiag(m::Matrix, region) = (squeeze(sum(m, region), region) .- diag(m)) ./ (size(m, region)-1)\n",
"\n",
"function mean_nodiag{T}(m::Matrix{T})\n",
" nrow, ncol = size(m)\n",
" total = zero(T)\n",
" @inbounds for j in 1:(ncol-1)\n",
" @simd for i in (j+1):ncol\n",
" total += m[i, j]\n",
" end\n",
" end\n",
" total / div(ncol*(ncol-1), 2)\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
],
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Plot(...)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k = 0\n",
"for sample in SAMPLES\n",
" list = PairwiseListMatrix(rand(div(sample*(sample-1),2)))\n",
" mat = full(list)\n",
" @test_approx_eq mean_nodiag(list) mean_nodiag(mat)\n",
" k += 1\n",
" TIME[k] = @elapsed mean_nodiag(list)\n",
" k += 1\n",
" TIME[k] = @elapsed mean_nodiag(mat)\n",
"end\n",
"\n",
"plot(x=XS, y=TIME, color=NAMES, Geom.point, Geom.smooth)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
],
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Plot(...)"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k = 0\n",
"for sample in SAMPLES\n",
" list = PairwiseListMatrix(rand(div(sample*(sample-1),2)))\n",
" mat = full(list)\n",
" @test_approx_eq mean_nodiag(list, 1) mean_nodiag(mat, 1)\n",
" k += 1\n",
" TIME[k] = @elapsed mean_nodiag(list, 1)\n",
" k += 1\n",
" TIME[k] = @elapsed mean_nodiag(mat, 1)\n",
"end\n",
"\n",
"plot(x=XS, y=TIME, color=NAMES, Geom.point, Geom.smooth)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Vector{PairwiseListMatrix}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### SUM"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
],
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Plot(...)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k = 0\n",
"for sample in SAMPLES\n",
" list = PairwiseListMatrix{Float64, false,Vector{Float64}}[ PairwiseListMatrix(rand(div(150*(150-1),2))) for i in 1:sample ]\n",
" mat = Matrix{Float64}[ full(l) for l in list ] \n",
" @test_approx_eq sum(list) sum(mat)\n",
" k += 1\n",
" TIME[k] = @elapsed sum(list)\n",
" k += 1\n",
" TIME[k] = @elapsed sum(mat)\n",
"end\n",
"\n",
"plot(x=XS, y=TIME, color=NAMES, Geom.point, Geom.smooth)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### MEAN"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
],
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Plot(...)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k = 0\n",
"for sample in SAMPLES\n",
" list = PairwiseListMatrix{Float64, false, Vector{Float64}}[ PairwiseListMatrix(rand(div(150*(150-1),2))) for i in 1:sample ]\n",
" mat = Matrix{Float64}[ full(l) for l in list ] \n",
" @test_approx_eq mean(list) mean(mat)\n",
" k += 1\n",
" TIME[k] = @elapsed mean(list)\n",
" k += 1\n",
" TIME[k] = @elapsed mean(mat)\n",
"end\n",
"\n",
"plot(x=XS, y=TIME, color=NAMES, Geom.point, Geom.smooth)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 0.5.0",
"language": "julia",
"name": "julia-0.5"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "0.5.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}