This repository implements an approach for Causal Inference on Time Series. It is heavily based on a paper by Chen, "A time series causal model".
It also contains the base implementation of Chen's algorithm and a simple time series data generator.
pip install -U -r requirements.txt
To start, first generate a causally dependent time series.
Params:
dimensions
(required) - the number of time series to generatemax_p
(required) - the max time lag of any causal effects, i.e. for a point in timet
there is at most a causal relationshipX<sub>t-p</sub> --> Y<sub>t</sub>
data_length
- the number of data points to generateincoming_edges
- the number of causal relationships per time series
from CIoTS import CausalTSGenerator
generator = CausalTSGenerator(dimensions=3, max_p=4, data_length=10000, incoming_edges=2)
data = generator.generate()
To visualize based on what model the dataset has been generated, we can visualize the partial time graph.
plt.title('Original graph')
generator.draw_graph()
To estimate the graph, we run Chen's algorithm and render the result as graph.
from CIoTS import pc_chen, partial_corr_test, draw_graph
predicted_graph = pc_chen(partial_corr_test, data, p=4, alpha=0.05)
plt.title('Estimated graph')
draw_graph(predicted_graph, dimensions=3, max_p=4)
We can also print metrics of the algorithm's accuracy.
from CIoTS import evaluate_edges
pd.DataFrame(evaluate_edges(generator.graph, predicted_graph), index=[0])
The above tutorial runs Chen's base algorithm. Our research will focus on estimating max_p
automatically and estimating the graph iteratively. This is still work in progress.