Trajectories of moving objects provide crucial clues for video event analysis especially in surveillance applications. In this paper, we study the problem of detecting anomalous events by analyzing the motion trajectories in videos. Different trajectories of the same category may have varying relative velocities, in addition to the variations and noises in location samples; hence the core of the problem is to provide a robust and accurate function for measuring the similarities of trajectory pairs. We propose a novel learning based algorithm for estimating the similarities of the multi-dimensional sequence pairs, and then an anomaly detection framework is presented to detect anomalous motion trajectories in surveillance videos. Our proposed algorithm offers several advantages over the traditional algorithms for dealing with the trajectories of moving objects. First, the similarity measurement is robust against data imperfections such as noise, algorithmic error and etc. Second, we introduce a learning algorithm which allows the similarity function to be adapted to the particular problems being solved. Third, the proposed anomaly detection framework is fully automatic and without parametric distribution assumption on the data. The experiments on both outdoor and indoor surveillance videos validate the effectiveness of our proposed framework in detecting anomalous trajectories.