2022-10-07

# Single molecule localization using Julia and JuMP (1)

I recently got very interested in both Julia and single-molecule localization algorithms, so I figured maybe I can try to use Julia to write some simple implementations of these algorithms as a fun weekend project.

In this first attempt, I tried using Julia's JuMP library to implement the basic idea of the classic paper *Nature Methods, 2010, 7 (5): 373* where the authors introduced the key comcepts of MLE for single-molecule localization and provided some thorough derivations of the relevant equations.

Through this attempt, I was happily surprised again by the expressiveness of Julia, especially how easy to write Julia code that almost line-to-line corresponds to the equations, as well as the convenience of using JuMP to solve optimization problems.

I am neither an expert in Julia nor mathematical programming. I am just learning new things myself through this project. This post is for sharing my experience as well as a note for myself. Please feel free to let me know if I made any mistakes, or if there's anything I can improve.

Also in this first attempt, there is zero optimization other than a simple implementation of the equations into Julia code. The script's performance is not great (about 80ms for each peak). I will leave the further optimization to the later episode of this project.

Some background for people who are not familiar with Julia and JuMP:

**Julia** is a high-level programing language launched in 2012. Here is some introduction of it from Wikipedia

Julia is a high-level, dynamic programming language. Its features are well suited for numerical analysis and computational science.

Distinctive aspects of Julia's design include a type system with parametric polymorphism in a dynamic programming language; with multiple dispatch as its core programming paradigm. Julia supports concurrent, (composable) parallel and distributed computing (with or without using MPI or the built-in corresponding to "OpenMP-style" threads), and direct calling of C and Fortran libraries without glue code. Julia uses a just-in-time (JIT) compiler that is referred to as "just-ahead-of-time" (JAOT) in the Julia community, as Julia compiles all code (by default) to machine code before running it.

**JuMP** is a library in Julia that specifically designed for mathematical optimizations. Here is a brief description of it from their website:

JuMP is a domain-specific modeling language for mathematical optimization embedded in Julia. It currently supports a number of open-source and commercial solvers for a variety of problem classes, including linear, mixed-integer, second-order conic, semidefinite, and nonlinear programming.

All the equations below are from the Supplementary information of the paper *Fast, single-molecule localization that achieves theoretically minimum uncertainty**Nature Methods, 2010, 7 (5): 373*

The rest of the page is an embedded Pluto Notebook. Read more about it here. Embedding this notebook (an html file) into the Gatsby site is probably something worth another post later (actually pretty simple).