Amberrizvi / 41189-assignment-template

A Jupyter Notebook template for 41189 modelling assignments
0 stars 0 forks source link

Preferential attachment model help #4

Open Amberrizvi opened 3 years ago

Amberrizvi commented 3 years ago

Hi @juancastilla

Please see the latest changes in section 4.1 - this outlines what I want to do with this model. Below are some links to preferential attachment models (using the barabasi graph function) I could edit. I will also look into building the code from scratch, but I'm having trouble finding similar code for reference as I try to do this:

Amberrizvi commented 3 years ago

I have found this too, what do you think? seems straightfoward

def scale_free(n,m): if m < 1 or m >=n: raise nx.NetworkXError("Preferential attactment algorithm must have m >= 1" " and m < n, m = %d, n = %d" % (m, n))

Add m initial nodes (m0 in barabasi-speak)

G=nx.empty_graph(m)

# Target nodes for new edges
targets=list(range(m))
# List of existing nodes, with nodes repeated once for each adjacent edge
repeated_nodes=[]
# Start adding the other n-m nodes. The first node is m.
source=m
while source<n:
    # Add edges to m nodes from the source.
    G.add_edges_from(zip([source]*m,targets))
    # Add one node to the list for each new edge just created.
    repeated_nodes.extend(targets)
    # And the new node "source" has m edges to add to the list.
    repeated_nodes.extend([source]*m)
    # Now choose m unique nodes from the existing nodes
    # Pick uniformly from repeated_nodes (preferential attachement)
    targets = _random_subset(repeated_nodes,m)
    source += 1
return G
juancastilla commented 3 years ago

Hi Amber, the code you pasted above is already implemented in NetworkX. You just need to call the nx.barabasi_albert_graph() function, like so:

image

Amberrizvi commented 3 years ago

What line of code should I be changing in order to create a different degree of bias? @juancastilla

juancastilla commented 3 years ago

Hi Amber, OK now I understand what you want to do. I believe this code is easier to understand and modify:

https://github.com/AlxndrMlk/Barabasi-Albert_Network https://github.com/AlxndrMlk/Barabasi-Albert_Network/blob/master/BA_model.py

The part you need to modify to add bias is probably this one

node_proba = node_degr / (2 * len(G.edges()))

node_proba is a list of probabilities that is used to decide where incoming nodes are attaching to. To add the bias, you would need to increase the probabilities for the most connected nodes. In the traditional Barabasi model the assignment is random (to the most connected nodes) but you want to artificially increase the probabilities for the highly connected nodes. For example, you can increase the probabilities of the top N nodes in node_proba.

Note that node_proba should add up to 1!

Does this make sense?

Give it a try and let me know how you go

Juan

Amberrizvi commented 3 years ago

@juancastilla Would it be something like this?:

node_proba = node_degr / (2 * len(G.edges())) node_proba = 0.6 node_proba = 0.4

Sorry, this could be completely incorrect but I'm only just learning Python!

Thank you!

juancastilla commented 3 years ago

not quite @Amberrizvi

node_proba is an array of numbers, each one indicating the probability that a new node attaches to it. To introduce the bias you need to decrease the probabilities of some nodes and increase the probabilities of others (by the same amount so that the sum of the array = 1)

You can run the code line by line in Jupyter and explore the intermediate results. That's the best way to understand how it works

juancastilla commented 3 years ago

@Amberrizvi print out the values in node_proba by adding "print(node_proba)" after that line and you will understand what I mean

Amberrizvi commented 3 years ago

@juancastilla For some reason I'm getting an error with section 2 of the code that wasn't occurring before, when it is pasted exactly as it was from the original. Any idea what it might be?


NameError Traceback (most recent call last)

in 1 # SECTION 2 - VISUALISATION FUNCTION 2 ----> 3 def k_distrib(graph=G, scale='lin', colour='#40a6d1', alpha=.8, expct_lo=1, expct_hi=10, expct_const=1): 4 plt.close() 5 num_nodes = graph.number_of_nodes() NameError: name 'G' is not defined
juancastilla commented 3 years ago

That code is expecting a graph G as an input. From what I can see, you need to run Section 3 before Section 2. Section 3 generates the graph, Section 3 visualises it. Should be the other way around. @Amberrizvi

Amberrizvi commented 3 years ago

Thank you :)

Amberrizvi commented 3 years ago

Another question @juancastilla I have added the array in: node_proba = (0.6, 0.4) However, when the model is visualised in both a linear and a log graph they seem to be straight (rather than having the log distribution). Have I done something wrong with the code?

juancastilla commented 3 years ago

the array node_proba cannot be fixed, it should grow as the algorithms add more nodes to the network. This array stores the probabilities for all the nodes in the network i.e. the probability that a new node attaches to it. So if you fix it to (0.6, 0.4) it wont work

Amberrizvi commented 3 years ago

Oh okay, thank you!