# Linearization and Weights

This paper is still under construction. It is not in publishable form yet, but I am open for suggestions.

[This chapter is going away... It is being split into separate chapter to cover weights and a chapter to cover linearization. Linearization will be moved to the "Shot Preprocessing" Chapter.

# Linearization

The data we start with in a cave survey is generally something like "Distance, Azimuth, and Inclination". I'm not aware of any program anywhere that can solve the adjustment problem directly with this information without making some simplifying assumptions.

So, the data is normally "reduced" to vectors that record change in (X, Y, Z) for the shot before attempting any sort of adjustment.

This is such an obvious step for everyone that no real thought is normally put into it. Anybody with a little trigonometry can derive the proper transformations, and even the inverse transformations.

Since the inverse transformations exist, cave surveyors (and some real surveyors) usually have the "gut feel" that this transformation makes no difference to the problem.

If all we were doing was just plotting the data, this transformation makes no difference and peoples' intuition is right.

Why this changes the problem

Standard Least squares techniques assume that the problem is linear. This means that if the data is close to the original problem, the solution is going to be close to the solution of the original problem.

*HOWEVER* if the data is NOT close to the original problem the solution can be VERY far from the actual solution.

If we are planning to adjust the data, then the transformation suddenly makes a BIG difference.

The exact weighting also makes a difference. If the data is far from the actual data (for example if there are blunders) then the weighting is also going to be far from the original data.

# Covariances

The critical question for a reasonable adjustment is: how much slop do we have in what we are adjusting? The mathematics community has a convenient measure of this called "Variance". When it relates several variables it is called "Covariance". [Variance is (conveniently) standard deviation squared.]

If there are (for example) three variables that we are relating, the covariance matrix is three by three. If the variables are unrelated, then the off diagonal elements of the matrix are zero.

One expects that any covariance matrix taken from real life is a symmetric matrix.

[EXPAND WITH EXAMPLES](Consider DAI or DAG as uncorrelated, but derive full correlated version for the code.)

There are other measures of the slop in measurements, such as "standard deviation". [Which turns out to be the square root of the variance.] But variances are used for survey's because it has very nice computational properties. For example, if you have two connected shots, with covariance matrices A and B, the covariance matrix of the combination of them is just A+B. (If we were doing weight matrices A and B, the result would have to be 1/(1/A+1/B) which is much uglier. If we were dealing with standard deviations, the expression would be sqrt(A*A + B*B).

# Computing Covariances

[Any statistics book doing multivariant statistics and transformations will cover this.]

If we have a transformation that transforms (X,Y,Z) = TRANS(D,A,I) then we can compute a good approximation to what it does with the variances with the expression:

```[covariance matrix of (XYZ)]
=
[Jacobian of TRANS]
* [covariance matrix of DAI]
* [Transpose of Jacobian of TRANS]
```

This approximation is what is called a "linear approximation" to the correct values. What this means practically is that for values that are close to our given values we compute covariances that are close to the real values.

Jacobians of transformations are easily computed with a bit of calculus, which makes them a bit uglier than the basic transformations, but still within the reach of anyone that has had calculus.

[We will use the symbols P(g,x) for Partial Derivative of g with respect to x ]

The Jacobian of a transformation T that takes (A, B, C...) into (X, Y, Z, ....) is just:

```[ P(X,A)  P(X,B)  P(X,C) ... ]
[ P(Y,A)  P(Y,B)  P(Y,C) ... ]
[ P(Z,A)  P(Z,B)  P(Z,C) ... ]
[    .       .       .   ... ]
[    .       .       .   ... ]
[    .       .       .   ... ]
```

# Divers Transformations

Cave divers usually carry fairly accurate depth gauges. For this reason it is easier for them to record Distance, Azimuth, Depth. (We'll call depth "Gauge" here so we don't have two variables abbreviating to the same letter)

The obvious transformation of this DAG to (X, Y, Z) is:

```X =      sine(A) * sqrt(D*D-G*G)
Y =    cosine(A) * sqrt(D*D-G*G)
Z = D

X =   sine(A) * D * (cos(arcsine(G/D)))
Y = cosine(A) * D * (cos(arcsine(G/D)))
Z = G
```

The situation is actually more complex than this for the divers, because what they are measuring is not the change in depth for the shot, but the actual Z value.

# Standard Transformations

The most usual data recorded in a cave survey is the Distance between one survey point and the next, the Inclination between the points, and the Azimuth between them. (Often referred to as DAI information in the US.)

The obvious transformation of this DAI to (X, Y, Z) is:

```X =   sine(A) * cosine(I) * D
Y = cosine(A) * cosine(I) * D
Z =               sine(I) * D
```

So the Jacobian matrices of our standard transformations are:

J(DAI -> XYZ):

```[ P(sin(A)*cos(I)*D,D)  P(sin(A)*cos(I)*D,A)  P(sin(A)*cos(I)*D,I) ]
[ P(cos(A)*cos(I)*D,D)  P(cos(A)*cos(I)*D,A)  P(cos(A)*cos(I)*D,I) ]
[        P(sin(I)*D,D)         P(sin(I)*D,A)         P(sin(I)*D,I) ]
```

which a little calculus reduces to

```[  sin(A)*cos(I)         cos(A)*cos(I)*D     -sin(A)*sin(I)*D ]
[  cos(A)*cos(I)        -sin(A)*cos(I)*D     -cos(A)*sin(I)*D ]
[         sin(I)        0                            cos(I)*D ]
```

(relax, you don't need calculus to use it now that it is derived.)

*NOTE* The partial derivatives here were computed assuming A and I were already converted to radians. (Important later.)

Similarly with our other transformations.

********** However **********

As this sits Jacobian has some problems. For vertical shots it is not full rank, and can lead to degenerate variance matrices.

# Weights

[SHOULD THIS BE FIRST, AND VARIANCES THE DERIVED CONCEPT?]

People (at least in this culture) have an intuitive idea called "weight". Things that are more accurate have higher weight, and things that are less accurate have lower weight. Mathematicians have carefully defined "mathematical weights" that match the primitive intuitions. Fortunately for us, weights turn out to be the inverse of variances. Without going into the details, weight matrices turn out to be the inverse of variance matrices.

# Code

[What part of the library should I release here?]

# Summary

What we started with: A priori estimates of the accuracy of the instruments, a collection of survey shots.

What we ended with: (XYZ) information for the shots, and a covariance matrix for the shot. (Which is can be converted to having a weight matrix for the shot.)

# Examples

Let us assume a variance of 1 degree for angles, and 0.01 meters for distance measurements. We will assume, for the sake of demonstration, that all the measurements are uncorrelated. (This means that the distance measurements, for example, don't make any difference to the angle measurements.) [Note that one degree is Pi/180 radians = roughly 0.017 radians]

This gives a DAI covariance matrix of:

```       D     A      I
D  [ 0.01  0.0    0.0   ]
A  [ 0.0   0.017  0.0   ]
I  [ 0.0   0.0    0.017 ]
```

Since the Jacobian was computed with angles assumed in radians, we have to do the variances for it as radians also.

We could have done the Jacobian with angles in degrees, but the resulting matrix would have been quite ugly... It is easier to keep our transformations simple, and just convert angles. (Besides, it would be very ugly to have to compute Jacobians for degrees, gradians, mils, etc.]

## North

(D,A,I) = (10, 0, 0)

(DX, DY, DZ) = (0, 10, 0)

the Jacobian is: [I need to check this...]

```D  A  I
D [ 0 10  0 ]
A [ 1  0  0 ]
I [ 0  0 10 ]
```

Giving a covariance matrix of:

```[ 0 10  0 ]   [ 0.010 0     0     ]   [  0  1  0 ]
[ 1  0  0 ] * [ 0     0.017 0     ] * [ 10  0  0 ]  =
[ 0  0 10 ]   [ 0     0     0.017 ]   [  0  0 10 ]

X    Y    Z
X [ 1.7  0    0    ]
Y [ 0    0.10 0    ]
Z [ 0    0    1.7  ]
```

*NOTE* Remember that these are Variances, which are the squares of standard deviations.

*NOTE* Remember also that these are the result of linearizing the equations. These are therefore close for values close to what we were given, but will be far off for values far off of what we were given.

The magnitude of this matrix is roughly 0.030462.

The weight matrix is the inverse of the covariance matrix, and approximately

```[ 5.88  0   0    ]
[ 0    10.0 0    ]
[ 0     0   5.88 ]
```

Which has a magnitude of about 345.7

## North East

(D,A,I) = (10, 45, 0)

(DX, DY, DZ) = (7.1, 7.1 ,0) [Rounded to one digit after the decimal point.]

Giving a covariance matrix of:

```     X    Y     Z
X [  0.88 -0.87 0.00 ]
Y [ -0.87  0.88 0.00 ]
Z [  0.00  0.00 1.75 ]
```

And if computed reasonably exactly, this also has the same magnitude as the variance for the north shot (about 0.03). Since the magnitude of the covariances is the same the magnitude of the weights will also be the same.

Common sense says that the magnitude of the total uncertainty in a shot should be independent of what direction it points, and that the uncertainty of each axis depends on the orientation, and is NOT independent of the uncertainty in adjoining axes.

## Counter Example

As a counter example to the thought that X, Y, and Z can be taken totally independently, let us consider what happens if we take this as independent adjustments in X, Y, and Z. This is equivalent to zeroing the off diagonal elements in the example above. This gives

```     X    Y     Z
X [  0.88  0.00 0.00 ]
Y [  0.00  0.88 0.00 ]
Z [  0.00  0.00 1.75 ]
```

We've now thrown away several terms that are almost as large as the ones we've kept. We've also thrown away the fact that varying the azimuth affects BOTH X and Y in a related manner instead of an independent manner.

The magnitude of the matrix is now about 1.35 instead of about 0.03, which is DRAMATICLY different than it was, with the change in the final weight being just as dramatic. (Roughly 0.74 magnitude instead of about 32)

# Go to ...

```This page is http://www.cc.utah.edu/~nahaj/cave/survey/intro/linearizing-and-weights.html