Causal reasoning and causal search with Python (for beginners)

For beginners who want to learn causal reasoning and causal exploration, I wrote a book to learn while actually implementing a Python program for causal analysis in Google Colaboratory.

As a single book, it will be the first new issue in about a year since "Learn while making! Deep learning by PyTorch".

In this article, I will give an overview of the motivation for writing a book on causal analysis, causal reasoning with Python, and causal search.


** "Learn while making! Causal analysis with Python-Introduction to the practice of causal reasoning and causal search" ** ● Released on June 30, 2020 ● Author: Yutaro Ogawa (Self-introduction, Twitter) @ Information Services International-Dentsu (ISID) ● Publisher: Mynavi Publishing

q1.png

https://www.amazon.co.jp/dp/4839973571/


** Contents of this article **

● 0. Motivation to write this book ● 1. Cases that require causal reasoning ● 2. Data for which causal reasoning is desired ● 3. Method of causal reasoning ● 4. Causal search method ● 5. Deep learning × Causal search

0. Motivation to write this book

DX has become a hot topic these days, and the importance of digitizing business and data-driven business practices is increasing.

Along with that, it is necessary to analyze the results of implementing various measures for data-driven business and properly verify the effects.

Therefore, causal reasoning is needed.

However (in my opinion),

  1. No books (few) deal with both causal reasoning and causal search

  2. There is no book for beginners that implements and explains causal reasoning analysis programs in Python. (Small, especially for business people)

  3. In the field of causal search, Bayesian networks are also a concern, but there are no books (few) that explain Bayesian networks together in causal search books.

  4. There are no books (few) that explain recent causal reasoning / causal search analysis methods using machine learning / deep learning.

I felt (although it is my subjectivity).

** I can understand causal reasoning and causal search to some extent with one book, and there is no introductory book that analyzes implementation and analysis in Python ~ ** Let's write it ourselves! What I thought was the motivation for writing this book.

1. Cases that require causal reasoning

Excerpts from this book, ** Learn while making! Causal analysis with Python ~ Introduction to causal reasoning and causal search ** However, I will explain causal reasoning and causal search.

First, I will introduce ** fictitious cases ** that require causal reasoning.


You work for a company and work as a manager of the Human Resources department.

From Mr. A, who is in charge of conducting employee training

Conducted for all employees who have subordinates and wish to participate ** "For bosses: Point training for career interviews with subordinates" **

I have received a report on the effectiveness of the training.

The expected effect is that the boss who received this training will be able to conduct good career interviews with his subordinates. This is to improve the satisfaction of subordinate employees with career interviews.

I received the following specific report from Mr. A.

  1. From subordinate employees (boss attends the above-mentioned point training for career interviews) and subordinate employees (boss does not attend training), 100 people are randomly selected and [Satisfaction with career interview] Was surveyed

  2. As a result of calculating the average of [satisfaction of career interviews] for each 100 people, the average satisfaction of employees whose boss has taken this training is higher than the average satisfaction of employees whose boss has not received training. Was also high

  3. 100 subordinate employees who conducted the questionnaire survey this time are randomly selected. The only difference is whether or not the boss has been trained

  4. Therefore, "For superiors: Point training for career interviews with subordinates" has the effect of improving the satisfaction level of career interviews with subordinates.

  5. The magnitude of the effect of the training is the difference between the average values of the [satisfaction level of career interviews] of the subordinates surveyed this time.

The above is the report I received from my subordinate Mr. A.

If you receive such a report, you may feel ** discomfort **.

And the following conversation will continue.

"Thank you, Mr. A, for the training and the verification of the effect. I just have something to worry about.

You are verifying the effect of taking the training on [satisfaction with career interviews with the employees' subordinates].

I confirm, this training was conducted for all the boss employees who wanted to participate by soliciting applicants for participation, right? "

"Yes."

"Hmm ... The boss who wants to take this ** "For bosses: Point training for career interviews with subordinates" ** In the first place, I think he is a boss who has a high ** [enthusiasm for training subordinates] **.

If you are an employee who has a boss who is enthusiastic about training such subordinates, ** "Even if that boss has not received this training" **, I feel that the ** [satisfaction level of career interviews] ** of that subordinate is higher than the satisfaction level of a subordinate who has a boss who has not taken training. "

・ ・ ・

Perhaps such a conversation will develop.

● For causal reasoning

Now, let's draw a picture of the content of this conversation that you, the director of the Human Resources Department, wondered (Fig. 1.1.1).

q2.png

Looking at Figure 1.1.1, ・ ** Arrow ①: The higher the [enthusiasm of training boss's subordinates], the higher the probability that the boss will take [for bosses: point training for career interviews with subordinates] **

・ ** Arrow ②: [Boss attends training] and [Satisfaction with subordinates' career interviews] increase **

There is a relationship. This arrow (2) shows the effect of the training being verified this time.

If there are only two arrows (1) and (2), Mr. A's report is correct, but as you, the HR manager, are concerned,

・ ** Arrow ③: The higher the [zeal of training boss's subordinates], the higher the [satisfaction with the career interviews of subordinates] **

It seems that you can fully imagine the arrow (relationship).

Therefore, in Fig. 1.1.1, it is not possible to accurately estimate the effect of training unless the effect of arrow ② is estimated while considering the existence of arrow ③.

** Causal reasoning is a method of estimating the direct causal effect that you want to obtain while considering the causal effect (effect of the arrow) from other variables (factors) in this way **.

2. Data for which causal reasoning is desired

Let's check the data of the personnel training given in this example.

q3.png

Data like this continues. The variable Z indicates attendance of training, 1 indicates attendance, and 0 indicates non-attendance. If this variable Z is 1, how much is the satisfaction level of the interviews of subordinate employees improved?

Next, check the data in a graph.

q4.png

From the graph

  1. It can be seen that those who have taken the training (Z = 1 is more satisfied than Z = 0) are more satisfied with the interviews with Y: subordinates.
  2. The data for both the blue and orange points tend to rise to the right, and it can be seen that the higher the zeal for training the subordinates of the x: boss, the higher the satisfaction level of the interview with the Y: subordinates.

** "Then, please find the effect of Training Z from this data." **

This is (an example) a challenge given to data scientists working in the enterprise.

3. Method of causal reasoning

Now let's infer the above data causally.

However, it is difficult to explain all the contents of causal reasoning in this article.

** Learn while making! Causal analysis with Python ~ Introduction to causal reasoning and causal search **, Chapters 1 to 3 Explains the prior knowledge required for causal reasoning.

** In "Chapter 1 Let's Understand the Difference between Correlation and Causality" **, using personnel training and TV commercials as an example, simply calculating the difference in mean value between the group that received the measure and the group that did not receive the measure , I will explain the point that the effect of treatment is not required. In addition, I will explain the concept of spurious correlation, which is the reason why estimation by the difference in mean values does not work, and explain the three patterns in which spurious correlation occurs. And at the end of Chapter 1, on Google Colaboratory, we will actually implement and confirm the three patterns that generate spurious correlations.

** "Chapter 2 Understanding the Types of Causal Effects" ** introduces anti-real virtual, potential outcome variables, and various causal effects (ATE, ATT, etc.). Then, I will introduce the do operator, which is an intervention operation, and explain that causal reasoning is difficult if a spurious correlation is created by an indirect causal effect, but the causal arrow can be erased by the intervention operation. Finally, I will explain the adjustment formula that describes this intervention operation mathematically without using the do operator.

** "Chapter 3 Understanding Graph Representations and Backdoor Criteria" ** introduces structural equation models, causal diagrams, and directed acyclic graph DAGs. Next, in order to carry out causal reasoning in the DAG, we will explain the d separation that closes the backdoor path by organizing the variables that should be considered and the variables that should be ignored.

From Chapter 1 to Chapter 3, we will explain the knowledge necessary for causal reasoning in order, and from Chapter 4, we will implement and explain a specific analysis program for causal reasoning.

** "Chapter 4 Let's implement causal reasoning" ** explains and implements regression analysis, which is a basic causal reasoning method, IPTW method using propensity score, and DR method that combines regression analysis and IPTW method. to hold.

However, the data of the personnel training introduced in this article is a problem that the effect cannot be estimated accurately by the basic causal reasoning method in Chapter 4.

Specifically, we set the problem that each variable (factor) works non-linear rather than linearly, and the effect of training Z changes depending on the zeal of x: boss's subordinate training.

In particular,

Y = Z×t(x) + 0.3x + 2.0 + 0.1×noize

I am creating pseudo data with.

First, since there is a 0.3x term, the higher the x: zeal for training subordinates of the boss, the higher the satisfaction of the interview with Y: subordinates.

Since there is a section of Z × t (x), the boss who took the training (Z = 1) will have the effect of t (x) added (** This is the effect of taking the training **).

This t (x) is this time ・ If the zeal x of boss's subordinate training is less than 0, the effect is low, and the satisfaction level Y increases by 0.5. ・ Satisfaction Y increases by 0.7 when x is 0 or more and less than 0.5. ・ Satisfaction Y increases by 1.0 when x is 0.5 or more.

will do.

In other words, the training effect changes depending on the variable x: the zeal of training the subordinates of the boss, which is an interaction and non-linear state involving Z and x.

I think that the situation where the boss who is more enthusiastic about training his subordinates has a greater effect even if he / she takes the same training is a valid situation as a real problem.

Lastly, the question of whether or not to take training in the first place is

Z_prob=sigmoid(5.0x-5.0*noise)

The sigmoid function is used to find the training attendance probability Z_prob for each person, and the probability Z_prob determines whether or not each person will attend the training. x: The higher the zeal of training the subordinates of the boss, the higher the probability of taking ** "For bosses: Point training for career interviews with subordinates" **.

The above is the pseudo data generation rule.

However, what we can actually get as data is the values of the three variables shown above.

x: Enthusiasm for training boss's subordinates Y: Satisfaction with interviews with subordinates Z: Whether the boss took the training

only.

Since we want to make causal inferences of non-linear phenomena this time, we need to build a non-linear regression model.

Machine learning comes out here.

Meta-Learners (T-Learner, S-Learner, X-Learner) and Doubly Robust Learning are common as causal inference methods using machine learning, and these methods are explained and implemented in this book. I will.

The specific procedure cannot be explained in this article, but if you use S-Learner and X-Learner to obtain the training effect, the following results will be obtained.

q5.png

X-Learner is more accurate than S-Learner. In both methods, it is properly desired that the training effect is gradually changing depending on x: the zeal of training the subordinates of the boss.

The result of this causal reasoning is

** "Please find the effect of Training Z from the following data." **

q4.png

It is a solution to the business problem.

It is important for a data scientist working in a company to acquire the ability to perform this causal reasoning analysis.

From Chapter 1 of this book, ** Learn while making! Causal analysis with Python ~ Introduction to causal reasoning and causal search ** Up to Chapter 5 is "Part 1 Causal Reasoning".

I will explain the causal reasoning method so far step by step.

4. Causal search method

Chapters 6 to 8 are "Part 2 Causal Search".

In causal reasoning, the relationship between causality and the direction of causality were known.

This causal relationship is unknown in the causal search, and the relationship is estimated.

For example

● Large-scale questionnaire on lifestyle and illness ● Survey of employee attitudes associated with work style reforms, such as ease of work, job satisfaction, and satisfaction with organizations and bosses in companies Such,

This is a method to find the causal relationship that exists between the survey items after collecting many items by questionnaire survey.

** "Chapter 6 Implementation of LiNGAM" ** explains and implements causal search by LiNGAM assuming a structural equation model with linear, non-circulating, and non-Gaussian noise. The point of LiNGAM is that it is closely related to independent component analysis and that a structural equation model is obtained, and while explaining that point, we will explain the flow of the algorithm while actually implementing it.

** "Chapter 7 Implementation of Bayesian Networks" ** first introduces skeletons, PDAGs, and conditional probability table CPTs as an explanation of Bayesian networks. Then, BIC is taken up as a Bayesian score showing the goodness of the fit of the observation data to the Bayesian network, and the calculation is explained and implemented. Next, we will explain and implement the method for testing the independence of two variables, and introduce three types of network estimation methods. Finally, in Section 7.5, we introduced the PC algorithm as a method of structural learning by conditional independent testing, and estimated and estimated the Bayesian network with data that made "For bosses: Point training for career interviews with subordinates" a little complicated. Estimates the values of unobserved variables from.

In causal analysis and causal search, not only machine learning and deep learning but also Bayesian networks are important, but I feel that there are few books that briefly describe Bayesian networks.

** As an introduction to the Bayesian network, I tried to make it a good introduction to learning the world view of the Bayesian network **.

The content of this chapter is as follows.

q6.png

q7.png

q8.png

q9.png

5. Deep learning × Causal search

At the end of this book, ** "Chapter 8 Causal Search Using Deep Learning" ** deals with causal search based on GAN (Generative Adversarial Network) technology of deep learning, its outline, network structure, and We will explain and implement the actual causal search.

As of 2020, research using deep learning in various fields is increasing, and research using deep learning is also progressing in the field of causal search.

For example ・ Causal search using graph neural network ・ Causal search using deep reinforcement learning ・ Causal search using GAN (Generative Adversarial Networks) Etc. have been announced.

The causal search method using deep learning is a field where research has just begun, and we believe that more sophisticated methods will be announced in the future.

Therefore, I was wondering whether to publish causal search using deep learning in this book, but I would like readers to feel the fields that are expected to develop in the future, so I will explain and implement it at the end of this book. It was made.

In this book, we will explain and implement ** SAM (Structural Agnostic Model) **, which is a causal search method using GAN, among the causal search methods using deep learning.

After explaining the concept of SAM and the network configuration graphically with the following image, we will actually implement the code and carry out a causal search.

q10.png

q11.png

All the contents of this document can be carried out by Google Colaboratory, and causal search by SAM is also carried out by using GPU in Google Colaboratory.

Section 8.5 of this book also describes how to use the GPU with Google Colaboratory.

at the end

This is the introduction of Causal Reasoning and Causal Search (for beginners) books using Python.

** "Learn while making! Causal analysis with Python-Introduction to the practice of causal reasoning and causal search" ** ● Released on June 30, 2020 ● Author: Yutaro Ogawa (Self-introduction, Twitter) @ Dentsu International Information Services Co., Ltd. (ISID) ) ● Publisher: Mynavi Publishing

Thank you for reading the above.


[Critical responsibility] Information Services International-Dentsu (ISID) AI Transformation Center Development Group Yutaro Ogawa [Disclaimer] The content of this article itself is the opinion / transmission of the author, not the official opinion of the company to which the author belongs. [Remarks] The AI department development team of Information Services International-Dentsu (ISID) is looking for members to acclaim. If you are interested, please click here


Recommended Posts

Causal reasoning and causal search with Python (for beginners)
INSERT into MySQL with Python [For beginners]
[Python] Read images with OpenCV (for beginners)
WebApi creation with Python (CRUD creation) For beginners
[For beginners] Try web scraping with Python
Automatically search and download YouTube videos with Python
Solve with Python [100 past questions that beginners and intermediates should solve] (028 --033 breadth-first search)
~ Tips for Python beginners from Pythonista with love ① ~
python textbook for beginners
Binary search with python
Binary search with Python3
~ Tips for Python beginners from Pythonista with love ② ~
OpenCV for Python beginners
[Introduction for beginners] Working with MySQL in Python
Solve with Python [100 selected past questions that beginners and intermediates should solve] (024 --027 Depth-first search)
Rock-paper-scissors poi in Python for beginners (answers and explanations)
Benchmark for C, Java and Python with prime factorization
Crawling with Python and Twitter API 1-Simple search function
[Introduction for beginners] Reading and writing Python CSV files
Python # How to check type and type for super beginners
Recursively search for files and directories in Python and output
Encryption and decryption with Python
Python and hardware-Using RS232C with Python-
Learning flow for Python beginners
Search for strings in Python
Python3 environment construction (for beginners)
Full bit search with Python
Python #function 2 for super beginners
Basic Python grammar for beginners
100 Pandas knocks for Python beginners
Python for super beginners Python #functions 1
python with pyenv and venv
Search engine work with python
Search twitter tweets with python
Python #list for super beginners
[Python] Depth-first search and breadth-first search
~ Tips for beginners to Python ③ ~
Streamline web search with python
Works with Python and R
Solve with Python [100 selected past questions that beginners and intermediates should solve] (015 --017 Full search: Permutation full search)
Solve with Python [100 selected past questions that beginners and intermediates should solve] (010 --014 Full search: Bit full search)
Solve with Python [100 selected past questions that beginners and intermediates should solve] (001 --004 All search: All enumeration)
Solving with Ruby and Python AtCoder ABC151 D Breadth-first search
[For beginners] Summary of standard input in Python (with explanation)
Installation procedure for Python and Ansible with a specific version
How to learn TensorFlow for liberal arts and Python beginners
Analyze stocks with python and look for favorable trading phases
Library for specifying a name server and dig with python
Solve AtCoder Problems Boot camp for Beginners (Medium 100) with python
[Python] Accessing and cropping image pixels using OpenCV (for beginners)
This and that for using Step Functions with CDK + Python
Shining life with Python and OpenCV
Robot running with Arduino and python
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
Python Exercise for Beginners # 2 [for Statement / While Statement]
Scraping with Python, Selenium and Chromedriver
Python for super beginners Python # dictionary type 1 for super beginners
Learn search with Python # 2bit search, permutation search