C语言 为什么不计算gamma变量的均方误差?

6vl6ewon  于 2022-12-11  发布在  其他
关注(0)|答案(1)|浏览(95)

所以我写了这段代码,计算不同样本和β值下alpha和beta的均方估计值,alpha的值是3,因为我们把三个指数分布加起来就得到了gamma变量。
但是,这段代码没有给出任何正确的输出。

# include <stdio.h>
# include <math.h>
# include <stdlib.h> // Don't forget to include this

main()
{
  int n,N; // Sample size and number of simulations
  long double alpha=3,beta,suma=0.0,sumb=0.0,msea,mseb;  
  int i,j,k;
  printf("Enter the number of simulations");
  scanf("%d", &N);
  printf("Enter the sample size");
  scanf("%d", &n);
  printf("Enter the value of beta");
  scanf("%Lf", &beta);
  //Simulation starts to calculate MSE
  for(i=0;i<N;i++)
    {
      float msea=0.0,mseb=0.0,sum=0.0,sumsq=0.0; //Vlaue is reset at every iteration, so declared inside i loop
      for(j=0;j<n;j++)//each sample
        {
          float g;
          for(k=0;k<alpha;k++)
              g += (double)(-1/beta)*log(1-((double)rand()/RAND_MAX));//sum of exp is gamma
          sum += g;
          sumsq += g*g;
        }
      float xbar = sum/n;
      float var = sumsq/n - xbar*xbar;
      suma += pow ((xbar*xbar/var - alpha),2);
      sumb += pow ((xbar/var - beta),2);
    }
  msea = suma/n;
  mseb = sumb/n;
  printf("MSE of alpha is = %Lf", msea);
  printf("MSE of beta is = %Lf", mseb);
  
  return 0;
}

你能帮我找出我的错误吗?

xfb7svmp

xfb7svmp1#

代码至少存在以下问题:

未初始化的g

float g;
 for (k = 0; k < alpha; k++)
   g += (double) (-1 / beta) * log(1 - ((double) rand() / RAND_MAX));

一个月一个月

log(1-((double)rand()/RAND_MAX))可能会提供log(0)。我怀疑log(1 - (((double) rand() + 0.5)/ (RAND_MAX + 1u)))会提供更好的分发。

3个FP类型

float, double, long double有很多上变频和下变频。
始终使用double,直到确定需要其他宽度。

未使用的对象

不使用float msea=0.0,mseb=0.0。提示:启用所有警告并保存时间。

使用%g

"%g"的信息量更大。

// printf("MSE of alpha is = %Lf", msea);
printf("MSE of alpha is = %Lg", msea);

OP报告“我做了更改,它仍然在输出“。

MSE of alpha is = 105.474
MSE of beta is = 31.4536

我怀疑OP还没有做出所有的改变。

# include <stdio.h>
# include <math.h>
# include <stdlib.h> // Don't forget to include this

int main() {
  int n, NN; // Sample size and number of simulations
  double alpha = 3, beta, suma = 0.0, sumb = 0.0, msea, mseb;
  int i, j, k;
  printf("Enter the number of simulations");
//  scanf("%d", &NN);
  printf("Enter the sample size");
//  scanf("%d", &n);
  printf("Enter the value of beta");
//  scanf("%f", &beta);
  NN = 1000;
  n = 20;
  beta = 1.5;
  //Simulation starts to calculate MSE
  for (i = 0; i < NN; i++) {
    double sum = 0.0, sumsq = 0.0; //Vlaue is reset at every iteration, so declared inside i loop
    for (j = 0; j < n; j++) //each sample
        {
      double g = 0;
      for (k = 0; k < alpha; k++)
        g += (double) (-1 / beta)
            * log(1 - (((double) rand() + 0.5) / (RAND_MAX + 1u)));
      sum += g;
      sumsq += g * g;
    }
    double xbar = sum / n;
    double var = sumsq / n - xbar * xbar;
    suma += pow((xbar * xbar / var - alpha), 2);
    sumb += pow((xbar / var - beta), 2);
  }
  msea = suma / n;
  mseb = sumb / n;
  printf("MSE of alpha is = %g\n", msea);
  printf("MSE of beta is = %g\n", mseb);

  return 0;
}

相关问题