number of items to replace is not a multiple of , MonteCarlo Stochastic Process R error - simulation
##
set.seed(123)
SimpleEulerApproximation = function(T,x,a,b,delta){
numberofSteps = T/delta;
TimeSteps = rep(numberofSteps,1);
Y = rep(numberofSteps,1)
Y[1] = x;
for (i in 1:numberofSteps){
TimeSteps[i] = 0 + i*delta;
}
for (j in 2:numberofSteps){
Y[j] = Y[j-1] + a*Y[j-1]*delta + b*Y[j-1]*rnorm(1,0,sqrt(delta));
}
##plot(TimeSteps,Y, type = "l")
}
SimpleEulerApproximation(1,20,-0.01,0.25,0.001)
set.seed(123)
MultipleEulerApproximation = function(T,x,a,b,delta,numberofTrajectories){
numberofSteps = round(T/delta);
TimeSteps = rep(numberofSteps,1);
Y = rep(numberofSteps,rep(numberofTrajectories))
Y = data.matrix(Y)
for (i in 1:numberofTrajectories){
Y[,i] = SimpleEulerApproximation(T,x,a,b,delta);
}
for (i in 1:numberofSteps){
TimeSteps[i] = 0 + i*delta;
}
AverageTrajectory = rep(numberofSteps,1)
for (i in 1:numberofSteps){
AverageTrajectory[i] = mean(Y[i,])
}
##plot(TimeSteps,AverageTrajectory)
}
MultipleEulerApproximation(1,52,0.12,0.30,0.0001,10000)
MonteCarloSimulation = function(T,x,r,sigma,K,delta,numberofTrajectories){
Y = MultipleEulerApproximation(T,x,r,sigma,delta,numberofTrajectories);
lastStep = round(T/delta);
max(Y[lastStep,]-K,0);
size(Y)
price = 1/numberofTrajectories * sum(max(Y[lastStep,]-K,0))*exp(-r*T)
}
MonteCarloSimulation(0.25,52,0.12,0.3,50,0.0001,10000)
When I run the code for multipleEulerApproximation, I get replacement has length 0 error. Can someone help me with this? Much Appreciated.
The first one is simple Euler Approximation for stochastic differential equation dXt =
−0.1Xtdt + 0.25XtdBt, X0 = 20 over the time interval [0, 1] with time step size
∆ = 0.001.
The second chunk of code is for multipleeulerapproximation that is where the error.
The third-chunk is for calculating European call option price using projections.
Related
Why is this function producing a value of 'Inf'?
I have the following function below which I need to translate into MATLAB code, I'm pretty sure I've got the function translated properly. However I've got a small issue with the code. Function to translate: When I attempt to run this function the value of phi comes out as being inf I'm really not sure why this is happening? Also, can someone tell me if I'm doing the right thing with the sum part of this function? In my mind this is telling me to apply this function to every value and then take that value and add it to phi, but I'm not sure if that's what I'm actually doing. function [exists] = detectHaarWatermark(watermarkedCoefs, watermark) coefsSize = size(watermarkedCoefs,1); phi = 0; numeratorTotal = 0; denomanatorTotal = 0; for i = 1 : coefsSize for j = 1 : coefsSize y = watermarkedCoefs(i,j) %Watermarked coefficient w = watermark(i,j) %Watermark % val = ((w * sign(y))^2) / (w^2) numerator = (w * sign(y)) numeratorTotal = numeratorTotal + numerator; numeratorTotal = numeratorTotal^2; denomanator = (w^2) denomanatorTotal = denomanatorTotal + denomanator; val = numeratorTotal / denomanatorTotal; phi = val; end end phi stdDev = std2(watermarkedCoefs) if phi > (10*stdDev) exists = true; else exists = false; end end %end function Edit: numerator = (sum(sum(watermark.*(sign(watermarkedCoefs))))); numerator = power(numerator, 2); denomanator = sum(sum(watermark.^2)); phi = numerator / denomanator
Function to compute Scallop Loss
I want to make a function that computes the value of the Scallop loss for a Rectangular, Hamming and Blackman window using the Scallop loss formula. I have created a function but it only returns that answer 0, have I made an error? function s_l = scallop loss(len) window = rectwin(len); num_total = 0; den_total = 0; for n = 0:len-1 A1 = exp(-((1i*(n)*pi)/len)); A2 = window(n+1)*A1; num = abs(A2); den = win(n+1); num_total = num_total + num; den_total = den_total + den: end result = 20*log(num_total/den_total); s_l = result;
You have a maths problem: abs(sum(A)) != sum(abs(A)) They are not the same! Change your code to: window = rectwin(len); num_total = 0; den_total = 0; for n = 0:len-1 A1 = exp(-((1i*(n)*pi)/len)); A2 = window(n+1)*A1; num = A2; % !! den = abs(window(n+1)); % you also forgot the abs here num_total = num_total + num; den_total = den_total + den; end num_total=abs(num_total); % !! result = 20*log(num_total/den_total); s_l = result;
please change the log() function to log10(). without it the result is completely wrong :) I mean this line: result = 20*log10*(num_total/den_total);
Numerical Integration by Simpsons method
I am trying to solve this integration by simpsons method and plot the result in a figure.The figure is taking only the value of P0= -6 from the for loop. When I set I(K,P) it gives the error: Attempted to access P0(0); index must be a positive integer or logical How can I solve it? alpha = 45; beta = 185; gamma_e = 116; % Gain values G_ei = -18.96; G_ee = 18.52; G_sr = -0.26; G_rs = 16.92; G_es = 2.55; G_re = 4.67; G_se = 0.73; G_sn = 2.78; G_esre = G_es*G_sr*G_re; G_srs = G_sr*G_rs; G_ese = G_es*G_se; G_esn = G_es*G_sn; t_0 = 0.085; % corticothalamic loop delay in second r_e = 0.086; % Excitatory axon range in metre f = linspace(-40,40,500); % f = frequency in Hz w = 2*pi*f; % angular frequency in radian per second delt_P = 0.5; L=zeros(1,500); Q=repmat(L,1); P=repmat(L,1); %%%%%%%%%%%%% integration %%%%%%%%%%%% a = -80*pi; b = 80*pi; n=500; I=repmat(L,1); P_initial = repmat(L,1); P_shift = repmat(L,1); p = repmat(L,1); for k = 1:length(w) for P0 = [6 -6] L_initial = #(w1) (1-((1i*w1)/alpha))^(-1)*(1-((1i*w1)/beta))^(-1); Q_initial = #(w1) (1/(r_e^2))*((1-((1i*w1)/gamma_e))^(2) - (1/(1-G_ei*L_initial(w1)))*.... (L_initial(w1)*G_ee + (exp(1i*w1*t_0)*(L_initial(w1)^2*G_ese +L_initial(w1)^3*G_esre))/(1-L_initial(w1)^2*G_srs))); P_initial = #(w1) (pi/r_e^4)* (abs((L_initial(w1)^2*G_esn)/((1-L_initial(w1)^2*G_srs)*.... (1-G_ei*L_initial(w1)))))^2 * abs((atan2((imag(Q_initial(w1))),(real(Q_initial(w1)))))/imag(Q_initial(w1))); G = 150*exp(- (f - P0).^2./(2*(delt_P).^2)); P2 = #(w1) G(k) + P_initial(w1); L_shift = #(w1) (1-((1i*(w(k)-w1))/alpha))^(-1)* (1-((1i*(w(k)-w1))/beta))^(-1); Q_shift = #(w1) (1/(r_e^2))*((1-((1i*(w(k)-w1))/gamma_e))^(2) - (1/(1-G_ei*L_shift(w1)))*... (L_shift(w1)*G_ee + (exp(1i*(w(k)-w1)*t_0)*(L_shift(w1)^2*G_ese +L_shift(w1)^3*G_esre))/(1-L_shift(w1)^2*G_srs))); P_shift = #(w1) (pi/r_e^4)* (abs((L_shift(w1)^2*G_esn)/((1-L_shift(w1)^2*G_srs)*(1-G_ei*L_shift(w1)))))^2 *.... abs((atan2((imag(Q_shift(w1))),(real(Q_shift(w1)))))/imag(Q_shift(w1))); p = #(w1) P2(w1)*P_shift(w1); % Power spectrum formula for P(w1)*p(w-w1) I(k) = simprl(p,a,b,n); end end figure(1) plot(f,I,'r--') figure(2) plot(f,G,'k')
At the moment you only use the results for P0 = -6 as you save them in I(k). First you save the result for P0 = 6 later you overwrite it and save the other. The results of P0 = 6are neither used nor saved. If you write your code as follows this will be clarifyied. for k = 1:length(w) L_shift = #(w1) (1-((1i*(w(k)-w1))/alpha))^(-1)* (1-((1i*(w(k)-w1))/beta))^(-1); Q_shift = #(w1) (1/(r_e^2))*((1-((1i*(w(k)-w1))/gamma_e))^(2) - (1/(1-G_ei*L_shift(w1)))*... (L_shift(w1)*G_ee + (exp(1i*(w(k)-w1)*t_0)*(L_shift(w1)^2*G_ese +L_shift(w1)^3*G_esre))/(1-L_shift(w1)^2*G_srs))); P_shift = #(w1) (pi/r_e^4)* (abs((L_shift(w1)^2*G_esn)/((1-L_shift(w1)^2*G_srs)*(1-G_ei*L_shift(w1)))))^2 *.... abs((atan2((imag(Q_shift(w1))),(real(Q_shift(w1)))))/imag(Q_shift(w1))); for P0 = [6 -6] G = 150*exp(- (f - P0).^2./(2*(delt_P).^2)); P2 = #(w1) G(k) + P_initial(w1); p = #(w1) P2(w1)*P_shift(w1); I(k) = simprl(p,a,b,n); end end You can't access I(k,P) as I is an vector not an matrix. However this will give you Index exceeds matrix dimensions. You could save the results for P0 = -6 in one variable and P0 = 6 in the other variable as the results in your code do not depent on each other.
MATLAB - Adaptive Step Size Runge-Kutta
I've programmed in MATLAB an adaptive step size RK4 to solve a system of ODEs. The code runs without error, however it does not produce the desired curve when I try to plot x against y. Instead of being a toroidal shape, I simply get a flat line. This is evident from the fact that r is outputting a constant value. After checking the outputs of each line, they are not outputting constants or errors or inf or NaN, rather they are outputting both a real and imaginary component (complex numbers). I have no idea as to why this is occurring and I believe it to be the source of my trouble. function AdaptRK4() parsec = 3.08*10^18; r_1 = 8.5*1000.0*parsec; % in cm theta_1 = 0.0; a = 0.5*r_1; gam = 1; grav = 6.6720*10^-8; amsun = 1.989*10^33; amg = 1.5d11*amsun; gm = grav*amg; u_1 = 20.0*10^5; v = sqrt(gm/r_1); time = 0.0; epsilon = 0.00001; m1 = 0.5; m2 = 0.5; m3 = 0.5; i = 1; nsteps = 50000; deltat = 5.0*10^12; angmom = r_1*v; angmom2 = angmom^2.0; e = -2*10^5.0*gm/r_1+u_1*u_1/2.0+angmom2/(2.0*r_1*r_1); for i=1:nsteps deltat = min(deltat,nsteps-time); fk3_1 = deltat*u_1; fk4_1 = deltat*(-gm*r_1*r_1^(-gam)/(a+r_1)^(3- gam)+angmom2/(r_1^3.0)); fk5_1 = deltat*(angmom/(r_1^2.0)); r_2 = r_1+fk3_1/4.0; u_2 = u_1+fk4_1/4.0; theta_2 = theta_1+fk5_1/4.0; fk3_2 = deltat*u_2; fk4_2 = deltat*(-gm*r_2*r_2^(-gam)/(a+r_2)^(3-gam)+angmom2/(r_2^3.0)); fk5_2 = deltat*(angmom/(r_2^2.0)); r_3 = r_1+(3/32)*fk3_1 + (9/32)*fk3_2; u_3 = u_1+(3/32)*fk4_1 + (9/32)*fk4_2; theta_3 = theta_1+(3/32)*fk5_1 + (9/32)*fk5_2; fk3_3 = deltat*u_3; fk4_3 = deltat*(-gm*r_3*r_3^(-gam)/(a+r_3)^(3-gam)+angmom2/(r_3^3.0)); fk5_3 = deltat*(angmom/(r_3^2.0)); r_4 = r_1+(1932/2197)*fk3_1 - (7200/2197)*fk3_2 + (7296/2197)*fk3_3; u_4 = u_1+(1932/2197)*fk4_1 - (7200/2197)*fk4_2 + (7296/2197)*fk4_3; theta_4 = theta_1+(1932/2197)*fk5_1 - (7200/2197)*fk5_2 + (7296/2197)*fk5_3; fk3_4 = deltat*u_4; fk4_4 = deltat*(-gm*r_4*r_4^(-gam)/(a+r_4)^(3-gam)+angmom2/(r_4^3.0)); fk5_4 = deltat*(angmom/(r_4^2.0)); r_5 = r_1+(439/216)*fk3_1 - 8*fk3_2 + (3680/513)*fk3_3 - (845/4104)*fk3_4; u_5 = u_1+(439/216)*fk4_1 - 8*fk4_2 + (3680/513)*fk4_3 - (845/4104)*fk4_4; theta_5 = theta_1+(439/216)*fk5_1 - 8*fk5_2 + (3680/513)*fk5_3 - (845/4104)*fk5_4; fk3_5 = deltat*u_5; fk4_5 = deltat*(-gm*r_5*r_5^(-gam)/(a+r_5)^(3-gam)+angmom2/(r_5^3.0)); fk5_5 = deltat*(angmom/(r_5^2.0)); r_6 = r_1-(8/27)*fk3_1 - 2*fk3_2 - (3544/2565)*fk3_3 + (1859/4104)*fk3_4-(11/40)*fk3_5; u_6 = u_1-(8/27)*fk4_1 - 2*fk4_2 - (3544/2565)*fk4_3 + (1859/4104)*fk4_4-(11/40)*fk4_5; theta_6 = theta_1-(8/27)*fk5_1 - 2*fk5_2 - (3544/2565)*fk5_3 + (1859/4104)*fk5_4-(11/40)*fk5_5; fk3_6 = deltat*u_6; fk4_6 = deltat*(-gm*r_6*r_6^(-gam)/(a+r_6)^(3-gam)+angmom2/(r_6^3.0)); fk5_6 = deltat*(angmom/(r_6^2.0)); fm3_1 = m1 + 25*fk3_1/216+1408*fk3_3/2565+2197*fk3_4/4104-fk3_5/5; fm4_1 = m2 + 25*fk4_1/216+1408*fk4_3/2565+2197*fk4_4/4104-fk4_5/5; fm5_1 = m3 + 25*fk5_1/216+1408*fk5_3/2565+2197*fk5_4/4104-fk5_5/5; fm3_2 = m1 + 16*fk3_1/135+6656*fk3_3/12825+28561*fk3_4/56430-9*fk3_5/50+2*fk3_6/55; fm4_2 = m2 + 16*fk4_1/135+6656*fk4_3/12825+28561*fk4_4/56430-9*fk4_5/50+2*fk4_6/55; fm5_2 = m3 + 16*fk5_1/135+6656*fk5_3/12825+28561*fk5_4/56430-9*fk5_5/50+2*fk5_6/55; R3 = abs(fm3_1-fm3_2)/deltat; R4 = abs(fm4_1-fm4_2)/deltat; R5 = abs(fm5_1-fm5_2)/deltat; err3 = 0.84*(epsilon/R3)^(1/4); err4 = 0.84*(epsilon/R4)^(1/4); err5 = 0.84*(epsilon/R5)^(1/4); if R3<= epsilon time = time+deltat; fm3 = fm3_1; i = i+1; deltat = err3*deltat; end if R4<= epsilon time = time+deltat; fm4 = fm4_1; i = i+1; deltat = err4*deltat; end if R5<= epsilon time = time+deltat; fm5 = fm5_1; i = i+1; deltat = err5*deltat; end e=2*gm^2.0/(2*angmom2); ecc=(1.0+(2.0*e*angmom2)/(gm^2.0))^0.5; x(i)=r_1*cos(theta_1)/(1000.0*parsec); y(i)=r_1*sin(theta_1)/(1000.0*parsec); time=time+deltat; r(i)=r_1; time1(i)=time; end figure() plot(x,y, '-k'); TeXString = title('Plot of Orbit in Gamma Potential Obtained Using RK4') xlabel('x') ylabel('y')
You are getting complex values because at some point npts - time < 0. You may want to print out the values of deltat to check the error. Also, your code doesn't seem to take into account the case when the error estimate is larger than your tolerance. When your error estimate is greater than your tolerance you have to: Shift back the time AND solution calculate a new step-size based on a formula, and recalculate your solution and error estimate. The fact that you don't know how many iterations you will have to go through makes the use of a for-loop for adaptive runge Kutta a bit awkward. I suggest using a while loop instead.
You are using "i" in your code. "i" returns the basic imaginary unit. "i" is equivalent to sqrt(-1). Try to use another identifier in your loops and only use "i" or "j" in calculations where complex numbers are involved.
Index exceeds matrix dimensions error in Runge-Kutta method: Matlab
I'm trying to make a time stepping code using the 4th order Runge-Kutta method but am running into issues indexing one of my values properly. My code is: clc; clear all; L = 32; M = 32; N = 32; % No. of elements Lx = 2; Ly = 2; Lz = 2; % Size of each element dx = Lx/L; dy = Ly/M; dz = Lz/N; % Step size Tt = 1; t0 = 0; % Initial condition T = 50; % Final time dt = (Tt-t0)/T; % Determining time step interval % Wave characteristics H = 2; % Wave height a = H/2; % Amplitude Te = 6; % Period omega = 2*pi/Te; % Wave rotational frequency d = 25; % Water depth x = 0; % Location of cylinder axis u0(1:L,1:M,1:N,1) = 0; % Setting up solution space matrix (u values) v0(1:L,1:M,1:N,1) = 0; % Setting up solution space matrix (v values) w0(1:L,1:M,1:N,1) = 0; % Setting up solution space matrix (w values) [k,L] = disp(d,omega); % Solving for k and wavelength using Newton-Raphson function %u = zeros(1,50); %v = zeros(1,50); %w = zeros(1,50); time = 1:1:50; for t = 1:T for i = 1:L for j = 1:M for k = 1:N eta(i,j,k,t) = a*cos(omega*time(1,t); u(i,j,k,1) = u0(i,j,k,1); v(i,j,k,1) = v0(i,j,k,1); w(i,j,k,1) = w0(i,j,k,1); umag(i,j,k,t) = a*omega*(cosh(k*(d+eta(i,j,k,t))))/sinh(k*d); vmag(i,j,k,t) = 0; wmag(i,j,k,t) = -a*omega*(sinh(k*(d+eta(i,j,k,t))))/sinh(k*d); uRHS(i,j,k,t) = umag(i,j,k,t)*cos(k*x-omega*t); vRHS(i,j,k,t) = vmag(i,j,k,t)*sin(k*x-omega*t); wRHS(i,j,k,t) = wmag(i,j,k,t)*sin(k*x-omega*t); k1x(i,j,k,t) = dt*uRHS(i,j,k,t); k2x(i,j,k,t) = dt*(0.5*k1x(i,j,k,t) + dt*uRHS(i,j,k,t)); k3x(i,j,k,t) = dt*(0.5*k2x(i,j,k,t) + dt*uRHS(i,j,k,t)); k4x(i,j,k,t) = dt*(k3x(i,j,k,t) + dt*uRHS(i,j,k,t)); u(i,j,k,t+1) = u(i,j,k,t) + (1/6)*(k1x(i,j,k,t) + 2*k2x(i,j,k,t) + 2*k3x(i,j,k,t) + k4x(i,j,k,t)); k1y(i,j,k,t) = dt*vRHS(i,j,k,t); k2y(i,j,k,t) = dt*(0.5*k1y(i,j,k,t) + dt*vRHS(i,j,k,t)); k3y(i,j,k,t) = dt*(0.5*k2y(i,j,k,t) + dt*vRHS(i,j,k,t)); k4y(i,j,k,t) = dt*(k3y(i,j,k,t) + dt*vRHS(i,j,k,t)); v(i,j,k,t+1) = v(i,j,k,t) + (1/6)*(k1y(i,j,k,t) + 2*k2y(i,j,k,t) + 2*k3y(i,j,k,t) + k4y(i,j,k,t)); k1z(i,j,k,t) = dt*wRHS(i,j,k,t); k2z(i,j,k,t) = dt*(0.5*k1z(i,j,k,t) + dt*wRHS(i,j,k,t)); k3z(i,j,k,t) = dt*(0.5*k2z(i,j,k,t) + dt*wRHS(i,j,k,t)); k4z(i,j,k,t) = dt*(k3z(i,j,k,t) + dt*wRHS(i,j,k,t)); w(i,j,k,t+1) = w(i,j,k,t) + (1/6)*(k1z(i,j,k,t) + 2*k2z(i,j,k,t) + 2*k3z(i,j,k,t) + k4z(i,j,k,t)); a(i,j,k,t+1) = ((u(i,j,k,t+1))^2 + (v(i,j,k,t+1))^2 + (w(i,j,k,t+1))^2)^0.5; end end end end At the moment, the values seem to be fine for the first iteration but then I have the error Index exceeds matrix dimension in the line calculating eta. I understand that I am not correctly indexing the eta value but am not sure how to correct this. My goal is to update the value of eta for each loop of t and then use that new eta value for the rest of the calculations. I'm still quite new to programming and am trying to understand indexing, especially in 3 or 4 dimensional matrices and would really appreciate any advice in correctly calculating this value. Thanks in advance for any advice!
You declare time = 1:1:50; which is just a row vector but access it here eta(i,j,k,t) = a*cos(omega*time(i,j,k,t)); as if it were an array with 4 dimensions. To correctly access element x of time you need to use syntax time(1,x); (as it is a 1 x 50 array)