# PCA for three-dimensional linear fit on time-resolved trajectory

I study the behavior of organisms that are able of self-locomotion and that show directed motion toward one another. This directed motion occurs through the detection of chemical trails released by the organisms as they move. These trails are detected and followed by other organisms nearby.

An encounter occurs when the organism that follows the trail is within a small separation distance from the organism that releases the trail. In this case, the direction vectors of the two organisms v$_a$ and v$_b$ are more or less aligned and their dot product is positive. An encounter may also occur via head-on collision, as shown in the figure below. In this case, two particles collide because of their random motion only and the dot product of v$_a$ and v$_b$ is negative. My goal is to quantify the contribution of head-on collisions and trail-assisted collisions in the total number of encounters.

Because of measurement noise and also because these organisms have a jerky motion composed of short segments separated by sharp turns, it is not straightforward to define the direction vector of an organism prior to encounter. A relatively robust method to get the general orientation of the motion is to fit a straight line to the last segment of the trajectory via principal component analysis. I compute the eigenvectors of the covariance matrix of the centered coordinates (or directly the right singular vectors of the centered coordinates) and I select the eigenvector that is associated with the largest eigenvalue.

While this gives the orientation of the motion, it does not give the direction, since the sign of the eigenvectors is arbitrary determined during the decomposition. Obviously, eigenvectors are not unique and by definition, multiplying by any constant, including minus one, gives another valid eigenvector.

However, this prevents the determination of reliable direction vectors. For instance, for the two trajectories in the figure below, the direction vectors (in black) given by the decomposition point in the wrong direction.

How can I obtain direction vectors that are consistently pointing in the direction of motion? In other words, it is possible to use the time information of the Lagrangian trajectories to retrieve the correct sign of the eigenvectors?

Many thanks.

This figure may help to understand the problem.

• Why don't you proceed as follows? Take the pairwise difference of direct successors. Then you get three differences (X, Y and Z). If the particle moves 'more or less into the same direction' at the end of its trajectory then these differences should be normally distributed around their mean. Hence, you take usual estimator, namely the empirical mean. Maybe in the calculation of that mean you need to put a little more weight onto the very last part than the one at the beginning (because the particle might still change its direction rapidly in an 'early stage' of the last part). – Fabian Werner Aug 17 '18 at 9:56
• Alternatively you can just directly do three separate linear regressions on the X, Y and Z components (with time as input feature), i.e. you get three slopes a, b, c which then together form the vector of the direction. I have done this (in a more abstract way) with aircrafts and it worked out quite well. – Fabian Werner Aug 17 '18 at 9:58
• Thank you! Fitting three separate robust regressions on the components of the last segment works very well. I got overwhelmed by this principal component thing and completely missed this obvious solution. – Ironclad Lunatic Aug 19 '18 at 10:13