Path integral in Matlab - matlab

I know that doing Feynman path Integral on Matlab is time consuming compare to Fortran or C.
However, do someone have a Matlab code of harmonic oscillator via path integral?
I didn't manage to find any on the web (and even on Matlab forum).
Below a Fortran code which I don't know how to translate to Matlab (I am novice)
Thanks, Joni
! qmc . f90 : Feynman path i n t e g r a l for ground s t a t e wave Function
Program qmc
Implicit none
Integer :: i,j , max , element , prop ( 100 )
Real *8 :: change , ranDom , energy , newE , oldE , out , path ( 100 )
max = 250000
open ( 9 , FILE = ’qmc.dat’ , Status = ’Unknown’ )
! initial path and probability
Do j = 1 , 100
path (j) = 0.0
prop (j) = 0
End Do
! find energy of initial path
oldE = energy(path , 100)
! pick random element , change by random
Do i = 1 , max
element = ranDom ( )*100 + 1
change = ((ranDom() - 0.5)*2)
path (element) = path(element) + change
newE = energy ( path , 100) ! find new energy
! Metropolis algorithm
If ((newE > oldE) .AND. (exp( - newE + oldE ) < ranDom ())) then
path (element) = path (element) - change
EndIf
! add up probabilities
Do j = 1 , 100
element = path(j)*10 + 50
prop (element) = prop(element) + 1
End Do
oldE = newE
End Do
! write output data to file
Do j = 1 , 100
out = prop(j)
write (9 , *) j - 50 , out/max
End Do
close (9)
Stop ’data saved in qmc.dat’
End Program qmc
! Function calculates energy of the system
Function energy ( array , max )
Implicit none
Integer :: i , max
Real*8 :: energy , array (max)
energy = 0
Do i = 1 , (max - 1)
energy = energy + (array(i+ 1) - array(i))**2 + array(i)**2
End Do
Return
End

This is an open source code for calculating Feynman integrals in MATLAB: http://arxiv.org/pdf/1205.6872v1.pdf which can be run on any ordinary CPU and much faster on a GPU.
Since it only uses extremely efficient built-in MATLAB functions which are compiled to machine code, it's not expected to be significantly slower than FORTRAN or C (keeping in mind that the computational cost of calculating Feynman integrals scales exponentially with respect to the number of time steps, meaning that FORTRAN, C, and MATLAB will all be slow in many cases, and the differences between them will be much smaller than the difference between taking 12 time steps and 13 time steps).
If you run this MATLAB code on a GPU it will in fact be faster than the FORTRAN or C implementation (only a CUDA FORTRAN or CUDA C code will be able to compare).
If you have more questions about this code you can email the author at dattani.nike#gmail.com

Related

Matlab AR digital fiter computation w/o loops

I'm given the a(k) matrix and e(n) and I need to compute y(n) from the following eq:
y(n) = sum(k = 1 to 10 )( a(k)*y(n-k) ) + e(n).
I have the a matrix(filter coefficients) and the e matrix (the residual), therefore there is only 1 unknown: y, which is built by the previous 10 samples of y.
An example to this equation:
say e(0) (my first residual sample) = 3
and y(-10) to y(-1) = 0
then y(0), my first sample in the signal y, would just be e(0) = 3:
y(0) = a(1)*y(-1) + a(2)*y(-2) + .... + a(10)*y(-10) + e(0) = e(0) = 3
and if e(1) = 4, and a(1) = 5, then
y(1) = a(1)*y(0) + a(2)*y(-1) + a(3)&y(-2) + ... + a(10)*y(-9) + e(1) = 19
The problem is
I don't know how to do this without loops because, say, y(n) needs y(n-1), so I need to immediately append y(n-1) into my matrix in order to get y(n).
If n (the number of samples) = say, 10,000,000, then using a loop is not ideal.
What I've done so far
I have not implemented anything. The only thing I've done for this particular problem is research on what kind of Matlab functions I could use.
What I need
A Matlab function that, given an equation and or input matrix, computes the next y(n) and automatically appends that to the input matrix, and then computes the next y(n+1), and automatically append that to the input matrix and so on.
If there is anything regarding my approach
That seems like it's on the wrong track, or my question isn't clear enough, of if there is no such matlab function that exists, then I apologize in advance. Thank you for your time.

How to convert deep learning gradient descent equation into python

I've been following an online tutorial on deep learning. It has a practical question on gradient descent and cost calculations where I been struggling to get the given answers once it was converted to python code. Hope you can kindly help me get the correct answer please
Please see the following link for the equations used
Click here to see the equations used for the calculations
Following is the function given to calculate the gradient descent,cost etc. The values need to be found without using for loops but using matrix manipulation operations
import numpy as np
def propagate(w, b, X, Y):
"""
Arguments:
w -- weights, a numpy array of size (num_px * num_px * 3, 1)
b -- bias, a scalar
X -- data of size (num_px * num_px * 3, number of examples)
Y -- true "label" vector (containing 0 if non-cat, 1 if cat) of size
(1, number of examples)
Return:
cost -- negative log-likelihood cost for logistic regression
dw -- gradient of the loss with respect to w, thus same shape as w
db -- gradient of the loss with respect to b, thus same shape as b
Tips:
- Write your code step by step for the propagation. np.log(), np.dot()
"""
m = X.shape[1]
# FORWARD PROPAGATION (FROM X TO COST)
### START CODE HERE ### (≈ 2 lines of code)
A = # compute activation
cost = # compute cost
### END CODE HERE ###
# BACKWARD PROPAGATION (TO FIND GRAD)
### START CODE HERE ### (≈ 2 lines of code)
dw =
db =
### END CODE HERE ###
assert(dw.shape == w.shape)
assert(db.dtype == float)
cost = np.squeeze(cost)
assert(cost.shape == ())
grads = {"dw": dw,
"db": db}
return grads, cost
Following are the data given to test the above function
w, b, X, Y = np.array([[1],[2]]), 2, np.array([[1,2],[3,4]]),
np.array([[1,0]])
grads, cost = propagate(w, b, X, Y)
print ("dw = " + str(grads["dw"]))
print ("db = " + str(grads["db"]))
print ("cost = " + str(cost))
Following is the expected output of the above
Expected Output:
dw [[ 0.99993216] [ 1.99980262]]
db 0.499935230625
cost 6.000064773192205
For the above propagate function I have used the below replacements, but the output is not what is expected. Please kindly help on how to get the expected output
A = sigmoid(X)
cost = -1*((np.sum(np.dot(Y,np.log(A))+np.dot((1-Y),(np.log(1-A))),axis=0))/m)
dw = (np.dot(X,((A-Y).T)))/m
db = np.sum((A-Y),axis=0)/m
Following is the sigmoid function used to calculate the Activation:
def sigmoid(z):
"""
Compute the sigmoid of z
Arguments:
z -- A scalar or numpy array of any size.
Return:
s -- sigmoid(z)
"""
### START CODE HERE ### (≈ 1 line of code)
s = 1 / (1+np.exp(-z))
### END CODE HERE ###
return s
Hope someone could help me understand how to solve this as I couldn't continue with rest of the tutorials without understanding this. Many thanks
You can calculate A,cost,dw,db as the following:
A = sigmoid(np.dot(w.T,X) + b)
cost = -1 / m * np.sum(Y*np.log(A)+(1-Y)*np.log(1-A))
dw = 1/m * np.dot(X,(A-Y).T)
db = 1/m * np.sum(A-Y)
where sigmoid is :
def sigmoid(z):
s = 1 / (1 + np.exp(-z))
return s
After going through the code and notes a few times was finally able to figure out the error.
First it needs calculating Z and then pass it to the sigmoid function, instead of X
Formula for Z = w(T)X+b. So in python this is calculated as below
Z=np.dot(w.T,X)+b
Then calculate A by passing z to sigmoid function
A = sigmoid(Z)
Then dw can be calculated as below
dw=np.dot(X,(A-Y).T)/m
Calculation of the other variables; cost and derivative of b will be as follows
cost = -1*((np.sum((Y*np.log(A))+((1-Y)*(np.log(1-A))),axis=1))/m)
db = np.sum((A-Y),axis=1)/m
def sigmoid(x):
#You have it right
return 1/(1 + np.exp(-x))
def derivSigmoid(x):
return sigmoid(x) * (1 - sigmoid(x))
error = targetSample - output
#Make sure to keep the sigmoided value around. For instance, an output that has already been sigmoided can be used to get the sigmoid derivative faster (output = sigmoid(x)):
dOutput = output * (1 - output)
Looks like you're already working on the backprop. Just thought I'd help simplify some of the forward prop for you.

convert MATLAB optimise function to julia

I am trying to convert a Matlab fmincon optimisation function to julia.
but without much luck.
I think NLopt or IPopt can be used.
the default examples work but when i try to change the values it doesnt seem to iterate.
using NLopt
count = 0 # keep track of # function evaluations
function myfunc(x::Vector, grad::Vector)
if length(grad) > 0
grad[1] = 0
grad[2] = 0.5/sqrt(x[2])
end
global count
count::Int += 1
println("f_$count($x)")
sqrt(x[2])
end
function myconstraint(x::Vector, grad::Vector, a, b)
if length(grad) > 0
grad[1] = 3a * (a*x[1] + b)^2
grad[2] = -1
end
(a*x[1] + b)^3 - x[2]
end
opt = Opt(:LD_MMA, 2)
lower_bounds!(opt, [-Inf, 0.])
xtol_rel!(opt,1e-4)
min_objective!(opt, myfunc)
inequality_constraint!(opt, (x,g) -> myconstraint(x,g,2,0), 1e-8)
inequality_constraint!(opt, (x,g) -> myconstraint(x,g,-1,1), 1e-8)
(minf,minx,ret) = optimize(opt, [1.234, 5.678])
println("got $minf at $minx after $count iterations (returned $ret)")
This one works fine. it gives 2 results and 11 iterations
using NLopt
count = 0 # keep track of # function evaluations
function myfunc(x)
x[1]^2 + 5*x[2] - 3
end
function myconstraint(x)
100*x[1] + 2000*x[2] == 100
end
opt = Opt(:LD_MMA, 2)
lower_bounds!(opt, [0, 0])
upper_bounds!(opt,[10,5])
xtol_rel!(opt,1e-10)
min_objective!(opt, myfunc);
inequality_constraint!(opt, (x) -> myconstraint(x), 1e-10)
(minf,minx,ret) = optimize(opt, [0.1,0.1])
This doesnt work, It just gives a result and 0 iterations
using NLopt
count = 0 # keep track of # function evaluations
function myfunc(x::Vector, grad::Vector)
x[1]^2 + 5*x[2] - 3
end
function myconstraint(result::Vector, x::Vector, grad::Vector)
100*x[1] + 2000*x[2] == 100
end
opt = Opt(:LD_MMA, 2)
lower_bounds!(opt, [0., 0.])
upper_bounds!(opt, [10.,5.])
xtol_rel!(opt,1e-4)
min_objective!(opt, myfunc)
inequality_constraint!(opt, (x,g) -> myconstraint(x), 1e-8)
(minf,minx,ret) = optimize(opt, [0., 0.])#even with [5.,10.]
this just gives me a result and 0 iterations.
anyone any idea what i a doing wrong?
I'm having a tough time reconstructing what you've done based on the comment marathon, so instead I thought I would just provide code indicating how I would have solved this problem. Note, the following code assumes your inequality constraint is 100*x[1] + 2000*x[2] <= 100:
using NLopt
function myfunc(x::Vector{Float64}, grad::Vector{Float64})
if length(grad) > 0
grad[1] = 2 * x[1]
grad[2] = 5
end
xOut = x[1]^2 + 5*x[2] - 3
println("Obj func = " * string(xOut))
return(xOut)
end
function myconstraint(x::Vector{Float64}, grad::Vector{Float64})
if length(grad) > 0
grad[1] = 100
grad[2] = 2000
end
xOut = 100*x[1] + 2000*x[2] - 100
println("Constraint val = " * string(xOut))
return(xOut)
end
opt = Opt(:LD_MMA, 2)
lower_bounds!(opt, [0, 0])
upper_bounds!(opt,[10,5])
xtol_rel!(opt,1e-10)
min_objective!(opt, myfunc);
inequality_constraint!(opt, myconstraint, 1e-10)
(minf,minx,ret) = optimize(opt, [0.1,0.1])
There are three main differences between my code and yours:
I've explicitly included the gradient function updates. This will make the convergence routine more efficient for any gradient-based algorithm (of which MMA is one).
I'm printing out the values of my objective function and constraint function at each step. This is useful for debugging purposes, and to get some idea of what is happening under the hood.
I've made it very clear in my code that the return value from my constraint function is f(x), where the constraint is defined by the equation f(x) <= 0. This is the appropriate syntax for use with NLopt.
You stated in the comments that you are getting an error when you try to change inequality_constraint! to equality_constraint!. This is because your algorithm of choice (MMA) only supports inequality constraints. A description of the NLopt algorithms can be found here. Note:
only MMA and SLSQP support arbitrary nonlinear inequality constraints, and only SLSQP supports nonlinear equality constraints
So, switch your algorithm to :LD_SLSQP, and voila, you can now switch your inequality constraint to an equality constraint.

Coding of Ito Stochastic Process

I am trying to implement a routine in mathematica/matlab for a stochastic process. Any code written here is for mathematica, but if someone can help me with encoding this in matlab (if they're more familiar with that) then that would be fine as well. However, mathematica is the priority if possible.
I will state what I want to get after going over the equations first.
The following is the Ito stochastic Process that I am interested in (where z(t)=[x(t),y(t)]:
with the following quantities:
We also have the following (from now I will type x(t),y(t) as x,y):
where, is the first exit time and
GT(x) is a smooth function of x (please see at the end)
Goal: I want to get Q0 in terms of x and only.
===============================================================================
To find the first exit time maybe the following can be used (courtesy of b.gatessucks). Please note that the following is a mathematica code.
CONSTRAINT FOR EXIT TIME:
const[x_, y_] := And[10^-8 <= y <= 10^-3, 0.9*(Uc) <= a1*x^2/2 - a3*x^4/4 <= 1.1*(Uc)]
x0 = 0.1; (* starting point for x[t] *) y0 = 0.1; (* starting point for y[t] *)
proc = ItoProcess[ {\[DifferentialD]x[t] == y[t] \[DifferentialD]t, \[DifferentialD]y[t] == (-G*y[t] - (a1*x[t] - a3*x[t]^3) - eps*b3b*y[t]^3) \[DifferentialD]t + Sqrt[2*eps*G] \[DifferentialD]w[t]}, {t, x[t], y[t], Boole[const[x[t], y[t]]]}, {{x, y}, {x0, y0}}, {t, 0}, w \[Distributed] WienerProcess[]]
Exit time is found:
SeedRandom[3]
sim = RandomFunction[proc, {0, 1, 0.001}];
First#Select[sim[[2, 1, 1]], #[[4]] == 0 &]
that outputs {t, x[t], y[t], Boole[const[x[t], y[t]]]}
I need to be able to use the above code to find the exit time and then use it in Q0. The above snippet to find exit time produces non zero times for properly chosen .
================================================================================
Numerical Task that needs to be set up to find Q0(x):
--> We start from the integral term in this expression. First find 's for many initial conditions (say 100 for now -- i.e., 100 exit times) starting from inside the domain (maybe by using the snippet already mentioned in this post). Now the integral can be evaluated for each of the 100 exit times as functions of x and .
--> Now, the expectation of the integral in Q0 is a sample average (by Law of Large Numbers) of all the 100 integrals evaluated before. Thus, Q0 can be found and it should only be a function of x and .
================================================================================
My issues:
The code above for the exit time only seems to produce non-zero exit times for appropriately chosen initial conditions. If someone can elucidate as to how to pick the appropriate such that there will be sufficient exit time being produced then that would be appreciative. I really want to keep my constraints as specified above in const[x_, y_], but if there seems to be no hope in finding tractable results then I wouldn't mind relaxing it.
The code below for GT(x) results in singularities -- I received error message from DSolve regarding indeterminate expressions....both b0[xc] and DrhoDy[xc] become indeterminate and so the DSolve gives problems when using the initial condition GT[xc] as it becomes indeterminate as well...any way around this matter would be gladly appreciated.
Finally, I really need someone's help in evaluating the 100 integrals in mathematica efficiently (since the terms are huge) for each of the exit times found previously and taking the expectation. I am not sure as to how to find the exit times properly.
================================================================================
To find GT(x):
b1b = 0.9; b3b = .8; a1b = 0.1; a3b = 0.2; G = (1/0.1^2)*
b1b; a1 = (1/.1^2)*a1b; a3 = (1/.1^2)*a3b; xc = Sqrt[a1/a3]; Uc =
a1*xc^2/2 - a3*xc^4/4; L = (G + Sqrt[G^2 + 4*(-(a1 - 3*a3*xc^2))])/2;
y[x_] := (x *a1 - x^3*a3)/(G + L)
b0[x_] := (y[
x]*(a1*x \[Minus]
a3*x ^3)*(1 \[Minus] (a1 \[Minus] 3*a3*x ^2)) \[Minus]
G*y[x]*(a1 \[Minus] 3*a3*x ^2)) /(y[
x] ^2 + (\[Minus]G*y[x] + a1*x \[Minus] a3*x ^3) ^2)
DrhoDy [x_] :=
Sqrt[y[x]^ 2 /(y[x] ^2 + (\[Minus]G*y[x] + a1*x \[Minus] a3*x ^3) ^2)]
linearequation =
y[x]*GT '[x] + b0[x]*GT[x] == G*DrhoDy [x]^2 *GT[x]^ 3;
GT[x] =
DSolve[{linearequation, GT[xc] == Sqrt[b0[xc]/( G*DrhoDy [xc]^2 )]},
GT, x]
ODE:
that satisfies the Initial Condition:
where,
and

Monte Carlo simulation with condition loop in MATLAB

I am a physicist.
I have written a code on Monte Carlo simulation with condition loop.
I am getting some error in running this simulation code. I want to consider the positive value of the simulation result. When I run the code, I get an error.
I am running 10000 iterations and I have five parameters such as A,B,C,D and E. I am generating a random number for each parameter by using variance and mean of every parameter with the help of normal distribution.
The code is as follows:
n = 10000;
Constant = 5;
Arand = (3*10^(12)*randn(1,n)) + 7*10^(6)*ones(1,n);
Brand = (9*randn(1,n)) + 17*ones(1,n);
Crand = (2*10^(-4)*randn(1,n)) + 0.2*ones(1,n);
Drand = (0.0017*randn(1,n)) + 0.50*ones(1,n);
Erand = (0.00004*randn(1,n)) + 1.5*ones(1,n);
if P1 > 0
P1 = Constant*Arand.*Brand.*Crand.*Drand.*(1/Erand)
end
plot(P1);
P1 = Constant*Arand.*Brand.*Crand.*Drand./Erand;
It's not clear what is P1 before the if statement.
Note, that "if P1 > 0" means "if all(P1 > 0)".
If you want to plot only those points where P1 is positive you need to change these lines:
if P1 > 0
P1 = Constant*Arand.*Brand.*Crand.*Drand.*(1/Erand)
end
plot(P1);
to the following:
Define P1:
P1 = Constant*Arand.*Brand.*Crand.*Drand./Erand;
and then choose to plot only the positive values:
plot(P1(P1>0));
Hope this helps.