程序运行不正常 [英] program isn't running properly

查看:69
本文介绍了程序运行不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,在我发布我的代码之前,有没有人在这一刻

?如果没有人在那里阅读就没有意义,

回答它= P

hello, before i post my code for this, is there anyone out there at
this moment? no sense in posting if no one is out there to read and
answer it =P

推荐答案

是的,好吧,不妨我猜测它的所有代码。

的背景是试图通过蒙特卡罗模拟方法计算欧洲货币

期权的价格。没有

错误就可以很好地编译,但是当我去运行它时,在输入之后,它就会停止做任何事情(或者说它没有那么慢)在

分钟内完成!?!?!?)任何帮助都会很棒!我怀疑

错误的两个函数我定义为uniform_deviate和

normal_deviate,但我不太确定。


这里是:


#include< iostream>

#include< math.h>

using namespace std;


双Wt,v,So,Si,r,T,Vi,Vt,V1,V2,U,W,LP,K,Am,Bm,Z,
上,下,西格玛,亩;

int a,b,N,M,Vj,i;


double uniform_deviate()

{

a = 1366;

b = 150889;

N = 714025;

Vj =(a * Vj + b)%N;

U = Vj /(N-1);

返回U;

}

double normal_deviate(double mu,double sigma)

{

V1 = 1.0;

V2 = 1.0 ;

而(V1 * V1 + V2 * V2 1)

{

V1 = 2 * uniform_deviate() - 1;

V2 = 2 * uniform_deviate() - 1;

}

W =(V1 * V1 + V2 * V2);

Z = V1 * pow(( - 2 * log(W))/ W,0.5);

返回Z;

}


双MAX(双x,双y)

{

if(x> y)

{

返回x;

}

其他

{

返回y;

}

}


int main()

{

cout<<""什么是初始股票价格? "<< endl;

cin>>所以;

cout<<""什么是利率? "<< endl;

cin>> r;

cout<<"股票的波动性是多少? "<< endl;

cin>> v;

cout<<"到期时间是什么时候? "<< endl;

cin>> T;

cout<<"模型中的模拟数量是多少? "<< endl;

cin>> M;

cout<<"该选项的执行价格是多少? "<< endl;

cin>> K;


Vj = 1;

Vt = 0.0 ;

LP = 0.0;


double值[10]; //似乎不想让我分配一个

尺寸M由于某种原因,所以我在10点小,看看我是否可以让它工作。

for(i = 0; i< M; i ++)

{

Si = So * exp((r-0.5 * v * v)* T + v * pow(T,0.5)* normal_deviate(0,1));

值[i] = exp(-r * T)* MAX(K-Si,0);

Vt = exp( -r * T)* MAX(K-Si,0);

Vt = Vt + Si;

}

Am = Vt / M;

for(i = 0; i< M; i ++)

{

LP = LP + pow((Values [i] -Am),2.0);

}

Bm =(1 /(M-1))* LP;

Lower = Am- ((1.96 * Bm)/(pow(M,0.5)));

Upper = Am +((1.96 * Bm)/(pow(M,0.5)));


cout<< 均值= << Am<< endl;

cout<< 方差是= << Bm<< endl;

cout<< 置信区间是(<<<<<<"<<<<<<<""<<<<<<<"<<<<<<<<<<<<<<<< ;

返回0;

}

right, well, might as well i guess post up all the code for it. the
context of it is trying to calculate the price of a european put
option via monte carlo simulation methods. it compiles fine with no
errors, but when i go to run it, after the inputs, it just stops doing
anything (or is it that slow that it doesn''t finish within a
minute!?!?!?) any help would be great! i suspect there is something
wrong with the two functions i have defined as uniform_deviate and
normal_deviate, but am not really sure.

here it is:

#include<iostream>
#include<math.h>
using namespace std;

double Wt, v, So, Si, r, T, Vi, Vt, V1, V2, U, W, LP, K, Am, Bm, Z,
Upper, Lower, sigma, mu;
int a, b, N, M, Vj, i;

double uniform_deviate ()
{
a=1366;
b=150889;
N=714025;
Vj = (a*Vj + b) % N;
U=Vj/(N-1);
return U;
}
double normal_deviate (double mu, double sigma)
{
V1=1.0;
V2=1.0;
while(V1*V1+V2*V2 1)
{
V1=2*uniform_deviate()-1;
V2=2*uniform_deviate()-1;
}
W=(V1*V1+V2*V2);
Z=V1*pow((-2*log(W))/W,0.5);
return Z;
}

double MAX(double x, double y)
{
if(x>y)
{
return x;
}
else
{
return y;
}
}

int main()
{
cout <<"What is the initial stock price? "<<endl;
cin >>So;
cout <<"What is the interest rate? "<<endl;
cin >>r;
cout <<"What is the volatility of the stock? "<<endl;
cin >>v;
cout <<"What is the time to maturity? "<<endl;
cin >>T;
cout <<"What is the number of simulations in the model? "<<endl;
cin >>M;
cout <<"What is the strike price of the option? "<<endl;
cin >>K;

Vj=1;
Vt=0.0;
LP=0.0;

double Values[10];//doesn''t seem to want to let me assign an array of
size M here for some reason, so i made it small at 10 to see if i
could get it working.
for(i=0; i<M; i++)
{
Si = So*exp((r-0.5*v*v)*T+v*pow(T,0.5)*normal_deviate(0,1));
Values[i] = exp(-r*T)*MAX(K-Si,0);
Vt = exp(-r*T)*MAX(K-Si,0);
Vt = Vt + Si;
}
Am = Vt/M;
for(i=0; i<M; i++)
{
LP=LP+pow((Values[i]-Am),2.0);
}
Bm = (1/(M-1))*LP;
Lower = Am-((1.96*Bm)/(pow(M,0.5)));
Upper = Am+((1.96*Bm)/(pow(M,0.5)));

cout << "The mean is = " <<Am <<endl;
cout << "The variance is = " <<Bm <<endl;
cout << "The confidence interval is (" <<Lower << ", " <<Upper <<")
"<<endl;
return 0;
}


ia ****** @ gmail.com 写道:

对,好吧,不妨我想为它发布所有代码。

的背景是试图通过蒙特卡罗模拟方法计算欧洲货币

期权的价格。没有

错误就可以很好地编译,但是当我去运行它时,在输入之后,它就会停止做任何事情(或者说它没有那么慢)在

分钟内完成!?!?!?)任何帮助都会很棒!我怀疑

错误的两个函数我定义为uniform_deviate和

normal_deviate,但我不太确定。


这里是:


#include< iostream>

#include< math.h>

using namespace std;


双Wt,v,So,Si,r,T,Vi,Vt,V1,V2,U,W,LP,K,Am,Bm,Z,
上,下,西格玛,亩;

int a,b,N,M,Vj,i;


double uniform_deviate()

{

a = 1366;

b = 150889;

N = 714025;

Vj =(a * Vj + b)%N;

U = Vj /(N-1);

返回U;

}

double normal_deviate(double mu,double sigma)

{

V1 = 1.0;

V2 = 1.0 ;

而(V1 * V1 + V2 * V2 1)

{

V1 = 2 * uniform_deviate() - 1;

V2 = 2 * uniform_deviate() - 1;

}

W =(V1 * V1 + V2 * V2);

Z = V1 * pow(( - 2 * log(W))/ W,0.5);

retu Z;

}


双MAX(双x,双y)

{

if(x> y)

{

返回x;

}

其他

{

返回y;

}

}


int main()

{

cout<<""什么是初始股票价格? "<< endl;

cin>>所以;

cout<<""什么是利率? "<< endl;

cin>> r;

cout<<"股票的波动性是多少? "<< endl;

cin>> v;

cout<<"到期时间是什么时候? "<< endl;

cin>> T;

cout<<"模型中的模拟数量是多少? "<< endl;

cin>> M;

cout<<"该选项的执行价格是多少? "<< endl;

cin>> K;


Vj = 1;

Vt = 0.0 ;

LP = 0.0;


double值[10]; //似乎不想让我分配一个

尺寸M由于某种原因,所以我在10点小,看看我是否可以让它工作。

for(i = 0; i< M; i ++)

{

Si = So * exp((r-0.5 * v * v)* T + v * pow(T,0.5)* normal_deviate(0,1));

值[i] = exp(-r * T)* MAX(K-Si,0);

Vt = exp( -r * T)* MAX(K-Si,0);

Vt = Vt + Si;

}

Am = Vt / M;

for(i = 0; i< M; i ++)

{

LP = LP + pow((Values [i] -Am),2.0);

}

Bm =(1 /(M-1))* LP;

Lower = Am- ((1.96 * Bm)/(pow(M,0.5)));

Upper = Am +((1.96 * Bm)/(pow(M,0.5)));


cout<< 均值= << Am<< endl;

cout<< 方差是= << Bm<< endl;

cout<< 置信区间是(<<<<<<"<<<<<<<""<<<<<<<"<<<<<<<<<<<<<<<< ;

返回0;

}
right, well, might as well i guess post up all the code for it. the
context of it is trying to calculate the price of a european put
option via monte carlo simulation methods. it compiles fine with no
errors, but when i go to run it, after the inputs, it just stops doing
anything (or is it that slow that it doesn''t finish within a
minute!?!?!?) any help would be great! i suspect there is something
wrong with the two functions i have defined as uniform_deviate and
normal_deviate, but am not really sure.

here it is:

#include<iostream>
#include<math.h>
using namespace std;

double Wt, v, So, Si, r, T, Vi, Vt, V1, V2, U, W, LP, K, Am, Bm, Z,
Upper, Lower, sigma, mu;
int a, b, N, M, Vj, i;

double uniform_deviate ()
{
a=1366;
b=150889;
N=714025;
Vj = (a*Vj + b) % N;
U=Vj/(N-1);
return U;
}
double normal_deviate (double mu, double sigma)
{
V1=1.0;
V2=1.0;
while(V1*V1+V2*V2 1)
{
V1=2*uniform_deviate()-1;
V2=2*uniform_deviate()-1;
}
W=(V1*V1+V2*V2);
Z=V1*pow((-2*log(W))/W,0.5);
return Z;
}

double MAX(double x, double y)
{
if(x>y)
{
return x;
}
else
{
return y;
}
}

int main()
{
cout <<"What is the initial stock price? "<<endl;
cin >>So;
cout <<"What is the interest rate? "<<endl;
cin >>r;
cout <<"What is the volatility of the stock? "<<endl;
cin >>v;
cout <<"What is the time to maturity? "<<endl;
cin >>T;
cout <<"What is the number of simulations in the model? "<<endl;
cin >>M;
cout <<"What is the strike price of the option? "<<endl;
cin >>K;

Vj=1;
Vt=0.0;
LP=0.0;

double Values[10];//doesn''t seem to want to let me assign an array of
size M here for some reason, so i made it small at 10 to see if i
could get it working.
for(i=0; i<M; i++)
{
Si = So*exp((r-0.5*v*v)*T+v*pow(T,0.5)*normal_deviate(0,1));
Values[i] = exp(-r*T)*MAX(K-Si,0);
Vt = exp(-r*T)*MAX(K-Si,0);
Vt = Vt + Si;
}
Am = Vt/M;
for(i=0; i<M; i++)
{
LP=LP+pow((Values[i]-Am),2.0);
}
Bm = (1/(M-1))*LP;
Lower = Am-((1.96*Bm)/(pow(M,0.5)));
Upper = Am+((1.96*Bm)/(pow(M,0.5)));

cout << "The mean is = " <<Am <<endl;
cout << "The variance is = " <<Bm <<endl;
cout << "The confidence interval is (" <<Lower << ", " <<Upper <<")
"<<endl;
return 0;
}



哇,我建议你尝试使用全局变量让自己休息一下,它显然是一个严重的瘾。


问题在这里


U = Vj /(N-1 );


你显然打算将它作为一个浮点除法,但因为

Vj和N是整数,它是一个整数除法,结果是

总是0.这意味着你的循环永远不会终止。所以换到这个


U =(双倍)Vj /(N-1);


,至少你会在U中获得一个小数值。是否那个'b
足以修复你的程序我不确定,有一些奇怪的东西

关于你的代码我写过,但我想这是正在进行的工作。


john

Wow, I suggest you try and wean yourself off using global variables, its
clearly a serious addiction.

Problem is here

U=Vj/(N-1);

You obviously intend this to be a floating point division, but because
Vj and N are integers it is an integral division and the result is
always 0. This means your loop never terminates. So change to this

U=(double)Vj/(N-1);

and at the very least you''ll get a fractional value in U. Whether that''s
enough to fix you program I''m not sure, there are a few strange things
about the code you''ve written, but I guess it''s work in progress.

john


这似乎修复了它没有运行的问题。但是现在

它每次给我一个0的方差,我认为这是我的函数中的一个问题,因为我被告知我应该有参数mu和sigma,

但是从来没有真正使用它们。


断开全局变量,如果我知道它们是什么
是! ;-)


你找到的其他奇怪的东西是什么?


这是当你从数学中带人来的时候会发生的事情/>
财务背景,并尝试使用

开发C ++程序,没有关于如何正确构建它们的真实指令。我们肯定是

计算机编程noobs =(并且发现它非常令人沮丧。所以我认为我认为我所做的一切对于具有丰富C ++知识的人来说似乎是基本的,奇怪的和厚实的。

>

但感谢您的帮助到目前为止!


ian

That seems to have fixed the problem of it not running. However now
it gives me a variance of 0 every time, which i think is a problem in
my function, because I''m told I should have parameters mu and sigma,
yet never really use them.

as for weaning off global variables, if only i knew what they
were! ;-)

what are the other strange things you find?

This is what happens when you take people from a Mathematics and
Financial background, and try and make them develop C++ programs with
no real instruction on how to properly build them. We are definately
computer programming "noobs" =( and find it very frustrating. So I
think everything I do is gonna seem basic, strange, and chunky to
anyone with an extensive knowledge of C++

but thanks for the help so far!

ian


这篇关于程序运行不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆