Skip to content

Commit

Permalink
bumb new version
Browse files Browse the repository at this point in the history
  • Loading branch information
mcosovic committed May 27, 2022
1 parent e20632b commit e6be682
Show file tree
Hide file tree
Showing 26 changed files with 204 additions and 445 deletions.
8 changes: 2 additions & 6 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
name = "FactorGraph"
uuid = "528f6532-2a0d-4856-bb42-8bcefd89a10f"
authors = ["Mirsad Cosovic <[email protected]>"]
version = "0.1.3"
version = "0.1.4"

[deps]
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
XLSX = "fdbf4ff8-1666-58a4-91e7-1b58723a45e0"

[compat]
HDF5 = "0.15.6"
PrettyTables = "1.1.0"
XLSX = "0.7.6"
PrettyTables = "1.3.1"
julia = "1.6"
56 changes: 24 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,12 @@ To use FactorGraph package, add the following code to your script, or alternativ
```julia-repl
using FactorGraph
```
---

---

#### Quick start whitin continuous framework
The following examples are intended for a quick introduction to FactorGraph package within the continuous framework.

- Synchronous message passing schedule using the vanilla GBP algorithm:
```julia-repl
using FactorGraph
gbp = continuousModel("data33_14.h5") # initialize the graphical model using HDF5 input
for iteration = 1:200 # the GBP inference
messageFactorVariable(gbp) # compute messages using the vanilla GBP
messageVariableFactor(gbp) # compute messages using the vanilla GBP
end
marginal(gbp) # compute marginals
displayData(gbp) # show results
```

- Synchronous message passing schedule using the broadcast GBP algorithm:
```julia-repl
using FactorGraph
Expand All @@ -59,22 +46,7 @@ for iteration = 1:50 # the GBP inference
messageVariableFactorBroadcast(gbp) # compute messages using the broadcast GBP
end
marginal(gbp) # compute marginals
```

- Synchronous message passing schedule using broadcast GBP with Kahan–Babuška algorithm with the plotting of the marginal mean through iterations:
```julia-repl
using FactorGraph
using Plots
gbp = continuousModel("data33_14.h5") # initialize the graphical model
x6 = [] # save the state variable marginal
for iteration = 1:50 # the GBP inference
messageFactorVariableKahan(gbp) # compute messages using the GBP with Kahan-Babuska
messageVariableFactorKahan(gbp) # compute messages using the GBP with Kahan-Babuska
marginal(gbp) # compute marginals
push!(x6, gbp.inference.mean[6]) # save state variable marginal
end
plot(collect(1:50), x6) # show plot
displayData(gbp) # show results
```

- Synchronous message passing schedule using the vanilla GBP algorithm in the dynamic framework:
Expand Down Expand Up @@ -163,7 +135,28 @@ Following examples are intended for a quick introduction to FactorGraph package
```julia-repl
using FactorGraph
bp = discreteTreeModel("discrete6_4.xlsx") # initialize the tree graphical model
probability1 = [1]
table1 = [0.2; 0.3; 0.4; 0.1]
probability2 = [1; 2; 3]
table2 = zeros(4, 3, 1)
table2[1, 1, 1] = 0.2; table2[2, 1, 1] = 0.5; table2[3, 1, 1] = 0.3; table2[4, 1, 1] = 0.0
table2[1, 2, 1] = 0.1; table2[2, 2, 1] = 0.1; table2[3, 2, 1] = 0.7; table2[4, 2, 1] = 0.1
table2[1, 3, 1] = 0.5; table2[2, 3, 1] = 0.2; table2[3, 3, 1] = 0.1; table2[4, 3, 1] = 0.1
probability3 = [4; 2]
table3 = zeros(2, 3)
table3[1, 1] = 0.2; table3[2, 1] = 0.8
table3[1, 2] = 0.5; table3[2, 2] = 0.5
table3[1, 3] = 0.5; table3[2, 3] = 0.5
probability4 = [4]
table4 = [0.4; 0.6]
probability = [probability1, probability2, probability3, probability4]
table = [table1, table2, table3, table4]
bp = discreteTreeModel(probability, table) # initialize the tree graphical model
while bp.graph.forward # inference from leaves to the root
forwardVariableFactor(bp) # compute forward messages
forwardFactorVariable(bp) # compute forward messages
Expand All @@ -175,7 +168,6 @@ end
marginal(bp) # compute normalized marginals
```


[documentation-badge]: https://github.com/mcosovic/FactorGraph.jl/workflows/Documentation/badge.svg
[build-badge]: https://github.com/mcosovic/FactorGraph.jl/workflows/Build/badge.svg
[documentation]: https://mcosovic.github.io/FactorGraph.jl/stable/
53 changes: 23 additions & 30 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,6 @@ using FactorGraph
#### Quick start whitin continuous framework
The following examples are intended for a quick introduction to FactorGraph package within the continuous framework.

- Synchronous message passing schedule using the vanilla GBP algorithm:
```julia-repl
using FactorGraph
gbp = continuousModel("data33_14.h5") # initialize the graphical model using HDF5 input
for iteration = 1:200 # the GBP inference
messageFactorVariable(gbp) # compute messages using the vanilla GBP
messageVariableFactor(gbp) # compute messages using the vanilla GBP
end
marginal(gbp) # compute marginals
displayData(gbp) # show results
```

- Synchronous message passing schedule using the broadcast GBP algorithm:
```julia-repl
using FactorGraph
Expand All @@ -71,22 +58,7 @@ for iteration = 1:50 # the GBP inference
messageVariableFactorBroadcast(gbp) # compute messages using the broadcast GBP
end
marginal(gbp) # compute marginals
```

- Synchronous message passing schedule using broadcast GBP with Kahan–Babuška algorithm with the plotting of the marginal mean through iterations:
```julia-repl
using FactorGraph
using Plots
gbp = continuousModel("data33_14.h5") # initialize the graphical model
x6 = [] # save the state variable marginal
for iteration = 1:50 # the GBP inference
messageFactorVariableKahan(gbp) # compute messages using the GBP with Kahan-Babuska
messageVariableFactorKahan(gbp) # compute messages using the GBP with Kahan-Babuska
marginal(gbp) # compute marginals
push!(x6, gbp.inference.mean[6]) # save state variable marginal
end
plot(collect(1:50), x6) # show plot
displayData(gbp) # show results
```

- Synchronous message passing schedule using the vanilla GBP algorithm in the dynamic framework:
Expand Down Expand Up @@ -175,7 +147,28 @@ Following examples are intended for a quick introduction to FactorGraph package
```julia-repl
using FactorGraph
bp = discreteTreeModel("discrete6_4.xlsx") # initialize the tree graphical model
probability1 = [1]
table1 = [0.2; 0.3; 0.4; 0.1]
probability2 = [1; 2; 3]
table2 = zeros(4, 3, 1)
table2[1, 1, 1] = 0.2; table2[2, 1, 1] = 0.5; table2[3, 1, 1] = 0.3; table2[4, 1, 1] = 0.0
table2[1, 2, 1] = 0.1; table2[2, 2, 1] = 0.1; table2[3, 2, 1] = 0.7; table2[4, 2, 1] = 0.1
table2[1, 3, 1] = 0.5; table2[2, 3, 1] = 0.2; table2[3, 3, 1] = 0.1; table2[4, 3, 1] = 0.1
probability3 = [4; 2]
table3 = zeros(2, 3)
table3[1, 1] = 0.2; table3[2, 1] = 0.8
table3[1, 2] = 0.5; table3[2, 2] = 0.5
table3[1, 3] = 0.5; table3[2, 3] = 0.5
probability4 = [4]
table4 = [0.4; 0.6]
probability = [probability1, probability2, probability3, probability4]
table = [table1, table2, table3, table4]
bp = discreteTreeModel(probability, table) # initialize the tree graphical model
while bp.graph.forward # inference from leaves to the root
forwardVariableFactor(bp) # compute forward messages
forwardFactorVariable(bp) # compute forward messages
Expand Down
20 changes: 3 additions & 17 deletions docs/src/man/continuousInput.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ The FactorGraph package supports continuous random variables, where local functi
```
Hence, the local function is associated with mean ``z_i``, variance ``v_i``, and linear equation ``h_i(\mathcal{X}_i)``. To describe the joint probability density function ``g(\mathcal{X})``, it is enough to define the Jacobian matrix containing coefficients of the equations, and vectors of mean and variance values. Then, the ``i``-th row of the Jacobian matrix, with consistent entries of mean and variance, defines the local function ``\psi_i(\mathcal{X}_i)``.

The input data used to describe the joint probability density function can be given using HDF5 and XLSX input files or by passing data directly via command-line arguments. The examples of the input files are located in the `src/example`. Note that, with large-scale systems, we strongly recommend using the HDF5 file data format.

Thus, the input data structure includes the `jacobian` variable, while variables `observation` and `variance` represent mean and variance vectors, respectively. The functions `continuousModel()` and `continuousTreeModel()` accept HDF5 and XLSX input files with fields/sheets `jacobian`, `observation` and `variance`. Also, these functions accept variables `jacobian`, `observation` and `variance` directly via function arguments.
Thus, the input data structure includes the `jacobian` variable, while variables `observation` and `variance` represent mean and variance vectors, respectively. The functions `continuousModel()` and `continuousTreeModel()` accept variables `jacobian`, `observation` and `variance`.

---


#### Build the graphical model by passing arguments
#### Build the graphical model
Let us observe the following joint probability density function:
```math
g(\mathcal{X}) \propto \exp\Bigg\{-\cfrac{[2.5 - 0.2x_1]^2}{2\cdot 1.1}\Bigg\}\exp\Bigg\{-\cfrac{[0.6 - (2.1x_1 + 3.4x_2)]^2}{2\cdot 3.5}\Bigg\}
Expand All @@ -35,16 +33,4 @@ gbp = continuousModel(jacobian, observation, variance)
Here, the variable `gbp` holds the main composite type related to the continuous model. In the case of a tree factor graph, when you want to use a forward-backward algorithm, then the following command can be used:
```julia-repl
gbp = continuousTreeModel(jacobian, observation, variance)
```

---

#### Build the graphical model using HDF5 or XLSX files
Further, we can represet the `jacobian` variable using row and column indecies:
```julia-repl
jacobian = zeros(3, 3)
jacobian[1, 1] = 1; jacobian[1, 2] = 1; jacobian[1, 3] = 0.2
jacobian[2, 1] = 2; jacobian[2, 2] = 1; jacobian[2, 3] = 2.1
jacobian[3, 1] = 2; jacobian[3, 2] = 2; jacobian[3, 3] = 3.4
```
The Jacobian matrix stored in the HDF5 or XLSX input file requires format with row and column indices with the corresponding coefficient. The input file must contain three fields with the HDF5, or three sheets with the XLSX file, with names `jacobian`, `observation` and `variance`.
```
17 changes: 1 addition & 16 deletions docs/src/man/continuousModel.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,7 @@ In addition, we also provide several functions for factor graph manipulation.

Input arguments DATA of the function `continuousModel()` describe the graphical model, while the function returns `ContinuousModel` type.

Loads the system data using h5-file from the package:
```julia-repl
gbp = continuousModel("data33_14.h5")
```

Loads the system data using xlsx-file from the package:
```julia-repl
gbp = continuousModel("data33_14.xlsx")
```

Loads the system data from a custom path:
```julia-repl
gbp = continuousModel("C:/name.h5")
```

Loads the system data passing arguments directly:
Loads the system data passing arguments:
```julia-repl
gbp = continuousModel(jacobian, observation, variances)
```
Expand Down
17 changes: 1 addition & 16 deletions docs/src/man/continuousTreeModel.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,7 @@ The subtype `ContinuousTreeGraph` describes the tree factor graph obtained based

Input arguments DATA of the function `continuousTreeModel()` describe the tree graphical model, while the function returns `ContinuousTreeModel` type.

Loads the system data using h5-file from the package:
```julia-repl
gbp = continuousTreeModel("data12_11.h5")
```

Loads the system data using xlsx-file from the package:
```julia-repl
gbp = continuousTreeModel("data13_11.xlsx")
```

Loads the system data from a custom path:
```julia-repl
gbp = continuousTreeModel("C:/name.h5")
```

Loads the system data passing arguments directly:
Loads the system data passing arguments:
```julia-repl
gbp = continuousTreeModel(jacobian, observation, variances)
```
Expand Down
19 changes: 3 additions & 16 deletions docs/src/man/discreteInput.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ The FactorGraph package supports disrecte random variables, where each random va
```
Hence, the local function is associated with the conditional distribution ``p_i(x_i|\mathcal{X}_i \setminus x_i)`` and the set of random variables ``\mathcal{X}_i``. The conditional distribution takes probability values over all possible state combinations of the random variables from the set ``\mathcal{X}_i`` forming the conditional probability table. To describe the joint probability density function ``g(\mathcal{X})``, it is enough to define the container that saves indices of discrete variables and conditional probability tables.


The input data used to describe the joint probability density function can be given using HDF5 and XLSX input files or by passing data directly via command-line arguments. The examples of the input files are located in the `src/example`. Note that, with large-scale systems, we strongly recommend using the HDF5 file data format.

Thus, the parameters that describe the factor graph structure are represented by variable `probability` containing indices of random variables, while variable `table` saves conditional probability tables. The function `discreteTreeModel()` accepts HDF5 and XLSX input files with variables `probability` and `table`. Also, this function accepts variables `probability` and `table` directly via function arguments.
Thus, the parameters that describe the factor graph structure are represented by variable `probability` containing indices of random variables, while variable `table` saves conditional probability tables. The function `discreteTreeModel()` accepts variables `probability` and `table`.

---


#### Build the graphical model by passing arguments
#### Build the graphical model
Let us observe the following joint probability density function:
```math
g(\mathcal{X}) \propto p_1(x_1)p_2(x_1|x_2)p_3(x_1|x_2,x_3),
Expand Down Expand Up @@ -66,14 +63,4 @@ inputData = Dict("probability" => (probability1, probability2, probability3),
"table" => (table1, table2, table3))
bp = discreteTreeModel(inputData)
```

----

#### Build the graphical model using HDF5 file
The HDF5 input data stored above format in the fields `probability` and `table`. Inside these fields, each subfield holds data about specific conditional probability distribution with a unique subfield name. Subfield names through fields `probability` and `table` must match.

---

#### Build the graphical model using XLSX file
The input data stored in the XLSX file requires conditional probability tables in the native form with discrete variable indices. Further, the number of possible states should be given for each random variable.
```
17 changes: 1 addition & 16 deletions docs/src/man/discreteTreeModel.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,7 @@ The subtype `DiscreteTreeGraph` describes the tree factor graph obtained based o

Input arguments DATA of the function `discreteTreeModel()` describe the tree graphical model, while the function returns `DiscreteTreeModel` type.

Loads the system data using h5-file from the package:
```julia-repl
bp = discreteTreeModel("discrete6_4.h5")
```

Loads the system data using xlsx-file from the package:
```julia-repl
bp = discreteTreeModel("discrete6_4.xlsx")
```

Loads the system data from a custom path:
```julia-repl
bp = discreteTreeModel("C:/name.h5")
```

Loads the system data passing arguments directly:
Loads the system data passing arguments:
```julia-repl
bp = discreteTreeModel(probability, table)
```
Expand Down
2 changes: 1 addition & 1 deletion src/FactorGraph.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module FactorGraph

using SparseArrays, LinearAlgebra
using HDF5, XLSX
using Random
using PrettyTables, Printf
using Test

### Form a continuous factor graph and initialize messages and marginals
include("continuousgraph.jl")
Expand Down
Loading

0 comments on commit e6be682

Please sign in to comment.