How is inference by enumeration done on bayesian networks? - artificial-intelligence

For instance, if given the following Bayesian network and probabilities how would I find P(BgTV | not(GfC). I attempted to do so by simply using the equivalence that P(A|B) = P(A and B)/P(B) but that resulted in me having a value of 200% which is not possible. Do I need to treat George_feeds_cat as a dependent event as per the network and use what I know from baseball_game_on_TV and George_watches_TV to calculate the odds? Any guidance would be much appreciated!

Indeed, you need all the parameters for answering your question (oCF seems to be independent from BgTV and GwTV but knowing GfC, this is not the case anymore).
Using the columns, instead of the symbols, you want :
P(A|C)=P(A,C)/P(C)=sum_{B,D} P(A,B,C,D) / sum_{A,B,D} P(A,B,C,D)
with the joint distribution factorized using the BN
P(A,B,C,D)=P(A)*P(B|A)*P(C)*P(D|C,B)
In Python using the package pyAgrum, you would write :
# model
import pyAgrum as gum
bn=gum.fastBN("BgTV->GwTV->GfC<-oCF")
# where do those numbers come from ? :-)
bn.cpt("BgTV").fillWith([1-0.3041096,0.3041096])
bn.cpt("oCF").fillWith([1-0.169863,0.169863])
bn.cpt("GwTV")[{"BgTV":0}]=[1-0.1181102,0.1181102]
bn.cpt("GwTV")[{"BgTV":1}]=[1-0.9279279,0.9279279]
bn.cpt("GfC")[{"GwTV":0,"oCF":0}]=[1-0.9587629,0.9587629]
bn.cpt("GfC")[{"GwTV":0,"oCF":1}]=[1-0.3157895,0.3157895]
bn.cpt("GfC")[{"GwTV":1,"oCF":0}]=[1-0.706422,0.706422]
bn.cpt("GfC")[{"GwTV":1,"oCF":1}]=[1-0.0416667,0.0416667]
# compute
joint=bn.cpt("BgTV")*bn.cpt("GwTV")*bn.cpt("GfC")*bn.cpt("oCF")
joint.margSumIn(["GfC","BgTV"])/joint.margSumIn("GfC")
which should give you
|| BgTV |
GfC ||0 |1 |
------||---------|---------|
0 || 0.5159 | 0.4841 |
1 || 0.7539 | 0.2461 ||
Where you see that P(BgTV=1|GfC=0)=48.41%
Using a notebook, the model :
And the inference (using another method with junction tree) :

Related

Query on TFP Probabilistic Model

In the TFP tutorial, the model output is Normal distribution. I noted that the output can be replaced by an IndependentNormal layer. In my model, the y_true is binary class. Therefore, I used an IndependentBernoulli layer instead of IndependentNormal layer.
After building the model, I found that it has two output parameters. It doesn't make sense to me since Bernoulli distribution has one parameter only. Do you know what went wrong?
# Define the prior weight distribution as Normal of mean=0 and stddev=1.
# Note that, in this example, the we prior distribution is not trainable,
# as we fix its parameters.
def prior(kernel_size, bias_size, dtype=None):
n = kernel_size + bias_size
prior_model = Sequential([
tfpl.DistributionLambda(
lambda t: tfd.MultivariateNormalDiag(loc=tf.zeros(n), scale_diag=tf.ones(n))
)
])
return prior_model
# Define variational posterior weight distribution as multivariate Gaussian.
# Note that the learnable parameters for this distribution are the means,
# variances, and covariances.
def posterior(kernel_size, bias_size, dtype=None):
n = kernel_size + bias_size
posterior_model = Sequential([
tfpl.VariableLayer(tfpl.MultivariateNormalTriL.params_size(n), dtype=dtype),
tfpl.MultivariateNormalTriL(n)
])
return posterior_model
# Create a probabilistic DL model
model = Sequential([
tfpl.DenseVariational(units=16,
input_shape=(6,),
make_prior_fn=prior,
make_posterior_fn=posterior,
kl_weight=1/X_train.shape[0],
activation='relu'),
tfpl.DenseVariational(units=16,
make_prior_fn=prior,
make_posterior_fn=posterior,
kl_weight=1/X_train.shape[0],
activation='sigmoid'),
tfpl.DenseVariational(units=tfpl.IndependentBernoulli.params_size(1),
make_prior_fn=prior,
make_posterior_fn=posterior,
kl_weight=1/X_train.shape[0]),
tfpl.IndependentBernoulli(1, convert_to_tensor_fn=tfd.Bernoulli.logits)
])
model.summary()
screenshot of the results executed the codes on Google Colab
I agree the summary display is confusing but I think this is an artifact of the way tfp layers are implemented to interact with keras. During normal operation, there will only be one return value from a DistributionLambda layer. But in some contexts (that I don't fully grok) DistributionLambda.call may return both a distribution and a side-result. I think the summary plumbing triggers this for some reason, so it looks like there are 2 outputs, but there will practically only be one. Try calling your model object on X_train, and you'll see you get a single distribution out (its type is actually something called TensorCoercible, which is a wrapper around a distribution that lets you pass it into tf ops that call tf.convert_to_tensor -- the resulting value for that op will be the result of calling your convert_to_tensor_fn on the enclosed distribution).
In summary, your distribution layer is fine but the summary is confusing. It could probably be fixed; I'm not keras-knowledgeable enough to opine on how hard it would be.
Side note: you can omit the event_shape=1 parameter -- the default value is (), or "scalar", which will behave the same.
HTH!

Require a field as part of a Rust trait

Is it possible to require that a struct have a particular field as part of a trait? I am doing some web automation in Rust with the thirtyfour_sync crate. I want to write some traits with default implementations for page objects that will implement them. Basically, I know for a fact that every struct that is going to implement this trait will have a field called "driver" that holds a reference to a WebDriver struct. I want to use this driver field in my default implementation of the trait.
error[E0609]: no field `driver` on type `&Self`
--> src\base_order_details.rs:13:30
|
10 | / pub trait BaseOrderDetails {
11 | |
12 | | fn oid(&self) -> WebDriverResult<String> {
13 | | let oid_title = self.driver.find_element(By::XPath("//*[text()=\"Order ID:\"]"))?;
| | ^^^^^^
... |
Is there a way to let the compiler know that anything implementing this trait will have a field driver of type &WebDriver?
I discovered the answer to my question while drafting it. No, you cannot access fields from traits. As a solution, I have added a get_driver method to the trait and used it within the default implementation, so the user can simply implement the get_driver method and default the rest.
pub trait BaseOrderDetails {
fn get_driver(&self) -> &WebDriver;
fn oid(&self) -> WebDriverResult<String> {
let oid_title = self.get_driver().find_element(By::XPath("//*[text()=\"Order ID:\"]"))?;
// do some other stuff
I found this solution in the Rust Book chapter on traits, under Default Implementations: https://doc.rust-lang.org/book/ch10-02-traits.html

Combining multiple external datasets with cucumber/selenium test environments

When developing automated tests using cucumber and selenium webdriver in java, I use excel spreadsheets as datasets for the gherkin scenarios instead of the traditional Examples tables, using a simple table with only the row numbers in my feature files. This works very well when doing tests that only make use of data from one spreadsheet at a time, but when implementing tests that make use of multiple spreadsheets, how does one ensure the it iterates over every combination.
For example, when testing multiple configurations and their impact on the main interface, I provide the configuration data, let's say 3 combinations of different configurations, using the first spreadsheet, and in my gherkin feature I only enter the row numbers and use the code to handle the actual reading of data.
When the user uses configuration from row <ExcelRow>
...
Examples:
| ExcelRow |
| 1 |
| 2 |
| 3 |
The problem arises when I want to test such configurations with different combinations of inputs in the main interface, also provided via a separate excel spreadsheet. I want configuration from row 1 to be run with all rows from the second spreadsheet, before moving on to row 2's configuration and so on.
Manually using the examples table to do the combinations does the job when working with smaller data sets
Examples:
| ConfigRow | InputRow |
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
| 3 | 1 |
| 3 | 2 |
The problem arises when using very large datasets, where the examples table starts to clutter the feature file despite only containing the row numbers.
I tried implementing the actual input testing as a single step that loops over the entire excel spreadsheet for each configuration, but that forced me to do my assertions in the same loop and not in the Then step.
If you want to mention only config row in feature file and you want that some other rows to be executed for each config row then you may want to utilize cucumber-guice and make it #ScenarioScoped . Cucumber-guice can initialize same classes for each scenario independently. You would need these dependencies in your pom
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-guice</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.2.3</version>
<scope>test</scope>
</dependency>
Then , in a global class you can do
import io.cucumber.guice.ScenarioScoped;
#ScenarioScoped
public class Global {
public Helpers help;
//constructor
public Global() {
//helper class would contain code that does all the second excel sheet work
help = new Helpers();
}
}
In step def you can do
//import Global and guice dependencies
import yourPackage.Global;
import com.google.inject.Inject;
...
...
public class stepDef {
#Inject
Global global;
#When ("the user uses configuration from row {int}")
public void useConfigs(){
global.help.doSomeExcelWork();
}
#Then ("I assert from excel sheet")
public void doAssertions(){
//do assertions here. These
global.help.doAssertion();
}
}
Your helper class could be something like this
public class Helper {
public void doSomeExcelWork(){
//do excel work
}
public void doAssertion(){
//return values for your assertions
}
}
Your feature file would look like
When the user uses configuration from row <ExcelRow>
Examples:
| ExcelRow |
| 1 |
| 2 |
Then I assert from excel sheet
Now , for all your examples (scenarios) global would be injected independently and the then statement also would be called for each example row
I am not sure if that is possible to do via Cucumber. You may try searching for Cucumber Java dynamic examples generation like here.
I just would like to question if Cucumber/Gherkin are the right tools for what you wanted to achieve. The primary goal of Gherkin / Cucumber / Specflow scenarios is demonstrate the behavior of the system to anyone reading the feature file. So hiding the data in "linked" files can be accepted if they hold a complex piece of data which is as a single unit, provided the file name demonstrates what is special about the data inside.
What you might be looking for are Parameterized Tests and Data Providers that are available in JUnit 5 and TestNG 2, 3. If you write automation framework in the way that Cucumber, or other test framework becomes only a thin wrapper around it which "assembles" the test, you can generate tests on the fly.
For example your step: "When the user uses configuration from row", becomes
public void whenUserUsesConfiguration(SutConfiguration configuration) {
// your configuration setup goes here
// but you do not read configuration from file in this method
}
Method above can be used in both Cucumber steps and JUnit/TestNG test without loosing any readability, or understandability.
By splitting your tests into two parts, one for understanding general system behavior and accessible to all stakeholders, and the one that check lots of small nuances, but both using the same framework will allow you to have greater flexibility, and more pleasant development experience.

How do I implement a controlled Rx in Cirq/Tensorflow Quantum?

I am trying to implement a controlled rotation gate in Cirq/Tensorflow Quantum.
The readthedocs.io at https://cirq.readthedocs.io/en/stable/gates.html states:
"Gates can be converted to a controlled version by using Gate.controlled(). In general, this returns an instance of a ControlledGate. However, for certain special cases where the controlled version of the gate is also a known gate, this returns the instance of that gate. For instance, cirq.X.controlled() returns a cirq.CNOT gate. Operations have similar functionality Operation.controlled_by(), such as cirq.X(q0).controlled_by(q1)."
I have implemented
cirq.rx(theta_0).on(q[0]).controlled_by(q[3])
I get the following error:
~/.local/lib/python3.6/site-packages/cirq/google/serializable_gate_set.py in
serialize_op(self, op, msg, arg_function_language)
193 return proto_msg
194 raise ValueError('Cannot serialize op {!r} of type {}'.format(
--> 195 gate_op, gate_type))
196
197 def deserialize_dict(self,
ValueError: Cannot serialize op cirq.ControlledOperation(controls=(cirq.GridQubit(0, 3),), sub_operation=cirq.rx(sympy.Symbol('theta_0')).on(cirq.GridQubit(0, 0)), control_values=((1,),)) of type <class 'cirq.ops.controlled_gate.ControlledGate'>
I have the qubits and symbols initialized as:
q = cirq.GridQubit.rect(1, 4)
symbol_names = x_0, x_1, x_2, x_3, theta_0, theta_1, z_2, z_3
I do re-use the circuits with various circuits.
My question: How do I properly implement a controlled Rx in Cirq/Tensorflow Quantum?
P.S. I can't find a tag for Google Cirq
Follow up:
How does this generalize to the similar situations of Controlled Ry and controlled Rz?
For Rz I found a gate decomposition at https://threeplusone.com/pubs/on_gates.pdf, involving H.on(q1), CNOT(q0, q1), H.on(q2), but this is not yet an CRz with an arbitrary angle. Would I introduce the angle before the H?
For the Ry, I did not find a decomposition yet, neither the CRy.
What you have is a completely correct implementation of a controlled X rotation in Cirq. It can be used in simulation and other things like cirq.unitary without any issues.
TFQ only supports a subset of gates in Cirq. For example a cirq.ControlledGate can have an arbitrary number of control qubits, which in some cases can make it harder to decompose down to primitive gates that are compatible with NiSQ hardware platforms (This is why cirq.decompose doesn't do anything to ControlledOperations). TFQ only supports these primitive style gates , for a full list of the supported gates, you can do:
tfq.util.get_supported_gates().keys()
In your case it is possible to come up with a simpler implementation of this gate. First we can note that cirq.rx(some angle) is equal to cirq.X**(some angle / pi) offset by a global phase:
>>> a = cirq.rx(0.3)
>>> b = cirq.X**(0.3 / np.pi)
>>> cirq.equal_up_to_global_phase(cirq.unitary(a), cirq.unitary(b))
True
Lets move to using X now. Then the operation we are after is:
>>> qs = cirq.GridQubit.rect(1,2)
>>> a = (cirq.X**0.3)(qs[0]).controlled_by(qs[1])
>>> b = cirq.CNOT(qs[0], qs[1]) ** 0.3
>>> cirq.equal_up_to_global_phase(cirq.unitary(a), cirq.unitary(b))
True
Since cirq.CNOT is in the TFQ supported gates it should be serializable without any issues. If you want to make a symbolized version of the gate you can just replace the 0.3 with a sympy.Symbol.
Answer to follow up: If you want to do a CRz you can do the same thing you did above, swapping out the CNOT gate for the CZ gate. For CRy it's not as easy. For that I would recommend doing some combination of: cirq.Y(0) and cirq.YY(0, 1).
Edit: tfq-nightly builds and likely releases after 0.4.0 now include support for arbitrary controlled gates. So on these versions of tfq you could also do things like cirq.Y(...).controlled_by(...) to achieve the desired result now too.

how to use arima.rob

does anyone use arima.rob() function described by Eric Zivot and Jiahui Wang in { Modelling Financial Time Series with S-PLUS } ?
I have a question about it:
I used a dataset of network traffic flows that has anomaly, and I tried to predict the last part of dataset by robust ARIMA method (Arima.rob() function) .I compare this model with arima.mle of S-PLUS. But Unexpectedly, arima.rob’s prediction did not better than that.
I’m not sure my codes are correct and may be the reason of fault is my codes.
Please, help me if I used Arima.rob inappropriately?
tmp.rr<-arima.rob((tmh75)~1,p=2,d=1,q=2,freq=24,maxiter=4,max.fcal=80000)
tmp.for<-predict(tmp.rr,n.predict=10,newdata=df1,se=T)
plot(tmp.for,tmh75)
summary(tmp.for)
my code for classic arima:
`model <- list(list(order=c(2,1,2)),list(order=c(3,1,2),period=24))
fith <- arima.mle(tmh75-mean(tmh75),model=model)
foreh <- arima.forecast(tmh75,n=25,model=fith$model)
tsplot(tmh75,foreh$mean,foreh$mean+foreh$std.err,foreh$mean-foreh$std.err)
`

Resources