我正在尝试从头开始编写pagerank算法,到目前为止我构建了随机矩阵@implementation NSObject (HACK)
- (id)responderToSelector:(SEL)selector
{
if ([self respondsToSelector:selector] && [self.class instancesRespondToSelector:selector]) {
return self; // the class and the instance actually will respond to the selector when called
}
if ([self respondsToSelector:selector]) {
// invocations are forwarded
id forward = [self forwardingTargetForSelector:selector];
if (forward && forward != self) {
return [forward responderToSelector:selector];
}
NSMethodSignature *ms = [self methodSignatureForSelector:selector];
if (ms) {
MockInvocation *mockInvocation = [MockInvocation invocationWithMethodSignature:ms];
mockInvocation.selector = selector;
[self forwardInvocation:mockInvocation];
return mockInvocation.target ?: mockInvocation.innerTarget;
}
}
return nil;
}
@interface MockInvocation : NSInvocation
@property (nonatomic, weak) id innerTarget;
@end
@implementation IIOMockInvocation
- (void)invoke
{
}
- (void)invokeWithTarget:(id)target
{
_innerTarget = target;
}
@end
,现在我试图找到限制概率。限制概率计算如下:
G
结果应该是另一个具有正元素且总和为1的向量。当我进行计算时,我的结果收敛为零。这是代码:
- choose a random vector pi, such that each element is positive and sums to 1
- compute pi * G
- compute pi * G^2
- compute pi * G^3
- repeat until convergence
算法应该收敛到一个结果,结果的总和应该是1.但是我得到import numpy as np
# Define stochastic matrix (rows sum to 1), dimensions [6, 6]
G = np.array([[ 8.29187396e-04, 4.98341625e-01, 8.29187396e-04,
4.98341625e-01, 8.29187396e-04, 8.29187396e-04],
[ 3.32779623e-01, 5.53709856e-04, 3.32779623e-01,
5.53709856e-04, 3.32779623e-01, 5.53709856e-04],
[ 1.66666667e-01, 1.66666667e-01, 1.66666667e-01,
1.66666667e-01, 1.66666667e-01, 1.66666667e-01],
[ 4.98341625e-01, 4.98341625e-01, 8.29187396e-04,
8.29187396e-04, 8.29187396e-04, 8.29187396e-04],
[ 1.66666667e-01, 1.66666667e-01, 1.66666667e-01,
1.66666667e-01, 1.66666667e-01, 1.66666667e-01],
[ 1.65016502e-03, 1.65016502e-03, 1.65016502e-03,
1.65016502e-03, 9.91749175e-01, 1.65016502e-03]])
# Compute probability vector by repeated multiplication
n_iter = 200 # Number of iterations
pi = np.random.random(size=6) # Random starting vector
pi /= sum(pi) # Normalise
evolution = [np.dot(pi, G**i) for i in range(1, n_iter + 1)] # Stores each step
。此外,如果我在每次迭代中绘制结果,很明显结果将最终收敛为零:
我哪里错了?我确信转换矩阵是正确的,所以我认为问题出在最后的计算中。 任何帮助将不胜感激!