Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
575 views
in Technique[技术] by (71.8m points)

numerical partial derivative in MatLab

How can I compute the numerical partial derivative of a probability density function (PDF) in Matlab? I'm not looking for a solution using automatic differences or a symbolic solution.

Given the following example:

arg = (-1:.01:1)';
mu = 0;
sigma = 0.5;

f = normpdf(arg,mu,sigma);

Is it possible to compute the numerical partial derivative of df/dsigma? Or am I stuck to having to use the automatic differences or the Symbolic Math toolbox?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I assume that the actual function is not the PDF of the normal distribution. You might try using complex step differentiation if you only need the first derivative:

mu = 0;
sigma = 0.5;
f = @(x)normpdf(x,mu,sigma);
x = -1:0.01:1;
h = 2^-28;
dx = imag(f(x+1i*h))/h;

Or if you hold x and mu constant and vary sigma:

mu = 0;
x = 0;
g = @(sigma)normpdf(x,mu,sigma);
sigma = 0.25:0.01:0.75;
h = 2^-28;
dsigma = imag(g(sigma+1i*h))/h;

This technique is fast, simple, and very accurate. You can download this as a convenient function, cdiff, from my GitHub.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...