* [27] C.H. Kraft, J.W. Pratt, and A. Seidenberg.  Intuitive probability on finite sets.  The Annals of Mathematical Statistics, 30(2):408–419, 1959.
  * [28] H. Leitgeb.  Hype: A system of hyperintensional logic (with an application to semantic paradoxes).  Journal of Philosophical Logic, 48(2):305–405, 2019.
  * [29] G.M. Moisil.  Logique modale.  Disquisitiones mathematicae et physicae, 2:3–98, 1942.
  * [30] D. Nelson.  Constructible falsity.  The Journal of Symbolic Logic, 14(1):16–26, 1949.
  * [31] S. Odintsov and H. Wansing.  Routley star and hyperintensionality.  Journal of Philosophical Logic, 50:33–56, 2021.
  * [32] H. Omori and H. Wansing.  40 years of FDE: An introductory overview.  Studia Logica, 105(6):1021–1049, 2017.
  * [33] A. Přenosil.  Reasoning with Inconsistent Information.  PhD thesis, Charles University, Prague, 2018.
  * [34] C. Rauszer.  Applications of kripke models to heyting-brouwer logic.  Studia Logica, 36(1/2):61–71, 1977.
  * [35] C. Rauszer.  An algebraic and Kripke-style approach to a certain extension of intuitionistic logic.  dissertation, Institute of Mathematics, Polish Academy of Sciences, 1980\.
  * [36] D. Scott.  Measurement structures and linear inequalities.  Journal of mathematical psychology, 1(2):233–247, 1964.
  * [37] K. Segerberg.  Qualitative probability in a modal setting.  In Studies in Logic and the Foundations of Mathematics, volume 63, pages 341–352. Elsevier, 1971.
  * [38] Y. Shramko.  Hilbert-style axiomatization of first-degree entailment and a family of its extensions.  Annals of Pure and Applied Logic, 172(9):103011, October 2021.
  * [39] N.J.J. Smith.  Vagueness and Degrees of Truth.  Oxford University Press, USA, 2008.
  * [40] F. Touazi, C. Cayrol, and D. Dubois.  Possibilistic reasoning with partially ordered beliefs.  J. Appl. Log., 13(4):770–798, December 2015.
  * [41] H. Wansing.  Constructive negation, implication, and co-implication.  Journal of Applied Non-Classical Logics, 18(2–3):341–364, 2008\.
  * [42] S.K.M. Wong, Y. Yao, and P. Bollmann.  Characterization of comparative belief structures.  International journal of man-machine studies, 37(1):123–133, 1992\.
  * [43] S.K.M. Wong, Y.Y. Yao, P. Bollmann, and H.C. Bürger.  Axiomatization of qualitative belief structure.  IEEE Transactions on Systems, Man, and Cybernetics, 21(4):726–734, 1991.
  * [44] C. Zhou.  Belief functions on distributive lattices.  Artificial Intelligence, 201:1–31, 2013.

##  Appendix A Completeness proof of
$\mathcal{H}\mathsf{G}^{2}(\rightarrow,\Yleft)$ and
$\mathcal{H}\mathsf{G}^{2}(\rightarrowtriangle,\multimap)$

In this section, we prove the completeness of
$\mathcal{H}\mathsf{G}^{2}(\rightarrow,\Yleft)$ and
$\mathcal{H}\mathsf{G}^{2}(\rightarrowtriangle,\multimap)$ via the equivalence
between their algebraic and frame semantics.

######  Definition A.1 ($\mathsf{G}^{2}$-frames).

A $\mathsf{G}^{2}$-frame is a tuple $\mathbb{F}=\langle W,\preccurlyeq\rangle$
with $W\neq\varnothing$ and $\preccurlyeq$ being a reflexive linear order on
$W$.

######  Definition A.2 (Models and semantics).

A model on the frame $\mathbb{F}$ is a tuple
$\mathfrak{M}=\langle\mathbb{F},v^{+},v^{-}\rangle$ with
$v^{+},v^{-}:\mathsf{Prop}\to 2^{W}$ (positive and negative valuations) such
that if $s\in v^{+}(p)$ and $s\preccurlyeq s^{\prime}$, then $s\in v^{+}(p)$
(and likewise for $v^{-}$). Using these maps, the positive and negative
support of formulas at state $s\in W$ is defined as follows.

$\mathfrak{M},s\vDash^{+}p$ | iff | $w\in v^{+}(p)$
---|---|---
$\mathfrak{M},s\vDash^{-}p$ | iff | $w\in v^{-}(p)$
$\mathfrak{M},s\vDash^{+}\neg\phi$ | iff | $\mathfrak{M},s\vDash^{-}\phi$
$\mathfrak{M},s\vDash^{-}\neg\phi$ | iff | $\mathfrak{M},s\vDash^{+}\phi$
$\mathfrak{M},s\vDash^{+}\phi_{1}\wedge\phi_{2}$ | iff |  $\mathfrak{M},s\vDash^{+}\phi_{1}$ and $\mathfrak{M},s\vDash^{+}\phi_{2}$
$\mathfrak{M},s\vDash^{-}\phi_{1}\wedge\phi_{2}$ | iff |  $\mathfrak{M},s\vDash^{-}\phi_{1}$ or $\mathfrak{M},s\vDash^{-}\phi_{2}$
$\mathfrak{M},s\vDash^{+}\phi_{1}\vee\phi_{2}$ | iff |  $\mathfrak{M},s\vDash^{+}\phi_{1}$ or $\mathfrak{M},s\vDash^{+}\phi_{2}$
$\mathfrak{M},s\vDash^{-}\phi_{1}\vee\phi_{2}$ | iff |  $\mathfrak{M},s\vDash^{-}\phi_{1}$ and $\mathfrak{M},s\vDash^{-}\phi_{2}$
$\mathfrak{M},s\vDash^{+}\phi_{1}\rightarrow\phi_{2}$ | iff | $\forall s^{\prime}\succcurlyeq s:\mathfrak{M},s^{\prime}\vDash^{+}\phi_{1}\Rightarrow\mathfrak{M},s^{\prime}\vDash^{+}\phi_{2}$
$\mathfrak{M},s\vDash^{-}\phi_{1}\rightarrow\phi_{2}$ | iff | $\exists s^{\prime}\preccurlyeq s:\mathfrak{M},s^{\prime}\nvDash^{-}\phi_{1}~{}\&~{}\mathfrak{M},s^{\prime}\vDash^{-}\phi_{2}$
$\mathfrak{M},s\vDash^{+}\phi_{1}\Yleft\phi_{2}$ | iff | $\exists s^{\prime}\preccurlyeq s:\mathfrak{M},s^{\prime}\vDash^{+}\phi_{1}~{}\&~{}\mathfrak{M},s^{\prime}\nvDash^{+}\phi_{2}$
$\mathfrak{M},s\vDash^{-}\phi_{1}\Yleft\phi_{2}$ | iff | $\forall s^{\prime}\preccurlyeq s:\mathfrak{M},s^{\prime}\nvDash^{-}\phi_{1}\Rightarrow\mathfrak{M},s^{\prime}\nvDash^{-}\phi_{2}$
$\mathfrak{M},s\vDash^{+}\phi_{1}\rightarrowtriangle\phi_{2}$ | iff | $\forall s^{\prime}\succcurlyeq s:\mathfrak{M},s^{\prime}\vDash^{+}\phi_{1}\Rightarrow\mathfrak{M},s^{\prime}\vDash^{+}\phi_{2}$
$\mathfrak{M},s\vDash^{-}\phi_{1}\rightarrowtriangle\phi_{2}$ | iff | $\mathfrak{M},s\vDash^{+}\phi_{1}~{}\&~{}\mathfrak{M},s\vDash^{-}\phi_{2}$
$\mathfrak{M},s\vDash^{+}\phi_{1}\multimap\phi_{2}$ | iff | $\exists s^{\prime}\preccurlyeq s:\mathfrak{M},s^{\prime}\vDash^{+}\phi_{1}~{}\&~{}\mathfrak{M},s^{\prime}\nvDash^{+}\phi_{2}$
$\mathfrak{M},s\vDash^{-}\phi_{1}\multimap\phi_{2}$ | iff |  $\mathfrak{M},s\vDash^{-}\phi_{1}$ or $\mathfrak{M},s\vDash^{+}\phi_{2}$

######  Definition A.3 (Entailment).

$\Gamma$ (locally) entails $\phi$ in $\mathsf{G}^{2}$ — denoted
$\Gamma\models\phi$ — iff for any $\mathfrak{M}$ and $s\in\mathfrak{M}$ it
holds that

$\text{if }\mathfrak{M},s\vDash^{+}[\Gamma]\text{ then
}\mathfrak{M},s\vDash^{+}\phi$

######  Theorem A.4 ($\mathcal{H}\mathsf{G}^{2}(\rightarrow,\Yleft)$
completeness).

Let
$\Gamma\cup\\{\phi\\}\subseteq\mathscr{L}_{\mathsf{G}^{2}(\rightarrow,\Yleft)}$.
Then $\Gamma\models\phi$ iff there is a derivation of $\phi$ from $\Gamma$ in
$\mathcal{H}\mathsf{G}^{2}(\rightarrow,\Yleft)$ s.t. $\mathsf{nec}$ is applied
only to $\mathcal{H}\mathsf{G}^{2}(\rightarrow,\Yleft)$ theorems.

###### Proof.

Recall first, that adding $\mathsf{prel}$ axioms to $\mathcal{H}\mathsf{HB}$
produces a calculus for Gödel logic with co-implication which is complete
w.r.t. linear frames. We use De Morgan laws to transform every formula into
its negation normal form. Now let $\phi^{*}$ be a negation normal form of
$\phi$ where each literal $\neg p$ is substituted with a fresh variable
$p^{*}$. We know from [41, Lemma 12] that $\phi$ is falsified on a
$\mathsf{G}^{2}(\rightarrow,\Yleft)$ model iff $\phi^{*}$ is falsified on an
$\mathsf{HB}$ model _over the same frame_. Thus,
$\mathcal{H}\mathsf{G}^{2}(\rightarrow,\Yleft)$ is complete w.r.t. the class
of linear frames.

∎

The completeness result for
$\mathcal{H}\mathsf{G}^{2}(\rightarrowtriangle,\multimap)$ can be proved in
the same manner.

######  Theorem A.5
($\mathcal{H}\mathsf{G}^{2}(\rightarrowtriangle,\multimap)$ completeness).

Let
$\Gamma\cup\\{\phi\\}\subseteq\mathscr{L}_{\mathsf{G}^{2}(\rightarrowtriangle,\multimap)}$.
Then $\Gamma\models\phi$ iff there is a derivation of $\phi$ from $\Gamma$ in
$\mathcal{H}\mathsf{G}^{2}(\rightarrow,\Yleft)$ s.t. $\mathsf{nec}$ is applied
only to $\mathcal{H}\mathsf{G}^{2}(\rightarrowtriangle,\multimap)$ theorems.

It now remains to show that every $\mathsf{G}^{2}$ valuations $e_{1}$ and
$e_{2}$ on $[0,1]^{\Join}$ can be faithfully transformed into valuations on
some linear model and vice versa.

######  Definition A.6 (Model counterpart of a $\mathsf{G}^{2}$ valuation).

Let $\mathbf{v}$ be a $\mathsf{G}^{2}$ valuation on $[0,1]^{\Join}$. A model
$\mathfrak{M}_{\mathbf{v}}=\langle\mathbb{Q},\leq_{\mathbf{v}},v^{+}_{\mathbf{v}},v^{-}_{\mathbf{v}}\rangle$
is a  _counterpart_ of $\mathbf{v}$ if for its valuations $v^{+}_{\mathbf{v}}$
and $v^{-}_{\mathbf{v}}$ it holds that:

$\displaystyle v^{+}_{\mathbf{v}}(p)=\mathbb{Q}$ $\displaystyle\text{ iff
}\mathbf{v}_{1}(p)=1$ $\displaystyle v^{-}_{\mathbf{v}}(p)=\mathbb{Q}$
$\displaystyle\text{ iff }\mathbf{v}_{2}(p)=1$ $\displaystyle
v^{+}_{\mathbf{v}}(p)=\varnothing$ $\displaystyle\text{ iff
}\mathbf{v}_{1}(p)=0$ $\displaystyle v^{-}_{\mathbf{v}}(p)=\varnothing$
$\displaystyle\text{ iff }\mathbf{v}_{2}(p)=0$ $\displaystyle
v^{+}_{\mathbf{v}}(p)\subseteq v^{+}_{\mathbf{v}}(q)$ $\displaystyle\text{ iff
}\mathbf{v}_{1}(p)\leqslant\mathbf{v}_{1}(q)$ $\displaystyle
v^{-}_{\mathbf{v}}(p)\subseteq v^{-}_{\mathbf{v}}(q)$ $\displaystyle\text{ iff
}\mathbf{v}_{2}(p)\leqslant\mathbf{v}_{2}(q)$ $\displaystyle
v^{-}_{\mathbf{v}}(p)\subseteq v^{+}_{\mathbf{v}}(q)$ $\displaystyle\text{ iff
}\mathbf{v}_{2}(p)\leqslant\mathbf{v}_{1}(q)$ $\displaystyle
v^{+}_{\mathbf{v}}(p)\subseteq v^{-}_{\mathbf{v}}(q)$ $\displaystyle\text{ iff
}e_{1}(p)\leqslant e_{2}(q)$

######  Lemma A.7.

Let
$\phi,\phi^{\prime}\in\mathscr{L}_{\mathsf{G}^{2}(\rightarrow,\Yleft)}\cup\mathscr{L}_{\mathsf{G}^{2}(\rightarrowtriangle,\multimap)}$,
$\mathbf{v}$ be a valuation on $[0,1]^{\Join}$, and
$\mathfrak{M}_{\mathbf{v}}$ be a counterpart of $\mathbf{v}$. Then it holds
that

$\displaystyle\mathbf{v}_{1}(\phi)=1$ $\displaystyle\text{ iff
}\mathfrak{M}_{\mathbf{v}}\vDash^{+}\phi$
$\displaystyle\mathbf{v}_{2}(\phi)=1$ $\displaystyle\text{ iff
}\mathfrak{M}_{\mathbf{v}}\vDash^{-}\phi$
$\displaystyle\mathbf{v}_{1}(\phi)\leqslant\mathbf{v}_{1}(\phi^{\prime})$
$\displaystyle\text{ iff }v_{\mathbf{v}}^{+}(\phi)\subseteq
v_{\mathbf{v}}^{+}(\phi^{\prime})$
$\displaystyle\mathbf{v}_{2}(\phi)\leqslant\mathbf{v}_{2}(\phi^{\prime})$
$\displaystyle\text{ iff }v_{\mathbf{v}}^{-}(\phi)\subseteq
v_{\mathbf{v}}^{-}(\phi^{\prime})$
$\displaystyle\mathbf{v}_{1}(\phi)\leqslant\mathbf{v}_{2}(\phi^{\prime})$
$\displaystyle\text{ iff }v_{\mathbf{v}}^{+}(\phi)\subseteq
v_{\mathbf{v}}^{-}(\phi^{\prime})$
$\displaystyle\mathbf{v}_{2}(\phi)\leqslant\mathbf{v}_{1}(\phi^{\prime})$
$\displaystyle\text{ iff }v_{\mathbf{v}}^{-}(\phi)\subseteq
v_{\mathbf{v}}^{+}(\phi^{\prime})$

###### Proof.

We proceed by induction on $\phi$. Then the basis cases of variables and
constants hold by construction.

For the induction steps, we consider only the case of
$\phi=\psi\rightarrowtriangle\psi^{\prime}$. The other ones are
straightforward or can be obtained in a similar manner. In the following, we
let $v^{\circ}_{\mathbf{v}}$ to stand for the counterpart of $\mathbf{v}_{j}$.

$\displaystyle\mathbf{v}_{1}(\psi\rightarrowtriangle\psi^{\prime})=1$
$\displaystyle\text{ iff
}\mathbf{v}_{1}(\psi)\leqslant\mathbf{v}_{1}(\psi^{\prime})$
$\displaystyle\text{ iff }v^{+}_{\mathbf{v}}(\psi)\subseteq
v^{+}_{\mathbf{v}}(\psi^{\prime})$ (by IH) $\displaystyle\text{ iff
}\mathfrak{M}_{\mathbf{v}}\vDash^{+}\psi\rightarrowtriangle\psi^{\prime}$
$\displaystyle\mathbf{v}_{2}(\psi\rightarrowtriangle\psi^{\prime})=1$
$\displaystyle\text{ iff }\mathbf{v}_{1}(\psi)=1\text{ and
}e_{2}(\psi^{\prime})=1$ $\displaystyle\text{ iff
}\mathfrak{M}_{\mathbf{v}}\vDash^{+}\psi\text{ and
}\mathfrak{M}_{\mathbf{v}}\vDash^{-}\psi^{\prime}$ (by IH)
$\displaystyle\text{ iff
}\mathfrak{M}_{\mathbf{v}}\vDash^{-}\psi\rightarrowtriangle\psi^{\prime}$
$\displaystyle\mathbf{v}_{1}(\psi\rightarrowtriangle\psi^{\prime})\leqslant\mathbf{v}_{j}(\phi^{\prime})$
$\displaystyle\text{ iff
}\left[\begin{matrix}\mathbf{v}_{1}(\psi)\leqslant\mathbf{v}_{1}(\psi^{\prime})\\\
\text{and}\\\
\mathbf{v}_{j}(\phi^{\prime})\geqslant\mathbf{v}_{2}(\mathbf{0})\end{matrix}\right]\text{
or }\left[\begin{matrix}\mathbf{v}_{1}(\psi)>\mathbf{v}_{1}(\psi^{\prime})\\\
\text{and}\\\
\mathbf{v}_{1}(\psi^{\prime})\leqslant\mathbf{v}_{j}(\phi^{\prime})\end{matrix}\right]$
$\displaystyle\text{ iff
}\left[\begin{matrix}v^{+}_{\mathbf{v}}(\psi)\subseteq
v^{+}_{\mathbf{v}}(\psi^{\prime})\\\ \text{and}\\\
v^{\circ}_{\mathbf{v}}(\phi^{\prime})\supseteq
v^{-}_{\mathbf{v}}(\mathbf{0})\end{matrix}\right]\text{ or
}\left[\begin{matrix}v^{+}_{\mathbf{v}}(\psi)\supsetneq
v^{+}_{\mathbf{v}}(\psi^{\prime})\\\ \text{and}\\\
v^{+}_{\mathbf{v}}(\psi^{\prime})\subseteq
v^{\circ}_{\mathbf{v}}(\phi^{\prime})\end{matrix}\right]$ (by IH)
$\displaystyle\text{ iff
}v^{+}_{\mathbf{v}}(\psi\rightarrowtriangle\psi^{\prime})\subseteq
v^{\circ}_{\mathbf{v}}(\phi^{\prime})$
$\displaystyle\mathbf{v}_{2}(\psi\rightarrowtriangle\psi^{\prime})\leqslant\mathbf{v}_{j}(\phi^{\prime})$
$\displaystyle\text{ iff
}\mathbf{v}_{1}(\psi)\leqslant\mathbf{v}_{j}(\phi^{\prime})\text{ or
}\mathbf{v}_{2}(\psi^{\prime})\leqslant\mathbf{v}_{j}(\phi^{\prime})$
$\displaystyle\text{ iff }v^{+}_{\mathbf{v}}(\psi)\subseteq
v^{\circ}_{\mathbf{v}}(\phi^{\prime})\text{ or
}v^{-}_{\mathbf{v}}(\psi^{\prime})\subseteq
v^{\circ}_{\mathbf{v}}(\phi^{\prime})$ (by IH) $\displaystyle\text{ iff
}v^{-}_{\mathbf{v}}(\psi\rightarrowtriangle\psi^{\prime})\subseteq
v^{\circ}_{\mathbf{v}}(\phi^{\prime})$

∎

######  Definition A.8 (Algebraic counterparts).

Let $\mathfrak{M}=\langle W,\preccurlyeq,v^{+},v^{-}\rangle$ be a
$\mathsf{G}^{2}(\rightarrowtriangle)$ model. We say that algebraic valuations
$v^{\mathfrak{M}}_{1}$ and $v^{\mathfrak{M}}_{2}$ are _counterparts_ of
$\mathfrak{M}$ if it holds that:

$\displaystyle v^{\mathfrak{M}}_{1}(p)=1$ $\displaystyle\text{ iff
}v^{+}(p)=W$ $\displaystyle v^{\mathfrak{M}}_{2}(p)=1$ $\displaystyle\text{
iff }v^{-}(p)=W$ $\displaystyle v^{\mathfrak{M}}_{1}(p)=0$
$\displaystyle\text{ iff }v^{+}(p)=\varnothing$ $\displaystyle
v^{\mathfrak{M}}_{2}(p)=0$ $\displaystyle\text{ iff }v^{-}(p)=\varnothing$
$\displaystyle v^{\mathfrak{M}}_{1}(p)\leqslant v^{\mathfrak{M}}_{1}(q)$
$\displaystyle\text{ iff }v^{+}(p)\subseteq v^{+}(q)$ $\displaystyle
v^{\mathfrak{M}}_{2}(p)\\!\leqslant\\!v^{\mathfrak{M}}_{2}(q)$
$\displaystyle\text{ iff }v^{-}(p)\subseteq v^{-}(q)$ $\displaystyle
v^{\mathfrak{M}}_{1}(p)\\!\leqslant\\!v^{\mathfrak{M}}_{2}(q)$
$\displaystyle\text{ iff }v^{+}(p)\\!\subseteq\\!v^{-}(q)$ $\displaystyle
v^{\mathfrak{M}}_{2}(p)\leqslant v^{\mathfrak{M}}_{1}(q)$ $\displaystyle\text{
iff }v^{-}(p)\\!\subseteq\\!v^{+}(q)$

######  Lemma A.9.

Let
$\phi,\phi^{\prime}\in\mathscr{L}_{\mathsf{G}^{2}(\rightarrow,\Yleft)}\cup\mathscr{L}_{\mathsf{G}^{2}(\rightarrowtriangle,\multimap)}$.
Then, for any $\mathsf{G}^{2}$-model
$\mathfrak{M}=\langle\mathbb{F},v^{+},v^{-}\rangle$ and any valuations
$v^{\mathfrak{M}}_{1}$ and $v^{\mathfrak{M}}_{2}$ induced by $\mathfrak{M}$,
it holds that:

$\displaystyle\mathfrak{M}\vDash^{+}\phi$ $\displaystyle\text{ iff
}v^{\mathfrak{M}}_{1}(\phi)=1$ $\displaystyle\mathfrak{M}\vDash^{-}\phi$
$\displaystyle\text{ iff }v^{\mathfrak{M}}_{2}(\phi)=1$ $\displaystyle
v^{+}(\phi)\subseteq v^{+}(\phi^{\prime})$ $\displaystyle\text{ iff
}e_{1}^{\mathfrak{M}}(\phi)\leqslant e_{1}^{\mathfrak{M}}(\phi^{\prime})$
$\displaystyle v^{-}(\phi)\subseteq v^{-}(\phi^{\prime})$ $\displaystyle\text{
iff }e_{2}^{\mathfrak{M}}(\phi)\leqslant e_{2}^{\mathfrak{M}}(\phi^{\prime})$
$\displaystyle v^{+}(\phi)\subseteq v^{-}(\phi^{\prime})$ $\displaystyle\text{
iff }e_{1}^{\mathfrak{M}}(\phi)\leqslant e_{2}^{\mathfrak{M}}(\phi^{\prime})$
$\displaystyle v^{-}(\phi)\subseteq v^{+}(\phi^{\prime})$ $\displaystyle\text{
iff }e_{2}^{\mathfrak{M}}(\phi)\leqslant e_{1}^{\mathfrak{M}}(\phi^{\prime})$

###### Proof.

Analogously to lemma A.7. ∎

We can now finally prove theorem 2.10.

###### Proof of theorem 2.10.

The soundness part can be easily proved by a routine check of axioms and
rules. Now, assume that
$\Gamma\not\models_{\mathsf{G}^{2}(\rightarrow,\Yleft)}\phi$. Then, by [5,
Proposition 5], there is a $\mathsf{G}^{2}$ valuation s.t.
$e_{1}[\Gamma]>e_{1}(\phi)$. Hence, by lemma A.7, there is a model
$\mathfrak{M}$ and $w\in\mathfrak{M}$ s.t. $\mathfrak{M},w\vDash^{+}[\Gamma]$
but $\mathfrak{M},w\nvDash^{+}\phi$. Thus, by theorem A.4, we obtain that
$\phi$ is not $\mathcal{H}\mathsf{G}^{2}(\rightarrow,\Yleft)$ derivable from
$\Gamma$.

The case of $\mathcal{H}\mathsf{G}^{2}(\rightarrowtriangle,\multimap)$ can be
tackled in a similar manner. ∎


# Scalable Monte Carlo for Bayesian Learning

Paul Fearnhead  Christopher Nemeth  Chris J. Oates and Chris Sherlock


# Single Image Dehazing via Model-based Deep-Learning

###### Abstract

Model-based single image dehazing algorithms restore images with sharp edges
and rich details at the expense of low PSNR values. Data-driven ones restore
images with high PSNR values but with low contrast, and even some remaining
haze. In this paper, a novel single image dehazing algorithm is introduced by
integrating model-based and data-driven approaches. Both transmission map and
atmospheric light are initialized by the model-based methods, and refined by
deep learning based approaches which form a neural augmentation. Haze-free
images are restored by using the transmission map and atmospheric light.
Experimental results indicate that the proposed algorithm can remove haze well
from real-world and synthetic hazy images.

##  1 Introduction

Due to the effect of light scattering through small particles accumulated in
the air, hazy images suffer from contrast loss of captured objects [1], color
distortion [2], and reduction of dynamic range [3]. Existing works on high-
level image analysis such as object detection might not perform well on the
hazy images, especially for those real-world hazy images with heavy haze [5].
It is highly demanded to study single image dehazing.

Single image dehazing is widely studied in the fields of image processing and
computer vision. Two popular types of single image dehazing algorithms are
model-based ones [2, 4, 5] and data-driven ones [6, 7]. The model-based ones
are on top of the Koschmieders law [8]. They can improve the visibility of
real-world hazy images well regardless of haze degree but they cannot achieve
high PSNR and SSIM values on the synthetic sets of hazy images. On the other
hand, the data-driven ones perform well on the synthetic sets while their
performance is poor for real-world hazy images, especially for those hazy
images with heavy haze [9]. It is thus desired to have a single image dehazing
algorithm which is applicable for both the synthesized and the real-world hazy
images.

In this paper, a novel single image dehazing algorithm is proposed by fusing
the model-based and data-driven approaches. Same as the model-based methods in
[2, 4, 5], both the atmospheric light and the transmission map are required by
the proposed algorithm. They are initialized by using model-based methods, and
refined by data-driven approaches. The atmospheric light is initialized by
using the hierarchical searching method in [11] which is based on the
Koschmieders law [8]. The transmission map is initialized by using the dark
direct attenuation prior (DDAP) in [5] which is extended from the dark channel
prior (DCP) in [2]. The DDAP is applicable to all the pixels in the hazy
image. Unfortunately, the initial transmission map suffers from morphological
artifacts. The morphological artifacts caused by the DDAP are then reduced by
the novel haze line averaging algorithm in [5] which is designed by using the
concept of haze line in [4].

The initial atmospheric light and transmission map are then refined via the
popular generative adversarial network (GAN) [21]. The initial atmospheric
light and transmission map can be regarded as noisy atmospheric light and
transmission map. The main function of the GAN is to reduce the noise of the
initial atmospheric light and transmission map. Following this idea, the
generator of the proposed GAN is constructed on top of the latest DNN for
noise reduction in [12] and the Res2Net in [13]. The discriminator of the
proposed GAN is based on the PatchGAN in [14]. The proposed GAN is trained by
using 500 hazy images from the multiple real-world foggy image defogging
(MRFID) dataset in [9] and 500 hazy images from the realistic single image
dehazing (RESIDE) dataset in [23]. Existing data-driven dehazing algorithms
such as [6, 7, 16, 22] are trained by using more than 10K hazy images in the
RESIDE dataset in [23]. All ground-truth images of haze-free images in the
RESIDE dataset [23] are available. The $l_{1}$ loss function is applied to
measure the restored images for those hazy images in the RESIDE dataset. There
is a clean image for each hazy image in the MRFID dataset but the clean and
hazy images are captured under different lighting conditions. A new loss
function is derived by using an extreme channel which is extended from the
dark channel in [2]. The new loss function, the adversarial loss function
[21], and one more new loss function on the gradient of the restored image are
used to measure the restored images for the hazy images in the MRFID dataset.
The model-based estimation and the data-driven refinement forms a neural
augmentation which can be applied to improve the interpretability of pure
data-driven approaches [15]. Experimental results show that the proposed
algorithm outperforms existing data-driven dehazing algorithms for the real-
world hazy images from the dehazing quality index (DHQI) point of view [24].
The main contribution is to propose a novel model-based deep learning
framework which is applicable to the synthetic and real-world hazy images. The
number of training data can be reduced significantly.

Fig. 1: The proposed framework for single image dehazing via model-based GAN.
The generator is on top of the DNN in [12], and the discriminator is based on
the PatchGAN in [14]. Each dual attention block (DAB) [12] contains spatial
attention and channel attention modules. The Recursive Residual Group (RRG)
contains several DABs and one $3*3$ convolution layers.

##  2 Neural Augmented Single Image Dehazing

The atmospheric light $A$ and the transmission map $t$ are initialized by
model-based methods, and refined by data-driven approaches. The model-based
and data-driven approaches form a neural augmentation [15]. The overall
framework is shown in Figure 1.

###  2.1 Model-Based Initialization of $A$ and $t$

In this work, the hierarchical searching method in [11] is adopted to estimate
an initial value of the $A$. The hazy image $Z$ is divided into four
rectangular regions. The score of each region is defined as the mean of the
average pixel value subtracted by the standard deviation of the pixel values
within the region along all the color channels. The region with the highest
score is further divided into four smaller regions. This process is repeated
until the size of the selected region is smaller than a pre-specified
threshold. Within the finally selected region, the color vector, which
minimizes the distance $\|[Z_{r}(p)-255,Z_{g}(p)-255,Z_{b}(p)-255]\|$ is
selected as the atmospheric light. The initial $A$ might not be very accurate,
it will be refined by using a data-driven approach.

The dark direct attenuation of a hazy image $Z$ is defined as

$\displaystyle\psi_{\rho}(I*t)(p)=\min_{p^{\prime}\in\Omega_{\rho}(p)}\\{\min_{c\in\\{R,G,B\\}}\\{I_{c}(p^{\prime})t(p^{\prime})\\}\\},$
(1)

where $\Omega_{\rho}(p)$ is a square window centered at the pixel $p$ of a
radius $\rho$ which is usually selected as 7. By assuming that
$\psi_{\rho}(I*t)(p)$ is zero, an initial transmission map $t_{0}(p)$ is
estimated as [5]

$\displaystyle t_{0}(p)=1-\psi_{\rho}(\frac{Z}{A})(p).$ (2)

(a) a hazy image

(b) an initial transmission map

(c) a refined transmission map by haze line averaging

(d) a dehazed image by initial transmission map

(e) a dehazed image by refined transmission map

Fig. 2: Comparison of initial and refined transmission maps as well as their
corresponding dehazed images.

As shown in Figure 2(c), there are visibly morphological artifacts in the
restored image $I$ if the $t_{0}(p)$ is directly applied to remove the haze
from the hazy image $Z$. The haze line averaging method in [5] is adopted to
reduce the morphological artifacts. The haze line $H(p^{\prime\prime})$ is
defined as [4]

$\displaystyle
H(p^{\prime\prime})=\\{p|\sum_{c\in\\{R,G,B\\}}|I_{c}(p^{\prime\prime})-I_{c}(p)|=0\\}.$
(3)

Each haze line can be identified by using a color shift hazy pixel which is
defined as $(Z(p)-A)$. The $(Z(p)-A)$ can be converted into the spherical
coordinates, $[r(p)$, $\theta(p)$, $\psi(p)]$ where $\theta(p)$ and $\psi(p)$
are the longitude and latitude, respectively, and $r(p)$ is $\|Z(p)-A\|$. The
morphological artifacts can be reduced by the following haze line averaging
[5]:

$\displaystyle t(p)=\frac{\sum_{p^{\prime}\in
H_{s}(p^{\prime\prime})}t_{0}(p^{\prime})}{\sum_{p^{\prime}\in
H_{s}(p^{\prime\prime})}r(p^{\prime})}r(p),$ (4)

where $H_{s}(p^{\prime\prime})$ is a subset of $H(p^{\prime\prime})$, and it
is obtained as follows:

A 2-D histogram binning of $\theta$ and $\psi$ with uniform edges in the range
$[0,2\pi]\times[0,\pi]$ is adopted to generate an initial set of
$H(p^{\prime\prime})$. The bin size is chosen as $\pi/720\times\pi/720$. An
upper bound $\nu$ is defined for the cardinality of the final sets. It is
usually selected as 200. Let $|H(p^{\prime\prime})|$ be the cardinality of the
set $H(p^{\prime\prime})$. The set $H(p^{\prime\prime})$ is dividedly into
$\max\\{1,|H(p^{\prime\prime})|/\nu)$ sub-sets for a non-empty
$H(p^{\prime\prime})$.

As illustrated in Figure 2(d), the morphological artifacts are significantly
reduced by the haze-line averaging (4). The remaining artifacts will be
removed by a data-driven refinement.

###  2.2 Data-Driven Refinement of $A$ and $t$

Instead of using edge-preserving smoothing filters [18, 19, 20], the GAN [21]
is utilized to refine the atmospheric light and transmission map. The
generator is thus built up on top of the latest DNN for the noise reduction in
[12]. The residual net in [12] is replaced by the Res2Net in [13] to well
capture the multi-scale features at a granular level. Both spatial attention
module and channel attention module are utilized in [12]. The modules can
suppress the less useful features and only allow the propagation of more
informative ones. Therefore, they effectively deal with the uneven
distribution of haze. The DNN for the atmospheric light is simpler than the
DNN for the transmission map. The discriminator is based on the PatchGAN in
[14].

The proposed GAN is trained by using 500 images from the MRFID dataset [9] and
500 images with heavy haze regenerated from the RESIDE dataset [23]. Depth is
estimated by using the algorithm in [26]. The scattering coefficients of 100
images are randomly generated in $[1.2,2.0]$ and those of the others are
randomly selected in $[2.5,3.0]$. The $A_{c}$’s are randomly generated in
$[0.625,1.0]$ for the color channel $c$ independently. The heavily hazy images
are then generated by using the Koschmieders law [8]. The MRFID dataset
contains foggy and clean images of 200 outdoor scenes in different lighting
conditions. For each scene, one clear image and four foggy images of different
densities defined as slightly, moderately, highly, and extremely foggy, are
manually selected from images taken from these scenes over the course of one
calendar year.

Loss functions also play an important role in the proposed GAN. Since both the
atmospheric light $A$ and transmission map $t(p)$ are refined by the GAN, the
loss functions are defined by using the following restored image $I$:

$I(p)=\frac{Z(p)-A}{\max\\{t(p),0.1\\}}+A.$ (5)

Due to the different lighting conditions, the conventional loss functions
include $l_{1}$ and $l_{2}$ loss functions are not applicable for the MRFID
dataset. A new loss function is proposed by introducing an extreme channel.
Let $\bar{I}_{c}(p)$ be defined as

$\displaystyle\bar{I}_{c}(p)=\min\\{I_{c}(p),255-I_{c}(p)\\},$ (6)

and the extreme channel of the image $I$ is defined as

$\displaystyle\phi_{\rho}(I)(p)=\psi_{\rho}(\bar{I})(p).$ (7)

Considering a pair of hazy image $Z$ and clean image $T$ which are captured
from the same scene, the corresponding haze-free image of the hazy image is
$I$. It can be known from the conventional imaging model [25] that

$\displaystyle[T(p),I(p)]=[L_{T}(p),L_{I}(p)]R(p),$ (8)

where $L_{T}(p)$ and $L_{I}(p)$ are the intensities of ambient light when the
images $T$ and $I$ are captured. $R(p)$ is the ambient reflectance coefficient
of surface, and it highly depends on the smoothness or texture of the surface.
Since both the $L_{T}(p)$ and $L_{I}(p)$ are constant in a small neighborhood,
$\phi_{\rho}(I)(p)$ and $\phi_{\rho}(T)(p)$ are usually determined by the
reflection $R(p)$. Thus, it follows that

$\displaystyle\phi_{\rho}(I)(p)\approx\phi_{\rho}(T)(p)\approx 0.$ (9)

The extreme channel of the restored image is required to match that of the
clean image $T$ while the dark channel of the restored image is required to be
zero in [27]. Let $W$ and $H$ be the width and height of the image $I$,
respectively. The first new loss function for the MRFID dataset is defined by
using the extreme channels $\phi_{\rho}(I)$ and $\phi_{\rho}(T)$ as

$L_{e}=\frac{1}{WH}\sum_{p}\|\phi_{\rho}(I)(p)-\phi_{\rho}(T)\|_{2}^{2}.$ (10)

Table 1: Average PSNR value of 500 outdoor hazy images in the SOTS for different algorithms $\uparrow$. |  FD-GAN [22] |  RefineDNet [16] |  FFA-Net [6] |  PSD [10] |  DCP [18] |  HLP [4] |  MSBDN [7] | Ours
---|---|---|---|---|---|---|---|---
PSNR | 20.78 | 20.80 | 32.13 | 15.15 | 17.49 | 18.06 | 30.25 | 21.42
Table 2: Average DHQI values of 79 real-world outdoor hazy images for different algorithms $\uparrow$. |  FD-GAN [22] |  RefineDNet [16] |  FFA-Net [6] |  PSD [10] |  DCP [18] |  HLP [4] |  MSBDN [7] | Ours
---|---|---|---|---|---|---|---|---
DHQI | 51.00 | 57.57 | 55.33 | 50.60 | 51.92 | 52.75 | 54.32 | 58.88
Fig. 3: Comparison of different haze removal algorithms. From left to right,
hazy images, dehazed images by FD-GAN [22], RefineDNet [16], FFA-Net [6], PSD
[10], DCP [18], HLP [4], MSBDN [7], and ours, respectively.

Let $\nabla_{h}$ and $\nabla_{v}$ represent the horizontal and vertical
gradients, respectively. The gradients of the restored image are required to
approach those of the clean image $T$ rather than zeros as in [27]. The second
new loss function for the MRFID dataset is defined by using the gradients of
the restored image $I$ and clean image $T$ as

$\displaystyle
L_{t}=\frac{{\displaystyle\sum_{p,c}}(|\nabla_{h}I_{c}(p)-\nabla_{h}T_{c}(p)|+|\nabla_{v}I_{c}(p)-\nabla_{v}T_{c}(p)|)}{WH}.$
(11)

The third adversarial loss function for the MRFID dataset is defined by using
the restored image $I$ and clean image $T$ as

$\displaystyle L_{adv}=\log(D(T))+\log(1-D(I)).$ (12)

The overall loss function for the images in the MRFID dataset is defined as

$L_{d,1}=w_{adv}L_{adv}+w_{e}L_{e}+L_{t},$ (13)

where $w_{adv}$ and $w_{e}$ are two constants, and they are empirically
selected as 1 and 200, respectively.

Since the ground-truth image $T$ is available in the RESIDE dataset, the loss
function for the image in the RESIDE dataset is defined as

$\displaystyle L_{d,2}=\frac{1}{WH}\sum_{p}|I(p)-T(p)|.$ (14)

  1. Step 1.

Initialize the atmospheric light $A_{0}$ from the hazy image $Z$ using the
method in [11].

  2. Step 2.

Initialize the transmission map $t_{0}$ by using the DDAP as in the equation
(2).

  3. Step 3.

Reduce the morphological artifacts of $t_{0}$ via the nonlocal haze line
averaging (4).

  4. Step 4.

Refine the atmospheric light and transmission map using the GAN in Figure 1.

  5. Step 5.

Restore the haze free image $I$ via the equation (5).

Algorithm 1 Model-based single image deep dehazing

The atmospheric light $A$ and transmission map $t(p)$ in the equation (4) are
refined by minimizing the following overall loss function in each batch:

$\displaystyle L_{d}=w_{R}L_{d,2}+L_{d,1},$ (15)

where $w_{R}$ is a constant, and it is empirically selected as 100 in this
paper. The proposed algorithm is summarized in algorithm 1.

##  3 Experimental Results

Due to the space limitation, the ablation studies are omitted and this section
focuses on comparing the proposed dehazing algorithm with seven state-of-the-
art ones including RefineDNet [16], FFA-Net [6], PSD [10], DCP [18], HLP [4],
MSBDN [7], and FD-GAN [22]. The algorithms HLP [4] and DCP [18] are model-
based algorithms, the proposed algorithm and the RefineDNet [16] are
combination of model-based and data-driven approaches, while the others are
data-driven algorithms. All the results in [16, 6, 10, 7, 22] are generated by
their publicly shared codes.

The proposed GAN is trained by using the hazy and clean images of 125 outdoor
scenes from the MRFID dataset with the corresponding 500 hazy images as well
as 500 images with heavy haze generated using ground-truth images from the
RESIDE dataset [23]. The hazy images of the 25 outdoor scenes from the MRFID
dataset with the corresponding 100 hazy images are selected as the the
validation set. All these hazy images are randomly selected from the two
datasets. The test images comprise 500 outdoor hazy images in the synthetic
objective testing set (SOTS) [23], and 79 real-world hazy images in [5] which
include 31 images are from the RESIDE [23], and the 19 images from [17] and
the Internet.

The PSNR is first adopted to compare all these algorithms by using 500
synthetic outdoor hazy images in the SOTS [23]. The average PSNR values of the
eight algorithms are given in Table 1. The FFA-Net [6] and MSBDN [7] are two
CNN based dehazing algorithms, and they are optimized to provide high PSNR
values on the SOTS dataset. Both the proposed algorithm and the algorithm in
[16] indeed outperform the model-based algorithms in [4] and [18].

The quality index DHQI in [24] is then adopted to compare all these algorithms
by using the 79 real-world hazy images in [5]. The average DHQI values of the
79 real-world outdoor hazy images are given in Table 2. The proposed algorithm
outperforms others from the DHQI point of view.

Finally, all these dehazing algorithms are compared subjectively as in Figure
3. Readers are invited to view to electronic version of figures and zoom in
them so as to better appreciate differences among all the images. Although the
FFA-Net [6] and MSBDN [7] achieve high PSNR and SSIM values for the synthetic
hazy images, their restored results are blurry for real-world hazy images and
the dehazed images are not photo-realistic. In addition, the haze is not
reduced well if it is heavy. The DCP [18], HLP [4], RefineDNet [16], FD-GAN
[22], and the proposed algorithm can be applied to generate photo realistic
images. There are visible morphological artifacts in the restored images by
the PSD [10], RefineDNet [16] and FD-GAN [22]. Textures generated by the
RefineDNet [16] and FD-GAN [22] are different from the real ones. The DCP [18]
and HLP [4] restore more vivid and sharper images at the expense of amplified
noise in sky regions. All these problems are overcome by the proposed
algorithm.

##  4 Conclusion Remarks and Discussion

A new single image dehazing algorithm is introduced in this paper. Both
transmission map and atmospheric light are obtained by a neural augmentation
which consists of model-based estimation and data-driven refinement. They are
then applied to restore a haze-free image. Experimental results validate that
the proposed algorithm removes haze well from the synthetic and real-world
hazy images. The proposed neural augmentation reduces the number of training
data significantly. This paper focused on day-time hazy images. The proposed
framework will be extended to study night-time hazy images [28] in our future
research.

## References

  * [1] S. G. Narasimhan and S. K. Nayar,  “Contrast restoration of weather degraded images,”  IEEE Trans. On Pattern Analysis and Machine Learning, vol. 25, no. 6, pp. 713-724, Jun. 2003.
  * [2] K. He, J. Sun, and X. Tang,  “Single image haze removal using dark channel prior,”  IEEE Trans. On Pattern Analysis and Machine Learning, vol 33, no. 12, pp. 2341-2353, Dec. 2011.
  * [3] F. Kou, Z. Wei, W. Chen, X. Wu, C. Wen, and Z. Li,  “Intelligent detail enhancement for exposure fusion,”  IEEE Trans. on Multimedia, vol. 20, no. 2, pp. 484–495, Feb. 2018.
  * [4] D. Berman, T. Treibitz, and S. Avidan,  “Non-local Image Dehazing,”  in IEEE Conference on Computer Vision and Pattern Recognition, pp. 1674-1682, 2016.
  * [5] Z. Li, H. Shu, and C. Zheng,  “Multi-scale single image dehazing Using Laplacian and Gaussian pyramids,”  IEEE Trans. on Image Processing, vol. 30, no. 12, pp. 9270-9279, Dec. 2021.
  * [6] X. Qin, Z. Wang, Y. Bai, X. Xie, and H. Jia,  “FFA-Net: Feature fusion attention network for single image dehazing,”  in AAAI Conference on Artificial Intelligence, pp. 11908-11915, 2020.
  * [7] H. Dong, J. Pan, L. Xiang, Z. Hu, X. Zhang, F. Wang, and M. Yang,  “Multi-scale boosted dehazing network with dense feature fusion,”  in IEEE Conference on Computer Vision and Pattern Recognition, 2020.
  * [8] H. Koschmieder,  “Theorie der horizontalen sichtweite,”  in Proc. Beitrage Phys. Freien Atmos., pp. 33-53, 1924.
  * [9] W. Liu, F. Zhou, T. Lu, J. Duan, and G. Qiu,  “Image defogging quality assessment: real-world database and method,”  IEEE Trans. on Image Processing, 30, pp. 176-190, 2021.
  * [10] Z. Chen, Y. Wang, Y. Yang, and D. Liu,  “PSD: principled synthetic-to-real dehazing guided by physical priors,”  in IEEE Conference on Computer Vision and Pattern Recognition, 2021.
  * [11] J. H. Kim, W. D. Jang, J. Y. Sim, and C. F. Kim,  “Optimized contrast enhancement for real-time image and video dehazing,”  Journal of Visual Communication and Image Representation, vol. 24, no. 3, pp. 410-425, Apr. 2013.
  * [12] S. Zamir, A. Arora, S. Khan, M. Hayat, F. Khan, M. Yang, and L. Shao,  “CycleISP: real image restoration via improved data synthesis,”  in IEEE/CVF Conference on Computer Vision and Pattern Recognition, 2020.
  * [13] S. Gao, M. Cheng, K. Zhao, X. Zhang, M. Yang, and P. Torr,  “Res2Net: A New Multi-scale Backbone Architecture,”  IEEE Trans. on Pattern Analysis and Machine Intelligence, vol. 43, no. 2, pp. 652-662, Feb. 2021.
  * [14] P. Isola, J. Zhu, T. Zhou, and A. A. Efros,  “Image-to-image translation with conditional adversarial networks,”  in IEEE/CVF Conference on Computer Vision and Pattern Recognition, pp. 1125-1134, 2017.
  * [15] N. Shlezinger, J. Whang, Y. C. Eldar, and A. G. Dimakis,  “Model-based deep learning,”  arXiv Preprint, arXiv: 2012.08405v2, Jun 2021.
  * [16] S. Zhao, L. Zhang, Y. Shen, and Y. Zhou,  “RefineDNet: A weakly supervised refinement framework for single image dehazing,”  IEEE Trans. on Image Processing, vol. 30, pp. 3391-3404, 2021.
  * [17] R. Fattal,  “Dehazing using color-lines,”  ACM Trans. on Graphics, vol 34, no. 1, articale 13, Jan. 2014.
  * [18] K. He, J. Sun, and X. Tang,  “Guided image filtering,”  IEEE Trans. on Pattern Analyis and Machine Intelligence, vol. 35, no. 6, pp. 1397-1409, Jun. 2013.
  * [19] Z. Li, J. Zheng, Z. Zhu, W. Yao, and S. Wu,  “Weighted guided image filtering,”  IEEE Trans. on Image Processing, vol. 24, no. 1, pp. 120-129, Jan. 2015.
  * [20] F. Kou, W. Chen, Z. Li, and C. Wen,  “Content adaptive image detail enhancement,”  IEEE Signal Processing Letters, vol. 22, no. 2, pp. 211-215, Feb. 2015.
  * [21] I. J. Goodfellow, J. Pouget-Abadie, M. Mirza, B. Xu, D. Warde-Farley, S. Ozair, A. Courville, and Y. Bengi,  “Generative adversarial nets,”  in NIPS 2014, Canada, Dec. 2014.
  * [22] Y. Dong, Y. Liu, H. Zhang, S. Chen, and Y. Qiao,  “FD-GAN: Generative adversarial networks with fusiondiscriminator for single image dehazing,”  in Proceedings of the AAAI Conference on Artificial Intelligence, pp. 10728-10736, 2020.
  * [23] B. Li, W. Ren, D. Fu, D. Tao, D. Feng, W. Zeng, and Z. Wang,  “Benchmarking single-image dehazing and beyond,”  IEEE Trans. on Image Processing vol. 28, no. 1, pp. 492-505, Jan. 2018.
  * [24] X. Min, G. Zhai, K. Gu, X. Yang, and X. Guan,  “Objective quality evaluation of dehazed images,”  IEEE Trans. on Intelligent Transportation Systems, vol. 20, no. 8, pp. 2879-2892, Aug. 2019.
  * [25] R. C. Gonzalez and R. E. Woods,  Digital Image Processing,  Englewood Cliffs, NJ, USA: Prentice-Hall, 2002.
  * [26] Z. Li and N. Snavely,  “Megadepth: Learning single-view depth prediction from internet photos,”  in IEEE/VF Conference on Computer Vision and Pattern Recognition, pp. 2041–2050, 2018.
  * [27] L. Li, Y. Dong, W. Ren, J. Pan, C. Gao, N. Sang, and M. Yang,  “Semi-supervised image dehazing,”  IEEE Trans. on Image Processing, vol. 29, pp. 2766-2779, 2020.
  * [28] M. Yang, J. Liu, and Z. Li,  “Super-pixel based single nighttime image haze removal,”  IEEE Trans. on Multimedia, vol. 20, no. 11, pp. 3008-3018, Nov. 2018.


# Rcall: Calling R from MATLAB

Janine Egert1,2 Corresponding author<EMAIL_ADDRESS>Clemens
Kreutz1,2,3

(${}^{\text{\sf 1}}$ Institute of Medical Biometry and Statistics (IMBI),
Faculty of Medicine and Medical Center Freiburg - University of Freiburg,
79104 Freiburg, Germany
${}^{\text{\sf 2}}$ Centre for Integrative Biological Signalling Studies
(CIBSS), University of Freiburg, 79104 Freiburg, Germany
${}^{\text{\sf 3}}$ Center for Data Analysis and Modelling (FDM), University
of Freiburg, 79104 Freiburg, Germany)

##  1 Introduction

With the computational and methodological advances in the field of
bioinformatics, more and more software tools and functionalities have been
developed in the last decades. However, many algorithms are available only for
one programming language, which prevents rapid implementation of state-of-the-
art methods. Translating existing tools or switching software is undesirable
and time consuming. For this reason, there is a need for function interfaces
that enable evaluations of functions implemented in another programming
language.

MATLAB is one of the most commonly used software in disciplines such as
engineering and physical science. R is one of the most frequently applied
programs for statistical computations. An R interface for MATLAB combines the
full feature set of two high-level scientific computing software. Especially
due to the large community effort in R, an R interface greatly augments the
MATLAB programming language with existing well-tested and approved algorithms
from the R community.

Existing R interfaces for the MATLAB environment are restricted to specific
operating systems, e.g. the package ’RMatlab’ ([Lang and Liu, 2004]) for Unix
systems, the R server ’Rserve’ ([Urbanek, 2019]) for Unix and Mac OS systems,
and the ’MATLAB R-link’ ([Henson, 2021]) for Windows systems. The ’MATLAB
R-link’ is based on the ’R (D)COM’ server ([Baier, 2008]). However, the non-
commercial version of the ’R (D)COM’ server on the CRAN archive is deprecated.

We present the Rcall interface, which allows the direct application of R
packages within the MATLAB environment. Other R function interfaces are more
flexible and allow calling R from multiple programming languages (’MATLAB
R-link’) or also support bidirectional functionality (’RMatlab’). Rcall aims
to extend the available methods for communicating with R by offering a well-
functioning and easy-to-integrate interface for all common operating systems,
in particular also Windows systems are supported.

##  2 Implementation

Figure 1: A. The main steps of the Rcall implementation are described and
illustrated. The main functions of the Rcall interface are highlighted by a
light orange box. Usage of the R package ’limma’ is shown as an example. B.
The volcano plot of the exemplary differential expression analysis. The top
two genes are highlighted by their gene ID.

Once the Rcall folder is added to the MATLAB path, Rcall is available in any
MATLAB script without any installation procedure. The only requirements for
Rcall are running MATLAB and R installations including the applied R packages.

The implementation consists of five main steps which are illustrated in Figure
1. First, Rcall is initialized (1). The R libraries, R path, and the path to
the R libraries can be specified by the user, otherwise the current system
configurations are used automatically. In Windows, the latest R version on the
system is located. The variables defined in the Rpush command are stored in a
temporary MATLAB workspace file by using the ‘R.Matlab’ package ([Bengtsson
and Jacobson, 2018]) (2). With each call of the Rpush command the temporary
MATLAB workspace file is expanded.

The R commands are specified in MATLAB by the Rrun function as string argument
(3). To reduce the amount of computation required to invoke a new R instance,
the R commands are cached until a result is demanded via the Rpull function.
In the Rpull function, the cached R commands are passed to an automatically
generated, temporary R script. The R script is executed in batch mode via a
system command line call (4). To reduce computation time, only R commands
defined between two Rpull calls are evaluated. The input arguments of the
Rpull function specify which output variables are passed to a result MATLAB
workspace file by the ’R.Matlab’ package. All data types which are supported
by the ’R.Matlab’ package such as scalar, vector, matrix, array, character,
etc. can be transferred. To extend the functionality of the ’R.Matlab’
package, Rcall assigns corresponding data types such as R data.frames to
MATLAB tables and R lists to MATLAB structures, vice versa in the Rpush
command. An overview of the data type conversion is depicted in Table 1.

MATLAB | R
---|---
Numeric | Numeric
Character | Character
Cell | List
Structure | List
Table | Data frame
Table 1: Data type conversion implemented in Rcall.

Nested object and list export is supported by passing the object elements
individually to MATLAB and then combining them into a MATLAB structure. In
general, and especially for large or complex objects, it is more efficient to
extract relevant information from the R object and pass only the extracted
results to MATLAB. To facilitate error handling, error messages that appear in
R are forwarded to the MATLAB console. All scripts and variables remain stored
until they are cleared by the Rclear function (5). This allows repeated use of
the variables and objects.

To illustrate the use of Rcall, a differential expression analysis is
performed on a simulated gene expression data set using the R package ’limma’
([Ritchie et al., 2015]). The data $dat$ consists of 10,000 log2-normally
distributed genes which consists of two probes with three replicates each. In
the second probe, 10 % of the genes are differentially expressed. The
predictor vector $grp$ corresponds to the sample indices. The lmFit R function
fits a linear model to the gene expression data. The empirical Bayes adjusted
t-statistics and p-values are calculated by the eBayes R function. To
visualize the differential expression, a volcano plot is generated and saved
using the pdf R function (Figure 1B.).

The results of the linear model fit are stored in a MArrayLM object fit which
is converted to a structure fitM in MATLAB. Alternatively, the object elements
can be assigned to separate variables and transferred individually to MATLAB
which is shown in the example for the p-values p. The execution of the example
code took on average 4.5 seconds real time in MATLAB and 0.8 seconds in R on a
2.60GHz Intel(R) Core(TM) i5-7300U with 4 logical processors.

Rcall invokes any R functionality from object creation to code evaluation and
visualization. Major limitations of Rcall are that R commands requiring user
input are not supported, complex data objects can only be transferred if
conversion to list is possible, and rendering of graphics is restricted to
functionality that is provided by R in batch mode.

##  3 Conclusion

Rcall enables the direct execution of R commands from the MATLAB work
environment. Evaluation and transfer of basic data types is supported. The
variables are converted to the corresponding data types in R and MATLAB. Rcall
provides an easy-to-integrate system and due to its execution in batch mode,
it works on all major operating systems (Unix, MS Windows, Mac OS X) without
sophisticated installation or configuration procedures. No special settings or
third party software are required.

##  4 Funding

This work has been supported by the Federal Ministry of Education and Research
of Germany [EA:Sys,FKZ031L0080 to J.E. and C.K.]; and the Excellence
Initiative of the German Federal and State Governments [CIBSS-
EXC-2189-2100249960-390939984 to C.K.].

##  5 Conflict of Interest

The authors declare that there is no conflict of interest regarding the
publication of this article.

## References

  * [Baier, 2008] Baier, T. (2008).  R/Scilab (D)Com Server 3.0-1B5.  https://cran.r-project.org/contrib/extra/dcom/.
  * [Bengtsson and Jacobson, 2018] Bengtsson, H. and Jacobson, A. (2018).  R.matlab: Read and Write MAT Files and Call MATLAB from Within R.  https://CRAN.R-project.org/package=R.matlab.
  * [Henson, 2021] Henson, R. (2021).  MATLAB R-link.  MATLAB Central File Exchange.  https://www.mathworks.com/matlabcentral/fileexchange/ 5051-matlab-r-link.
  * [Lang and Liu, 2004] Lang, D. T. and Liu, B. (2004).  RMatlab (Version 0.2-5).  http://www.omegahat.net/RMatlab/.
  * [Ritchie et al., 2015] Ritchie, M. E., Phipson, B., Wu, D., Hu, Y., Law, C. W., Shi, W., and Smyth, G. K. (2015).  limma powers differential expression analyses for RNA-sequencing and microarray studies.  Nucleic Acids Research, 43(7):e47.
  * [Urbanek, 2019] Urbanek, S. (2019).  Rserve: Binary R server.  https://CRAN.R-project.org/package=Rserve.