Friday, June 10, 2022

Week 3: RCoT and LiNGAM

 DirectLiNGAM

I begin to read the original paper for DirectLiNGAM, below are my summary:

  • DirectLiNGAM does not need initial guess and hyperparameters, and is guaranteed to converge within a fixed small steps if the data fits the model.
  • DirectLiNGAM first finds an exogenous variable, then removes it from the model by eliminating all its effect on other variables. After removing, we have a new model with residuals by removing effect of the exogenous variable, then we keep doing this step until the last variable. At the end we get the estimated causal relations, then we can use LSE to compute the causal effect between variables.
  • In order to find exogenous variable, we need to do LSE and test the independence of every variable and their residuals from LSE. This step requires an independence test, and is very time consuming.
  • With prior knowledge, we can accelerate the algorithm by finding exogenous directly or more easily.

Continuing Experiment on RCoT


Experiments results are recorded in google doc.

Bug Fix


When running experiments on non-linear relationships, bug occurs that the lgb4 algorithm may produce complex number in intermediate step and cause the program to crash. I fix it by finding the original python code for lgb4 and update the code related.

Searching for Detection Boundary of Linear and Non-linear


The goal of these experiments recorded above is to find what kind of causal relation can RCoT detect, and what kind of relation it can not.

The conclusion of the experiments above are:
  • RCoT can easily detect linear causal relationship and correctly classify whether the relation is independent or dependent.
  • Even just add in a mild non-linear relationship like square, the error occurs for difficult examples.
  • More complex non-linear relationship will cause more errors.
  • For common cause structure, it is interesting to find that relation like y = abs(x) ** 1.5 can be detect, while y = x ** 2 can not.

Evaluate the Effect of Parameter R


In RCoT algorithm, the parameter R indicates how many samples are being used for Fourier Features. Intuitively, increasing R will increase the running time but also increase the accuracy.

I modify the code, make R an adjustable parameter and do some experiments on it.

However, in experiments, the results show that:
  • The running time will be highly influenced by the value of parameter R, and the relationship is almost O(R^2). One test for conditional dependency when using R=100 will result in less than 1 second, one test when R=500 is likely 6-7s, one test when R=1000 is likely 25s, and one test when R=2000 is likely 110s. Therefore, it is almost impossible to use all samples when the sample size is above 10K.
  • It turns out that changing R value will not influence much of the test results. For non-linear relationships, even I increase the R value to 3000, the RCoT algorithm still can not correctly classify whether a conditional relation is dependent or not. For linear relationships, even I decrease the R value to 10, the RCoT algorithm still can give correct results for all tests, and the margin does not have significant decreasing.

Direct Test


I simply modify the code provided for testing the direct algorithm and do some simple test on the currently Pairwise Lingam Algorithm (Hyperbolic Tangent (HT) variant).

The results do not turn out good even I increase the noise to make the relationship less determined.




The algorithm can not give the correct results for A, B, C and N, N2 relationship, which should be very simple examples.

Plan for Next Week


  • Integrate RCoT into ProbSpace fully, write appropriate comments and make pull request for it.
  • Look into Lingam algorithm, read related papers, examine why it won't work, find other alternate algorithms.

Friday, June 3, 2022

Week 2: Integrate and experiment with RCoT algorithm

 RCoT algorithm


Learning RCoT algorithm


Continue the task from last week, I finished reading RCoT and RIT paper to have a rough understanding of the algorithm. On Tuesday, Roger also gave me a lecture on RKHS and RCoT, which built an intuitive understanding of these concepts. Though the mathematical proof is still a little difficult for me to understand, the whole process began to make sense to me.

I also learned RCoT algorithm by using debug mode to read the code, which is very useful to understand what the algorithm actually done.

Integrate RCoT algorithm


After the learning of the algorithm, I integrated the code into the Because module and completed the test program for RCoT.




































It is easy to change method between traditional probability method and RCoT algorithm. There are something should be noted:
  • I fixed a bug in RCoT algorithm, which just uses the first variable in the list of conditional variables.
  • I modified function normalize in normalize.py, making it more clear and no need to do extra matrix transpose after normalization.
  • I changed one line in RCoT algorithm from L = sc.cholesky((Czz + (np.eye(num_f) * 1e-10)), lower=True) to L = sc.cholesky((Czz + (np.eye(num_f) * 1e-6)), lower=True) to avoid error from scipy saying that Czz is not positive semi-definite and is unable to decompose.
  • I changed the default value for parameter num_f (Number of features for conditioning set) from 5 to 100, which is the same as in the original R code. This modification improves the algorithm accuracy while does not increase the running time much.
  • I choose .99 as the threshold to determine whether two variables are dependent.

Experiments with RCoT algorithm


Some experiments' results are recorded below (Right now in this doc I use .95 as threshold for RCoT, I haven't added new records since meeting in Thursday):
https://docs.google.com/document/d/12C91ALhd9XIxHtONafTw8MlbwRTQdfrrYWMOIZIA75E/edit?usp=sharing

After meeting in Thursday, I did a lot extra experiments, and there are something should be noted from experiments:
  • I found the original R code and run the RCoT algorithm on the same dataset using two implementations. Since the algorithm has randomness, and the same random seed does not provide the same results in both R and Python, it is impossible to get the exactly same results. Therefore, I run the algorithm on the same quest like dependence(A, C, [B, D]) multiple times in both R and Python to compare the results. It turns out that the two implementations match and there are no big difference between them. Since p-value itself is a random variable, I would say the two implementations provide the same results.
  • After I change the num_f  from 5 to 100, the RCoT algorithm can now easily detects dependence and independence correctly when I use simple linear relations. After generating data using complex non-linear relations, the algorithm may give wrong results especially on example like dependence(A, C, [B, D]) and dependence(B, D, [A, C]). Since the original R code also makes mistake on these examples, I think these examples are true difficult examples for RCoT algorithm.
  • The randomness in RCoT algorithm is very strong. Since p-value is from uniform distribution when NULL hypothesis is correct, and the random nature in RCoT algorithm, sometimes it will provide incorrect results or subtle results (around 0.5), and the results from different runs can vary a lot.
  • From the experiments, setting threshold to .99 and using formula (1-p) ** log(0.5, 0.99) to translate the results always provide satisfied results. When given strong dependent pair of variables, the RCoT algorithm can always get result of 0, or less than 0.01. And for independence pairs, the result will vary from 0 to 1. When using this formula, result p-value in [0.05, 1] will be turned into almost 0, and p-value in [0, 0.001] will be turned into almost 1. Examples between [0.001, 0.05] can be seen as subtle case, which can be taken on extra test if needed.

LPB and Kolmagarov-Smirnov


After experiments on RCoT, I also looked into LPB code and learned what this algorithm is trying to achieve. I also learned Kolmagarov-Smirnov, which is used in the original probability method.

Other Stuff


I spent some time finishing company course of Harassment Prevention Training and CDA-RSG Cyber Defense Onboarding Curriculum.

Plan for next week


  • After Roger fix the problem in probability method, I will run a through experiments on both methods and compare them.
  • Evaluate LPB as an alternative to Kolmagarov-Smirnov in our current independence test.
  • Understand DirectLiNGAM algorithm, and our Causal Direction Conjecture.

Friday, May 27, 2022

Week 1 Day 4+5: Dive into Because module and begin with RcoT algorithm

Dive into Because Module

On Thursday we had a weekly team meeting. Roger continued with the lecture on Causality and HPCC system causality toolkit. We shared our progress and specifically I shared a test result which didn't meet expectations. After the meeting I tried to explore the problem myself, which is also a good start to understand how Because module calculates conditional probability in detail.

Problem description
















The causal model is a typical inverted-V structure for IVA, IVB and IVC. It is easy to know that given IVB, IVA and IVC should be independent.











The result did not meet the expectations. When given IVB, the expectation for IVC is still highly influenced by the value of IVA. Tests were done repeatedly for data size of 100000 and 1000000, and the results are against expectation consistently.
This problem intrigues me to know how conditional calculations are done by Because module.

Tackle of the problem

I did it by using debug mode and to see the intermediate variables in probTest.py. 







Specifically, I examined the code line ivcGupper = ps.E('IVC', [('IVA', upper), 'IVB'], power=pwr), where upper=0.901. Since the power is set to 2, we sampled 5 IVB to approximate the conditionalized expectation, and in the first loop we use IVB=-0.009. Since we know that IVC is determined by IVB plus a logistic distribution with mean of 0, then we should expect the distribution of IVC to have a mean around 0. And we can check the result distribution:

















Other part of the PDF is all 0. We can see that the distribution is skewed to 1, which is highly influenced by the value of IVA. After examine the SubSpace we get for this condition, the problem shows up that we actually have large portion of IVC have value around 0.60. The reason behind it is how SubSpace gets filtered data.











From my understanding, the data generating process in this example is too determined (noise is too small compared to the causal effect), which means when IVA is at upper bound value, we just don't have enough data when IVB is around 0. In this situation, we will skew the distribution since we will expand the interval to acquire enough data. And in the end, the result is skewed.

Conclusion

Before the work above, I actually don't have a understanding of why calculating conditional probability is a very difficult problem. Now I begin to understand why it's hard: condition means the increasing of dimension and the rapid decreasing of data samples. Traditional method would be very hard to deal with it.

Beside this, I also examine:
  • how the Because module calculate dependency
  • how the Because module calculate ACE, CDE
  • how the Causality submodule calculate intervene formula

RcoT algorithm

I have began to deal with the RcoT algorithm:
  1. Read wiki about this work, watch the presentation done by Mayank.
  2. Read the RKHS blog to have an intuitive understanding of RKHS (Great article! Very easy to understand).
  3. Begin to read original paper of RcoT algorithm.

Plan for next week

  1. Understand RcoT algorithm by reading papers and codes
  2. Integrate code with Because module
  3. Do some test on the performance and accuracy about RcoT

Wednesday, May 25, 2022

Week 1 Day 2+3: First glance at Because module

On Tuesday, I had a team meeting with Roger and Arun. In the meeting, Roger shared the basic concept of causality and the overview of HPCC system causality toolkit.

Other than that, in these two days, I mainly worked on the test examples in Because module.

Synth submodule: By running genTest.py and viewing the code in gen_data.py, understand the process of generating synthesis data, and how to write the model description to utilize this power tool. It is the basic for the experiments next.

Probability submodule: By running test examples, understand the capability of main class ProbSpace, such as the calculation of the probability or conditional probability for any combinations of variables, approximation of the distribution of variables, calculating basic statistics of distribution, and the plot of PDF. Details are still need to be examined, like the details of DP, JP, UP to calculate conditional PDF, how to calculate dependency, the effect of Power parameter.

Causality submodule: By running test examples, understand the capability of main class cGraph, which combines the hypothesized causal model and data. PropSpace is applied on data to have statistic calculation. Graph algorithm is applied on causal model to calculate causal related formula like do operation, and deduce independence and dependence. It can calculate ACE, CDE and CIE, scan data to find causal relationship, and validate the hypothesized causal model by independence and dependence relationships between variables. Implementation details still need to be check, like how to do the scan on graph, how to deduce dependence relationship from graph.

Some questions generated when doing the test and will be discussed further, some typo are discovered in code and will be amended.

Monday, May 23, 2022

Week 1 Day 1: Setup

 Today is the first day of my internship with LexisNexis.

For this internship, my main job is to improve the underlying causal inference algorithms which apply on real dataset, evaluate alternate algorithms, assess the issues encountered and develop new algorithms about causal inference. My initial task would be to view and understand the code of Because module, which is a causal inference package, and learn the implemented algorithms for causal inference.

But first of all, I need to setup everything and build environment for future work.

Today I mainly accomplished:

  1. Have a short meeting with Lorraine, set up my Email and Teams account.
  2. Finish RSG Phishing and Training Programs and CDA-RSG Cyber Defense Onboarding Curriculum.
  3. Have a kick off meeting with Roger, discuss my initial work. Roger also shared some test examples with me to have a better understand of the Because module.
  4. Install Because module, do some tests and experiments on Synth submodule.

Week 10: Sensitive Feature in RCoT

 1 Sensitive Feature in RCoT The need for adding a parameter sensitive into RCoT algorithm is that, for data from reality, the variables alw...