开发者

Converting a math formula into MATLAB code

开发者 https://www.devze.com 2023-01-31 17:14 出处:网络
How I can convert this math formula to a vectorized MATLAB code? a(i) = Sum (Log (b(i) / b(i-1)) - Sum (Log (b(i) / b(i-1)), 10) , 10 )

How I can convert this math formula to a vectorized MATLAB code?

a(i) = Sum (Log (b(i) / b(i-1)) - Sum (Log (b(i) / b(i-1)), 10) , 10 )

b is a vector and i > 10.

Also, Sum (d(i),n) = d(i) + d(i-1) + ... + d(i-n+1开发者_如何学C), with i > n


Before coding the formula in MATLAB, there is a great deal of simplification you can perform. First, define a function f(i) as follows:

f(i) = Log(b(i)/b(i-1)) = Log(b(i)) - Log(b(i-1))

Then, applying the Sum function as you have defined it, you can see that many of the terms cancel one another out:

Sum(f(i),10) = f(i) + f(i-1) + ... + f(i-8) + f(i-9)
             = Log(b(i))   - Log(b(i-1)) +
               Log(b(i-1)) - Log(b(i-2)) +
               ... +
               Log(b(i-8)) - Log(b(i-9)) +
               Log(b(i-9)) - Log(b(i-10))

==> Sum(f(i),10) = Log(b(i)) - Log(b(i-10))

Now, here's where it gets a little tricky, because there are potentially two ways to interpret your nested Sum operations: one where the inner summation variable i depends on the outer summation variable i, and one where the inner summation variable i is independent of the outer summation variable i.


When the inner i depends on the outer i...

then for every term of the outer summation, the inner summation is computed starting from the decremented value of i for that term. For example, the fifth term of the outer summation would look like this:

... + f(i-4) - Sum(f(i-4),10) + ...

The equation for a(i) will therefore simplify as follows:

a(i) = Sum(f(i) - Sum(f(i),10),10)
     = Sum(Log(b(i)) - Log(b(i-1)) - Log(b(i)) + Log(b(i-10)),10)
     = Sum(Log(b(i-10)) - Log(b(i-1)),10)
     = Log(b(i-10)) - Log(b(i-1)) +
       Log(b(i-11)) - Log(b(i-2)) +
       ... + 
       Log(b(i-18)) - Log(b(i-9)) +
       Log(b(i-19)) - Log(b(i-10))

              [b(i-11)*b(i-12)*...*b(i-18)*b(i-19)]
==> a(i) = Log[-----------------------------------]
              [  b(i-1)*b(i-2)*...*b(i-8)*b(i-9)  ]

And this final equation can be coded in MATLAB as follows:

a(i) = log(prod(b(i-(11:19)))/prod(b(i-(1:9))));


When the inner i is independent of the outer i...

then the result of the inner summation will essentially appear to be a constant with respect to the outer summation. For example, the fifth term of the outer summation would look like this:

... + f(i-4) - Sum(f(i),10) + ...

The equation for a(i) will therefore simplify as follows:

a(i) = Sum(f(i) - Sum(f(i),10),10)
     = Sum(f(i),10) - 10*Sum(f(i),10)
     = -9*Sum(f(i),10)
     = 9*Log(b(i-10)) - 9*Log(b(i))

==> a(i) = 9*Log(b(i-10)/b(i))

And this final equation can be coded in MATLAB as follows:

a(i) = 9*log(b(i-10)/b(i));


Not sure what concept you are expressing here (your formula doesn't have matched parentheses!) but if that's truly what you want, you could simplify as follows:

log(b(i:i+9)./b(i-1:i+8))  

is equivalent to

 diff(log(b(i-1:i+9)))

So

I would transform b into log returns first:

c = diff(log(b));

then

a(i+1) = sum( c(i:i+9) - sum(c(i:i+9)));

Perhaps with the formula simplified, you can see more clearly what you want.


a(i) = sum( log(b(i-9:i:)./b(i-10:i-1)) - sum(log(b(i-9:i)./b(i-10:i-1))))

Consider Sum(f(i), n). It is equivalent to sum(f(i-n+1:i)). So Sum(Log(f(i)), n) is sum(log(f(i-n+1:i))) (take elementwise operation, and then sum the result).

EDIT

Mathematically, isn't

Sum(Log(b(i)/b(i-1)), n)
= \sum_{j=0..n-1}(Log(b(i-j)/b(i-j-1)))
= \sum_{j=0..n-1}(Log(b(i-j)) - Log(b(i-j-1)))
= Log(b(i)) - Log(b(i-n))
= Log(b(i)/b(i-n))

??

So then,

a(i) = Sum(Log(b(i)/b(i-1)) - Sum(Log(b(i)/b(i-1)), n)  , n)
= Sum(Log(b(i)/b(i-1)), n) - Sum(Sum(Log(b(i)/b(i-1)), n), n)
= Log(b(i)/b(i-n)) - Sum(Log(b(i)/b(i-n)), n)

??

The problem is with the definition of Sum. which really should specify the index.

And how does the inner Sum's index interact with the outer sum's (both are i).

0

精彩评论

暂无评论...
验证码 换一张
取 消