---
# System prepended metadata

title: Network
tags: [DS]

---

# Network Science
Imagine the chaos of a global financial system without interconnected banks, or the isolation of a community with no transportation routes. Networks, the intricate webs of relationships that connect us, are the invisible backbone of our world. This note delves into the fascinating field of network science, a powerful lens for understanding complex systems across diverse domains, from social interactions and the internet to biology and transportation.

![Screenshot 2024-11-17 at 10.23.13 PM](https://hackmd.io/_uploads/Skn8huPfJe.png)


## What are Networks?
In network science, we represent systems using two key elements:

* **Nodes:** These are the individual units within a system, such as people in a social network, computers on the internet, or genes in an organism.
* **Links:** These represent the connections or interactions between nodes, such as friendships, hyperlinks, or protein interactions. (See Figure 1)

This abstraction allows us to apply a common set of tools and concepts to analyze a wide range of seemingly disparate systems.

![Screenshot 2024-11-17 at 10.24.54 PM](https://hackmd.io/_uploads/BkMa3dwMkl.png =70%x)


### Why Networks Matter
Network science has become increasingly important due to its ability to provide insights into complex real-world problems. Here are a few examples:

* **Social Sciences:** Network analysis helps us understand how social media platforms influence political polarization, identify key figures in criminal networks, and design effective public health campaigns. For example, researchers analyzed Twitter data to track the spread of misinformation during the 2016 US presidential election.
* **Biology:** Network models are used to study the interactions between genes and proteins, leading to a deeper understanding of diseases like cancer. A recent study mapped the network of protein interactions in cancer cells to identify potential drug targets.
* **Technology:** The internet itself is a vast network, and network science is crucial for optimizing its performance, enhancing cybersecurity, and developing new communication technologies. For instance, network analysis is used to design efficient routing algorithms for data packets.


### A Brief History
The roots of network science can be traced back to the 18th century with Leonhard Euler's solution to the Seven Bridges of Königsberg problem. This puzzle, which involved finding a path that crossed each of the city's seven bridges only once, led to the development of graph theory, a fundamental building block of network science. Euler's insights laid the groundwork for the mathematical tools we use today to analyze network structure.

![Screenshot 2024-11-17 at 10.21.20 PM](https://hackmd.io/_uploads/SJ2J2uDfkl.png)


### Objectives
By the end of this note, you will be able to:

* **Quantify Node Importance:** Measure centrality and identify influential nodes in a network.
* **Analyze Network Structure:** Characterize connectivity patterns and statistical properties.
* **Build Network Models:** Generate synthetic networks and test hypotheses about real-world networks.
* **Detect Communities:** Identify groups of densely interconnected nodes.
* **Understand Network Dynamics:** Analyze how processes like information or disease spread through networks.

![image](https://hackmd.io/_uploads/S11n4jYfke.png)


### Tools and Software
This note focuses on the core concepts of network science, but practical application is essential for a well-rounded understanding. To that end, you'll be introduced to the following tools:

* **NetworkX:** A Python library offering a versatile toolkit for creating, manipulating, analyzing, and visualizing network data. Its intuitive interface and extensive functionality make it a popular choice for both beginners and experienced network scientists. Explore its capabilities further at https://networkx.org/.
* **iGraph:** Available in R, Python, and C, iGraph provides a robust platform for network analysis and visualization. It excels in handling large-scale networks and offers efficient algorithms for various network metrics and community detection. Discover more about iGraph at https://igraph.org/.
* **Gephi:** This open-source software focuses on the interactive visualization and exploration of network graphs.  Gephi's user-friendly interface allows for dynamic manipulation of network layouts, coloring, and filtering, facilitating the identification of patterns and insights. Download Gephi and explore its features at https://github.com/gephi/gephi.



## Network Fundamentals
A **network**, or **graph**, provides a structured way to represent relationships between objects. It consists of **nodes** (or **vertices**) connected by **links** (or **edges**). Networks are used to model a wide array of systems, from social connections and transportation routes to biological interactions and the internet.


### Types of Networks
Networks can be classified based on their directionality and the presence of weights on the links:

* **Directionality:**
    * **Directed networks:** Links have a direction, indicating a one-way relationship. Examples include Twitter followers, where User A can follow User B without the reverse being true, or website links, where Website A might link to Website B without a reciprocal link.
    * **Undirected networks:** Links represent mutual connections, such as friendships, where a connection between Person A and Person B implies a connection in both directions.
* **Weight:**
    * **Weighted networks:** Links have associated weights, which can represent the strength, capacity, or cost of the connection. For example, in a road network, the weight of a link between two cities could represent the distance or travel time.
    * **Unweighted networks:** Links simply indicate the presence or absence of a connection, without any additional information about the strength of the relationship.


### Network Representations
Efficient storage and analysis of network data depend on choosing the right data structure. Two common representations are:

1. **Adjacency Matrix:** This is an $N \times N$ matrix, where $N$ is the number of nodes in the network. The entry in row $i$ and column $j$ indicates the relationship between nodes $i$ and $j$. In an unweighted network, this entry is 1 if the nodes are connected and 0 otherwise. For weighted networks, the entry can represent the weight of the link.
2. **Link List (Edge List):** This representation lists all the links in the network. Each link is represented by a pair of nodes. For example, in an undirected network, the link between nodes $A$ and $B$ would be represented as $(A, B)$. In a directed network, the link from $A$ to $B$ would be represented as $(A, B)$, while the link from $B$ to $A$ would be $(B, A)$.


### Code Snippet (Python)
The following code demonstrates how to create and visualize networks using the `networkx` library in Python:

```python=
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np

def create_and_visualize_networks():
    # Create a graph from an adjacency matrix
    adj_matrix = np.array([
        [0, 1, 1],
        [1, 0, 1],
        [1, 1, 0]
    ])
    graph_matrix = nx.from_numpy_array(adj_matrix)

    # Create a graph from an edge list
    edge_list = [(0, 1), (0, 2), (1, 2)]
    graph_list = nx.Graph()
    graph_list.add_edges_from(edge_list)

    # Visualize the graphs
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))

    nx.draw(graph_matrix, ax=ax1, with_labels=True, 
            node_color='lightblue', node_size=500)
    ax1.set_title("Graph from Adjacency Matrix")

    nx.draw(graph_list, ax=ax2, with_labels=True, 
            node_color='lightgreen', node_size=500)
    ax2.set_title("Graph from Edge List")

    plt.tight_layout()
    plt.show()

    # Print information about the graphs
    print(f"Matrix graph - Nodes: {graph_matrix.nodes()}, "
          f"Edges: {graph_matrix.edges()}")
    print(f"List graph - Nodes: {graph_list.nodes()}, "
          f"Edges: {graph_list.edges()}")

# Run the demonstration
create_and_visualize_networks()
```

![image](https://hackmd.io/_uploads/BkMuhR9zkg.png)

```
Matrix graph - Nodes: [0, 1, 2], Edges: [(0, 1), (0, 2), (1, 2)]
List graph - Nodes: [0, 1, 2], Edges: [(0, 1), (0, 2), (1, 2)]
```



## Characterizing Network Structure
How can we understand the complex relationships between individuals in a social network, or the connections between web pages on the internet? How can we quantify the influence of a person or a website within these vast networks? This section explores the fundamental concepts and tools used to characterize the structure of networks, providing insights into their organization and behavior.


### Node Degree and Degree Distribution
The **degree** of a node is a fundamental concept in network science. It tells us how many connections a node has to other nodes in the network. Think of it as a measure of a node's "popularity" or "connectedness."

#### Definition of Degree
In an **undirected network**, where connections are mutual (like friendships), the degree of a node is simply the number of links it has. For example, in a social network, if you have five friends, your node would have a degree of five.

A network where all nodes have the same degree is called a **regular network**. Imagine a perfectly structured grid where every point is connected to exactly four neighbors – that's a regular network.

![image](https://hackmd.io/_uploads/SJ5P1aYMJg.png)

#### Degree in Directed Networks
In **directed networks**, where connections have a direction (like followers on Twitter), we distinguish between:

* **In-degree:** The number of links coming into a node. This is like the number of followers you have on social media.
* **Out-degree:** The number of links going out of a node. This is like the number of people you follow on social media.

#### Degree Distribution
The **degree distribution** of a network gives us a broader picture of the network's structure. It tells us the probability of a randomly chosen node having a certain degree.  In simpler terms, it shows how many nodes have one connection, how many have two, and so on.

##### Key Characteristics
* **Heavy Tails:** In many real-world networks, you'll find a few nodes with a very large number of connections (like celebrities on social media) and many nodes with just a few connections. This creates a "heavy-tailed" distribution.
* **Power-law Behavior:** Often, the tail of the degree distribution (the part with the high-degree nodes) follows a power law. This means that the probability of finding a node with a very high degree decreases rapidly but never quite reaches zero.
* **Average Degree:** This is simply the average number of connections per node in the network.

![image](https://hackmd.io/_uploads/ryAtg6Fzke.png)

#### The Friendship Paradox
Ever felt like your friends are more popular than you? That's the **friendship paradox** in action! It states that, on average, your friends have more friends than you do. This seems counterintuitive, but it's a natural consequence of network structure.

Why does it happen? Because you're more likely to be friends with someone who has many friends (a high-degree node) than with someone who has few friends.  Those popular individuals skew the average number of friends your friends have.

![image](https://hackmd.io/_uploads/B18MZTFz1e.png)

#### Code Snippet
```python=
def analyze_degree_distribution(n_nodes=20, m_edges=2):
    # Create Barabási-Albert graph
    graph = nx.barabasi_albert_graph(n_nodes, m_edges, seed=42)
    
    # Calculate degrees
    degrees = [deg for (node, deg) in graph.degree()]
    
    # Create visualization
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
    
    # Network visualization
    pos = nx.spring_layout(graph, seed=42)
    nx.draw(graph, pos, ax=ax1, node_color='lightblue',
           node_size=500, with_labels=True)
    ax1.set_title(f'Barabási-Albert Network\n(n={n_nodes}, m={m_edges})')
    
    # Degree distribution
    ax2.hist(degrees, bins=range(min(degrees), max(degrees) + 2, 1),
            align='left', rwidth=0.8, color='lightblue')
    ax2.set_xlabel('Node Degree')
    ax2.set_ylabel('Number of Nodes')
    ax2.set_title('Degree Distribution')
    ax2.grid(True, alpha=0.3)
    
    plt.tight_layout()
    plt.show()
    
    # Print statistics
    print("\nNetwork Statistics:")
    print(f"Number of nodes: {graph.number_of_nodes()}")
    print(f"Number of edges: {graph.number_of_edges()}")
    print(f"Average degree: {np.mean(degrees):.2f}")
    print(f"Maximum degree: {max(degrees)}")

# Run the analysis
analyze_degree_distribution()
```

![image](https://hackmd.io/_uploads/HkTA36Ff1g.png)


```
Network Statistics:
Number of nodes: 20
Number of edges: 36
Average degree: 3.60
Maximum degree: 13
```


### Walks and Paths in Networks
Walks and paths are essential concepts for understanding how information, influence, or resources flow through a network. Think of them as different ways to navigate the network's connections.

![image](https://hackmd.io/_uploads/BkL47pKG1l.png)

#### Basic Definitions
* **Walk:** A sequence of nodes where each consecutive pair is connected by a link. Walks can revisit nodes and links multiple times. Imagine wandering through a city without a specific destination, potentially crossing the same intersections or walking down the same streets multiple times.
* **Path:** A walk where each node is visited only once (except possibly the start and end nodes, if it's a closed path). Paths represent distinct routes without repetition. This is like planning a route with a specific destination in mind, where you avoid going in circles or revisiting places you've already been.

#### Uses
* **Walks:** Used to model dynamic processes on networks, such as:
    * **Random walks:** Simulate how information or disease might spread randomly through a network. Think of how a rumor might spread through a social network, jumping from person to person without a clear pattern.
    * **PageRank algorithm:** Used by Google to rank websites, based on the idea of a "random surfer" following links. A website is considered more important if many other important websites link to it.
* **Paths:** Crucial for:
    * **Finding shortest routes:** Applications like GPS navigation or finding the most efficient way to transport goods rely on finding shortest paths.
    * **Understanding network flow:** Analyzing how resources or information move through a network, like how internet traffic flows between different servers.

#### Distance Measures
The **distance** between two nodes is the length of the shortest path between them. It's the minimum number of "steps" you need to take to get from one node to another.

* **Example:** In a social network, the distance between you and a friend of a friend would be 2.

In undirected networks, distance satisfies the standard properties of a metric:

* **Non-negativity:** Distance is always greater than or equal to zero.
* **Identity:** The distance from a node to itself is zero.
* **Symmetry:** The distance from node A to node B is the same as the distance from node B to node A.
* **Triangle inequality:** The distance from node A to node C is less than or equal to the sum of the distances from A to B and from B to C.

In directed networks, symmetry may not hold. For example, in a network of website links, it might be possible to get from website A to website B in two clicks, but getting from website B back to website A might take five clicks.

#### Average Distance and Diameter
* **Average Distance:** The average distance between all pairs of nodes in the network. This gives you an idea of how "close" nodes are to each other on average. Many real-world networks exhibit the "small-world" phenomenon, where the average distance is surprisingly small, even for very large networks. This is the idea behind the "six degrees of separation" concept, suggesting that any two people on Earth are connected by an average of six or fewer intermediate acquaintances.
* **Diameter:** The maximum distance between any two nodes in the network. This tells you the "longest shortest path" in the network.

#### Connectedness
* **Undirected Networks:**
    * **Connected:** A network where there is a path between every pair of nodes. You can get from any node to any other node in the network.
    * **Connected Component:** If a network is not connected, it can be divided into connected components. Each connected component is a group of nodes where you can get from any node within the group to any other node within the group, but you can't get to nodes outside the group.
* **Directed Networks:**
    * **Strongly Connected:** There is a directed path from node A to node B and a directed path from node B to node A for every pair of nodes A and B in the network.
    * **Weakly Connected:**  There is a path between every pair of nodes if you ignore the direction of the links.

#### Code Snippet
```python=
def visualize_shortest_path(n_nodes=20, m_edges=2, source=0, target=10):
    # Create network
    graph = nx.barabasi_albert_graph(n_nodes, m_edges, seed=42)
    
    # Find shortest path
    shortest_path = nx.shortest_path(graph, source=source, target=target)
    avg_path_length = nx.average_shortest_path_length(graph)
    
    # Visualization
    plt.figure(figsize=(8, 6))
    pos = nx.spring_layout(graph, seed=42)
    
    # Draw basic network
    nx.draw(graph, pos, node_color='lightgray',
           node_size=500, with_labels=True,
           edge_color='lightgray', width=1)
    
    # Highlight shortest path
    path_edges = list(zip(shortest_path[:-1], shortest_path[1:]))
    nx.draw_networkx_edges(graph, pos, edgelist=path_edges,
                         edge_color='red', width=2)
    
    # Highlight source and target
    nx.draw_networkx_nodes(graph, pos, 
                         nodelist=[source, target],
                         node_color=['green', 'red'],
                         node_size=700)
    
    plt.title('Shortest Path in Network')
    plt.axis('off')
    plt.show()
    
    # Print information
    print(f"Shortest path from node {source} to node {target}:")
    print(f"Path: {' → '.join(map(str, shortest_path))}")
    print(f"Path length: {len(shortest_path) - 1} steps")
    print(f"Average shortest path length: {avg_path_length:.2f}")

# Run the visualization
visualize_shortest_path()
```

![image](https://hackmd.io/_uploads/H1lrQpptzyg.png)


```
Shortest path from node 0 to node 10:
Path: 0 → 10
Path length: 1 steps
Average shortest path length: 1.97
```


### Clustering Coefficient
The **clustering coefficient** measures the tendency of nodes in a network to cluster together. In essence, it quantifies how interconnected a node's neighbors are. Think of it as a measure of how tightly-knit a node's local community is within the larger network.

#### Local Clustering Coefficient
The local clustering coefficient $C_i$ of a node $v_i$ is defined as:

$$
C_i \equiv \frac{\text{number of triangles including the }i\text{th node}}{k_i(k_i-1)/2}
$$

where $k_i$ is the degree of node $v_i$. The denominator normalizes the coefficient, ensuring $0 \leq C_i \leq 1$.

* $C_i=1$: All of $v_i$'s neighbors are connected to each other.
* $C_i=0$: None of $v_i$'s neighbors are connected to each other.

![image](https://hackmd.io/_uploads/BJGCSTtfJl.png)

##### Example
Imagine a social network. If you have five friends, and all your friends are also friends with each other, your local clustering coefficient would be 1. This indicates a very close-knit group.

#### Global Clustering Coefficient
The global clustering coefficient C is the average of the local clustering coefficients over all nodes:

$$
C = \frac{1}{N}\sum_{i=1}^N C_i
$$

where $N$ is the number of nodes. This gives you an overall sense of how clustered the network is as a whole.

#### Small-World Networks
The clustering coefficient is crucial for understanding **small-world networks**, which have:

* High clustering coefficient (like regular lattices)
* Short average path lengths (like random networks)

This means that nodes tend to be clustered in local communities, but it's still relatively easy to get from one node to another across the network. Many real-world networks exhibit small-world properties, such as social networks, the internet, and even the neural networks in our brains.

![image](https://hackmd.io/_uploads/H1S8U6tfJe.png)

#### Triadic Closure and Link Prediction
A high clustering coefficient often indicates **triadic closure**, the tendency for nodes with a common neighbor to become connected. This principle is used in **link prediction** algorithms, which aim to predict the formation of future links based on the current network structure.

#### Code Snippet
```python=
def analyze_clustering(n_nodes=10, k_neighbors=4, p_rewiring=0.2):
    # Create Watts-Strogatz small-world network
    graph = nx.watts_strogatz_graph(n_nodes, k_neighbors, p_rewiring, seed=42)
    
    # Calculate clustering coefficients
    node_clustering = nx.clustering(graph)
    avg_clustering = nx.average_clustering(graph)
    
    # Visualization
    plt.figure(figsize=(8, 6))
    pos = nx.circular_layout(graph)
    
    # Draw edges
    nx.draw_networkx_edges(graph, pos)
    
    # Draw nodes colored by clustering coefficient
    nodes = nx.draw_networkx_nodes(graph, pos,
                                node_color=list(node_clustering.values()),
                                node_size=800,
                                cmap=plt.cm.viridis)
    
    # Add labels
    nx.draw_networkx_labels(graph, pos)
    plt.colorbar(nodes, label='Clustering Coefficient')
    
    plt.title('Node Clustering Coefficients')
    plt.axis('off')
    plt.show()
    
    # Print results
    print(f"\nAverage clustering coefficient: {avg_clustering:.3f}")
    print("\nIndividual clustering coefficients:")
    for node in sorted(graph.nodes()):
        print(f"Node {node}: {node_clustering[node]:.3f}")

# Run the analysis
analyze_clustering()
```

![image](https://hackmd.io/_uploads/Sy0u6pKzJg.png)

```
Average clustering coefficient: 0.513

Individual clustering coefficients:
Node 0: 0.500
Node 1: 0.400
Node 2: 0.333
Node 3: 0.333
Node 4: 0.333
Node 5: 0.667
Node 6: 0.500
Node 7: 1.000
Node 8: 0.400
Node 9: 0.667
```


### Centrality Measures
Centrality measures quantify the importance or influence of nodes within a network. While **degree centrality** (the number of connections) is a simple and intuitive measure, it doesn't always capture the full picture of a node's role in the network. Different centrality measures emphasize different aspects of node "importance."

#### Common Centrality Measures
* **Degree Centrality:** This is the most basic measure, simply counting the number of connections a node has. It's a good starting point, but it treats all connections equally and doesn't consider the importance of the nodes a node is connected to.
    * **Best for:** Identifying popular or well-connected nodes.
    * **Limitations:** Doesn't account for the influence of neighbors or the overall network structure.
* **Closeness Centrality:** Focuses on how easily a node can reach other nodes in the network. It's defined as the inverse of the average distance from a node to all other nodes. A higher closeness centrality indicates that a node is, on average, "closer" to all other nodes and can potentially spread information or influence more quickly.
    * **Best for:** Identifying nodes that can quickly interact with the entire network, such as in communication or transportation networks.
    * **Limitations:** Can be less informative in disconnected networks or networks with very different path lengths.
* **Betweenness Centrality:** Measures how often a node lies on the shortest paths between other nodes. It quantifies a node's potential to control information flow or act as a bridge between different parts of the network. Nodes with high betweenness centrality are often critical for network connectivity and can play important roles in mediating interactions.
    * **Best for:** Identifying nodes that act as bridges or gatekeepers in a network, such as influencers in social networks or critical points in a supply chain.
    * **Limitations:** Computationally expensive for large networks, and may not be as relevant in networks where shortest paths are not the primary concern.
* **Katz Centrality:** Takes into account both direct and indirect connections, giving more weight to shorter paths. It sums the number of walks of different lengths between nodes, with an exponential decay factor downweighting longer walks. This captures the idea that influence can propagate through the network, but its strength diminishes with distance.
    * **Best for:** Identifying influential nodes in networks where influence decays with distance, such as social networks or citation networks.
    * **Limitations:** Requires setting a decay parameter, which can be subjective, and may not be suitable for all types of networks.
* **PageRank:** Designed for directed networks, based on the idea that a node is important if it's linked to by other important nodes. It's like a recursive popularity contest, where nodes gain influence from the endorsements of other influential nodes. This is the algorithm that Google originally used to rank web pages.
    * **Best for:** Identifying influential nodes in directed networks, particularly web pages or social media accounts.
    * **Limitations:** Biased towards older nodes, and can be susceptible to manipulation through link spamming.

![image](https://hackmd.io/_uploads/B1lmdaKzkg.png)

| Centrality Measure | Focus | Best for | Limitations |
|-------------------|-------|----------|-------------|
| Degree | Number of connections | Popular nodes | Ignores neighbor influence |
| Closeness | Shortest path to all other nodes | Quick network access | Less useful in disconnected networks |
| Betweenness | Control over shortest paths | Bridges and gatekeepers | Computationally expensive |
| Katz | Direct and indirect influence | Influence with decay | Requires decay parameter |
| PageRank | Influence from endorsements | Web pages and social media | Biased towards older nodes |

#### Applications of Centrality Measures
Centrality measures are valuable for predicting node influence and understanding network dynamics in various contexts:

* **Social Networks:** Identifying influential users for targeted advertising, community detection, and understanding opinion dynamics. 
* **Epidemiology:** Targeting high-centrality individuals for vaccination to control disease spread, identifying super-spreaders, and predicting outbreak patterns.
* **Information Diffusion:** Understanding how information spreads through social media, identifying key spreaders, and combating misinformation.
* **Transportation Networks:** Identifying critical hubs and bottlenecks, optimizing traffic flow, and planning efficient routes.
* **Biological Networks:** Analyzing protein-protein interaction networks to identify key proteins involved in diseases, understanding gene regulatory networks, and developing drug targets.

#### Code Snippet
```python=
def analyze_centrality():
    # Create small directed network
    graph = nx.gnp_random_graph(10, 0.3, directed=True, seed=42)
    
    # Calculate centralities
    centralities = {
        'Closeness': nx.closeness_centrality(graph),
        'Betweenness': nx.betweenness_centrality(graph),
        'Katz': nx.katz_centrality(graph, alpha=0.1),
        'PageRank': nx.pagerank(graph)
    }
    
    # Plot
    fig, axes = plt.subplots(2, 2, figsize=(10, 10))
    pos = nx.spring_layout(graph, seed=42)
    
    # Draw networks with different centrality measures
    for (title, cent), ax in zip(centralities.items(), axes.ravel()):
        nodes = nx.draw_networkx_nodes(graph, pos,
                                     node_color=list(cent.values()),
                                     node_size=800,
                                     cmap='viridis',
                                     ax=ax)
        nx.draw_networkx_edges(graph, pos, ax=ax, arrows=True)
        nx.draw_networkx_labels(graph, pos, ax=ax)
        plt.colorbar(nodes, ax=ax)
        ax.set_title(title)
        ax.axis('off')
    
    plt.tight_layout()
    plt.show()
    
    # Print most central nodes
    print("\nMost Central Nodes:")
    for title, cent in centralities.items():
        top_node = max(cent.items(), key=lambda x: x[1])[0]
        print(f"{title}: Node {top_node}")

analyze_centrality()
```

![image](https://hackmd.io/_uploads/HJHa1RFzJx.png)

```
Most Central Nodes:
Closeness: Node 8
Betweenness: Node 2
Katz: Node 8
PageRank: Node 6
```



## Advanced Concepts
To truly understand the complexity of networks, we need to go beyond basic metrics. This section introduces advanced concepts that allow us to analyze networks through the lens of spectral properties and recurring patterns called motifs. These tools have applications in diverse fields, from understanding social dynamics to deciphering the structure of the human brain.


### Spectral Properties of Networks
The **spectral properties** of a network, derived from the eigenvalues and eigenvectors of associated matrices, offer a powerful way to analyze its structure and dynamics. Think of it like uncovering the hidden "fingerprint" of a network, revealing its fundamental characteristics. Different matrices provide unique perspectives:

* **Adjacency matrix $A$:** Represents direct connections between nodes.
* **Laplacian matrix $L$:** Captures the difference between a node's degree and its connections. Defined as $L=D−A$, where $D$ is the degree matrix.
* **Normalized Laplacian matrix $\tilde{L}$:** Scales the Laplacian matrix by node degrees. Useful for comparing networks with different degree distributions. Defined as $\tilde{L} = D^{-1/2}LD^{-1/2} = I - D^{-1/2}AD^{-1/2}$.

![image](https://hackmd.io/_uploads/r1eK25pYfke.png)

#### Why Eigenvalues and Eigenvectors Matter
Eigenvalues and eigenvectors of these matrices reveal important structural properties:

* **Connectedness:** The number of zero eigenvalues of $L$ or $\tilde{L}$ equals the number of connected components.
* **Spectral Gap:** The smallest non-zero eigenvalue of $L$ (the spectral gap) is crucial for understanding diffusion and synchronization processes.
* **Fiedler Vector:** The eigenvector corresponding to the second-smallest eigenvalue of $L$ (the Fiedler vector) provides insights into network structure, such as identifying bottlenecks and communities.
* **Bipartiteness:** The largest eigenvalue of $\tilde{L}$ equals 2 if and only if the network is bipartite (nodes can be divided into two groups with connections only between groups).

#### Applications
Spectral properties form the basis of many network analysis algorithms:

* **Centrality:** The dominant eigenvector of $\tilde{L}$ is closely related to PageRank. The dominant eigenvector of $A$ defines eigenvector centrality.
* **Community Detection:** The Fiedler vector and other eigenvectors can be used to identify communities.
* **Graph Partitioning:** Spectral methods can efficiently divide a network into smaller components.

#### Code Snippet
```python=
def analyze_spectral_properties(n_nodes=20, m_edges=2):
    # Create network
    graph = nx.barabasi_albert_graph(n_nodes, m_edges, seed=42)
    
    # Calculate Laplacian and eigenvalues
    laplacian = nx.laplacian_matrix(graph).toarray()
    eigenvalues = np.real(np.linalg.eigvals(laplacian))
    eigenvalues.sort()
    
    # Visualization
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
    
    # Network structure
    pos = nx.spring_layout(graph, seed=42)
    nx.draw(graph, pos, ax=ax1,
           node_color='lightblue',
           node_size=500,
           with_labels=True)
    ax1.set_title('Network Structure')
    
    # Eigenvalue spectrum
    ax2.plot(range(len(eigenvalues)), eigenvalues, 'bo-', markersize=8)
    ax2.grid(True, alpha=0.3)
    ax2.set_xlabel('Index')
    ax2.set_ylabel('Eigenvalue')
    ax2.set_title('Laplacian Eigenvalue Spectrum')
    
    plt.tight_layout()
    plt.show()
    
    # Print properties
    print("\nSpectral Properties:")
    print(f"Number of nodes: {n_nodes}")
    print(f"Smallest eigenvalue: {eigenvalues[0]:.6f}")
    print(f"Algebraic connectivity: {eigenvalues[1]:.6f}")
    print(f"Largest eigenvalue: {eigenvalues[-1]:.6f}")
    print(f"Spectral gap: {eigenvalues[1] - eigenvalues[0]:.6f}")
    print(f"\nGraph is {'connected' if nx.is_connected(graph) else 'not connected'}")
    print(f"Number of components: {sum(abs(ev) < 1e-10 for ev in eigenvalues)}")

# Run the analysis
analyze_spectral_properties()
```

![image](https://hackmd.io/_uploads/SJOZlCFMJl.png)

```
Spectral Properties:
Number of nodes: 20
Smallest eigenvalue: 0.000000
Algebraic connectivity: 0.942386
Largest eigenvalue: 14.195245
Spectral gap: 0.942386

Graph is connected
Number of components: 1
```


### Network Motifs
Imagine you're studying a social network and notice that groups of three friends often form tight triangles where everyone knows everyone else. Or perhaps you're analyzing a biological network and observe recurring patterns of interconnected genes that regulate specific functions. These recurring patterns are called network motifs, and they provide valuable clues about the organization and function of complex networks.

#### What are Network Motifs?
Network motifs are **statistically significant subgraphs**—they occur more frequently than expected by chance in a network. Think of them as the building blocks of complex networks, revealing recurring patterns of interconnections that may have functional significance.

![Screenshot 2024-11-17 at 9.39.24 PM](https://hackmd.io/_uploads/S1PffOwfJg.png)

#### Identifying Motifs
1. **Enumerate Subgraphs:** Identify and count all possible subgraphs of a certain size within the network. For example, you might count all the different ways three nodes can be connected to each other.
2. **Assess Statistical Significance:** Determine which subgraphs occur significantly more often than expected in a random network model. This helps us distinguish true motifs from patterns that arise simply by chance.

#### Why Statistical Significance Matters
Simply counting subgraphs can be misleading because some patterns might appear more often just due to the network's size or degree distribution. To account for this, we compare the observed subgraph frequencies to those expected in a null model, typically a randomized network with the same number of nodes and degree distribution as the original network. This allows us to identify motifs that are truly over-represented.

#### Z-Score Analysis
To quantify over- or under-representation of a subgraph $m$, we use the **Z-score**:

$$
Z = \frac{C(m) - \langle \tilde{C}(m) \rangle}{\text{std}[\tilde{C}(m)]}
$$

where:
* $C(m)$ is the frequency of subgraph $m$ in the original network.
* $\langle \tilde{C}(m) \rangle$ is the average frequency of $m$ in randomized networks.
* $\text{std}[\tilde{C}(m)]$ is the standard deviation of the frequency of $m$ in randomized networks.

A high Z-score indicates a motif (over-represented), while a low Z-score suggests under-representation.

#### Examples of Motifs in Different Networks
* **Social Networks:** Triangles (indicating close-knit groups), stars (representing a central hub), and chains (showing linear information flow).
* **Biological Networks:** Feed-forward loops (regulating gene expression), bi-fan motifs (involved in signal transduction), and feedback loops (controlling biological processes).
* **Neural Networks:** Specific patterns of interconnected neurons that may be associated with learning, memory, or other cognitive functions.

#### Applications
Motif analysis has been particularly successful in analyzing biological networks, where motifs can represent functional building blocks. It's also used in:

* **Social Networks:** Identifying communities, predicting user behavior, and understanding information diffusion.
* **Technological Networks:** Analyzing the internet's structure, designing robust communication networks, and improving network security.
* **Ecological Networks:** Understanding food webs, predicting species interactions, and analyzing the impact of environmental changes.



## Models of Networks
When analyzing real-world networks, it's essential to have suitable **network models** for comparison. These models serve as benchmarks, helping us determine if observed patterns are significant or simply due to chance. They can also provide insights into the mechanisms shaping network structure.

This chapter explores different network models, categorized into:

1. **Random Graph Models:** Treat links as random variables.
    * Example: **Erdős-Rényi (ER) model** - connects nodes randomly with a fixed probability.
2. **Mechanistic Models:** Aim to capture the dynamic processes that drive network formation.
    * Example: **Barabási-Albert (BA) model** - incorporates preferential attachment, where new nodes are more likely to connect to existing nodes with high degrees.


### The Erdős-Rényi Random Graph Model
The **Erdős-Rényi (ER) random graph model**, denoted by $G(N,p)$, is a cornerstone of network science. It generates random graphs with  N nodes, where each possible link exists with probability p, independently of other links.

![image](https://hackmd.io/_uploads/B1TmzAKG1l.png)

#### Properties
* **Number of Links:** Follows a binomial distribution. The expected number of links is $pN(N−1)/2$.
* **Degree Distribution:** Also follows a binomial distribution. For large, sparse networks, this can be approximated by a Poisson distribution: $$p(k) = \frac{\langle k \rangle^k}{k!}e^{-\langle k \rangle}$$ where $\langle k \rangle$ is the average degree.
* **Average Distance:** For dense ER graphs, the average shortest path length scales logarithmically with the number of nodes, exhibiting the "small-world" property.
* **Clustering Coefficient:** For sparse ER graphs, the clustering coefficient is low and approaches zero as the network size increases.

#### Phase Transition
The ER model shows a phase transition in the size of the largest connected component as the average degree $\langle k \rangle$ changes:

* $\langle k \rangle < 1$: Many small disconnected components.
* $\langle k \rangle = 1$: A giant component emerges.
* $\langle k \rangle > 1$: A giant component dominates the network.

![image](https://hackmd.io/_uploads/rJ6dcfqfJl.png)

#### Role in Network Science
* **Advantages:** Simplicity, analytical tractability, explains the "small-world" phenomenon.
* **Limitations:** Low clustering, unrealistic degree distribution for many real-world networks, lacks community structure.

#### Code Snippet
```python=
def analyze_er_graph(n=20, p=0.2):
    # Generate ER graph
    er_graph = nx.erdos_renyi_graph(n, p, seed=42)
    
    # Calculate properties
    degrees = dict(er_graph.degree())
    avg_clustering = nx.average_clustering(er_graph)
    
    # Visualization
    plt.figure(figsize=(12, 5))
    
    # Plot network
    plt.subplot(121)
    pos = nx.spring_layout(er_graph, seed=42)
    nx.draw(er_graph, pos,
           node_color='lightblue',
           node_size=500,
           with_labels=True)
    plt.title('Erdős-Rényi Network')
    
    # Plot degree distribution
    plt.subplot(122)
    plt.hist(list(degrees.values()), bins='auto',
            color='lightblue', edgecolor='black')
    plt.xlabel('Degree')
    plt.ylabel('Frequency')
    plt.title('Degree Distribution')
    
    plt.tight_layout()
    plt.show()
    
    # Print statistics
    print(f"\nNetwork Statistics (n={n}, p={p}):")
    print(f"Average degree: {np.mean(list(degrees.values())):.2f}")
    print(f"Average clustering coefficient: {avg_clustering:.3f}")
    print(f"Number of edges: {er_graph.number_of_edges()}")
    print("\nDegree range:")
    print(f"Min degree: {min(degrees.values())}")
    print(f"Max degree: {max(degrees.values())}")

# Run analysis
analyze_er_graph()
```

![image](https://hackmd.io/_uploads/rkiUsz9zye.png)

```
Network Statistics (n=20, p=0.2):
Average degree: 3.70
Average clustering coefficient: 0.153
Number of edges: 37

Degree range:
Min degree: 0
Max degree: 7
```


### The Configuration Model
The configuration model extends the Erdős-Rényi model by allowing for arbitrary degree distributions. This helps isolate the effects of degree heterogeneity on network properties.

#### Definition
The configuration model generates random graphs with a prescribed degree sequence $\{k_1, k_2, \dots, k_N\}$, where $k_i$ is the degree of node $v_i$. This sequence can be drawn from any degree distribution, as long as the sum of degrees is even.

![Screenshot 2024-11-17 at 9.40.46 PM](https://hackmd.io/_uploads/B1Yvf_PG1e.png)

#### Generation Method
1. **Create Stubs:** Assign each node a number of "stubs" (half-edges) equal to its degree.
2. **Connect Stubs:** Randomly connect pairs of stubs to form links.
3. **Avoid Invalid Connections:** Prevent self-loops and multiple edges.

#### Network Properties
* **Average Distance:** The scaling of the average distance with network size depends on the degree distribution. For power-law degree distributions with exponent $\gamma$, it can exhibit ultra-small-world behavior ($\gamma \leq 3$).
* **Clustering Coefficient:** Generally low unless the degree distribution is highly heterogeneous.
* **Spectral Properties:** The largest eigenvalue of the expected adjacency matrix is related to the degree distribution.

#### Why the Configuration Model Matters
The configuration model is a valuable tool in network science because it allows researchers to:

* Study the impact of degree heterogeneity on network properties.
* Generate random networks with specific degree distributions for comparison with real-world networks.
* Isolate the effects of degree heterogeneity from other structural features.

#### Code Snippet
```python=
def analyze_config_model(n=50, exponent=2.1):
    """
    Analyze configuration model with power-law degree sequence.
    """
    # Generate degree sequence and ensure minimum degree is 1
    sequence = [max(1, int(x)) for x in nx.utils.powerlaw_sequence(n, exponent)]
    
    # Ensure sum of degrees is even
    if sum(sequence) % 2 != 0:
        sequence[0] += 1
    
    # Generate configuration model graph
    G = nx.configuration_model(sequence, seed=42)
    
    # Convert to simple graph (remove self-loops and parallel edges)
    G = nx.Graph(G)
    
    # Get largest connected component
    largest_cc = max(nx.connected_components(G), key=len)
    G = G.subgraph(largest_cc).copy()
    
    # Calculate network properties
    degrees = dict(G.degree())
    
    # Visualization
    plt.figure(figsize=(12, 5))
    
    # Plot network
    plt.subplot(121)
    pos = nx.spring_layout(G, seed=42)
    nx.draw(G, pos,
           node_color='lightblue',
           node_size=300,
           with_labels=True)
    plt.title('Configuration Model Network\n(Largest Component)')
    
    # Plot degree distribution
    plt.subplot(122)
    plt.hist(list(degrees.values()), 
            bins=range(min(degrees.values()), max(degrees.values()) + 2, 1),
            color='lightblue', edgecolor='black')
    plt.xlabel('Degree')
    plt.ylabel('Frequency')
    plt.title('Degree Distribution')
    
    plt.tight_layout()
    plt.show()
    
    # Print statistics
    print(f"\nNetwork Statistics:")
    print(f"Original network size: {n}")
    print(f"Largest component size: {len(G)}")
    print(f"Average degree: {np.mean(list(degrees.values())):.2f}")
    print(f"Number of edges: {G.number_of_edges()}")
    print(f"Average path length: {nx.average_shortest_path_length(G):.3f}")
    print("\nDegree range:")
    print(f"Min degree: {min(degrees.values())}")
    print(f"Max degree: {max(degrees.values())}")

# Run analysis
analyze_config_model()
```

![image](https://hackmd.io/_uploads/S1VlRz5MJx.png)

```
Network Statistics:
Original network size: 50
Largest component size: 42
Average degree: 2.38
Number of edges: 50
Average path length: 3.128

Degree range:
Min degree: 1
Max degree: 15
```


### Growing Networks with Preferential Attachment
Many real-world networks grow over time, with new nodes and links constantly being added. Think of citation networks, the World Wide Web, and social networks. To understand these evolving structures, we need to understand the mechanisms behind their growth.

One key mechanism is **preferential attachment**, where new nodes are more likely to connect to existing nodes with high degrees. This "rich-get-richer" effect can lead to the emergence of hubs and a heavy-tailed degree distribution.

#### The Barabási-Albert (BA) Model
The **BA model** captures the essence of preferential attachment. It generates networks as follows:

1. Initialization: Start with a small initial network.
2. Growth: Add new nodes one by one. Each new node forms connections to existing nodes.
3. Preferential Attachment: The probability of connecting to an existing node is proportional to its current degree: $$\Pi(k_i) = \frac{k_i}{\sum_{j=1}^{N'} k_j}$$ where $k_i$ is the degree of node $v_i$ and $N'$ is the number of existing nodes.

![Screenshot 2024-11-17 at 9.37.34 PM](https://hackmd.io/_uploads/HkYjbdwM1l.png)

#### Properties of BA Networks
* **Degree Distribution:** The BA model produces networks with a power-law degree distribution: $$p(k) \propto k^{-3}$$
* **Average Distance:** The average shortest path length scales logarithmically with network size.
* **Clustering Coefficient:** Relatively low and decreases with network size.

#### Model Extensions
The basic BA model has limitations. More realistic extensions incorporate local mechanisms, such as:

* **Redirection:** A new node follows an existing link to reach a target node and then decides whether to connect to the target or its neighbor.
* **Copying:** A new node copies the neighbors of a randomly chosen existing node.

These mechanisms can lead to preferential attachment and other desirable network properties, like higher clustering and community structure.

#### Code Snippet
```python=
def analyze_ba_graph(n=20, m=3):
    """
    Analyze Barabási-Albert graph properties
    n: number of nodes
    m: number of edges per new node
    """
    # Generate BA graph
    ba_graph = nx.barabasi_albert_graph(n, m, seed=42)
    
    # Get properties
    degrees = [deg for (node, deg) in ba_graph.degree()]
    degree_dist = nx.degree_histogram(ba_graph)
    
    # Visualization
    plt.figure(figsize=(12, 5))
    
    # Plot network
    plt.subplot(121)
    pos = nx.spring_layout(ba_graph, seed=42)
    nx.draw(ba_graph, pos,
           node_color='lightblue',
           node_size=500,
           with_labels=True)
    plt.title('Barabási-Albert Network')
    
    # Plot degree distribution (log-log)
    plt.subplot(122)
    degrees_plot = np.array(range(len(degree_dist)))
    frequencies = np.array(degree_dist)
    
    # Remove zeros for log plot
    nonzero_mask = frequencies > 0
    plt.loglog(degrees_plot[nonzero_mask], frequencies[nonzero_mask], 
              'bo-', markersize=8)
    plt.grid(True)
    plt.xlabel('Degree (log)')
    plt.ylabel('Frequency (log)')
    plt.title('Degree Distribution\n(Log-Log Scale)')
    
    plt.tight_layout()
    plt.show()
    
    # Print statistics
    print(f"\nNetwork Statistics:")
    print(f"Number of nodes: {n}")
    print(f"Edges per new node: {m}")
    print(f"Total edges: {ba_graph.number_of_edges()}")
    print(f"Average degree: {2 * m:.1f} (expected), "
          f"{np.mean(degrees):.1f} (actual)")
    print(f"Degree range: {min(degrees)} to {max(degrees)}")

# Run analysis
analyze_ba_graph()
```

![image](https://hackmd.io/_uploads/BJ3ZbXqMyg.png)

```
Network Statistics:
Number of nodes: 20
Edges per new node: 3
Total edges: 51
Average degree: 6.0 (expected), 5.1 (actual)
Degree range: 3 to 12
```



## Mathematical Toolbox
This section provides a concise overview of essential mathematical tools used in network science. We'll cover probability theory, power-law distributions, maximum likelihood estimation, stochastic processes, and matrix algebra, highlighting their applications in understanding and analyzing networks.


### Probability Theory
Probability theory is essential for understanding and analyzing networks, particularly when dealing with uncertainty and random processes. It helps us model and interpret patterns in various network scenarios, from social connections to information spread.

#### Random Variables and Probability
* **Random Variables:** These represent uncertain quantities or events in a network. They can be:
    * **Discrete:** Take on distinct, separate values.
        * **Example:** The number of connections a user has on a social network.
        * **Mathematically:** Let $X$ be a discrete random variable. The probability that $X$ takes on the value x is denoted by $P(X=x)$ or $p(x)$.
    * **Continuous:** Can take on any value within a given range.
        * **Example:** The distance between two nodes in a geographical network. 
        * **Mathematically:** Let $X$ be a continuous random variable. The probability that $X$ falls within the interval $[a,b]$ is given by $$P(a \leq X \leq b) = \int_b^a f(x)dx$$ where $f(x)$ is the probability density function (PDF) of X.
* **Probability:** A measure of the likelihood of an event occurring. It's expressed as a number between 0 (impossible) and 1 (certain).
    * **Axioms of Probability:**
        * Non-negativity: $P(A) \geq 0$ for any event $A$.
        * Normalization: $P(\Omega)=1$, where $\Omega$ is the sample space (set of all possible outcomes).
        * Additivity: If $A$ and $B$ are mutually exclusive events, then $P(A \cup 

#### Probability Distributions
Probability distributions describe how the probabilities of different values of a random variable are distributed.

* **Discrete Distributions:**
    * **Probability Mass Function (PMF):** Assigns a probability to each possible value of a discrete random variable.
        * Example: When rolling a fair die, each face (1 to 6) has a PMF of 1/6, i.e., $P(X=i)=1/6$ for $i=1,2,\dots,6$.
    * **Common Discrete Distributions:**
        * **Bernoulli:** Models binary events (e.g., presence or absence of a link). PMF: $P(X=1)=p$, $P(X=0)=1−p$.
        * **Binomial:** Models the number of successes in a fixed number of trials (e.g., the number of connections a node has). PMF: $P(X=k) = \binom{n}{k}p^k(1-p)^{n-k}$, where $n$ is the number of trials and p is the probability of success on each trial.
        * **Geometric:** Models the number of trials until a success (e.g., the steps a random walker takes to reach a target). PMF: $P(X=k)=(1−p)^{k-1}p$
* **Continuous Distributions:**
    * **Probability Density Function (PDF):** Describes the probability "density" at each point for a continuous random variable. The probability of a variable falling within a specific interval is found by integrating the PDF over that interval.
    * **Common Continuous Distributions:**
        * **Uniform:** All values within a range are equally likely. PDF: $f(x)=\frac1{b-a}$ for $a \leq x \leq b$.
        * **Exponential:** Often used to model times between events (e.g., the duration of a network connection). PDF: $f(x)= \lambda e^{-\lambda x}$ for $x \geq 0$.
        * **Gaussian (Normal):** The "bell curve," commonly found in many natural phenomena. PDF: $f(x)= \frac1{\sqrt{2\pi\sigma^2}} e^{- \frac{(x-\mu)^2}{2 \sigma^2}}$, where $\mu$ is the mean and $\sigma^2$ is the variance.


#### Conditional Probability and Bayes' Rule
* **Conditional Probability:** The probability of an event happening given that another event has already happened.
    * Mathematically: $p(A|B) = \frac{p(A \cap B)}{p(B)}$, where $P(A|B)$ denotes the conditional probability of event $A$ given event $B$.
    * Example: In a disease spread network, the probability of a person getting infected given they are connected to an infected individual.
* **Bayes' Rule:** A way to update our beliefs about an event based on new evidence. It relates conditional probabilities.
    * **Mathematically:** $p(A|B) = \frac{p(B|A)p(A)}{p(B)}$
    * Example: In a social network, inferring the likelihood of two people knowing each other based on their shared connections.


#### Information Theory Concepts
* **Entropy:** Measures the uncertainty associated with a random variable. Higher entropy indicates greater uncertainty.
    * **Mathematically:** For a discrete random variable X with PMF $p(x)$, the entropy $H(X)$ is: $H(X) = -\sum_x p(x) \log p(x)$.
    * Example: A random network has higher entropy than a highly structured network.
* **Mutual Information:** Quantifies how much information two random variables share. Higher mutual information suggests a stronger relationship between the variables.
    * Mathematically: $I(X,Y)=H(X)+H(Y)−H(X,Y)$ where $H(X,Y)$ is the joint entropy of $X$ and $Y$.
    * Example: Measuring how much knowing the degree of a node tells us about its neighbors' degrees.


### Power-Law Distributions
Power-law distributions are a fascinating class of probability distributions that appear frequently in network science and the study of complex systems. Unlike more familiar distributions like the exponential or Gaussian, power-law distributions have "heavy tails," meaning they assign significant probability to extremely large values. This characteristic gives rise to unique properties and has important implications for understanding real-world phenomena.

#### Definition and Characteristics
A power-law distribution follows a specific mathematical form where the probability of observing a value decreases as a power of that value. A common example is the **Pareto distribution**:

$$
p(x) = Cx^{-\alpha} \text{ }(x \geq x_{\min})
$$

where:
* $x_\min > 0$ is the minimum value for which the power-law behavior holds.
* $\alpha > 0$ is the **power-law exponent**, which determines the rate at which the probability decreases.
* $C = (\alpha-1)x_{min}^{\alpha-1}$ is a normalization constant.

#### Key Properties
* **Heavy Tail:** The defining feature of a power-law distribution is its heavy tail. This means that even very large values have a non-negligible probability of occurring. This contrasts with exponential or Gaussian distributions, where the probability of observing extreme values quickly diminishes.
* **Scale Invariance:** Power-law distributions exhibit scale invariance, meaning that if you scale the variable by a constant factor, the overall shape of the distribution remains the same. This property is often described as "self-similarity."
* **Moments:** The moments of a power-law distribution (like the mean and variance) can diverge (become infinite) for certain values of the exponent $\alpha$. This has implications for statistical analysis and can lead to situations where traditional measures like the average are not meaningful.
* **Linear Behavior in Log-log Plots:** A distinctive visual characteristic of power-law distributions is that they appear as straight lines when plotted on a log-log scale. This provides a useful diagnostic tool for identifying power-law behavior in empirical data.

![image](https://hackmd.io/_uploads/By6N1hFzJx.png)

![Screenshot 2024-11-19 at 2.26.06 PM](https://hackmd.io/_uploads/HJqFJhFz1l.png)

#### Real-World Examples
Power-law distributions are found in a surprisingly diverse range of phenomena:

* **Network Degree Distributions:** Many real-world networks, including social networks, the internet, and citation networks, exhibit power-law degree distributions. This means that a few nodes have a very large number of connections (hubs), while most nodes have only a few.
* **Wealth and Income Distribution:** The distribution of wealth and income often follows a power-law, with a small fraction of the population holding a disproportionately large share of the total wealth. This is often referred to as the Pareto principle or the "80-20 rule."
* **City Sizes:** The distribution of city sizes within a country or region tends to follow a power-law, with a few very large cities and many smaller ones.
* **Word Frequency:** The frequency of words in a large text corpus often follows a power-law known as Zipf's law. A few words (like "the" and "a") occur very frequently, while most words are relatively rare.
* **Natural Phenomena:** Power-laws also appear in various natural phenomena, such as the size of earthquakes, the intensity of solar flares, and the size of forest fires.


### Maximum Likelihood Estimation (MLE)
When working with data, we often assume it comes from a specific probability distribution (like a Gaussian or an exponential). But how do we find the parameters of that distribution that best fit the observed data? Maximum Likelihood Estimation (MLE) provides a powerful and widely used framework for doing just that.

#### The Principle
The core idea behind MLE is simple yet elegant: choose the parameter values that maximize the likelihood of observing the data you actually have. In other words, MLE finds the parameter values that make the observed data the "most likely" outcome under the assumed probability distribution.

#### Formal Definition
Let's make this more concrete with some mathematical notation. Suppose we have a set of independent observations $\{x_1, x_2, \dots, x_n\}$ and we assume they are drawn from a probability distribution with parameters θ. The **likelihood function** $L(\theta|\{x_i\})$ is the probability of observing this data given the parameters $\theta$:

$$
L(\theta|{x_i}) = \prod_{i=1}^n p(x_i|\theta)
$$

where $p(x_i|\theta)$ is the probability density function (PDF) or probability mass function (PMF) of the distribution evaluated at $x_i$, given the parameters $\theta$.

MLE aims to find the value of $\theta$ that maximizes $L(\theta|\{x_i\})$. In practice, it's often easier to work with the log-likelihood function:

$$
\log L(\theta|{x_i}) = \sum_{i=1}^n \log p(x_i|\theta)
$$

Maximizing the log-likelihood is equivalent to maximizing the likelihood since the logarithm is a monotonically increasing function.

#### Illustrative Examples
* **Estimating the Mean of a Normal Distribution:** Suppose you have a sample of heights of students and you assume they follow a normal distribution. MLE would estimate the mean $\mu$ and standard deviation $\sigma$ of this distribution such that the observed heights are most likely under those parameters.
* **Estimating the Rate of a Poisson Process:** Imagine you are analyzing the number of customers arriving at a store each hour, and you assume this follows a Poisson process. MLE would estimate the rate parameter $\lambda$ that maximizes the likelihood of observing the actual customer arrival counts.
* **Estimating the Exponent of a Power-Law Distribution:** If you are studying the distribution of website traffic and believe it follows a power-law, MLE can be used to estimate the power-law exponent $\alpha$ that best fits the observed data.

#### Challenges and Considerations
While MLE is a powerful tool, it's important to be aware of its limitations and potential challenges:

* **Computational Complexity:** Finding the maximum likelihood estimate can be computationally challenging, especially for complex models or large datasets. Numerical optimization methods are often required.
* **Model Dependence:** MLE relies heavily on the chosen model being a good representation of the data. If the model is misspecified, the estimates may be inaccurate.
* **Bias:** MLE estimators can be biased, especially for small sample sizes. This means that the estimates may not be centered around the true parameter values.
* **Parameter Constraints:** Many distributions have constraints on their parameters (e.g., the variance of a normal distribution must be positive). These constraints need to be considered during the optimization process.
* **Estimating Power-Law Parameters:** Estimating the parameters of a power-law distribution using MLE presents specific challenges, particularly in determining the minimum value $x_{\min}$ where the power-law behavior starts.


### Stochastic Processes
Stochastic processes are powerful mathematical tools for modeling systems that evolve randomly over time. They are essential in network science for understanding how dynamic processes unfold on networks, from the spread of information to the formation of connections. Here's an overview of some key types:

#### Markov Chains
Imagine a system that jumps between different states, like a web surfer clicking from page to page or a message hopping between nodes in a communication network. A Markov chain models this behavior with a special property: the future state depends only on the current state, not the history of past states. This is known as the **Markov property**.

* **Formal Definition:** A discrete-time Markov chain is a sequence of random variables $\{X_0, X_1, X_2, \dots\}$  taking values in a set of states, where the probability of transitioning to the next state depends only on the current state: $$p(X_{t+1} = i_{t+1}|X_t = i_t, \ldots, X_1 = i_1, X_0 = i_0) = p(X_{t+1} = i_{t+1}|X_t = i_t)$$
* **Transition Matrix:** A Markov chain is often represented by a transition matrix $T$, where $T_{ij}$ is the probability of transitioning from state $i$ to state $j$.
* **Network Applications:**
    * **Random Walks:** Model how information or influence spreads through a network.
    * **PageRank Algorithm:** Used by Google to rank web pages based on the probability of a random web surfer landing on a page.
    * **Community Detection:** Markov chains can help identify communities or clusters of densely connected nodes.

#### Renewal Processes
Think of events happening randomly in time, like users logging into a website or packets arriving at a router. Renewal processes model these sequences, where the key concept is the inter-event time—the time between consecutive events.

* **Key Assumptions:** Inter-event times are independent and identically distributed.
* **Poisson Process:** A special type of renewal process where events happen at a constant rate and the time until the next event is memoryless (doesn't depend on how long it's been since the last event).
* **Network Applications:**
    * **Modeling Network Traffic:** Analyzing the arrival patterns of data packets.
    * **Reliability Analysis:** Studying the time between failures in network components.
    * **Queueing Theory:** Modeling waiting times in networks with limited capacity.

#### Branching Processes
Imagine a process where individuals reproduce and die over generations, like a chain of infections in an epidemic or the spread of a rumor on social media. Branching processes capture this behavior.

* **Galton-Watson Process:** A classic branching process where each individual produces a random number of offspring according to a fixed probability distribution.
![Screenshot 2024-11-17 at 9.58.15 PM](https://hackmd.io/_uploads/BJzF8_Dfyl.png)
* **Network Applications:**
    * **Epidemic Modeling:** Studying how diseases spread through a population based on contact networks.
    * **Social Network Analysis:** Analyzing the growth of online communities or the viral spread of information.
    * **Cascading Failures:** Modeling how failures in one part of a network can trigger failures in other parts.

#### Random Walks
Picture a "walker" moving randomly on a network, like a particle hopping between atoms in a material or a piece of information traversing the internet. Random walks describe this movement.

* **Jump Distribution:** Determines the probability of the walker moving to different neighbors.
* **Connection to Diffusion:** Random walks can approximate diffusion processes, where particles spread out over time due to random motion.
* **Network Applications:**
    * **Search Algorithms:** Used in algorithms for finding information or resources in a network.
    * **Community Detection:** Random walks can help identify communities by exploring local network structure.
    * **Recommendation Systems:** Used to recommend products or content to users based on their network connections and preferences.


### Matrix Algebra in Network Science
Matrices provide a powerful language for representing and analyzing networks. They allow us to capture the relationships between nodes in a concise and mathematically tractable way, opening the door to a wide range of network analysis techniques.

#### Matrices as Network Representations
* **Adjacency Matrix:** The most common way to represent a network with a matrix. In an adjacency matrix $A$, the entry $A_{ij}$ is 1 if nodes $i$ and $j$ are connected by an edge, and 0 otherwise. This simple representation encodes the entire network structure.
* **Other Matrix Representations:** Beyond adjacency matrices, other matrices can capture additional information:
    * **Laplacian Matrix:** Encodes information about the degrees of nodes and is used in spectral graph theory.
    * **Weight Matrix:** Represents weighted networks where edges have different strengths or capacities.

#### Eigenvalues and Eigenvectors
Eigenvalues and eigenvectors are fundamental concepts in linear algebra that have profound implications for network analysis.

* **Definition:** For a square matrix $A$, an eigenvector $u$ and its corresponding eigenvalue $\lambda$ satisfy: $$A\mathbf{u} = \lambda \mathbf{u}$$ This means that multiplying the eigenvector by the matrix simply scales the eigenvector by a factor of $\lambda$.
* **Network Interpretation:** Eigenvectors and eigenvalues reveal hidden structure within the network. For example, the eigenvector corresponding to the largest eigenvalue of the adjacency matrix (often called the **principal eigenvector**) can be used to identify the most "central" nodes in the network.

#### Eigenvalue Decomposition
For symmetric matrices (like the adjacency matrix of an undirected network), we can decompose the matrix in terms of its eigenvalues and eigenvectors:

$$
A = \sum_{\ell=1}^N \lambda_\ell \mathbf{u}_\ell \mathbf{u}_\ell^{\top}
$$

where $\lambda_\ell$ and $\mathbf{u}_\ell$ are the eigenvalues and eigenvectors of $A$. This decomposition has numerous applications in network analysis:

* **Dimensionality Reduction:** Representing the network in a lower-dimensional space using the dominant eigenvectors.
* **Spectral Clustering:** Using eigenvectors to partition the network into communities.
* **Network Dynamics:** Analyzing how processes spread and evolve on the network.

#### Perron-Frobenius Theorem
The Perron-Frobenius theorem provides important insights into the properties of non-negative matrices, which are often used to represent networks. It states that:

* A non-negative square matrix has a largest eigenvalue that is real and non-negative.
* If the matrix is irreducible (meaning the network is connected), this largest eigenvalue is unique and has a corresponding eigenvector with all positive entries.

This theorem has implications for understanding network connectivity and identifying important nodes.


#### Applications in Network Analysis
Eigenvalues and eigenvectors are used in a wide variety of network analysis techniques:

* **Centrality Measures:** Eigenvector centrality is a measure of node importance based on the principal eigenvector of the adjacency matrix.
* **Community Detection:** Spectral clustering algorithms use eigenvectors to identify clusters or communities of densely connected nodes.
* **Network Dynamics:** Eigenvalue analysis can be used to study how processes like information diffusion or disease spread evolve on a network.
* **Link Prediction:** Eigenvalues and eigenvectors can be used to predict missing links or future connections in a network.



## Reference
* Lambiotte, R. (2020). C5.4 Networks Lecture Notes. https://courses.maths.ox.ac.uk/mod/resource/view.php?id=50765


## Further Reading
* Albert, R., & Barabási, A. (2002). Statistical mechanics of complex networks. Reviews of Modern Physics, 74(1), 47–97. https://doi.org/10.1103/RevModPhys.74.47
* Masuda, N., & Lambiotte, R. (2016). A guide to temporal networks. World Scientific.
* Newman, M. (2018). Networks (2nd ed.). Oxford University Press. ISBN 978-0-19-252749-3
* Thurner, S., Hanel, R., & Klimek, P. (2018). Introduction to the theory of complex systems. Oxford University Press.