Beispiel #1
0
void updateError(int x_pos, int y_pos, uint sim_time) {
	normalizeBallParams(x_pos, y_pos, sim_time);
	
	float r = R_x();

	float curr_V = INV_DELTA_TIME * V_x();

	error_x = r + GAMMA*curr_V - old_V_x;

	old_V_x = curr_V;

	
	r = R_y();
	
	curr_V = INV_DELTA_TIME * V_y();

	error_y = r + GAMMA*curr_V - old_V_y;

	old_V_y = curr_V;


	//io_printf(IO_STD,"error x %d, error y %d\n", (int)(error_x*1000), (int)(error_y*1000));
}
//Simulate the price process:
void
Sim_Price_Disc::simulate(int retries) {

	//If we are re-simulating, we need to reset:
	if (simulated) {
	
		//X.simulate(retries);			//Re-simulate X
		X.simulate2();
		
		//Clear Y
		Y.clear();
		Y.push_back(y_0);
		
		//Reset the SBM Z:
		Z.reset();						
	
	}
	else {
	
		simulated = true;
		
		//Simulate volatility if necessary:
		if (X.is_simulated() == false) {
	
			//X.simulate(retries);
			X.simulate2();
		
		}
		
	}

	//Get the timestep vector from the volatility:
	vector<double> timestep = X.get_timestep();
	
	double h_t, drift;
	matrix vola(n,n), tmp(n,n);
	
	//For each discretisation step:
	for (unsigned int i=1; i<timestep.size(); ++i) {
	
		//Compute stepsize of the Brownian motion (in case this has changed):
		h_t = timestep[i] - timestep[i-1];

		//Even though we are not interested in eigenvalues anymore, we need the decomposition
		// for the matrix square root.
		matrix V_x(n,n), D_x(n,n);
		eig(V_x, D_x, X.get_X_i(i-1));
		
		//Compute next simulation point using the discretisation formula (see Section 5)
		matrix V_r(n,n), D_r(n,n);
		eig(V_r, D_r, (eye(n) - R * flens::transpose(R)));

		drift = (r - 0.5*trace(X.get_X_i(i-1))) * h_t;

		mat4mult(vola, Z.increment(h_t), V_r, matsqrt_diag(D_r), flens::transpose(V_r));
		tmp = X.get_W_incr_i(i) * flens::transpose(R) + vola;

		mat4mult(vola, V_x, matsqrt_diag(D_x), flens::transpose(V_x), tmp);

		//Add simulation point to storage:
    	Y.push_back(Y[i-1] + drift + trace(vola));

	}
	
}