-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from dglmoore/0.0.5-dev
Version 0.0.5
- Loading branch information
Showing
14 changed files
with
935 additions
and
11 deletions.
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
version: 0.0.4.{build} | ||
version: 0.0.5.{build} | ||
configuration: | ||
- Debug | ||
- Release | ||
|
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,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 |
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,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 |
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,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; | ||
} |
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,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; | ||
} |
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
Oops, something went wrong.