问题描述
我正在尝试使用Barabasi-Albert模型生成无标度网络.该模型预测度分布遵循p(k)〜k ^ -3,但我的显示为k ^ -2.
I'm trying to generate a scale-free network using the Barabasi-Albert model. The model predicts a degree distribution that follows p(k) ~ k^-3 but mine shows k^-2.
该算法取自Barabasi的书,网址为: http://barabasi.com/networksciencebook ,这是相关的段落:
The algorithm was taken from Barabasi's book at this URL: http://barabasi.com/networksciencebook,here is the relevant paragraph:
这是我的代码,有人可以帮我找出问题所在吗?
Here is my code, could someone please help me figure out what is wrong?
import numpy as np
import matplotlib.pyplot as plt
from collections import Counter
plt.rcParams["figure.figsize"] = (15,6)
#initialize values
N = 10000
k = 2
m = int(k / 2)
#initialize matrices
adjacency = np.zeros((N,N))
degrees = np.zeros(N)
#add links
for i in range(N):
degrees[i] = m
for c in range(m):
# choose a node with probability proportional to it's degree
j = np.random.choice(N, p = degrees / (2 * m * i + m + c))
degrees[j] += 1
adjacency[i][j] += 1
adjacency[j][i] += 1
def get_binned_data(labels, values, num):
min_label, max_label = min(labels), max(labels)
base = (max_label / min_label) ** (1 / num)
bins = [base**i for i in range(int(np.log(max_label) / np.log(base)) + 1)]
binned_values, binned_labels = [], []
counter = 0
for b in bins:
bin_size = 0
bin_sum = 0
while counter < len(labels) and labels[counter] <= b:
bin_size += values[counter]
bin_sum += values[counter] * labels[counter]
counter += 1
if(bin_size):
binned_values.append(bin_size)
binned_labels.append(bin_sum / bin_size)
return binned_labels, binned_values
labels, values = zip(*sorted(Counter(degrees).items(), key = lambda pair:
pair[0]))
binned_labels, binned_values = get_binned_data(labels, values, 15)
fig, (ax1, ax2) = plt.subplots(ncols = 2, nrows = 1)
fig.suptitle('Barabasi-Albert Model',fontsize = 25)
ax1.loglog(binned_labels, binned_values, basex = 10, basey = 10, linestyle =
'None', marker = 'o', color = 'red')
ax1.set(xlabel = 'degree', ylabel = '# of nodes')
ax1.set_title('log-log scale (log-binned)',{'fontsize':'15'})
ax2.plot(labels, values, 'ro')
ax2.set(xlabel = 'degree', ylabel = '# of nodes')
ax2.set_title('linear scale',{'fontsize':'15'})
plt.show()
推荐答案
您的代码未运行(np.random.choice中的概率不等于1).为什么不p = degrees/np.sum(degrees)
?
Your code does not run (probabilities in np.random.choice do not sum to 1). Why not p = degrees/np.sum(degrees)
?
根据维基百科,您需要从一些已经连接的节点开始,而您从零开始.另外,您可能应该在内部循环之后放置degrees[i] = m
,以避免形成从节点i到其自身的链接.
According to Wikipedia, you need to start with some already connected nodes, whereas you start from nothing. Also, you should probably put degrees[i] = m
after the inner loop to avoid forming links from node i to itself.
这可能会有所帮助,但我不清楚您如何生成学位图,因此我无法对其进行验证.
This might help, but it's not clear to me how you generate your degree plot, so I can't verify it.
这篇关于Barabasi-Albert模型,错误指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!