找到函数的固定点

时间:2013-03-06 20:17:57

标签: r iteration fixed-point

我试图找到逻辑分布函数的固定点,并确定不同参数值的固定点如何变化。代码如下:

nfxp.reps <- 0
err <- 10
p <- seq(0, 1, by = 0.0001)
pold <- p
gamma <- 6
k <- 3
while (err > 1E-12) {
  nfxp.reps <- nfxp.reps + 1
  if (nfxp.reps > 999) { 
    stop("The number of NFXP reps needs to be increased. \n")
  } 
  pnew <- plogis(-k + gamma * pold) 
  err <- max(abs(pnew - pold))
  pold <- pnew
}

上述代码在上述参数选择中运行良好:gamma和k - 找到3个固定点,2个稳定点和1个不稳定点(其中p = 0.5)。但是,如果我按比例更改上述参数,中间固定点高于或低于0.5,请说明:

gamma<-7
k<-3

循环无法定位中间固定点,即p = 0.3225(如果gamma = 7,k = 3)

4 个答案:

答案 0 :(得分:1)

我在新功能中重新排列代码。

p.fixed <- function(p0,tol = 1E-9,max.iter = 100,k=3,gamma=7,verbose=F){
  pold <- p0
  pnew <-  plogis(-k + gamma * pold) 
  iter <- 1
    while ((abs(pnew - pold) > tol) && (iter < max.iter)){
      pold <- pnew
      pnew <- plogis(-k + gamma * pold) 
      iter <- iter + 1
      if(verbose)
         cat("At iteration", iter, "value of p is:", pnew, "\n")
    }
    if (abs(pnew - pold) > tol) {
      cat("Algorithm failed to converge")
      return(NULL)
    }
    else {
      cat("Algorithm converged, in :" ,iter,"iterations \n")
      return(pnew)
    }
}

一些测试:

p.fixed(0.2,k=3,gamma=7)
Algorithm converged, in : 30 iterations 
[1] 0.08035782
> p.fixed(0.2,k=5,gamma=5)
Algorithm converged, in : 7 iterations 
[1] 0.006927088
> p.fixed(0.2,k=5,gamma=5,verbose=T)
At iteration 2 value of p is: 0.007318032 
At iteration 3 value of p is: 0.006940548 
At iteration 4 value of p is: 0.006927551 
At iteration 5 value of p is: 0.006927104 
At iteration 6 value of p is: 0.006927089 
At iteration 7 value of p is: 0.006927088 
Algorithm converged, in : 7 iterations 
[1] 0.006927088

答案 1 :(得分:1)

通过构造的固定点迭代无法在您的设置中找到不稳定的均衡,因为它是排斥的。换句话说,除非你从不稳定均衡开始,否则nfxp算法总是会远离它。

另一种方法是使用根解决方法。当然,不能保证找到所有固定点。这是一个简单的例子:

library(rootSolve) # for the uniroot.all function
pfind<-function(k=3,gamma=7) 
{
pdiff <-function(p0) p0-plogis(-k + gamma * p0) 
uniroot.all(p.diff,c(0,1))
}
> fps= pfind()
> fps
[1] 0.08036917 0.32257992 0.97925817

我们可以验证这一点:

pseq =seq(0,1,length=100)
plot(x=pseq ,y= plogis(-k + gamma *pseq),type= 'l')
abline(0,1,col='grey')
points(matrix(rep(fps,each=2), ncol=2, byrow=TRUE),pch=19,col='red')

希望这会有所帮助。

答案 2 :(得分:0)

我不太了解您使用的是哪个发行版; 这是我的定点方法的标准代码,我经常使用它,并在需要时进行更改(您必须在ftn中填写函数f(x);

<script src="https://code.jquery.com/jquery-1.8.3.min.js" integrity="sha256-YcbK69I5IXQftf/mYD8WY0/KmEDCv1asggHpJk1trM8=" crossorigin="anonymous"></script>
<script src="/js/jquery.address-1.5.js" type="text/javascript"></script> 

答案 3 :(得分:0)

不知道您到底做错了什么,但是我会给您我的代码,该代码始终可以找到固定点。下面的最后一个函数可用于计算函数g,其定义为g(x)= c * ftn(x)+ x。

fixpt_own <- function(x0, tol = 1e-6, max.iter = 100) {
  xold <- x0
  xnew <- ftn_g(xold)
  iter <- 1
  cat("At iteration 1 value of x is:", xnew, "\n")
  while ((abs(xnew-xold) > tol) && (iter < max.iter)) {
    xold <- xnew;
    xnew <- ftn_g(xold);
    iter <- iter + 1
    cat("At iteration", iter, "value of x is:", xnew, "\n")
  }
  if (abs(xnew-xold) > tol) {
    cat("Algorithm failed to converge\n")
    return(NULL)
  } else {
    cat("Algorithm converged\n")
    return(xnew)
  }
}
fixpt_own(3,1e-6,150)


ftn_g <- function(x){
  c <- 4;
  g <- c*(((1+x)/x - log(x))/(1+x)^2) + x;
  return(g)
}
相关问题