Skip to content

Commit

Permalink
Merge pull request #23 from dglmoore/0.0.5-dev
Browse files Browse the repository at this point in the history
Version 0.0.5
  • Loading branch information
dglmoore authored Aug 1, 2016
2 parents 7a515c6 + 8189c86 commit a4e841e
Show file tree
Hide file tree
Showing 14 changed files with 935 additions and 11 deletions.
12 changes: 6 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ cmake_minimum_required(VERSION 2.8)
project(inform C)

set(${PROJECT_NAME}_VERSION_MAJOR 0)
set(${PROJECT_NAME}_VERSION_MINOR 0.4)
set(${PROJECT_NAME}_VERSION_MINOR 0.5)
set(${PROJECT_NAME}_VERSION ${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR})

message(STATUS "inform version: ${${PROJECT_NAME}_VERSION}")

if (UNIX)
if (UNIX AND NOT APPLE)
add_definitions("-Wall -Wextra -Werror -Wno-unused-parameter -std=gnu1x")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -ggdb -pg")
endif()

if (APPLE)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -pg")
elseif (APPLE)
add_definitions("-Wall -Wextra -Werror -Wno-unused-parameter -std=gnu1x")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g")
set(CMAKE_MACOSX_RPATH ON)
endif()

Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 0.0.4.{build}
version: 0.0.5.{build}
configuration:
- Debug
- Release
Expand Down
14 changes: 13 additions & 1 deletion dist/package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ version=${version#"-- inform version: "}

target=inform-$version
prefix=dist/$target
tarball=$target"_linux-x86_64.tar.gz"

cmake -H. -Bbuild/production -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$prefix > /dev/null
make -C build/production all test
Expand All @@ -14,6 +13,19 @@ cp dist/install.sh $prefix > /dev/null
cp LICENSE $prefix > /dev/null
cp README.md $prefix > /dev/null

system=`uname -s`
hardware=`uname -m`

if [[ "$system" == "Darwin" ]]; then
platform="macosx"
elif [[ "$system" == "Linux" ]]; then
platform="linux"
else
echo "unsupported platform: $system"
fi

tarball=$target"_"$platform"-"$hardware".tar.gz"

cd dist
echo "Creating tarball: dist/$tarball"
tar czf $tarball $target
29 changes: 29 additions & 0 deletions include/inform/conditional_entropy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2016 ELIFE. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
#pragma once

#include <inform/error.h>

#ifdef __cplusplus
extern "C"
{
#endif

/**
* Compute the conditional entropy between two timeseries, using the
* first as the condition.
*/
EXPORT double inform_conditional_entropy(int const *xs, int const *ys,
size_t n, int bx, int by, double b, inform_error *err);

/**
* Compute the local conditional entropy between two timeseries, using the
* first as the condition.
*/
EXPORT double *inform_local_conditional_entropy(int const *xs, int const *ys,
size_t n, int bx, int by, double b, double *mi, inform_error *err);

#ifdef __cplusplus
}
#endif
29 changes: 29 additions & 0 deletions include/inform/relative_entropy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2016 ELIFE. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
#pragma once

#include <inform/error.h>

#ifdef __cplusplus
extern "C"
{
#endif

/**
* Compute the relative entropy between two timeseries, each considered as
* a timeseries of samples from two distributions.
*/
EXPORT double inform_relative_entropy(int const *xs, int const *ys, size_t n,
int b, double base, inform_error *err);

/**
* Compute the pointwise relative entropy between two timeseries, each
* considered as a timeseries of samples from two distributions.
*/
EXPORT double *inform_local_relative_entropy(int const *xs, int const *ys,
size_t n, int b, double base, double *re, inform_error *err);

#ifdef __cplusplus
}
#endif
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
set(${PROJECT_NAME}_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/active_info.c
${CMAKE_CURRENT_SOURCE_DIR}/block_entropy.c
${CMAKE_CURRENT_SOURCE_DIR}/conditional_entropy.c
${CMAKE_CURRENT_SOURCE_DIR}/dist.c
${CMAKE_CURRENT_SOURCE_DIR}/entropy_rate.c
${CMAKE_CURRENT_SOURCE_DIR}/error.c
${CMAKE_CURRENT_SOURCE_DIR}/mutual_info.c
${CMAKE_CURRENT_SOURCE_DIR}/relative_entropy.c
${CMAKE_CURRENT_SOURCE_DIR}/shannon.c
${CMAKE_CURRENT_SOURCE_DIR}/transfer_entropy.c
${CMAKE_CURRENT_SOURCE_DIR}/utilities/binning.c
Expand Down
121 changes: 121 additions & 0 deletions src/conditional_entropy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright 2016 ELIFE. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
#include <inform/conditional_entropy.h>
#include <inform/shannon.h>

static bool check_arguments(int const *xs, int const *ys, size_t n, int bx,
int by, inform_error *err)
{
if (xs == NULL)
{
INFORM_ERROR_RETURN(err, INFORM_ETIMESERIES, true);
}
else if (ys == NULL)
{
INFORM_ERROR_RETURN(err, INFORM_ETIMESERIES, true);
}
else if (n < 1)
{
INFORM_ERROR_RETURN(err, INFORM_ESHORTSERIES, true);
}
else if (bx < 2)
{
INFORM_ERROR_RETURN(err, INFORM_EBASE, true);
}
else if (by < 2)
{
INFORM_ERROR_RETURN(err, INFORM_EBASE, true);
}
for (size_t i = 0; i < n; ++i)
{
if (xs[i] < 0 || ys[i] < 0)
{
INFORM_ERROR_RETURN(err, INFORM_ENEGSTATE, true);
}
else if (bx <= xs[i] || by <= ys[i])
{
INFORM_ERROR_RETURN(err, INFORM_EBADSTATE, true);
}
}
return false;
}

inline static bool allocate(int bx, int by, inform_dist **x, inform_dist **xy,
inform_error *err)
{
if ((*x = inform_dist_alloc(bx)) == NULL)
{
INFORM_ERROR_RETURN(err, INFORM_ENOMEM, true);
}
if ((*xy = inform_dist_alloc(bx * by)) == NULL)
{
inform_dist_free(*x);
INFORM_ERROR_RETURN(err, INFORM_ENOMEM, true);
}
return false;
}

inline static void accumulate(int const *xs, int const *ys, size_t n, int by,
inform_dist *x, inform_dist *xy)
{
x->counts = n;
xy->counts = n;

for (size_t i = 0; i < n; ++i)
{
x->histogram[xs[i]]++;
xy->histogram[xs[i]*by + ys[i]]++;
}
}

inline static void free_all(inform_dist **x, inform_dist **xy)
{
inform_dist_free(*x);
inform_dist_free(*xy);
}

double inform_conditional_entropy(int const *xs, int const *ys, size_t n,
int bx, int by, double b, inform_error *err)
{
if (check_arguments(xs, ys, n, bx, by, err)) return NAN;

inform_dist *x = NULL, *xy = NULL;
if (allocate(bx, by, &x, &xy, err)) return NAN;

accumulate(xs, ys, n, by, x, xy);

double ce = inform_shannon_ce(xy, x, (double) b);

free_all(&x, &xy);

return ce;
}

double *inform_local_conditional_entropy(int const *xs, int const *ys,
size_t n, int bx, int by, double b, double *ce, inform_error *err)
{
if (check_arguments(xs, ys, n, bx, by, err)) return NULL;

if (ce == NULL)
{
ce = malloc(n * sizeof(double));
if (ce == NULL)
INFORM_ERROR_RETURN(err, INFORM_ENOMEM, NULL);
}

inform_dist *x = NULL, *xy = NULL;
if (allocate(bx, by, &x, &xy, err)) return NULL;

accumulate(xs, ys, n, by, x, xy);

for (size_t i = 0; i < n; ++i)
{
int z = xs[i]*by + ys[i];
ce[i] = inform_shannon_pce(xy, x, z, xs[i], (double) b);
}

free_all(&x, &xy);

return ce;
}
2 changes: 0 additions & 2 deletions src/mutual_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ inline static void free_all(inform_dist **x, inform_dist **y, inform_dist **xy)
inform_dist_free(*xy);
}

#include <stdio.h>

double inform_mutual_info(int const *xs, int const *ys, size_t n, int bx,
int by, double b, inform_error *err)
{
Expand Down
116 changes: 116 additions & 0 deletions src/relative_entropy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright 2016 ELIFE. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
#include <inform/relative_entropy.h>
#include <inform/shannon.h>

static bool check_arguments(int const *xs, int const *ys, size_t n, int b,
inform_error *err)
{
if (xs == NULL)
{
INFORM_ERROR_RETURN(err, INFORM_ETIMESERIES, true);
}
else if (ys == NULL)
{
INFORM_ERROR_RETURN(err, INFORM_ETIMESERIES, true);
}
else if (n < 1)
{
INFORM_ERROR_RETURN(err, INFORM_ESHORTSERIES, true);
}
else if (b < 2)
{
INFORM_ERROR_RETURN(err, INFORM_EBASE, true);
}
for (size_t i = 0; i < n; ++i)
{
if (xs[i] < 0 || ys[i] < 0)
{
INFORM_ERROR_RETURN(err, INFORM_ENEGSTATE, true);
}
else if (b <= xs[i] || b <= ys[i])
{
INFORM_ERROR_RETURN(err, INFORM_EBADSTATE, true);
}
}
return false;
}

inline static bool allocate(int b, inform_dist **x, inform_dist **y,
inform_error *err)
{
if ((*x = inform_dist_alloc(b)) == NULL)
{
INFORM_ERROR_RETURN(err, INFORM_ENOMEM, true);
}
if ((*y = inform_dist_alloc(b)) == NULL)
{
inform_dist_free(*x);
INFORM_ERROR_RETURN(err, INFORM_ENOMEM, true);
}
return false;
}

inline static void accumulate(int const *xs, int const *ys, size_t n,
inform_dist *x, inform_dist *y)
{
x->counts = n;
y->counts = n;

for (size_t i = 0; i < n; ++i)
{
x->histogram[xs[i]]++;
y->histogram[ys[i]]++;
}
}

inline static void free_all(inform_dist **x, inform_dist **y)
{
inform_dist_free(*x);
inform_dist_free(*y);
}

double inform_relative_entropy(int const *xs, int const *ys, size_t n, int b,
double base, inform_error *err)
{
if (check_arguments(xs, ys, n, b, err)) return NAN;

inform_dist *x = NULL, *y = NULL;
if (allocate(b, &x, &y, err)) return NAN;

accumulate(xs, ys, n, x, y);

double re = inform_shannon_re(x, y, base);

free_all(&x, &y);

return re;
}

double *inform_local_relative_entropy(int const *xs, int const *ys, size_t n,
int b, double base, double *re, inform_error *err)
{
if (check_arguments(xs, ys, n, b, err)) return NULL;

if (re == NULL)
{
re = malloc(n * sizeof(double));
if (re == NULL)
INFORM_ERROR_RETURN(err, INFORM_ENOMEM, NULL);
}

inform_dist *x = NULL, *y = NULL;
if (allocate(b, &x, &y, err)) return NULL;

accumulate(xs, ys, n, x, y);

for (size_t i = 0; i < (size_t) b; ++i)
{
re[i] = inform_shannon_pre(x, y, i, base);
}

free_all(&x, &y);

return re;
}
2 changes: 1 addition & 1 deletion src/shannon.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ double inform_shannon_pre(inform_dist const *p, inform_dist const *q,
{
double u = inform_dist_prob(p, event);
double v = inform_dist_prob(q, event);
return -log2(u/v) / log2(base);
return log2(u/v) / log2(base);
}
return NAN;
}
Expand Down
Loading

0 comments on commit a4e841e

Please sign in to comment.