コード例 #1
0
ファイル: widhof.c プロジェクト: rlaybourn/fuzzywater2
void main()
{
	double thetas[] = {1,1,1,1,1};
	double x[] = {0.82,1.1,1.42,1.58,2.0,2.19,2.3,2.5,2.7};
	double y[] = {8.3,13.5,20.0,23.5,35.0,40.0,43.0,46.0,50.0};
	int size = sizeof(thetas)/sizeof(double);
	int sizex = sizeof(x)/sizeof(double);
	int i= 0;
	printf("%f\n",Htheta(thetas,1,size));
	printf("%f\n",toerr(thetas,x,y,size,sizex));
	fitto(thetas,x,y,size,sizex);
	printf("%f\n",toerr(thetas,x,y,size,sizex));
	printf("%f %f %f %f %f \n",thetas[0],thetas[1],thetas[2],thetas[3],thetas[4]);
	for(i=0;i<sizex;i++)
	{
		printf("%f  %f\n",x[i],Htheta(thetas,x[i],size));
	}
}
コード例 #2
0
ファイル: widhof.c プロジェクト: rlaybourn/fuzzywater2
double toerr(double *thetas,double *x, double *y,int sizeth,int sizex)
{
	double total = 0;
	int counter = 0;
	int i = 0;
	for(i=0;i<sizex;i++)
	{
		total = total + y[i] - Htheta(thetas,x[i],sizeth);
	}
	return total;
}
コード例 #3
0
ファイル: ctrainsupport.c プロジェクト: rlaybourn/fuzzywater2
double toerr(double *thetas,double *x, double *y,int sizeth,int sizex) //find total error for given coeffs
{
	double total = 0;
	int counter = 0;
	int i = 0;
	for(i=0;i<sizex;i++)
	{
		total = total + y[i] - Htheta(thetas,x[i],sizeth);
	}
	return total;
}
コード例 #4
0
ファイル: widhof.c プロジェクト: rlaybourn/fuzzywater2
void fitto(double *thetas,double *x, double *y,int sizeth,int sizex)
{
	double newth[sizeth]; //temporary thetas
	double rate = 1 * pow(10,(sizeth * -1)); //learning rate
	double error = 100;
	int i =0;
	int j = 0;
	int counter = 0;
	for(i=0;i<sizeth;i++) //clear temp thetas
	{
		newth[i] = 0.0;
	}

	while(fabs(error) > 0.01)
	{
		for(j=0;j<sizeth;j++)
		{
			//printf("%d ",j);
			for(i=0;i<sizex;i++)
			{

				newth[j] += rate * ((y[i] - Htheta(thetas,x[i],sizeth)) * pow(x[i],(double)j));
			//	printf("%f ",newth[j]);
			}
			//printf("\n");
		}

		for(j=0;j<sizeth;j++)
		{
			thetas[j] = thetas[j] + newth[j];
			newth[j] = 0;

		}
		//printf("%f %f %f %f %f\n",thetas[0],thetas[1],thetas[2],thetas[3],thetas[4]);
		if(counter % 10000 == 0)
			printf("%f\n",toerr(thetas,x,y,sizeth,sizex));
		counter++;
		error = toerr(thetas,x,y,sizeth,sizex);
	}


	printf("%f\n",rate);

}
コード例 #5
0
ファイル: ctrainsupport.c プロジェクト: rlaybourn/fuzzywater2
double fitto(double *thetas,double *x, double *y,int sizeth,int sizex,double lrate) //perform fitting of polynomial to data
{
	double newth[sizeth]; //temporary thetas
	double rate = lrate * pow(10,(sizeth * -1)); //learning rate
	double error = 100;
	int i =0;
	int j = 0;
	int counter = 0;
	for(i=0;i<sizeth;i++) //clear temp thetas
	{
		newth[i] = 0.0;
	}

	while((fabs(error) > 0.01) && (counter < 1000000)) //carry on unitl error is less than 0.01 or 1 million interations have been carried out
	{
		for(j=0;j<sizeth;j++) //for each coeff
		{
			for(i=0;i<sizex;i++) //for each data point
			{

				newth[j] += rate * ((y[i] - Htheta(thetas,x[i],sizeth)) * pow(x[i],(double)j)); //find update value
			}
		}

		for(j=0;j<sizeth;j++) //update coeffs 
		{
			thetas[j] = thetas[j] + newth[j];
			newth[j] = 0;

		}

		counter++;
		error = toerr(thetas,x,y,sizeth,sizex);
	}

	return error;
}
コード例 #6
0
double PathDepOption::PriceByMC(BSModel Model, long N, double epsilon)
{
    double H=0.0; double Hsq=0.0;
    int d = Model.S0.size();
    SamplePath S(m);
    Matrix C = Model.C;

    Matrix CZ(d);
    for(int i=0;i<d;i++) CZ[i].resize(m);

    delta.resize(d); rho.resize(d); vega.resize(d); theta.resize(d); gamma.resize(d);
    Vector Hdelta(d), Hvega(d), Hrho(d), Htheta(d), Hgamma(d);


    for (int i=0; i<d; i++)
    {
        delta[i] = 0.0; Hdelta[i] = 0.0; Hvega[i] = 0.0; Hrho[i] = 0.0; Htheta[i] = 0.0;
    }

    for(long i=0; i<N; i++)
    {
        Model.GenerateSamplePath(T,m,S);

        GetZ(CZ, S, C, Model.S0, Model.r, T/m);
        H = (i*H + Payoff(S))/(i+1.0);
        Hsq = (i*Hsq + pow(Payoff(S),2.0))/(i+1.0);

        for (int j=0; j<d; j++)
        {
            Vector S0tmp = Model.S0;
            S0tmp[j] = (1.0+epsilon)*S0tmp[j];
            Matrix Cvega = C;
            Cvega[j] = (1.0 + epsilon)*C[j];


            Rescale(S, CZ, C, S0tmp, Model.r, T/m);
            Hdelta[j] = (i*Hdelta[j] + Payoff(S)) / (i+1.0);

            Rescale(S, CZ, C, Model.S0, Model.r*(1.0+epsilon), T/m);
            Hrho[j] = (i*Hrho[j] + Payoff(S)) / (i+1.0);

            Rescale(S, CZ, Cvega, Model.S0, Model.r, T/m);
            Hvega[j] = (i*Hvega[j] + Payoff(S)) / (i+1.0);

            Rescale(S, CZ, C, Model.S0, Model.r, T*(1.0+epsilon)/m);
            Htheta[j] = (i*Htheta[j] + Payoff(S)) / (i+1.0);

            S0tmp[j] = (1.0-epsilon)*Model.S0[j];
            Rescale(S, CZ, C, S0tmp, Model.r, T/m);
            Hgamma[j] = (i*Hgamma[j] + Payoff(S)) / (i+1.0);

            Rescale(S, CZ, C, Model.S0, Model.r, T/m);
        }
    }

    for(int i=0; i<d; i++)
    {
        delta[i] = exp(-Model.r*T) * ( Hdelta[i] - H ) / (epsilon*Model.S0[i]);
        rho[i] = (exp(-Model.r*T*(1.0+epsilon))*Hrho[i] - exp(-Model.r*T)*H)/(epsilon*Model.r);
        vega[i] = exp(-Model.r*T) * (Hvega[i] - H) / (epsilon*sqrt(C[i]^C[i]));
        theta[i] = -1.0*(exp(-Model.r*T*(1.0+epsilon))*Htheta[i] - exp(-Model.r*T)*H)/(epsilon*T);
        gamma[i] = exp(-Model.r*T)*(Hdelta[i]-2.0*H+Hgamma[i])/(Model.S0[i]*Model.S0[i]*epsilon*epsilon);

    }

    Price = exp(-Model.r*T)*H;
    PricingError = exp(-Model.r*T)*sqrt(Hsq-H*H)/sqrt(N-1.0);

    return Price;
}