Adding error check to simple entropy computation for any entries with invalid probability values

This commit is contained in:
Joseph Lizier 2024-08-05 21:59:32 +10:00
parent 3bad6e36ba
commit 6b2cf5c69c
3 changed files with 14 additions and 4 deletions

View File

@ -19,6 +19,8 @@ function result = entropy(p)
% Should we check any potential error conditions on the input?
% assert(sum(p(:)) == 1);
assert(abs(sum(p(:)) - 1) < 0.0001); % Will work for any dimensionality, and handles numerical rounding errors
assert(~any(p(:) > 1));
assert(~any(p(:) < 0));
% We need to take the expectation value over the Shannon info content at
% p(x) for each outcome x:

View File

@ -263,6 +263,10 @@
" # Should we check any potential error conditions on the input?\n",
" if (abs(np.sum(p) - 1) > 0.00001):\n",
" raise Exception(\"Probability distribution must sum to 1: sum is %.4f\" % np.sum(p))\n",
" if (np.any(p > 1)):\n",
" raise Exception(\"Probability distribution must have all entries <= 1\")\n",
" if (np.any(p < 0)):\n",
" raise Exception(\"Probability distribution must have all entries >= 0\")\n",
" \n",
" # We need to take the expectation value over the Shannon info content at\n",
" # p(x) for each outcome x:\n",
@ -299,9 +303,9 @@
"name": "stderr",
"output_type": "stream",
"text": [
"/tmp/ipykernel_1829157/3406804068.py:20: RuntimeWarning: divide by zero encountered in log2\n",
"/tmp/ipykernel_617811/3406804068.py:20: RuntimeWarning: divide by zero encountered in log2\n",
" return -np.log2(p)\n",
"/tmp/ipykernel_1829157/3262277363.py:24: RuntimeWarning: invalid value encountered in multiply\n",
"/tmp/ipykernel_617811/1887577405.py:28: RuntimeWarning: invalid value encountered in multiply\n",
" weightedShannonInfos = p*(infocontent(p))\n"
]
}
@ -345,9 +349,9 @@
"name": "stderr",
"output_type": "stream",
"text": [
"/tmp/ipykernel_1829157/3406804068.py:20: RuntimeWarning: divide by zero encountered in log2\n",
"/tmp/ipykernel_617811/3406804068.py:20: RuntimeWarning: divide by zero encountered in log2\n",
" return -np.log2(p)\n",
"/tmp/ipykernel_1829157/3262277363.py:24: RuntimeWarning: invalid value encountered in multiply\n",
"/tmp/ipykernel_617811/1887577405.py:28: RuntimeWarning: invalid value encountered in multiply\n",
" weightedShannonInfos = p*(infocontent(p))\n"
]
},

View File

@ -42,6 +42,10 @@ def entropy(p):
# Should we check any potential error conditions on the input?
if (abs(np.sum(p) - 1) > 0.00001):
raise Exception("Probability distribution must sum to 1: sum is %.4f" % np.sum(p))
if (np.any(p > 1)):
raise Exception("Probability distribution must have all entries <= 1")
if (np.any(p < 0)):
raise Exception("Probability distribution must have all entries >= 0")
# We need to take the expectation value over the Shannon info content at
# p(x) for each outcome x: