Jekyll2020-08-13T23:40:24+02:00http://localhost:4000/feed.xmlplankter.seAn amazing website.Bjorn PersmatsThe usability of Bayesian networks2019-06-26T23:32:50+02:002019-06-26T23:32:50+02:00http://localhost:4000/tech/the-usability-of-bayesian-networks<p><em>Originally posted on 2014-04-18. Since this was first written, GeNIe has been acquired by BayesFusion and is unfortunately no longer freeware.</em></p>
<hr />
<h2 id="bayesian-probability">Bayesian probability</h2>
<p><strong><em>Statistics!</em></strong> No matter what you think about it, no matter if you like it or not, you cannot say that statistics isn’t useful!
If you haven’t worked with statistics very much, some of it may feel a little blurry but when it comes to modeling uncertainty, you can’t get away from probabilities.
This post will be about a particular branch of statistics called <em>Bayesian probability</em>.
To be more precise, it will be about the statistical model called <em>Bayesian networks</em>.
<span id="more-146"></span>But first we need a bit of background.
If you feel like you know what bayesian probability is all about, you should be able to skip this.</p>
<p>Thomas Bayes was a British statistician, most famous for formulating the theorem called <em>Bayes' theorem</em>.
In its simplest form, the theorem looks like this:</p>
<p><img src="/assets/images/the-usability-of-bayesian-networks/bayes_theorem.png" alt="Bayes' Theorem" title="Bayes' Theorem" class="center-image" /></p>
<p><em>P(A|B) should be read as “the probability of A being true, under the condition that B is true”.</em></p>
<p>What the theorem says is basically that under the condition that B is true, the probability of A also being true is equal to the probability of B being true under the condition that A is true, times the probability of A being true (no matter what B is), divided by the probability of B being true (no matter what A is).</p>
<p>If this sounds like complete mumbo jumbo to you, you aren’t alone.
To make it easier to understand the theorem I will present a little example (completely made up!).</p>
<h2 id="example-computer-problems">Example: Computer problems</h2>
<p>Imagine that you’re running a help desk service.
Of the people calling you, 70 % use Windows, 20 % use Mac and 10 % use Linux.
You also know that of the Windows users, 30 % have problems installing a program, 60 % have driver problems and 10 % have network configuration problems.
Of the Mac users, 40 % have program installation problems, 40 % have driver problems and 20 % have problems configuring their network settings.
Lastly, of the Linux users, 10 % have problems installing a program, 10 % have driver problems and 80 % have network configuration problems.</p>
<p>Now, say that Peter needs help and calls you.
Before he explains his problem to you, the likelihood that he uses Linux is pretty small, only ten percent (as described above).
But if Peter tells you that he experiences problems with his network configuration, is the probability of him using Linux still 10 %?
It turns out that if you include this new information into your analysis, the probability of Peter being a Linux user rises to 42 %!</p>
<p>The reason for this is that the first question we asked was “what is the probability that Peter uses Linux?” while the other question was “what is the probability that Peter uses Linux if we know that he has network configuration problems?”.
To make the following equations more readable, let us say that:</p>
<ul>
<li>W = Peter uses Windows</li>
<li>M = Peter uses Mac</li>
<li>L = Peter uses Linux</li>
<li>K = Peter has a problem installing a program</li>
<li>D = Peter has a driver problem</li>
<li>N = Peter has a network problem</li>
</ul>
<p>From the text we also know that</p>
<ul>
<li><code class="highlighter-rouge">P(W) = 0.7, P(M) = 0.2, P(L) = 0.1</code> (probabilities for using the different OSes)</li>
<li><code class="highlighter-rouge">P(K|W) = 0.3, P(D|W) = 0.6, P(N|W) = 0.1</code> (different problems under Windows)</li>
<li><code class="highlighter-rouge">P(K|M) = 0.4, P(D|M) = 0.4, P(N|M) = 0.2</code> (different problems under Mac)</li>
<li><code class="highlighter-rouge">P(K|L) = 0.1, P(D|L) = 0.1, P(N|L) = 0.8</code> (different problems under Linux)</li>
</ul>
<p>The question “what is the probability that Peter uses Linux?” then means calculating P(L) = 0.1, while “what is the probability that Peter uses Linux if we know that he has network configuration problems?” means calculating P(L|N).
Even though we haven’t said anything about what P(L|N) might be, we can calculate it using Bayes’ theorem!</p>
<p><img src="/assets/images/the-usability-of-bayesian-networks/bayes_theorem.png" alt="Bayes' Theorem" title="Bayes' Theorem" class="center-image" /></p>
<p>By changing A and B to our own variables {W, M, L, K, D, N} we get</p>
<p><img src="/assets/images/the-usability-of-bayesian-networks/PL_N.png" alt="P(L_N)" title="P(L_N)" class="center-image" /></p>
<p>We already know what P(N|L) is, and we already know P(L), but the text doesn’t say anything about P(N). We can, however, calculate it. Since P(N) is “the probability that a user has network problems” it is the same as the summed probabilities <code class="highlighter-rouge">P(N) = P(N|W)*P(W) + P(N|M)*P(M) + P(N|L)*P(L)</code>. The likelihood that Peter is a Linux user thus becomes</p>
<p><img src="/assets/images/the-usability-of-bayesian-networks/PL_N_long.png" alt="P(L_N) long" title="P(L_N) long" class="center-image" /></p>
<p>And when the likelihood of Peter using Linux increased from 10 % to 42 %, the likelihood of him being a Windows user decreased from 70 % to about 37 % while Mac stayed almost the same, a small increase from 20 % to 21 %.
By using Bayesian inference it is possible to make use of extra information to make more accurate guesses about one’s environment.
This method is for example used by so called <a href="http://en.wikipedia.org/wiki/Expert_system">Expert systems</a> to make medical diagnoses.</p>
<h2 id="networks-and-genie">Networks and GeNIe</h2>
<p>The probabilistic model called Bayesian networks (or belief networks) makes, as the name suggests, heavy use of Bayesian probability.
A bayesian network can be thought of as a visualisation of probabilities that in some way or another depend on eachother.
A simple example (nicely presented on the Wikipedia article on Bayesian networks) is the relationship between rain, sprinkler usage and wet grass.
The usage of a sprinkler depends on whether it has rained or not (which is why there is an arrow from the node ‘Rain’ to ‘Sprinkler’), and the presence of wet grass depends both on whether a sprinkler has been used and whether it has rained or not (the arrows that go to ‘Grass wet’).</p>
<p><img src="/assets/images/the-usability-of-bayesian-networks/200px-SimpleBayesNetNodes.svg_.png" alt="200px-SimpleBayesNetNodes.svg" title="200px-SimpleBayesNetNodes.svg" class="center-image" /></p>
<p>There are many programs developed for using Bayesian networks, one of which is <a href="https://dslpitt.org/genie/">GeNIe</a> (Graphical Network Interface) developed by the Decision Systems Laboratory at the University of Pittsburgh.
GeNIe has a pretty intuitive graphical interface and uses the C++ library called SMILE (also developed by the Decision Systems Laboratory) as a backend.
Additionally, SMILE can be used as an independent library for Bayesian networks, something that allows developers to create programs that make use of Bayesian networks.</p>
<p>The previous example of computer problems which depend on the user’s operating system can be visualised by using GeNIe.
As can be seen in the picture below, only two nodes are required.
The ‘Operating System’ node contains three states; ‘Windows’, ‘Mac’ and ‘Linux’. The ‘Computer problem’ node also contains three states; ‘Installation’, ‘Driver’ and ‘Network’.</p>
<p><img src="/assets/images/the-usability-of-bayesian-networks/BN_comp_1.png" alt="BN_comp_1" title="BN_comp_1" class="center-image" /></p>
<p>The known probabilities for each node are entered in the node properties.</p>
<p><img src="/assets/images/the-usability-of-bayesian-networks/BN_comp_2.png" alt="BN_comp_2" title="BN_comp_2" class="center-image" />
<img src="/assets/images/the-usability-of-bayesian-networks/BN_comp_3png.png" alt="BN_comp_3png" title="BN_comp_3png" class="center-image" /></p>
<p>When the Bayesian network has been set up, it’s possible to set the observed evidence (the user had a network problem) and update the network.
By then holding the mouse over the nodes, GeNIe displays the calculated probabilities (which gives the same results as our own “manual” calculations!).</p>
<p><img src="/assets/images/the-usability-of-bayesian-networks/BN_comp_4.png" alt="BN_comp_4" title="BN_comp_4" class="center-image" /></p>
<p>This method is a bit easier than, as we did above, doing all the calculations by hand.
And by using SMILE (or any other library for Bayesian networks) in the programs you create, you too can exploit the usability of Bayesian networks!</p>Björn Persson MattssonStatistics! No matter what you think about it, no matter if you like it or not, you cannot say that statistics isn't useful! If you haven't worked with statistics very much, some of it may feel a little blurry but when it comes to modeling uncertainty, you can't get away from probabilities.Localization in robotics2018-01-02T23:16:52+01:002018-01-02T23:16:52+01:00http://localhost:4000/tech/localization-in-robotics<p><em>Originally posted on 2015-04-22.</em></p>
<hr />
<p>Imagine that you’re standing in your living room with your eyes closed.
Now, you might have a mental map of the room and where the furniture are, but would you be able to move around the room without looking and without touching objects to feel where you are?
How many steps would you have to take before you’re no longer sure exactly where you are?
Not that many, I imagine.</p>
<p>When you navigate through your home, you constantly (but unconsciously) scan the environment and update your mental map of where you are.
This helps you avoid bumping into objects, or walking into the wrong rooms.
It’s no surprise that autonomous robots need to do the same scanning and mental-map-recalibration to successfully navigate in an environment.
This is called ‘localization’.</p>
<p>In a university course I am currently taking and which deals with algorithms for mobile robots, localization is one of the covered techniques.
Just like a human can use her perceived step length to estimate where she is even though her eyes are closed, a wheeled robot can estimate its position based on the wheels’ movement.
But just like you would become more uncertain of your exact position the longer you walk with closed eyes, the robot’s estimated position will become less and less accurate unless it makes a scan of the environment and recalibrates itself.
The estimated position might drift because the wheels slip or the movement calculations are inexact.
Regardless of how the error arises, once there it will only grow larger.
And a robot which relies on its position in order to navigate will likely start bumping into obstacles when it actually tries to dodge them.
Unless it utilizes localization, that is.</p>
<p><img src="/assets/images/localization-in-robotics/est_pos_drift.png" alt="Drift of estimated position" title="Drift of estimated position" /></p>
<blockquote>
<p>An illustration of the need for localization in mobile robot navigation.
Here, the robot navigates without recalibrating its position in any way.
As a result, the perceived trajectory (red) deviates quite significantly from the actual (green) trajectory.
Figure adapted from “Introduction to Autonomous Robots” by professor Mattias Wahde.
See the references below for more details.</p>
</blockquote>
<h2 id="simulation">Simulation</h2>
<p>In the course I mentioned above, the students get to implement navigation using localization in Matlab.
We use the simple simulator ARSim (Autonomous Robot Simulator) to program a <a href="https://en.wikipedia.org/wiki/Differential_wheeled_robot">differentially-steered robot</a> to navigate an arena using potential-field navigation.
The robot uses a laser range finder (LRF) to determine the distances to nearby walls and obstacles, and also simulates what an LRF reading from its estimated position should be.
By comparing the actual LRF readings to the simulated readings, the robot gets a sense of how large the error of the estimated position is.
If the error is too large, then it is a good idea to run the localization process and recalibrate the estimated position.</p>
<p><img src="/assets/images/localization-in-robotics/arsim_drift.png" alt="ARSim position drift" title="ARSim position drift" /></p>
<blockquote>
<p>A snapshot from a simulation in ARSim.
The robot (portrayed by the green circle) must navigate through the arena and avoid the obstacles (blue).
Since no recalibration has been used, the estimated position (black circle) of the robot has drifted from the actual position, and while the robot believes it is far away from any obstacles, it is heading straight into one.
The red lines represent the laser rays.</p>
</blockquote>
<p>Now, in order to recalibrate the position, what we really want to do is find a robot pose (position + heading) in the mental map of the robot that minimizes the error.
Since the error is defined as the difference between the actual LRF reading and the simulated LRF reading, the error should be minimized when the robot’s perceived pose is the same as the actual pose.
For a robot that moves in two dimensions, this turns out to be a three-dimensional optimization problem (optimizing the x position, y position and heading).
In this context, even a simple search algorithm works well.
Assuming that our estimated pose already is pretty close to the actual pose (the error is not too large), we can just run a search in the vicinity of the estimated pose.
In our simulation, we just pick a random pose close to the estimated one.
If it turns out to be an improvement, it replaces the estimated pose, otherwise we discard it and try with a new random pose.
Then we just have to continue this process until the error is below a threshold or a certain number of search iterations have been run.
Surprisingly, even a naive search algorithm such as this works pretty good.
In the absolutely worst case we have failed to find a better estimated pose, but most likely we have found a better one.</p>
<p><img src="/assets/images/localization-in-robotics/arsim_drift_recal.png" alt="ARSim position drift with recalibration" title="ARSim position drift with recalibration" /></p>
<blockquote>
<p>By checking the error of the estimated position every third second and (if needed) running the recalibration process, the estimated position stays close to the actual one and the robot can move through the arena without running into obstacles.</p>
</blockquote>
<h2 id="conclusions">Conclusions</h2>
<p>This method of doing localization assumes that we have a very accurate map of the environment that the robot can use to simulate LRF readings from various poses.
This may be a severe drawback since the robot won’t be able to navigate through environments of which there aren’t any maps.
On the other hand, if you encounter the problem of navigating without any sort of map you’re generally already on a more advanced level than what is covered in the course I mentioned earlier.
If you come across this, you will likely have to look more into what is known as <a href="https://en.wikipedia.org/wiki/Simultaneous_localization_and_mapping">SLAM (Simultaneous Localization And Mapping)</a>.
However, in many cases, for example a robot navigating through a hospital, it would be enough to just do the preprocessing of acquiring an accurate map (e.g. by manually laser scanning the area) before setting the robot to its task.</p>
<p>This method also relies on some parameters that must be set to reasonable values, for example an error threshold that determine whether the recalibration should be run or not.
The robot could even run the recalibration continuously and take its movement into account, thereby removing the need for stopping, scanning, and checking an error threshold before recalibrating.
The search algorithm, in which the robot finds a better estimated pose, can also definitely be improved as needed.
There are lots of efficient search algorithms and optimization techniques for finding a good estimated pose in a three-dimensional space.
All in all, this method for localization is rather robust, as well as easy to implement and extend.</p>
<h2 id="links-and-references">Links and references</h2>
<p>(EDIT: while updating this post in early 2018, it seems the links below are unfortunately no longer valid)</p>
<p>Course webpage for Autonomous Agents: <a href="http://www.me.chalmers.se/~mwahde/courses/aa/2015/aa.html">http://www.me.chalmers.se/~mwahde/courses/aa/2015/aa.html</a></p>
<p>Wahde, Mattias. “Introduction to autonomous robots.” <em>Department of Applied Mechanics, Chalmers University of Technology, Goteborg, Sweden</em> (2012). Link: <a href="http://www.me.chalmers.se/~mwahde/courses/aa/2012/Wahde_IntroductionToAutonomousRobots.pdf">http://www.me.chalmers.se/~mwahde/courses/aa/2012/Wahde_IntroductionToAutonomousRobots.pdf</a></p>Björn Persson MattssonImagine that you’re standing in your living room with your eyes closed. Now, you might have a mental map of the room and where the furniture are, but would you be able to move around the room without looking and without touching objects to feel where you are? How many steps would you have to take before you’re no longer sure exactly where you are? Not that many, I imagine.Public site finally updated!2017-10-21T23:25:33+02:002017-10-21T23:25:33+02:00http://localhost:4000/update/public-site-finally-updated<p>Yay! At last!
The old ‘work-in-progress’ HTML site is gone from <a href="http://plankter.se">plankter.se</a>, and instead this is here.
Everything is not in place yet.
I am still missing the old blog posts from the Wordpress version I used a couple of years ago for the website, and the Projects page is awfully empty.
I also want to add links between blog posts so that it’s easier to navigate between newer and older posts.
There is always things to work on for, but Jekyll works really well so far and I have come to like it more and more.</p>Björn Persson MattssonYay! At last! The old ‘work-in-progress’ HTML site is gone from plankter.se, and instead this is here. Everything is not in place yet. I am still missing the old blog posts from the Wordpress version I used a couple of years ago for the website, and the Projects page is awfully empty. I also want to add links between blog posts so that it’s easier to navigate between newer and older posts. There is always things to work on for, but Jekyll works really well so far and I have come to like it more and more.Experimenting with Jekyll2017-09-29T04:53:28+02:002017-09-29T04:53:28+02:00http://localhost:4000/update/experimenting-with-jekyll<p>Right, so I’m working on updating my website to a working condition again and am testing a framework called <a href="https://jekyllrb.com/docs/home">Jekyll</a>.</p>
<p>I wouldn’t say that I’ve completely gotten the hang of it yet, but at least it seems promising.
So far I’m still in the stage where I figure out what I can do with Jekyll and, most of all, what I want to display here on the website.
Some of the posts from my old blog would be nice to have here as well because that content is still relevant…
Let’s just see if it’s possible to find the backup that I hope I made of that old Wordpress site…</p>Björn Persson MattssonRight, so I’m working on updating my website to a working condition again and am testing a framework called Jekyll.