# Applying Knowledge Inference on Event-Conjunction for Automatic Control in Smart Building
## 4 Proposal and Experiment
### Inference Engine
In this proposal, we utilized the MCMC sampling method to overcome the mentioned above issue. MCMC algorithms are aimed at generating samples from a given probability distribution. It is useful for obtaining a sequence of random samples from a probability distribution in which direct sampling is difficult, or the sample data is small or incomplete. Thus, instead of trying to deal with intractable computations involving the posterior, we can get samples from using the existing samples and some definite prior value to compute various punctual statistics to approximate the distribution by kernel density estimation. \[\overbrace{p(\mu\mid Data)}^{\text{posterior}}=\overbrace{\underbrace{p( Data\mid\mu)\cdot\overbrace{p(\mu)}}_{\text{marginal likelihood}}}^{\text{prior}} \tag{1}\]  
The Metropolis-Hastings algorithm, one of the most common methods of MCMC based sampling process, was utilized for drawing samples from probability distribution \(P(\Delta t)\), provided that we know a function \(f(\Delta t)\) is proportional to the density of \(P(\Delta t)\) and the values of \(f(\Delta t)\) can be calculated. The requirement that \(f(\Delta t)\) must only be proportional to the density, rather than exactly equal to it, makes the Metropolis-Hastings algorithm particularly useful while the size of event-related sensor observations is relatively small. **Local distance-based adjustment** In this sampling process, as shown in Equation (2), the prior distribution of \(\Delta t\) was adjusted according to the shortest distance \(Dist(e_{i},e_{j})\) between two space entities (\(e_{i}\), \(e_{j}\)) extracted from the BOT graph, where the prior probability \(P(\mu)\) is the probability of the hypothesis \(\mu\) before the Data D, was modeled as a Gaussian distribution. \[\mu\sim\text{N}(f_{Dist(e_{i},e_{j})},\sigma) \tag{2}\]where the value of \(f_{Dist(e_{i},e_{j})}\) showing the shortest distance is linearly-correlated with the count of hops between two triggered sensor nodes in the graph, as denoted in Equation (3). \[f_{Dist(e_{i},e_{j})}=ax+b \tag{3}\]  
Based on the shortest distance values extracted from the BOT graph and the \(\Delta t\) values collected in the evaluation, the values of a, b in the linear regression could be calculated by the Equations (4) and (5). Figure 9 shows the result of linear regression optimization of \(Dist(e_{i},e_{j})\) with several distance examples have been illustrated as well. \[C=\sum_{1}^{n}(y-\widehat{y})^{2},where\frac{\partial C}{\partial a}=0,\frac{ \partial C}{\partial b}=0 \tag{4}\]  
\[a=\frac{\sum_{1}^{n}xy-\frac{1}{n}\sum_{1}^{n}x\sum_{1}^{n}y}{\sum_{1}^{n}x^{2 }-\frac{1}{n}\big{(}\sum_{1}^{n}x\big{)}^{2}},b=\frac{1}{n}\sum_{1}^{n}(y-ax) \tag{5}\]  
Collected data of \(\Delta t_{1}\),..., \(\Delta t_{n}\), was used for computing the prior probability. With given the measured quantities \(\Delta t_{1}\),..., \(\Delta t_{n}\), the probability function has been modeled as normally distributed, shown in Equation (6). Figure 6:0  
In order to derive the approximated value \(\mu\) of \(\Delta t_{1}\),..., \(\Delta t_{n}\), PyMC3 [27] was utilized for performing Bayesian statistical sample processing focused on MCMC. PyMC3 is an open-source probabilistic programming framework written in Python. It is based on Theano.