<s>
Arithmetic	B-Algorithm
coding	I-Algorithm
(	O
AC	O
)	O
is	O
a	O
form	O
of	O
entropy	B-Algorithm
encoding	I-Algorithm
used	O
in	O
lossless	B-Algorithm
data	I-Algorithm
compression	I-Algorithm
.	O
</s>
<s>
Normally	O
,	O
a	O
string	O
of	O
characters	O
is	O
represented	O
using	O
a	O
fixed	O
number	O
of	O
bits	O
per	O
character	O
,	O
as	O
in	O
the	O
ASCII	B-Protocol
code	I-Protocol
.	O
</s>
<s>
When	O
a	O
string	O
is	O
converted	O
to	O
arithmetic	B-Algorithm
encoding	I-Algorithm
,	O
frequently	O
used	O
characters	O
will	O
be	O
stored	O
with	O
fewer	O
bits	O
and	O
not-so-frequently	O
occurring	O
characters	O
will	O
be	O
stored	O
with	O
more	O
bits	O
,	O
resulting	O
in	O
fewer	O
bits	O
used	O
in	O
total	O
.	O
</s>
<s>
Arithmetic	B-Algorithm
coding	I-Algorithm
differs	O
from	O
other	O
forms	O
of	O
entropy	B-Algorithm
encoding	I-Algorithm
,	O
such	O
as	O
Huffman	B-General_Concept
coding	I-General_Concept
,	O
in	O
that	O
rather	O
than	O
separating	O
the	O
input	O
into	O
component	O
symbols	O
and	O
replacing	O
each	O
with	O
a	O
code	O
,	O
arithmetic	B-Algorithm
coding	I-Algorithm
encodes	O
the	O
entire	O
message	O
into	O
a	O
single	O
number	O
,	O
an	O
arbitrary-precision	B-Algorithm
fraction	O
q	O
,	O
where	O
.	O
</s>
<s>
A	O
recent	O
family	O
of	O
entropy	B-Algorithm
coders	I-Algorithm
called	O
asymmetric	B-General_Concept
numeral	I-General_Concept
systems	I-General_Concept
allows	O
for	O
faster	O
implementations	O
thanks	O
to	O
directly	O
operating	O
on	O
a	O
single	O
natural	O
number	O
representing	O
the	O
current	O
information	O
.	O
</s>
<s>
A	O
more	O
efficient	O
solution	O
is	O
to	O
represent	O
a	O
sequence	O
of	O
these	O
three	O
symbols	O
as	O
a	O
rational	O
number	O
in	O
base	B-Algorithm
3	I-Algorithm
where	O
each	O
digit	O
represents	O
a	O
symbol	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
sequence	O
"	O
ABBCAB	O
"	O
could	O
become	O
0.0112013	O
,	O
in	O
arithmetic	B-Algorithm
coding	I-Algorithm
as	O
a	O
value	O
in	O
the	O
interval	O
0	O
,	O
1	O
)	O
.	O
</s>
<s>
The	O
next	O
step	O
is	O
to	O
encode	O
this	O
ternary	B-Algorithm
number	I-Algorithm
using	O
a	O
fixed-point	O
binary	O
number	O
of	O
sufficient	O
precision	B-Architecture
to	O
recover	O
it	O
,	O
such	O
as	O
0.00101100102	O
–	O
this	O
is	O
only	O
10	O
bits	O
;	O
2	O
bits	O
are	O
saved	O
in	O
comparison	O
with	O
naïve	O
block	O
encoding	O
.	O
</s>
<s>
In	O
general	O
,	O
arithmetic	B-Algorithm
coders	I-Algorithm
can	O
produce	O
near-optimal	O
output	O
for	O
any	O
given	O
set	O
of	O
symbols	O
and	O
probabilities	O
.	O
</s>
<s>
(	O
The	O
optimal	O
value	O
is	O
−log2P	O
bits	O
for	O
each	O
symbol	O
of	O
probability	O
P	O
;	O
see	O
Source	B-Protocol
coding	I-Protocol
theorem	I-Protocol
.	O
)	O
</s>
<s>
Compression	B-General_Concept
algorithms	I-General_Concept
that	O
use	O
arithmetic	B-Algorithm
coding	I-Algorithm
start	O
by	O
determining	O
a	O
model	O
of	O
the	O
data	O
–	O
basically	O
a	O
prediction	O
of	O
what	O
patterns	O
will	O
be	O
found	O
in	O
the	O
symbols	O
of	O
the	O
message	O
.	O
</s>
<s>
(	O
The	O
presence	O
of	O
this	O
symbol	O
means	O
that	O
the	O
stream	O
will	O
be	O
'	O
internally	O
terminated	O
 '	O
,	O
as	O
is	O
fairly	O
common	O
in	O
data	B-General_Concept
compression	I-General_Concept
;	O
when	O
this	O
symbol	O
appears	O
in	O
the	O
data	O
stream	O
,	O
the	O
decoder	O
will	O
know	O
that	O
the	O
entire	O
stream	O
has	O
been	O
decoded	O
.	O
)	O
</s>
<s>
Models	O
can	O
even	O
be	O
adaptive	B-Algorithm
,	O
so	O
that	O
they	O
continually	O
change	O
their	O
prediction	O
of	O
the	O
data	O
based	O
on	O
what	O
the	O
stream	O
actually	O
contains	O
.	O
</s>
<s>
The	O
probabilities	O
the	O
model	O
assigns	O
to	O
each	O
of	O
the	O
various	O
symbols	O
that	O
are	O
possible	O
at	O
this	O
stage	O
(	O
as	O
mentioned	O
earlier	O
,	O
higher-order	O
or	O
adaptive	B-Algorithm
models	O
mean	O
that	O
these	O
probabilities	O
are	O
not	O
necessarily	O
the	O
same	O
in	O
each	O
step	O
.	O
)	O
</s>
<s>
In	O
particular	O
,	O
it	O
is	O
only	O
necessary	O
to	O
transmit	O
enough	O
digits	O
(	O
in	O
whatever	O
base	O
)	O
of	O
the	O
fraction	O
so	O
that	O
all	O
fractions	O
that	O
begin	O
with	O
those	O
digits	O
fall	O
into	O
the	O
final	O
interval	O
;	O
this	O
will	O
guarantee	O
that	O
the	O
resulting	O
code	O
is	O
a	O
prefix	B-Algorithm
code	I-Algorithm
.	O
</s>
<s>
This	O
is	O
also	O
an	O
example	O
of	O
how	O
statistical	O
coding	O
methods	O
like	O
arithmetic	B-Algorithm
encoding	I-Algorithm
can	O
produce	O
an	O
output	O
message	O
that	O
is	O
larger	O
than	O
the	O
input	O
message	O
,	O
especially	O
if	O
the	O
probability	O
model	O
is	O
off	O
.	O
</s>
<s>
One	O
advantage	O
of	O
arithmetic	B-Algorithm
coding	I-Algorithm
over	O
other	O
similar	O
methods	O
of	O
data	B-General_Concept
compression	I-General_Concept
is	O
the	O
convenience	O
of	O
adaptation	O
.	O
</s>
<s>
The	O
above	O
explanations	O
of	O
arithmetic	B-Algorithm
coding	I-Algorithm
contain	O
some	O
simplification	O
.	O
</s>
<s>
In	O
particular	O
,	O
they	O
are	O
written	O
as	O
if	O
the	O
encoder	O
first	O
calculated	O
the	O
fractions	O
representing	O
the	O
endpoints	O
of	O
the	O
interval	O
in	O
full	O
,	O
using	O
infinite	B-Algorithm
precision	I-Algorithm
,	O
and	O
only	O
converted	O
the	O
fraction	O
to	O
its	O
final	O
form	O
at	O
the	O
end	O
of	O
encoding	O
.	O
</s>
<s>
Rather	O
than	O
try	O
to	O
simulate	O
infinite	B-Algorithm
precision	I-Algorithm
,	O
most	O
arithmetic	B-Algorithm
coders	I-Algorithm
instead	O
operate	O
at	O
a	O
fixed	O
limit	O
of	O
precision	B-Architecture
which	O
they	O
know	O
the	O
decoder	O
will	O
be	O
able	O
to	O
match	O
,	O
and	O
round	O
the	O
calculated	O
fractions	O
to	O
their	O
nearest	O
equivalents	O
at	O
that	O
precision	B-Architecture
.	O
</s>
<s>
An	O
example	O
shows	O
how	O
this	O
would	O
work	O
if	O
the	O
model	O
called	O
for	O
the	O
interval	O
to	O
be	O
divided	O
into	O
thirds	O
,	O
and	O
this	O
was	O
approximated	O
with	O
8	O
bit	O
precision	B-Architecture
.	O
</s>
<s>
Note	O
that	O
since	O
now	O
the	O
precision	B-Architecture
is	O
known	O
,	O
so	O
are	O
the	O
binary	O
ranges	O
we	O
'll	O
be	O
able	O
to	O
use	O
.	O
</s>
<s>
A	O
process	O
called	O
renormalization	O
keeps	O
the	O
finite	O
precision	B-Architecture
from	O
becoming	O
a	O
limit	O
on	O
the	O
total	O
number	O
of	O
symbols	O
that	O
can	O
be	O
encoded	O
.	O
</s>
<s>
For	O
however	O
many	O
digits	O
of	O
precision	B-Architecture
the	O
computer	O
can	O
handle	O
,	O
it	O
is	O
now	O
handling	O
fewer	O
than	O
that	O
,	O
so	O
the	O
existing	O
digits	O
are	O
shifted	O
left	O
,	O
and	O
at	O
the	O
right	O
,	O
new	O
digits	O
are	O
added	O
to	O
expand	O
the	O
range	O
as	O
widely	O
as	O
possible	O
.	O
</s>
<s>
Recall	O
that	O
in	O
the	O
case	O
where	O
the	O
symbols	O
had	O
equal	O
probabilities	O
,	O
arithmetic	B-Algorithm
coding	I-Algorithm
could	O
be	O
implemented	O
by	O
a	O
simple	O
change	O
of	O
base	O
,	O
or	O
radix	O
.	O
</s>
<s>
Because	O
arithmetic	B-Algorithm
coding	I-Algorithm
does	O
n't	O
compress	O
one	O
datum	O
at	O
a	O
time	O
,	O
it	O
can	O
get	O
arbitrarily	O
close	O
to	O
entropy	O
when	O
compressing	O
IID	O
strings	O
.	O
</s>
<s>
By	O
contrast	O
,	O
using	O
the	O
extension	O
of	O
Huffman	B-General_Concept
coding	I-General_Concept
(	O
to	O
strings	O
)	O
does	O
not	O
reach	O
entropy	O
unless	O
all	O
probabilities	O
of	O
alphabet	O
symbols	O
are	O
powers	O
of	O
two	O
,	O
in	O
which	O
case	O
both	O
Huffman	O
and	O
arithmetic	B-Algorithm
coding	I-Algorithm
achieve	O
entropy	O
.	O
</s>
<s>
When	O
naively	O
Huffman	B-General_Concept
coding	I-General_Concept
binary	O
strings	O
,	O
no	O
compression	O
is	O
possible	O
,	O
even	O
if	O
entropy	O
is	O
low	O
(	O
e.g.	O
</s>
<s>
Huffman	B-General_Concept
encoding	I-General_Concept
assigns	O
1	O
bit	O
to	O
each	O
value	O
,	O
resulting	O
in	O
a	O
code	O
of	O
the	O
same	O
length	O
as	O
the	O
input	O
.	O
</s>
<s>
One	O
simple	O
way	O
to	O
address	O
Huffman	B-General_Concept
coding	I-General_Concept
's	O
suboptimality	O
is	O
to	O
concatenate	O
symbols	O
(	O
"	O
blocking	O
"	O
)	O
to	O
form	O
a	O
new	O
alphabet	O
in	O
which	O
each	O
new	O
symbol	O
represents	O
a	O
sequence	O
of	O
original	O
symbols	O
–	O
in	O
this	O
case	O
bits	O
–	O
from	O
the	O
original	O
alphabet	O
.	O
</s>
<s>
With	O
this	O
grouping	O
,	O
Huffman	B-General_Concept
coding	I-General_Concept
averages	O
1.3	O
bits	O
for	O
every	O
three	O
symbols	O
,	O
or	O
0.433	O
bits	O
per	O
symbol	O
,	O
compared	O
with	O
one	O
bit	O
per	O
symbol	O
in	O
the	O
original	O
encoding	O
,	O
i.e.	O
,	O
compression	O
.	O
</s>
<s>
Allowing	O
arbitrarily	O
large	O
sequences	O
gets	O
arbitrarily	O
close	O
to	O
entropy	O
–	O
just	O
like	O
arithmetic	B-Algorithm
coding	I-Algorithm
–	O
but	O
requires	O
huge	O
codes	O
to	O
do	O
so	O
,	O
so	O
is	O
not	O
as	O
practical	O
as	O
arithmetic	B-Algorithm
coding	I-Algorithm
for	O
this	O
purpose	O
.	O
</s>
<s>
An	O
alternative	O
is	O
encoding	B-Algorithm
run	I-Algorithm
lengths	I-Algorithm
via	O
Huffman-based	O
Golomb-Rice	B-Algorithm
codes	I-Algorithm
.	O
</s>
<s>
Such	O
an	O
approach	O
allows	O
simpler	O
and	O
faster	O
encoding/decoding	O
than	O
arithmetic	B-Algorithm
coding	I-Algorithm
or	O
even	O
Huffman	B-General_Concept
coding	I-General_Concept
,	O
since	O
the	O
latter	O
requires	O
a	O
table	O
lookups	O
.	O
</s>
<s>
In	O
the	O
{	O
0.95	O
,	O
0.05	O
}	O
example	O
,	O
a	O
Golomb-Rice	B-Algorithm
code	I-Algorithm
with	O
a	O
four-bit	O
remainder	O
achieves	O
a	O
compression	O
ratio	O
of	O
,	O
far	O
closer	O
to	O
optimum	O
than	O
using	O
three-bit	O
blocks	O
.	O
</s>
<s>
Golomb-Rice	B-Algorithm
codes	I-Algorithm
only	O
apply	O
to	O
Bernoulli	O
inputs	O
such	O
as	O
the	O
one	O
in	O
this	O
example	O
,	O
however	O
,	O
so	O
it	O
is	O
not	O
a	O
substitute	O
for	O
blocking	O
in	O
all	O
cases	O
.	O
</s>
<s>
Basic	O
algorithms	O
for	O
arithmetic	B-Algorithm
coding	I-Algorithm
were	O
developed	O
independently	O
by	O
Jorma	O
J	O
.	O
Rissanen	O
,	O
at	O
IBM	O
Research	O
,	O
and	O
by	O
Richard	O
C	O
.	O
Pasco	O
,	O
a	O
Ph.D.	O
student	O
at	O
Stanford	O
University	O
;	O
both	O
were	O
published	O
in	O
May	O
1976	O
.	O
</s>
<s>
A	O
variety	O
of	O
specific	O
techniques	O
for	O
arithmetic	B-Algorithm
coding	I-Algorithm
have	O
historically	O
been	O
covered	O
by	O
US	O
patents	O
,	O
although	O
various	O
well-known	O
methods	O
have	O
since	O
passed	O
into	O
the	O
public	O
domain	O
as	O
the	O
patents	O
have	O
expired	O
.	O
</s>
<s>
Techniques	O
covered	O
by	O
patents	O
may	O
be	O
essential	O
for	O
implementing	O
the	O
algorithms	O
for	O
arithmetic	B-Algorithm
coding	I-Algorithm
that	O
are	O
specified	O
in	O
some	O
formal	O
international	O
standards	O
.	O
</s>
<s>
The	O
availability	O
of	O
licenses	O
under	O
RAND	O
terms	O
does	O
not	O
necessarily	O
satisfy	O
everyone	O
who	O
might	O
want	O
to	O
use	O
the	O
technology	O
,	O
as	O
what	O
may	O
seem	O
"	O
reasonable	O
"	O
for	O
a	O
company	O
preparing	O
a	O
proprietary	O
commercial	O
software	O
product	O
may	O
seem	O
much	O
less	O
reasonable	O
for	O
a	O
free	B-Application
software	I-Application
or	O
open	B-Application
source	I-Application
project	O
.	O
</s>
<s>
At	O
least	O
one	O
significant	O
compression	B-General_Concept
software	I-General_Concept
program	O
,	O
bzip2	B-Application
,	O
deliberately	O
discontinued	O
the	O
use	O
of	O
arithmetic	B-Algorithm
coding	I-Algorithm
in	O
favor	O
of	O
Huffman	B-General_Concept
coding	I-General_Concept
due	O
to	O
the	O
perceived	O
patent	O
situation	O
at	O
the	O
time	O
.	O
</s>
<s>
Also	O
,	O
encoders	O
and	O
decoders	O
of	O
the	O
JPEG	O
file	O
format	O
,	O
which	O
has	O
options	O
for	O
both	O
Huffman	B-General_Concept
encoding	I-General_Concept
and	O
arithmetic	B-Algorithm
coding	I-Algorithm
,	O
typically	O
only	O
support	O
the	O
Huffman	B-General_Concept
encoding	I-General_Concept
option	O
,	O
which	O
was	O
originally	O
because	O
of	O
patent	O
concerns	O
;	O
the	O
result	O
is	O
that	O
nearly	O
all	O
JPEG	O
images	O
in	O
use	O
today	O
use	O
Huffman	B-General_Concept
encoding	I-General_Concept
although	O
JPEG	O
's	O
arithmetic	B-Algorithm
coding	I-Algorithm
patents	O
have	O
expired	O
due	O
to	O
the	O
age	O
of	O
the	O
JPEG	O
standard	O
(	O
the	O
design	O
of	O
which	O
was	O
approximately	O
completed	O
by	O
1990	O
)	O
.	O
</s>
<s>
JPEG	O
XL	O
,	O
as	O
well	O
as	O
archivers	O
like	O
PackJPG	O
,	O
Brunsli	O
and	O
Lepton	O
,	O
that	O
can	O
losslessly	B-Algorithm
convert	O
Huffman	O
encoded	O
files	O
to	O
ones	O
with	O
arithmetic	B-Algorithm
coding	I-Algorithm
(	O
or	O
asymmetric	B-General_Concept
numeral	I-General_Concept
systems	I-General_Concept
in	O
case	O
of	O
JPEG	O
XL	O
)	O
,	O
showing	O
up	O
to	O
25%	O
size	O
saving	O
.	O
</s>
<s>
The	O
JPEG	O
image	B-General_Concept
compression	I-General_Concept
format	O
's	O
arithmetic	B-Algorithm
coding	I-Algorithm
algorithm	O
is	O
based	O
on	O
the	O
following	O
cited	O
patents	O
(	O
since	O
expired	O
)	O
.	O
</s>
<s>
Other	O
patents	O
(	O
mostly	O
also	O
expired	O
)	O
related	O
to	O
arithmetic	B-Algorithm
coding	I-Algorithm
include	O
the	O
following	O
.	O
</s>
<s>
The	O
Dirac	B-Application
codec	I-Application
uses	O
arithmetic	B-Algorithm
coding	I-Algorithm
and	O
is	O
not	O
patent	O
pending	O
.	O
</s>
<s>
Patents	O
on	O
arithmetic	B-Algorithm
coding	I-Algorithm
may	O
exist	O
in	O
other	O
jurisdictions	O
;	O
see	O
software	O
patents	O
for	O
a	O
discussion	O
of	O
the	O
patentability	O
of	O
software	O
around	O
the	O
world	O
.	O
</s>
<s>
Every	O
programmatic	O
implementation	O
of	O
arithmetic	B-Algorithm
encoding	I-Algorithm
has	O
a	O
different	O
compression	O
ratio	O
and	O
performance	O
.	O
</s>
