Posterior probability math to code

조회 수: 2 (최근 30일)
Zakaria Djebbara
Zakaria Djebbara 2021년 7월 21일
댓글: Zakaria Djebbara 2021년 7월 21일
I'm unsure whether I "translated" the math correctly into the code. I can't seem to make sense of the summation sign here. I know the values of x, but does the math say "sum of x from index 1 to whatever t I'm at?
sum(x(1:t))
Here's what I've come up with instead. Obviously the summation is missing.
x = [-0.46, 0.83, -3.26, -0.14, -0.68, -2.31, 0.57, 1.34, 4.58, 3.77];
T = length(x); % 10 numbers
sigma = 1;
posterior = [];
for t = 1:T
posterior = [posterior; exp((2/sigma^2) .* x(t))]; % This part
end
plot(posterior);
Could someone please have a look and direct me to some existing forum, but please not to https://se.mathworks.com/help/matlab/ref/sum.html because this doesn't help me translating the math to code.
Thanks a lot

채택된 답변

Yazan
Yazan 2021년 7월 21일
편집: Yazan 2021년 7월 21일
First of all, you have to pay attention to the fact that there is no equality in the relation you presented, but rather a proportionality. Meaning that the posterior is given by this relation up to a nonzero constant. Now, imagine that you have a random variable , the posterior at t is given by summing the values from to , where , multiplying the result by a constant then taking e to the power of the result. Obviously, some constraints should be imposed on the definition of X such that p becomes a proper probability function.
Assuming that you have defined X, T, and sigma properly in your code, you can use this to compute the posterior
p = arrayfun(@(t) exp(sum(X(t:T))*2./sigma^2), t);
  댓글 수: 1
Zakaria Djebbara
Zakaria Djebbara 2021년 7월 21일
Thanks for a proper answer. The key thing I didn't understand, which I do now given your explanation, was that the posterior is given by the summing of vector x from x1 to xT. So my code simply needed this change:
x = [-0.46, 0.83, -3.26, -0.14, -0.68, -2.31, 0.57, 1.34, 4.58, 3.77];
T = length(x); % 10 numbers
sigma = 1;
posterior = [];
for t = 1:T
posterior = [posterior; exp((2/sigma^2) .* sum(x(t:T)))]; % This part
end
plot(posterior);
It works beautifully now. Thanks again, Yazan.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by