# An introduction to Lock-in Feedback Currently, I am privileged to be working with Dr. Davide Iannuzzi, Professor in Experimental Physics, and Dr. Maurits Kaptein, assistant professor in Artificial Intelligence, on real-world applications of “Lock in Feedback” – where “Lock in Feedback” refers to their adaptation of the lock-in a amplifier algorithm used in physics and engineering as a generic tool to find maxima of unknown functions by experimentation, even when the maximum of the functions drifts or when the signal to noise ratio is low:

…our method is based on an approach used in physics and engineering, where, if a physical variable y depends on the value of a well controllable physical variable x, the search for argmaxx f(x) can be solved via what is nowadays considered as standard electronics. This approach relies on the possibility of making the variable x oscillate at a fixed frequency and to look at the response of the dependent variable y at the very same frequency by means of a lock-in amplifier. The method is particularly suitable when y is immersed in a high noise level, where other more direct methods would fail. Furthermore, should the entire curve shift (or, in other words, if argmaxx f(x) changes in time, also known as concept drift), the circuit will automatically adjust to the new situation and quickly reveal the new maximum position. This approach is widely used in a very large number of applications, both in industry and research, and is the basis for the Lock-in Feedback (LiF) method …1

For the details on the LiF algorithm I refer you to Davide’s and Maurits’ original paper – in a nutshell, Lock in feedback really just goes through the following steps, again and again:

1. Oscillate a controllable independent variable X around a set value at a fixed pace.
2. Apply the Lock-in amplifier algorithm.to obtain values of the amplitude if the outcome variable Y at the pace you set at step 1.
3. Is the amplitude of this variable zero? Congratulations, you have reached lock-in!
That is, you have found the optimal value of Y at the current value of X.
Still, this optimal value might shift over time, so move to step 1 and repeat the process to make sure we maintain lock-in.
4. Is the amplitude less than, or greater than zero?
Then move the set value around which we are oscillating our independent variable X up or down on the basis of the outcome.
Now move to step 1 and repeat.. An example of LiF where the price of a product oscillates in a controlled way, while you measure their response to those fluctuations. See how the amplitude of the revenue per customer moves towards zero when the price is closer and closer to the optimum revenue per customer. [Image: D.Iannuzzi]

In Davide’s and Maurits’ paper, they show that the workings of the LiF loop can be mathematically represented by two equations.

Firstly, an equation that represents x oscillating with time t (1): Secondly, an equation that integrates yω over a time T = 2πN ω , where N is a positive integer and T denotes the time needed to integrate N full oscillations (2): Davide and Maurits then apply these equations in one batch-oriented and one continuous time algorithm. Here I will focus on the latter algorithm, LiF-2, as represented by the following pseudocode: You can clearly recognize the line of pseudocode that represents equation 1 – this is the line that makes x oscillate with time t. The other lines busy themselves with integrating yω over a time T = 2πN ω, as per equation 2.

It is a relatively small step from pseudocode to conventional programming language code. In other words, we can now put LiF to work in some R and Python simulations! More on those simulations in later posts. For now, to wet your appetite, a straightforward conversion of the previous pseudocode to Python:

To demonstrate how you might implement the previous code fragment, a minimal simulation:

1. Kaptein, M., & Ianuzzi, D. (2015). Lock in Feedback in Sequential Experiment. arXiv, 7.