find a solution to a function subject to certain constrains
显示 更早的评论
define a random variable
X~N(0,1); Y=-X if |X|<c and Y=X if |X|>=c;
I would like to find a c* which make corr(X,Y)=0;
I tried to use fsolve, but it does not work.
Here is the code I tried:
syms c
X=randn(1000,1);
myfun=corr(X,[X(find(abs(X)<=c));-X(find(abs(X)))>c]);
fsolve(@myfun,0)
3 个评论
John Chilleri
2017-8-27
You would need to make your function a function of c:
X = randn(1000,1);
myfun=@(c) corr(X,[X(find(abs(X)<=c));-X(find(abs(X)))>c]);
fsolve(myfun,0)
However, it says the problem appears regular.
Experimenting with a simple code such as,
clear all
close all
clc
X = randn(1000,1);
c = -1:.01:1;
vals = zeros(length(c),1);
for i = 1:length(c)
Y = X.*sign(X-c(i));
vals(i) = corr(X,Y);
end
[minval,ind] = min(abs(vals));
cval = c(ind)
minval
also yields varying results. Intuitively there seems to be a likely answer, but to determine the value of c experimentally may be difficult.
Sorry for not being much help, good luck!
Walter Roberson
2017-8-27
According to Maple, the correlation works out to be
(-2*sqrt(Pi)*erf((1/2)*sqrt(2)*c)+sqrt(Pi)+2*sqrt(2)*c*exp(-(1/2)*c^2))/sqrt(Pi)
There is a real solution to that at approximately 1.538
John D'Errico
2017-8-27
Yes, but that is an analytical correlation. It won't apply to any given random sample.
回答(1 个)
John D'Errico
2017-8-27
编辑:John D'Errico
2017-8-27
Sigh. Why, in the name of god and little green apples??? I suppose you have a reason.
And why do you think that you needed to make c a symbolic variable?
c should be approximately 0. And I assume that it will approach 0 as the size of your random sample increases. But for any given random vector, it will not be exactly 0, just as the mean of a random sample will not be the distribution mean. As well, the result will not be a differentiable function of c. So fsolve will be expected to fail, or at best, run like a horse with 2 legs. It will trip a lot.
X = randn(1000,1);
Yfun = @(c) X.*((X > c)*2 - 1);
Cfun = @(c) corr(X,Yfun(c));
Cfun(0)
ans =
0.024479
So Cfun(0) is, as expected, not exactly zero. What a surprise. We can still plot it.
ezplot(Cfun,[-3,3])
Warning: Function failed to evaluate on array inputs; vectorizing the function may speed up its evaluation and avoid the need to loop over array elements.
> In ezplot>ezplot1 (line 488)
In ezplot (line 144)
grid on

As expected, you can see the jaggies. Cfun is indeed not a differentiable function of c. You CANNOT use fsolve to robustly solve a non-differentiable function. PERIOD.
ezplot(Cfun,[-.05,.05])
Warning: Function failed to evaluate on array inputs; vectorizing the function may speed up its evaluation and avoid the need to loop over array elements.
> In ezplot>ezplot1 (line 488)
In ezplot (line 144)
>> grid on

Now do you believe it is not differentiable? It does not even appear to be monotonic. There might even be multiple solutions for some vectors X.
Luckily for you, fzero is somewhat robust in this respect.
c0 = fzero(Cfun,[-3,3])
c0 =
0.37578
Cfun(c0)
ans =
6.5691e-05
ezplot(Cfun,c0 + [-.01,.01])
Warning: Function failed to evaluate on array inputs; vectorizing the function may speed up its evaluation and avoid the need to loop over array elements.
> In ezplot>ezplot1 (line 488)
In ezplot (line 144)
>> grid on

As you should see, Cfun is actually a piecewise constant function of c.
Can you solve for c analytically? Probably you can do a little more than I did here. But is it worth the effort? Probably not.
类别
在 帮助中心 和 File Exchange 中查找有关 Mathematics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!