> For the complete documentation index, see [llms.txt](https://help.imotions.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.imotions.com/lab/analysis/eye-tracking/fixation-classifiers/fixation-and-saccade-classification-with-hidden-markov-models.md).

# Fixation and saccade classification with Hidden Markov Models

Understand how Hidden Markov Models classify gaze samples into fixations and saccades, and which settings and exports iMotions Lab offers for this classifier.

Hidden Markov Models are an alternative to the [I-VT filter](https://help.imotions.com/lab/analysis/eye-tracking/fixation-classifiers/fixation-and-saccade-classification-with-the-i-vt-filter.md) and Duration dispersion filter. Hidden Markov Models implement machine learning and are more robust to individual levels of noise in eye tracking data, which is why they are the default filter for [remote data collection](https://help.imotions.com/lab/remote-data-collection/getting-started-with-online-data-collection.md) studies with [webcam-based eye tracking](https://help.imotions.com/lab/remote-data-collection/getting-started-with-online-data-collection/rdc-testing-with-webcam-eye-tracking.md). For a scientific overview, please see [Salvucci and Goldberg (2000)](https://dl.acm.org/doi/10.1145/355017.355028).

## About Hidden Markov Models

### What are Hidden Markov Models?

Hidden Markov Models are a statistical model for time series data, assuming every sample to be an observation of one of a range of possible states that cannot be observed directly, that is, hidden. In the scenario of fixation classification, it is assumed that every eye tracking sample either represents a data point collected during a fixation or a saccade, with the eye movement pattern assumed to be an unobservable Markov process. Transitioning from one state (the fixation) to the other (the saccade) has a certain probability, which is unknown and needs to be learned by the algorithm from the recorded data.

Since this computation is executed for each individual respondent independent from the other respondents, the algorithm can adapt to individual eye movement patterns and levels of data noise.

### Eye movement classification with Hidden Markov Models

* Data preparation:
  * If the sampling rate of the recording fluctuates, iMotions inserts empty samples where samples are missing, so that the data has a fixed sampling rate. These samples count as missing data.
  * From the binocular gaze data in x and y coordinates for the left and right eye, an average gaze point is computed. If data is missing from one eye, it is replaced with the gaze coordinate from the other eye. If the eye-tracker provides binocular gaze, this signal is used rather than calculating an average gaze point.
  * If gaze or distance data is missing from both eyes, the data gap can be filled with a linear interpolation from the start to the end of the gap. The maximum length for this interpolation can be modified with the parameter **Max Gap Length** in the section **Gap Fill-in (interpolation)**. The interpolation can also be turned off.
  * To smooth out noise in the data, it is possible to apply either a **Moving Average** filter or a **Median** filter inside a moving window around the current sample. The size of this window can be modified with the **Window Size** parameter in the section **Noise Reduction**. This smoothing is optional.
* Signal processing:
  * To compute the gaze velocity at a given sample, the algorithm computes the angular velocity between the first and last sample inside a time window around the current sample. The size of the time window is defined by the **Window Length** parameter in the **Fixation Filter Parameters** section. For this, the distance between the first and last sample inside this time window (in pixels, obtained from their x- and y gaze coordinates) is converted to an angle by taking the respondent's distance to screen into account (as measured by the eye tracker). Dividing this angle by the time lapse between the two samples gives a velocity value in °/s, the velocity computed for the current sample. If a sample inside a time window is missing, no velocity is computed.
  * For Neon by Pupil Labs recordings with eye state data, the angle is instead computed from the 3D gaze direction of the eyes' optical axis, so the velocity reflects the rotation of the eyes. See [Neon by Pupil Labs](#neon-by-pupil-labs) below.
  * Gaze acceleration is computed based on the difference in velocity between the current and its previous sample.
* Machine learning:
  * The entire respondent's recording is then fed into the Hidden Markov Model, implemented using the [HiddenMarkov R package](https://cran.r-project.org/package=HiddenMarkov).
  * In line with Salvucci & Goldberg (2000), the model initially assumes a probability of 0.95 to stay within each state (fixation or saccade) and of 0.05 to transition to the other state.
  * To start out, the model is given 15 °/s as average gaze velocity during a fixation with a standard deviation of 15 °/s, and 60 °/s as average velocity during a saccade with a standard deviation of 30 °/s.
  * These parameters are then optimized using a Baum-Welch algorithm in order to find the unknown parameters of the Hidden Markov Model.
  * A Viterbi algorithm then applies the results of this optimization and classifies whether a sample belonged to a fixation or saccade.
* Corrective steps:
  * Noise in the data can lead to jumps between the gaze points of a fixation, which leads to the classification of a saccade and therefore interruption of the fixation. To correct this, fixations in close temporal or spatial neighborhood can be merged together using the\_Max Time Between Fixations\_and\_Max Angle Between Fixation\_parameters in the\_Merge Adjacent Fixations\_section.
  * Short fixations can be discarded using the\_Minimum Fixation Duration\_parameter in the\_Discard Short Fixations\_section. This can be desired because too short fixations are sometimes regarded as not meaningful when used as an indicator of cognitive processing. Applying this option will compare every classified fixation against the\_Minimum Fixation Duration\_parameter and classify it as an unknown event in case the fixation is shorter than this threshold.
* Computation of fixation metrics:
  * Fixations are numbered from start of the recording to its end, as well as from start of the stimulus to its end. Only fixations with their start and end on the same stimulus get counted as belonging to this stimulus.
  * The start time of the fixation is calculated as the average between the timestamp of the first sample of the fixation and the sample preceding it. The end time of the fixation is the average of the timestamps of the last sample of the fixation and the one succeeding it. The time difference between fixation's start and end times is equal to the fixation's duration.
  * The x- and y coordinates of the fixation's centroid are determined by finding the point with the closest distance to all samples of the fixation.
  * Dispersion of the fixation is calculated with a root mean square of the samples belonging to it.
* Computation of saccade metrics:
  * Saccades are numbered from start of the recording to its end, as well as from start of the stimulus to its end. Only saccades with their start and end on the same stimulus get counted as belonging to this stimulus.
  * The start time of the saccade is calculated as the average between the timestamp of the first sample of the saccade and the sample preceding it. The end time of the saccade is the average of the timestamps of the last sample of the saccade and the one succeeding it. The time difference between saccade's start and end times is equal to the saccade's duration.
  * The gaze locations of the first and last sample belonging to the same saccade are used to compute the amplitude of the saccade.
  * Velocities of all samples belonging to the same saccade are compared, and the highest one is classified as the saccade's peak velocity.
  * Accelerations of all samples belonging to the same saccade are compared, and the highest (positive) value is classified as the saccade's peak acceleration.
  * Accelerations of all samples belonging to the same saccade are compared, and the lowest (negative) value is classified as the saccade's peak deceleration.

### Hidden Markov Models vs. other fixation classification filters

Hidden Markov Models are a machine learning based approach to fixation classification. Other than iMotions' other two filters, the [I-VT](https://help.imotions.com/lab/analysis/eye-tracking/fixation-classifiers/fixation-and-saccade-classification-with-the-i-vt-filter.md) and [Duration Dispersion](https://help.imotions.com/lab/analysis/eye-tracking/fixation-classifiers/fixation-classification-with-the-duration-dispersion-filter.md) filter, which both operate on a hard cut-off either based on velocity ([I-VT](https://help.imotions.com/lab/analysis/eye-tracking/fixation-classifiers/fixation-and-saccade-classification-with-the-i-vt-filter.md)) or spatial dispersion ([Duration Dispersion](https://help.imotions.com/lab/analysis/eye-tracking/fixation-classifiers/fixation-classification-with-the-duration-dispersion-filter.md)), Hidden Markov Models calculate individual parameters for each respondent. This approach makes them an accurate and robust method for fixation classification (Salvucci & Goldberg, 2000), but a less transparent method because the calculated parameters will never be revealed to the end-user.

An internal validation study executed in October 2021 showed very high consistency between the fixation classification of Hidden Markov Models and the I-VT filter for data recorded with standard screen based eye trackers, when executed with iMotions default parameters on three studies recorded on a total of 24 respondents with screen based eye trackers running at 60 and up to 600 Hz. Intraclass Correlation (ICC) of fixation count across 6x6 gridded AOIs was above 0.99 for all studies, indicating excellent reliability. A prediction of I-HMM data from I-VT data through confusion matrices revealed excellent accuracy above 0.94 and Kappa values above 0.8 for all respondents.

Consistency between Hidden Markov Models and the Duration Dispersion filter was somewhat lower. Intraclass Correlations of fixation count across 6x6 gridded AOIs were above 0.93 for the one study tested (15 respondents, 60 Hz screen based eye tracker). Confusion matrices revealed an accuracy of 0.75 or higher when predicting I-HMM data from Duration Dispersion data, and Kappa values of 0.43 or higher.

For the two studies (5 respondents each) recorded with iMotions' web camera based eye tracking algorithm, consistency between Hidden Markov Models and the I-VT filter was at medium levels. Fixation count across 6x6 gridded AOIs indicated good reliability with Intraclass Correlation values of 0.87 or higher, but predicting I-HMM data from I-VT had an accuracy of at least 0.73 with Kappa values ranging between 0.3-0.8 across respondents. Visual inspection of the data revealed that Hidden Markov Models better classified fixations from web camera based eye tracking data. Please note that the typically high levels of noise with low accuracy and precision will not be corrected by any fixation classification filter.

Hence, it is recommended to use Hidden Markov Models for remote data collection studies.

## Settings and export options in iMotions

### Parameters in the "Gaze Analysis" tab at study level:

![mceclip0.png](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-4989ae3d025fdbd3ebf00ced5f93e070cb59d16c%2F1529d1918b_xhx1cgdckwhlurx7x1pamceclip0.webp?alt=media)

* **Fixation Filter**: Option to choose between the Hidden Markov Model based filter explained in this article, the [I-VT filter](https://help.imotions.com/lab/analysis/eye-tracking/fixation-classifiers/fixation-and-saccade-classification-with-the-i-vt-filter.md), and the Duration Dispersion filter.
* **Gap Fill-in (interpolation)**: If selected, missing data is linearly interpolated from the last recorded sample before the data gap until the first recorded sample after the data gap, given the gap is no longer than the indicated maximum gap length. Unless interpolated, samples with missing data will be classified as an unknown eye movement.
* **Noise Reduction**: Smoothes the signal and replaces the coordinates of the current sample with a newly computed value. Please bear in mind that when the noise reduction is applied on data from a low-sampling eye tracker, one risks to filter out the signal.
  * Users can choose between a **Moving Average** or a **Median** filter applied on the samples inside the window. The moving average has a stronger smoothing effect than the median filter.
  * **Window Size**: Determines across how many samples the smoothing shall take place. The more samples are included, the stronger the smoothing.
* **Fixation Filter Parameters**
  * **Window Length**: Size of the time window around the current sample, used to compute the current gaze velocity. Smaller time windows will make the filter more sensitive to rapid changes in the gaze velocity. If the window size is too small, the two neighboring samples are used even if they fall outside the time window.
* **Discard Short Fixations**: Fixations falling below this threshold will be reclassified as an unknown eye movement.
* **Merge Adjacent Fixations**: Fixations in a close temporal or spatial neighborhood can be merged together

### Results:

The result of the fixation classification will be visible in the data replay.

![mceclip2.png](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-db4ba43a68de6f9b23e3bca21db2034e4ebe3ea6%2F4d3376230d_bxj7sttfe0ygc99fzpo9zamceclip2.webp?alt=media)

Depending on the visualization you chose, some parts of the graphs will be shown as a dotted line because there is no data available for this time point (for example if you are plotting the amplitude of a saccade, the time while a fixation was happening will be shown as a dotted line). Therefore, we recommend using the "Area" visualization for Fixation and Saccade visualizations.

The fixation classification will also be used for computing AOI metrics. You can export the AOI metrics in the export tab, as well as detailed statistics about the classified fixations and saccades.

![mceclip0.png](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-4b2d3cd882577f37f0b396674b36fd05d1ac758c%2F6aefcf9cef_5pzw4m4d0a18cfqqhhrw.webp?alt=media)

The eye movement classification can also be exported with the Sensor Data export.

![mceclip3.png](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-3c68e858d10dfcb8ecbd62ac42e63a4f9aad2e4c%2F2c44da482b_8tgqa3gsuwbv7sgagw2jwmceclip3.webp?alt=media)

### Eye movement data in detail:

| **Metric**                 | **Detailed description**                                                                                                                                                                                                                                                                 | **Unit**                           |
| -------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------- |
| Gaze X                     | Gaze data X-coordinate (relative to top-left corner of the screen). Average of the X-coordinates of the left and right eye.                                                                                                                                                              | Pixels (px)                        |
| Gaze Y                     | Gaze data Y-coordinate (relative to top-left corner of the screen). Average of the Y-coordinates of the left and right eye.                                                                                                                                                              | Pixels (px)                        |
| Interpolated Gaze X        | Gaze data X-coordinate (relative to top-left corner of the screen) with missing coordinates interpolated. Average of the X-coordinates of the left and right eye. Linear interpolation from the last sample before the gap until the first sample after the gap.                         | Pixels (px)                        |
| Interpolated Gaze Y        | Gaze data Y-coordinate (relative to top-left corner of the screen)with missing coordinates interpolated. Average of the Y-coordinates of the left and right eye. Linear interpolation from the last sample before the gap until the first sample after the gap.                          | Pixels (px)                        |
| Interpolated Distance      | Estimated distance between the eye-tracker and the eyes, with missing values interpolated. Average of the eye tracker's distance to the left and right eye. Linear interpolation from the last sample before the gap until the first sample after the gap.                               | Millimeters (mm)                   |
| Gaze Velocity              | Angular velocity of the gaze at the current sample point, i.e. how fast the eyes are moving at this point in time. Computation is based on a time window around the current sample. Distance between the first and last sample of this window is divided by the time lapse between them. | Degrees per second (°/s)           |
| Gaze Acceleration          | Angular acceleration of the gaze at the current sample point, i.e. how much the eyes increased in speed at the current sample. Computed as the difference in velocity between the current and previous sample.                                                                           | Degrees per second squared (°/s^2) |
| Fixation Index             | Fixation number, counting from start of the recording                                                                                                                                                                                                                                    | <p><br></p>                        |
| Fixation Index by Stimulus | Fixation number, counting from start of the stimulus. If first or last fixation is partly taking place during the previous or subsequent stimulus, it is excluded.                                                                                                                       | <p><br></p>                        |
| Fixation X                 | X-coordinate of the fixation centroid (relative to top-left corner of the screen)                                                                                                                                                                                                        | Pixels (px)                        |
| Fixation Y                 | Y-coordinate of the fixation centroid (relative to top-left corner of the screen)                                                                                                                                                                                                        | Pixels (px)                        |
| Fixation Start             | Start time of the fixation (relative to recording start), calculated as the average between the timestamp of the first sample of the fixation and the sample preceding it.                                                                                                               | Milliseconds (ms)                  |
| Fixation End               | End time of the fixation (relative to recording start), calculated as the average of the timestamps of the last sample of the fixation and the one succeeding it.                                                                                                                        | Milliseconds (ms)                  |
| Fixation Duration          | Duration of the fixation, calculated as the time difference between fixation's start and end times.                                                                                                                                                                                      | Milliseconds (ms)                  |
| Fixation Dispersion        | Dispersion of the fixation, i.e. how much the fixation's gaze points are spread out. Computed as root mean square of the samples belonging to the fixation.                                                                                                                              | Degrees (°)                        |
| Saccade Index              | Saccade number, counting from start of the recording                                                                                                                                                                                                                                     | <p><br></p>                        |
| Saccade Index by Stimulus  | Saccade number, counting from start of the stimulus. If first or last saccade is partly taking place during the previous or subsequent stimulus, it is excluded.                                                                                                                         | <p><br></p>                        |
| Saccade Start              | Start time of the saccade (relative to recording start), calculated as the average between the timestamp of the first sample of the saccade and the sample preceding it.                                                                                                                 | Milliseconds (ms)                  |
| Saccade End                | End time of the saccade (relative to recording start), calculated as the average of the timestamps of the last sample of the saccade and the one succeeding it.                                                                                                                          | Milliseconds (ms)                  |
| Saccade Duration           | Duration of the saccade, calculated as the time difference between saccade's start and end times.                                                                                                                                                                                        | Milliseconds (ms)                  |
| Saccade Amplitude          | Amplitude of the saccade, i.e. angular distance that the eyes travelled from start point to end point                                                                                                                                                                                    | Degrees (°)                        |
| Saccade Peak Velocity      | Peak velocity of the saccade, i.e. the maximal speed of the eyes during this saccade                                                                                                                                                                                                     | Degrees per second (°/s)           |
| Saccade Peak Acceleration  | Peak acceleration of the saccade, i.e. the maximal increase in speed of the eyes during this saccade                                                                                                                                                                                     | Degrees per second squared (°/s^2) |
| Saccade Peak Deceleration  | Peak deceleration of the saccade, i.e. the maximal decrease in speed of the eyes during this saccade                                                                                                                                                                                     | Degrees per second squared (°/s^2) |
| Saccade Direction          | Direction of the saccade from its start point to end point, indicated as clockwise angles: 0 degrees mean a horizontal saccade from left to right, 90 degrees a vertical saccade from top to bottom.                                                                                     | Degrees (°)                        |

### Important note about correct screen parameters:

To compute gaze angles for most eye-trackers, iMotions has to convert the difference in gaze coordinates (in pixels on the x- and y-axes) into gaze angles. For this, it is using the width and height from the display information populated in the study settings. For this computation to be correct, it is important that the parameters reflect the true size of your computer screen and its resolution, or size of the scene camera's field of view and its resolution, respectively, that you used during data collection.

Hidden Markov Models use an initial assumption about the velocity during a fixation and saccade, but will update these parameters over time. They can therefore compensate for the insecurity about screen parameters which is inherent in [remote data collection](https://help.imotions.com/lab/remote-data-collection/getting-started-with-online-data-collection.md) studies with [web camera based eye tracking](https://help.imotions.com/lab/remote-data-collection/getting-started-with-online-data-collection/rdc-testing-with-webcam-eye-tracking.md), for which the actual display size of the respondents' screens is not always known.

For Smart Eye Pro, iMotions receives 3D gaze data as well as information about the world model objects. Hence, gaze angles are computed from the 3D gaze vectors and do not require the computation outlined below.

For screen based eye trackers, the algorithm uses the distance between eyes and screen as it was recorded by the eye tracker in order to convert the distance on the screen into a gaze angle. For the model to work as accurately as possible, it is recommended to manually measure the width and height of your computer screen with a ruler, and fill them in accordingly.

For mobile eye tracking glasses and VR headsets, the algorithm uses the fixed field of view parameters (width, height, and distance) as they are entered in study settings. The exception is Neon recordings with eye state data, described in [Neon by Pupil Labs](#neon-by-pupil-labs).

![mceclip1.png](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-59c00d52e913504176ccf047d32d9c3c153288f4%2Fc93b46efaa_j8nwldvq06sfantaj2lug.webp?alt=media)

![mceclip7.png](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-fbbda24687aa8c7dbd3c3f1e7c99f93381347951%2F95f6ae982b_57qcd34lskgnsjvuliykpa.webp?alt=media)

### Neon by Pupil Labs

For Neon by Pupil Labs, the Hidden Markov Model filter uses the 3D gaze direction of the eyes' optical axis when the recording contains eye state data. Neon records eye state data when **Compute eye state** is enabled in the Neon Companion app or in Pupil Cloud, see [Neon glasses signals in iMotions](https://help.imotions.com/lab/sensor-setup/eye-tracking-glasses/pupil-labs/neon-by-pupil-labs-live-recording-and-other-study-types.md#neon-glasses-signals-in-imotions). Gaze angles and velocities are then based on the rotation of the eyes, and the field of view parameters in the study settings are not used. The results also contain a **Gaze 3D** signal group and the X, Y and Z direction of each fixation.

Without eye state data, the filter uses the 2D gaze in the scene camera video and the field of view parameters, as for other eye tracking glasses. Gaze mapping always uses the 2D gaze.

## References and further reading

Andersson, Richard, et al. "One algorithm to rule them all? An evaluation and discussion of ten eye movement event-detection algorithms." *Behavior research methods,* 49.2 (2017): 616-637.

Boccignone, G. (2019). Advanced statistical methods for eye movement analysis and modelling: a gentle introduction. In *Eye Movement Research,* (pp. 309-405). Springer, Cham.

Harte, D. (2021). HiddenMarkov. R package version 1.8-13. <https://cran.r-project.org/web/packages/HiddenMarkov/index.html>

Salvucci, Dario D.; Goldberg, Joseph H. (2000).*\[ACM Press the symposium - Palm Beach Gardens, Florida, United States (2000.11.06-2000.11.08)] Proceedings of the symposium on Eye tracking research & applications - ETRA '00 - Identifying fixations and saccades in eye-tracking protocols. , (0), 71–78.* doi:10.1145/355017.355028


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://help.imotions.com/lab/analysis/eye-tracking/fixation-classifiers/fixation-and-saccade-classification-with-hidden-markov-models.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
