Exercise 2.15 of SICP

Exercise 2.15: Eva Lu Ator, another user, has also noticed the different intervals computed by different but algebraically equivalent expressions. She says that a formula to compute with intervals using Alyssa’s system will produce tighter error bounds if it can be written in such a form that no variable that represents an uncertain number is repeated. Thus, she says, par2 is a “better” program for parallel resistances than par1. Is she right? Why?

> (print (div-interval (make-center-percent 6.8 1) (make-center-percent 6.8 1)))
[1.0002000200020003,1.9998000199979908]
> (print (div-interval (make-center-percent 6.8 .1) (make-center-percent 6.8 .1)))
[1.000002000002,.19999980000021655]

> (print (div-interval (make-center-percent 6.8 .1) (make-center-percent 3.4 .1)))
[2.000004000004,.19999980000021655]
> (print (div-interval (make-center-percent 6.8 .1) (make-center-percent 1.7 .1)))
[4.000008000008,.19999980000021655]
> (print (div-interval (make-center-percent 6.8 .1) (make-center-percent 1.7 .01)))
[4.000000440000004,.1099999890000035]

> (print (par1 (make-center-percent 6.8 .1) (make-center-percent 1.7 .01)))
[1.3600022771855311,.19199981581619266]
> (print (par2 (make-center-percent 6.8 .1) (make-center-percent 1.7 .01)))
[1.3599998237438813,.028000014256019334]

> (print (par1 (make-center-percent 6.8 .1) (make-center-percent 6.8 .1)))
[3.4000136000136,.2999992000024115]
> (print (par2 (make-center-percent 6.8 .1) (make-center-percent 6.8 .1)))
[3.4000000000000004,.10000000000000857]

> (print (par1 (make-center-percent 6.8 10) (make-center-percent 4.7 5)))
[2.844199964577264,22.613352145193346]
> (print (par2 (make-center-percent 6.8 10) (make-center-percent 4.7 5)))
[2.777440701636504,7.05260392723452]

Alyssa’s system will produce tighter error bounds. The errors are a lot tighter for par2 than par1. The reason that this is true is due to the fact that any operation between two intervals will increase errors. If two algebraically equivalent statements are evaluated, the statement with the least operations between intervals will produce less errors.

Here’s a slightly exaggerated example:
A = 10 \pm 0.1
A = \frac{A^7}{A^6} = \frac{A \cdot A \cdot A \cdot A \cdot A \cdot A \cdot A}{A \cdot A \cdot A \cdot A \cdot A \cdot A}

> (define A (make-center-percent 10 1))
> (print A)
[10.,.9999999999999963]
> (print (div-interval (interval-pow A 7) (interval-pow A 6)))
[10.084120477031101,12.92768447766068]

The most noticeable difference is between percent errors of 1% and 13%.

Exercise 2.14 of SICP

Exercise 2.14: Demonstrate that Lem is right. Investigate the behavior of the system on a variety of arithmetic expressions. Make some intervals A and B, and use them in computing the expressions A/A and A/B. You will get the most insight by using intervals whose width is a small percentage of the center value. Examine the results of the computation in center-percent form (see exercise 2.12).

> (print (div-interval (make-center-percent 6.8 1) (make-center-percent 6.8 1)))
[1.0002000200020003,1.9998000199979908]
> (print (div-interval (make-center-percent 6.8 .1) (make-center-percent 6.8 .1)))
[1.000002000002,.19999980000021655]

> (print (div-interval (make-center-percent 6.8 .1) (make-center-percent 3.4 .1)))
[2.000004000004,.19999980000021655]
> (print (div-interval (make-center-percent 6.8 .1) (make-center-percent 1.7 .1)))
[4.000008000008,.19999980000021655]
> (print (div-interval (make-center-percent 6.8 .1) (make-center-percent 1.7 .01)))
[4.000000440000004,.1099999890000035]

> (print (par1 (make-center-percent 6.8 .1) (make-center-percent 1.7 .01)))
[1.3600022771855311,.19199981581619266]
> (print (par2 (make-center-percent 6.8 .1) (make-center-percent 1.7 .01)))
[1.3599998237438813,.028000014256019334]

> (print (par1 (make-center-percent 6.8 .1) (make-center-percent 6.8 .1)))
[3.4000136000136,.2999992000024115]
> (print (par2 (make-center-percent 6.8 .1) (make-center-percent 6.8 .1)))
[3.4000000000000004,.10000000000000857]

> (print (par1 (make-center-percent 6.8 10) (make-center-percent 4.7 5)))
[2.844199964577264,22.613352145193346]
> (print (par2 (make-center-percent 6.8 10) (make-center-percent 4.7 5)))
[2.777440701636504,7.05260392723452]

Exercise 2.13 of SICP

Exercise 2.13: Show that under the assumption of small percentage tolerances there is a simple formula for the approximate percentage tolerance of the product of two intervals in terms of the tolerances of the factors. You may simplify the problem by assuming that all numbers are positive.

(a-\epsilon_1,a+\epsilon_1) \cdot (b-\epsilon_2,b+\epsilon_2)
(a+\epsilon_1)\cdot(b+\epsilon_2)=ab+a\epsilon_2+b\epsilon_1+\epsilon_1\epsilon_2
(a+\epsilon_1)\cdot(b-\epsilon_2)=ab-a\epsilon_2+b\epsilon_1-\epsilon_1\epsilon_2
(a-\epsilon_1)\cdot(b+\epsilon_2)=ab+a\epsilon_2-b\epsilon_1-\epsilon_1\epsilon_2
(a-\epsilon_1)\cdot(b-\epsilon_2)=ab-a\epsilon_2-b\epsilon_1+\epsilon_1\epsilon_2

Since a>0 and b>0:
(a+\epsilon_1)\cdot(b+\epsilon_2) \approx ab+ a\epsilon_2+b\epsilon_1+\epsilon_1\epsilon_2
(a-\epsilon_1)\cdot(b-\epsilon_2) \approx ab -a\epsilon_2-b\epsilon_1+\epsilon_1\epsilon_2
Assuming \epsilon_1 and \epsilon_2 are small, the quantity \epsilon_1\epsilon_2 can be discarded.
(a+\epsilon_1)\cdot(b+\epsilon_2) \approx ab+a\epsilon_2+b\epsilon_1
(a-\epsilon_1)\cdot(b-\epsilon_2) \approx ab-a\epsilon_2-b\epsilon_1

Since we are interested in the tolerance terms:
(a+\epsilon_1)\cdot(b+\epsilon_2) \approx a\epsilon_2+b\epsilon_1
(a-\epsilon_1)\cdot(b-\epsilon_2) \approx -a\epsilon_2-b\epsilon_1

The new product interval in terms of small tolerance terms looks like this:
[ab-(a\epsilon_2+b\epsilon_1),ab+(a\epsilon_2+b\epsilon_1)]

Exercise 2.12 of SICP

Exercise 2.12: Define a constructor make-center-percent that takes a center and a percentage tolerance and produces the desired interval. You must also define a selector percent that produces the percentage tolerance for a given interval. The center selector is the same as the one shown above.

> (print (make-center-percent 10 12))
[8.8,11.2]
> (percent (make-center-percent 10 12))
11.999999999999993

Exercise 2.11 of SICP

Exercise 2.11: In passing, Ben also cryptically comments: “By testing the signs of the endpoints of the intervals, it is possible to break mul-interval into nine cases, only one of which requires more than two multiplications.” Rewrite this procedure using Ben’s suggestion.

The assumption made here is that for any interval [x_1,x_2] : x_1 \le x_2
Using this assumption here are the 9 valid signs combinations.

Notice that the case of 3 will need a little extra logic since it is possible for the two negative lower bounds to multiply together and become larger than the two positive upper bounds.

> (print (mul-interval (make-interval 1 2) (make-interval 3 4)))
[3,8]
> (print (mult-interval (make-interval 1 2) (make-interval 3 4)))
[3,8]
> (print (mult-interval (make-interval -2 -1) (make-interval -4 -3)))
[3,8]
> (print (mul-interval (make-interval -2 -1) (make-interval -4 -3)))
[3,8]
> (print (mul-interval (make-interval -2 1) (make-interval -4 -3)))
[-4,8]
> (print (mult-interval (make-interval -2 1) (make-interval -4 -3)))
[-4,8]

I suspect this problem is given not because it’s an expert systems programmer’s way of solving it but for the interval arithmetic to sync in about always maximizing the width of the new interval whenever possible.

Exercise 2.10 of SICP

Exercise 2.10: Ben Bitdiddle, an expert systems programmer, looks over Alyssa’s shoulder and comments that it is not clear what it means to divide by an interval that spans zero. Modify Alyssa’s code to check for this condition and to signal an error if it occurs.

If an interval I = [-2,2] spans 0, the inverse, I-1=[0.5,-0.5] which is wrong since the lower limit cannot be greater than the upper limit.

> (div-interval (make-interval 1 1) (make-interval -2 2))
*** ERROR IN (console)@76.1 — The dividing interval cannot span 0.

Exercise 2.9 of SICP

Exercise 2.9: The width of an interval is half of the difference between its upper and lower bounds. The width is a measure of the uncertainty of the number specified by the interval. For some arithmetic operations the width of the result of combining two intervals is a function only of the widths of the argument intervals, whereas for others the width of the combination is not a function of the widths of the argument intervals. Show that the width of the sum (or difference) of two intervals is a function only of the widths of the intervals being added (or subtracted). Give examples to show that this is not true for multiplication or division.

(a-\epsilon_1,a+\epsilon_1) \pm (b-\epsilon_2,b+\epsilon_2)
 w_a=\frac{1}{2}(a+\epsilon_1-(a-\epsilon_1))=\epsilon_1
w_b=\frac{1}{2}(b+\epsilon_2-(b-\epsilon_2))=\epsilon_2

Addition:

(a-\epsilon_1,a+\epsilon_1)+(b-\epsilon_2,b+\epsilon_2)
\left((a+b)-(\epsilon_1+\epsilon_1),(a+b)+(\epsilon_1+\epsilon_1))\right)
 w_{a+b}=\epsilon_1+\epsilon_2=w_a+w_b

Subtraction:

(a-\epsilon_1,a+\epsilon_1)-(b-\epsilon_2,b+\epsilon_2)
\left((a-b)-(\epsilon_1+\epsilon_2),(a-b)+(\epsilon_1+\epsilon_2)\right)
 w_{a-b}=\epsilon_1+\epsilon_2= w_a+w_b

Multiplication:

(a-\epsilon_1,a+\epsilon_1) \cdot (b-\epsilon_2,b+\epsilon_2)
  1. (a+\epsilon_1)\cdot(b+\epsilon_2)=ab+a\epsilon_2+b\epsilon_1+\epsilon_1\epsilon_2
  2. (a+\epsilon_1)\cdot(b-\epsilon_2)=ab-a\epsilon_2+b\epsilon_1-\epsilon_1\epsilon_2
  3. (a-\epsilon_1)\cdot(b+\epsilon_2)=ab+a\epsilon_2-b\epsilon_1-\epsilon_1\epsilon_2
  4. (a-\epsilon_1)\cdot(b-\epsilon_2)=ab-a\epsilon_2-b\epsilon_1+\epsilon_1\epsilon_2

If any of the four intervals expressions are used as interval endpoints their width formula will not give the tidy results of addition/subtraction.

Division:

(a-\epsilon_1,a+\epsilon_1) \cdot (\frac{1}{b+\epsilon_2},\frac{1}{b-\epsilon_2})
  1. (a-\epsilon_1)\cdot\frac{1}{b+\epsilon_2}=\frac{a-\epsilon_1}{b+\epsilon_2}
  2. (a+\epsilon_1)\cdot\frac{1}{b+\epsilon_2}=\frac{a+\epsilon_1}{b+\epsilon_2}
  3. (a-\epsilon_1)\cdot\frac{1}{b-\epsilon_2}=\frac{a-\epsilon_1}{b-\epsilon_2}
  4. (a+\epsilon_1)\cdot\frac{1}{b-\epsilon_2}=\frac{a+\epsilon_1}{b-\epsilon_2}

Examples:

w1=0.68
w2=0.235
wtotal=0.915
> (print (add-interval (make-interval 6.12 7.48) (make-interval 4.465 4.935)))
[10.585,12.415]
wactual=0.915

w1=0.68
w2=0.235
wtotal=0.915
> (print (sub-interval (make-interval 6.12 7.48) (make-interval 4.465 4.935)))
[1.1850000000000005,3.0150000000000006]
wactual=0.915 (ignoring the floating point number errors)

w1=0.68
w2=0.235
wnaive_total=0.15980
Of course this doesn’t make sense since accuracy cannot be magically gained by multiplying two interval together. More on this in the division example.
> (print (mul-interval (make-interval 6.12 7.48) (make-interval 4.465 4.935)))
[1.1850000000000005,3.0150000000000006]
wactual=4.794

w1=0.68
w2=0.235
wnaive_total=2.89361702127659574468
> (print (div-interval (make-interval 6.12 7.48) (make-interval 4.465 4.935)))
[1.2401215805471126,1.6752519596864504]
wactual=.2175651895696689
How is it that wactual is smaller than each w1, w2? wnaive_total is just plain wrong and should be discarded.

The answer is that wactual isn’t smaller. It’s actually larger. The percentage of error that wactual represents, for it’s interval, is actually greater than either w1=10% and w2=5%. wactual = 14.9%.
6.8 \pm 0.68
0.68 = \frac{0.68}{6.8}=10\%
4.7 \pm 0.235
0.235 = \frac{0.235}{4.7}=5\%
0.2175651895696689 = \frac{.2175651895696689}{\left(\frac{1}{2}\cdot(1.2401215805471126+1.6752519596864504)\right)}=14.9\%

As expected the error is actually higher even though the aggregate number for wactual looks smaller.

Exercise 2.8 of SICP

Exercise 2.8: Using reasoning analogous to Alyssa’s, describe how the difference of two intervals may be computed. Define a corresponding subtraction procedure, called sub-interval.

In the resistor example, the resistor is measured at 6.8 ohm with a tolerance of 10%. This means the actual value is 6.8 \pm 0.68, which makes the interval [6.12 , 7.48]. The other resistor is 4.7 \pm 0.235 with the interval of [4.465 , 4.935]. Naively subtracting the two intervals would give, [1.655 , 2.545]. Which doesn’t seem wrong but it is. It is wrong because we are biasing the tolerance in one direction for both measurements. We assume that the first value is 6.8-0.68 and second 4.7-0.235. There is no basis to assume that the following case isn’t just as valid: 6.8-0.68 and 4.7+0.235.
It’s even clearer with the interval of 3 \pm 1 [2 , 4] and 2 \pm 1 [1 , 3]. Naively subtracting these terms yields an interval of 0 length, [1,1] or 1 \pm 0 which is nonsense.
In order to make sure no tolerance bias exists that makes calculations seem more accurate than is possible, the worst case measurement error must be assumed. The intervals will become as wide as possible to not give a false sense of accuracy.

> (print (sub-interval (make-interval 6.12 7.48) (make-interval 4.465 4.935)))
[1.1850000000000005,3.0150000000000006]

Exercise 2.7 of SICP

Exercise 2.7: Alyssa’s program is incomplete because she has not specified the implementation of the interval abstraction. Here is a definition of the interval constructor:

Define selectors upper-bound and lower-bound to complete the implementation.

Exercise 2.6 of SICP

Exercise 2.6: In case representing pairs as procedures wasn’t mind-boggling enough, consider that, in a language that can manipulate procedures, we can get by without numbers (at least insofar as nonnegative integers are concerned) by implementing 0 and the operation of adding 1 as

This representation is known as Church numerals, after its inventor, Alonzo Church, the logician who invented the calculus.

Define one and two directly (not in terms of zero and add-1). (Hint: Use substitution to evaluate (add-1 zero)). Give a direct definition of the addition procedure + (not in terms of repeated application of add-1).

In order to check these I will use the fact that Church numerals appear to be repeated function composition.
If I use the familiar inc function and pass an argument of 0 I should get the number I am looking for:

> ((one inc) 0)
1
> ((two inc) 0)
2
> (((add-1 two) inc) 0)
3

Now that I am convinced that my functions work it’s time to move on and create an addition function.

Now to check that it works properly:
> (((add one one) inc) 0)
2
> (((add two one) inc) 0)
3
> (((add two two) inc) 0)
4

Just for fun multiply looks like this: