# Why doesn’t mean square error work in case of angular data?

Suppose, the following are the first few lines from a dataset for solving a regression problem:

H   7.042 5.781 5.399  -9.118   5.488  7.470     0 0 0 1 0 1 1 3 0 0 0 1 0 0 0 0 0 0 0 0    2.144
H   5.781 5.399 5.373   5.488   5.166  6.452     0 1 1 3 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0    2.268
H   5.399 5.373 5.423   5.166   4.852  6.069     0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0    2.101
H   5.373 5.423 5.247   4.852   5.164  6.197     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0    2.222
H   5.423 5.247 5.485   5.164   4.943  6.434     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0    2.416
C   5.247 5.485 6.675   4.943   8.103  8.264     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0    3.028
C   5.485 6.675 6.372   8.103  -9.152  9.047     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0    -1.235
C   6.675 6.372 5.669  -9.152  -8.536 11.954     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0    -0.953
H   6.372 5.669 5.304  -8.536   5.433  6.703     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0    2.233
H   5.669 5.304 5.461   5.433   4.924  6.407     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0    2.313


The left-most column is the class data. The right-most column is Y data. The rest of the features are all angular data.

.

My initial setup for the model was as follows:

def create_model(n_hidden_1, n_hidden_2, num_features):
# create the model
model = Sequential()

# instantiate the optimizer

# compile the model
model.compile(
loss="mean_squared_error",
optimizer=opt,
metrics=["mean_squared_error"]
)

# return model
return model


This model didn’t produce the correct outcome. The expected output should be around $$0.200.20$$. However, the current setup is producing around $$0.800.80$$. These are radian values.

Someone told me that MSE doesn’t work in the case of angular data. So, I need to use a custom output layer and a custom error function.

Why doesn’t mean square error work in the case of angular data?

How can I solve this issue?

The problem with using MSE directly on angle values is that values can be very close together on the circle, but will have a large square error. For example, 359 degrees and 1 degrees are very close together on a circle, but very far apart in terms of squared difference.

To rectify this, you should use a distance metric that reflects the fact that you’re working with angles.

The Euclidean distance between two points on a circle is the length of the chord between the two points. So a simple and direct example of a distance considers the angle $$α\alpha$$ as a point on the unit circle $$(cosα,sinα)(\cos \alpha, \sin \alpha)$$, then computes the Euclidean distance in the ordinary way.

Elementary trigonometric identities simplify the expression.

d2E(θ,ϕ)=(cosθ−cosϕ)2+(sinθ−sinϕ)2=cos2θ−2cosθcosϕ+cos2ϕ+sin2θ−2sinθsinϕ+sin2ϕdE(θ,ϕ)=√2−2cos(θ−ϕ)\begin{align} d_E^2 (\theta, \phi) &= (\cos \theta - \cos \phi)^2 + (\sin \theta - \sin \phi)^2 \\ &= \cos^2 \theta - 2 \cos \theta \cos \phi + \cos^2 \phi + \sin^2 \theta - 2 \sin \theta \sin \phi + \sin^2 \phi \\ d_E (\theta, \phi) &= \sqrt{2 - 2 \cos (\theta - \phi) } \end{align}

By inspection, we can see that angles that are very close together have $$cos(θ−ϕ)\cos (\theta - \phi)$$ close to 1, so the distance is close to 0. On the other hand, angles that are very far apart have $$cos(θ−ϕ)\cos (\theta - \phi)$$ close to 0, so the distance is close to 1. This satisfies our intuitive notions of distance.

Of course, this is not the only option. This distance and some alternatives are discussed in these related threads: