Giter Site home page Giter Site logo

jbdatascience / notears-1 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jmoss20/notears

0.0 1.0 0.0 53 KB

Implementation of "DAGs with NO TEARS: Smooth Optimization for Structure Learning", by Zheng et al. (2018)

Jupyter Notebook 82.34% Python 17.66%

notears-1's Introduction

notears

Python package implementing "DAGs with NO TEARS: Smooth Optimization for Structure Learning", Xun Zheng, Bryon Aragam, Pradeem Ravikumar and Eric P. Xing (March 2018, arXiv:1803.01422)

This package implements the NOTEARS learning algorithm, and supplies a few useful utilities (e.g. for generating random graphs, simulating data from linear Gaussian models, measuring performance, and thresholding edge matrices returned by NOTEARS to ensure acyclicity).

Optimization is ultimately performed by the SciPy implementation of L-BFGS-B.

Dependencies

numpy
scipy
networkx

Usage

See example_usage.ipynb for a simple Jupyter notebook demonstrating usage.

In general, using this package looks like:

import notears

output_dict = notears.run(notears.notears_standard, data, notears.loss.least_squares_loss, notears.loss.least_squares_loss_grad)
thresholded_output = notears.utils.threshold_output(output_dict['W'])

Parameters

notears.run

notears.run(variant, data, loss, loss_grad, c=0.25, r=10.0, e=1e-8, rnd_W_init=False, output_all_progress=False, verbose=False)
  • variant: Which implementation of NOTEARS to use (at this time, only notears.notears_standard is implemented)
  • data: An n x d numpy array, containing n samples (rows) of data from d variables (columns)
  • loss: Function to use as loss function, can either pass notears.loss.least_squares_loss for least squares loss, notears.loss.least_squares_loss_cov for expectation of least squares loss (recommended when using a large number of samples), or define a custom loss function (see notears/loss.py for reference implementations).
  • loss_grad: Function returning the gradient of the function specified as loss, with respect to the adjacency matrix W. (e.g. notears.loss.least_squares_loss_grad or notears.loss.least_squares_loss_cov_grad)
  • c: minimum rate of progress c \in (0,1) (see paper)
  • r: penalty growth rate r > 1 (see paper)
  • e: acyclicity loss stopping criteria \epsilon > 0 (see paper)
  • rnd_W_init: boolean, denoting whether or not to initialize $W$ to standard normal random matrix (default is a zero matrix)
  • output_all_progress: whether to return only the final value of the optimization process, or all intermediate values

Calling this function returns a dictionary of the form {'h': h(W), 'loss': loss(W, data), 'W': W}, unless output_all_progress is true, in which case it returns an array of such dictionaries.

Utilities

Some useful utilities are provided in notears/utils.py, and can be accessed from notears.utils.

  • threshold_output(W, desired_edges=None, verbose=False): takes in a (possibly cyclic) adjacency matrix, returns an acyclic adjacency matrix either by removing as few edges (by weight) as possible, or by finding the acyclic graph with total number of edges closest to desired_edges.
  • generate_random_dag(num_nodes, num_edges, probabilistic=False, edge_coefficient_range=[0.5, 2.0]): returns tuple (adj_ mat, g) containing a weighted random DAG represented as numpy adjacency matrix and networkx DiGraph, respectively. The probabilistic flag determines whether the graph will contain strictly num_edges number of edges, or some random number of edges with expectation num_edges.
  • simulate_from_dag_lg(adj_mat, n_sample, mean=0, variance=1): simulates n_samples samples from the linear Gaussian model specified by weighted DAG adjacency matrix adj_mat, with error terms drawn from N(mean, variance).
  • compare_graphs_undirected(true_graph, estimated_graph): takes two adjacency matrices, and returns list [true_positives, false_positives, true_negatives, false_negatives] in terms of adjacencies (i.e. undirected edges).
  • compare_graphs_precision, compare_graphs_recall, compare_graphs_specificity: take in the output of compare_graphs_undirected, returns either precision, recall, or specificity, again in terms of adjacencies.

notears-1's People

Contributors

jmoss20 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.