# Bayesian approach for positioning

**URL:** <https://discourse.pymc.io/t/bayesian-approach-for-positioning/2859>\
**Category:** Questions\
**Created:** [March 5, 2019, 12:43pm UTC](https://discourse.pymc.io/t/bayesian-approach-for-positioning/2859 "2019-03-05T12:43:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![klimmaa](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/klimmaa/32/1585_2.png) [@klimmaa](https://discourse.pymc.io/u/klimmaa)\
**Post date:** [March 5, 2019, 12:43pm UTC](https://discourse.pymc.io/t/bayesian-approach-for-positioning/2859/1 "2019-03-05T12:43:02Z")

</div>

Dear All,

I’ve been struggling to replicate the results from [Bayesian Indoor Positioning Systems](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=2ahUKEwjr07GF9OrgAhWnxIsKHSCVCfwQFjABegQICRAC&url=http%3A%2F%2Fciteseerx.ist.psu.edu%2Fviewdoc%2Fdownload%3Fdoi%3D10.1.1.114.3348%26rep%3Drep1%26type%3Dpdf&usg=AOvVaw2T5cav60sVMRM_-yF_VlPn). To do that, I tried to use an approach previously discussed in [Model multivariate normal with separate means, dimension mismatch error](https://discourse.pymc.io/t/model-multivariate-normal-with-separate-means-dimension-mismatch-error/231), since It’s well known that the measurement vector for the Received Signal Strength (RSS) case follows a multivariate normal distribution with mean -\beta\log\left[\sqrt{\left( x - x\_i \right)^2 + \left( y - y\_i \right)^2} \right] and covariance matrix \textrm{diag}\left( \sigma\_1^2, \sigma\_2^2, ..., \sigma\_n^2 \right), where \beta is the path loss exponent (an environment specific parameter), (x, y) is the target (unknown) location, (x\_i, y\_i) is the anchor (known) location.

The original reference by D Madigan’s uses WINBUGS and its corresponding plate notation is

 ![plate_notation](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/0/02b53c5771d11d752eac3c23d856aad8f7023153.png)  
Where (X, Y) is the target position, D\_i is the separation distance between the target and i\! th anchor node, b\_{i0} and b\_{i1} are hyper parameters and \tau\_i is the respective variance.

My code excerpt considering four anchors column-wise follows:

```python
with pm.Model() as pos_model:
    α = pm.Normal('α', mu = 0, sd = 100, shape = 1)
    β = pm.Normal('β', mu = 0, sd = 100, shape = 1)
    μ0 = α - β*tt.log(shared(d_hat[:, 0]))
    μ1 = α - β*tt.log(shared(d_hat[:, 1]))
    μ2 = α - β*tt.log(shared(d_hat[:, 2]))
    μ3 = α - β*tt.log(shared(d_hat[:, 3]))
    σ = pm.HalfCauchy('sigma', 1, shape=4)
    cov = np.identity(4)
    μ = tt.stack([μ0, μ1, μ2, μ3]).T
    joint_obs = pm.MvNormal('joint', mu=μ, cov=cov, observed=ρ_hat)

with pos_model:
    trace = pm.sample()

```

Please, notice that `d_hat` and `ρ_hat` correspond to the simulated values and used as observed.

Even though this code works and allows me to estimate `α` and `β`, I can’t find a way of estimating the position of the unknown target node, namely (x, y). I’ve tried to use Deterministic, Potential and Stochastic classes (as in [Observing functions of random variables in PyMC](http://www.awebb.info/blog/observing_functions)), but couldn’t work out the multivariate case.

Any suggestion is really appreciated.

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [March 5, 2019, 5:48pm UTC](https://discourse.pymc.io/t/bayesian-approach-for-positioning/2859/2 "2019-03-05T17:48:21Z")

</div>

> [@klimmaa](#):
>
> I can’t find a way of estimating the position of the unknown target node

What do you mean by unknown target node? I am not clear where the observed (x, y) are modelled in the `pos_model``

---

<div class="post-metadata">

**Author:** ![klimmaa](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/klimmaa/32/1585_2.png) [@klimmaa](https://discourse.pymc.io/u/klimmaa)\
**Post date:** [March 6, 2019, 5:15am UTC](https://discourse.pymc.io/t/bayesian-approach-for-positioning/2859/3 "2019-03-06T05:15:45Z")

</div>

Thanks a lot for promptly replying.

Sorry, it’s unclear, but that’s the point I couldn’t connect the model to implementation… I’ll try to elaborate on that. Actually, I intend to do something like this

X \thicksim \textsf{Uniform}(0, L)  
Y \thicksim \textsf{Uniform}(0, W)  
D\_i \thicksim \sqrt{\left( X - x\_i \right)^2 + \left( Y - y\_i \right)^2}  
\mu\_i \thicksim \alpha + \beta \log(D\_i)  
\alpha \thicksim \textsf{Normal}(0, 100)  
\beta \thicksim \textsf{Normal}(0, 100)  
\sigma^2 \thicksim \textsf{HalfNormal}(10)  
\rho\_i \thicksim \textsf{Normal}(\mu\_i, \sigma^2)

So that, inside the sampling algorithm over the updating iterations (I guess)

\displaystyle X\_{(k + 1)} \thicksim \Pr(X)\prod\_{i=1}^N \Pr \left( \rho\_i \vert Y\_{(k)}, \sigma^2\_{(k)}, \alpha\_{(k)}, \beta\_{(k)} \right)  
\displaystyle Y\_{(k + 1)} \thicksim \Pr(Y)\prod\_{i=1}^N \Pr \left( \rho\_i \vert X\_{(k + 1)}, \sigma^2\_{(k)}, \alpha\_{(k)}, \beta\_{(k)} \right)

Then, the corresponding conditional distributions would be written as  
\displaystyle X\_{(k + 1)} \thicksim \exp\{-\frac{\sum\_{i=1}^N \left[\rho\_i - \alpha\_{(k)} - \beta\_{(k)} \log(D\_i)\right]^2}{2 \sigma^2\_{(k)}}\}, \text{with } D\_i \thicksim \sqrt{\left( X - x\_i \right)^2 + \left( Y\_{(k)} - y\_i \right)^2}  
\displaystyle Y\_{(k + 1)} \thicksim \exp\{-\frac{\sum\_{i=1}^N \left[\rho\_i - \alpha\_{(k)} - \beta\_{(k)} \log(D\_i)\right]^2}{2 \sigma^2\_{(k)}}\}, \text{with } D\_i \thicksim \sqrt{\left( X\_{(k + 1)} - x\_i \right)^2 + \left( Y - y\_i \right)^2}

I tried to implement that as a multilevel model (as discussed in [A Primer on Bayesian Methods for Multilevel Modeling](https://docs.pymc.io/notebooks/multilevel_modeling.html)), but it seems to be stuck to the priors because I always get (50, 50) as an estimate to (X, Y) when considering L = 100\! m and W = 100\! m.

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [March 6, 2019, 5:50am UTC](https://discourse.pymc.io/t/bayesian-approach-for-positioning/2859/4 "2019-03-06T05:50:13Z")

</div>

Hmm, you seems to describing some kind of Bayesian filter, which we dont currently have any example of. You might find [https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python](https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python) helpful.

---

<div class="post-metadata">

**Author:** ![klimmaa](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/klimmaa/32/1585_2.png) [@klimmaa](https://discourse.pymc.io/u/klimmaa)\
**Post date:** [March 6, 2019, 7:01am UTC](https://discourse.pymc.io/t/bayesian-approach-for-positioning/2859/5 "2019-03-06T07:01:41Z")

</div>

Hi,

Thanks a lot for your quick response… And for pointing directions.
