What is the "Regular Expression" for this given machine? - computation-theory

Given Machine
Actually I'm confused between two options
cxa(bcxa+d)x
cxa(bc+d)x
here "x" means * on previous letter/bracket followed by "x".

Write out our equations:
(q1) = (q1)c + (q2)b + e
(q2) = (q2)d + (q1)a
Simplify the expression for (q1) using the rule (q) = (q)x + y <=> (q) = yx*:
(q1) = ((q2)b + e)c*
(q2) = (q2)d + (q1)a
Replace the expression for (q1) into the expression for (q2), factorize the (q2) out of the RHS, and apply the rule from above:
(q1) = ((q2)b + e)c*
(q2) = (q2)d + ((q2)b + e)c*a
= (q2)d + (q2)bc*a + c*a
= (q2)(d + bc*a) + c*a
= (c*a)(d + bc*a)*
This appears to be what you have down for option 1.

Related

Random walk algorithm for pricing barrier options

How to realize the random walk algorithm with MATLAB? I don't understand this algorithm, because the condition 3 never holds. I post my code in the end.
Choose a time-step h>0 so that M=9/h is a integer.
Set log(L_0) = L_0 = 0.13 and Z(0)=0.
rho_k are independent random variables distributed by the law P(+-1)=0.5
1) log(L_{k+1}) = log(L_k)-0.5*sigma^2*h+sigma*sqrt(h)*rho_{k+1}
2) Z_{k+1} = Z_k - sigma* [(erfc((2*2^(1/2)*(log((25*exp(log L_k))/7) + 9/32))/3)/2 - erfc((2*2^(1/2)*(log(100*exp(log L_k)) + 9/32))/3)/2 + erfc((2*2^(1/2)*(log(7/(25*exp(log L_k))) - 9/32))/3)/56 - erfc((2*2^(1/2)*(log(196/(25*exp(log L_k))) - 9/32))/3)/56 + (exp(log L_k)*((2*2^(1/2)*exp(-(8*(log(7/(25*exp(log L_k))) - 9/32)^2)/9))/(3*exp(log L_k)*pi^(1/2)) - (2*2^(1/2)*exp(-(8*(log(196/(25*exp(log L_k))) - 9/32)^2)/9))/(3*exp(log L_k)*pi^(1/2))))/28 - exp(log L_k)*((2*2^(1/2)*exp(-(8*(log((25*exp(log L_k))/7) + 9/32)^2)/9))/(3*exp(log L_k)*pi^(1/2)) - (2*2^(1/2)*exp(-(8*(log(100*exp(log L_k)) + 9/32)^2)/9))/(3*exp(log L_k)*pi^(1/2))) - (14*2^(1/2)*exp(-(8*(log(7/(25*exp(log L_k))) + 9/32)^2)/9))/(75*exp(log L_k)*pi^(1/2)) + (14*2^(1/2)*exp(-(8*(log(196/(25*exp(log L_k))) + 9/32)^2)/9))/(75*exp(log L_k)*pi^(1/2)) + (2^(1/2)*exp(-(8*(log((25*exp(log L_k))/7) - 9/32)^2)/9))/(150*exp(log L_k)*pi^(1/2)) - (2^(1/2)*exp(-(8*(log(100*exp(log L_k)) - 9/32)^2)/9))/(150*exp(log L_k)*pi^(1/2))))]*sqrt(h)*rho_{k+1};
3) log L_k < log(H)+1/2*sigma^2*h-sigma*sqrt(h)
H=0.28
sigma=0.25
K=0.01
To realize the algorithm we follow the random walk generated by (1),
and at each time t_k; we check whether the condition 3 holds. If it does not, L_k has reached the boundary zone and we stop the chain at log(H). If it does,
we perform (1)-(2) to find log(L_k+1) and Z_{k+1}. If k+1=M; we stop, otherwise we continue with the algorithm.
The outcome of simulating each trajectory is a point (t_kappa, log(L_kappa), Z_kappa).
Evaluate the expectation E[(exp(log(L_{kappa}))-K)^+ * Chi(kappa=M) + Z_{kappa}]=price with the Monte Carlo technique and do 10^6 Monte Carlo runs.
Here is my code. It doesn't work, because I don't get something near 0.0657 (the result).
Y = zeros(1,M+1); %ln L_k = Y(k)
Z = zeros(1,M+1);
sigma = 0.25;
H = 0.28;
K = 0.01;
Y(1) = 0.13;
Z(1) = 0;
M = 90;
h = 0.1;
for k = 1:M+1
vec = [-1 1];
index = random('unid', length(vec),1);
x(1,k) = vec(index);
end %'Rho'
for k = 1:M
if Y(k) < log(H) + 1/2*sigma^2*h - sigma*sqrt(h) %Bedingung
Y(k+1) = Y(k) - (1/2)*(sigma)^2*h + sigma*sqrt(h)*x(k+1);
Z(k+1) = Z(k) + (-sigma*(erfc((2*2^(1/2)*(log((25*exp(Y(k)))/7) + 9/32))/3)/2 - erfc((2*2^(1/2)*(log(100*exp(Y(k))) + 9/32))/3)/2 + erfc((2*2^(1/2)*(log(7/(25*exp(Y(k)))) - 9/32))/3)/56 - erfc((2*2^(1/2)*(log(196/(25*exp(Y(k)))) - 9/32))/3)/56 + (exp(Y(k))*((2*2^(1/2)*exp(-(8*(log(7/(25*exp(Y(k)))) - 9/32)^2)/9))/(3*exp(Y(k))*pi^(1/2)) - (2*2^(1/2)*exp(-(8*(log(196/(25*exp(Y(k)))) - 9/32)^2)/9))/(3*exp(Y(k))*pi^(1/2))))/28 - exp(Y(k))*((2*2^(1/2)*exp(-(8*(log((25*exp(Y(k)))/7) + 9/32)^2)/9))/(3*exp(Y(k))*pi^(1/2)) - (2*2^(1/2)*exp(-(8*(log(100*exp(Y(k))) + 9/32)^2)/9))/(3*exp(Y(k))*pi^(1/2))) - (14*2^(1/2)*exp(-(8*(log(7/(25*exp(Y(k)))) + 9/32)^2)/9))/(75*exp(Y(k))*pi^(1/2)) + (14*2^(1/2)*exp(-(8*(log(196/(25*exp(Y(k)))) + 9/32)^2)/9))/(75*exp(Y(k))*pi^(1/2)) + (2^(1/2)*exp(-(8*(log((25*exp(Y(k)))/7) - 9/32)^2)/9))/(150*exp(Y(k))*pi^(1/2)) - (2^(1/2)*exp(-(8*(log(100*exp(Y(k))) - 9/32)^2)/9))/(150*exp(Y(k))*pi^(1/2))))*sqrt(h)*x(k+1);
else
Y(k)=log(H);
break
end
end
if k == M
end
if (L(M)-K) > 0
(L(M)-K);
else
0;
end
return
exp(Y(M))-K+Z(M)

Generate all valid combinations of N pairs of parentheses

UPDATE (task detailed Explanation):
We have a string consist of numbers 0 and 1, divided by operators |, ^ or &. The task is to create all fully parenthesized expressions. So the final expressions should be divided into "2 parts"
For example
0^1 -> (0)^(1) but not extraneously: 0^1 -> (((0))^(1))
Example for expression 1|0&1:
(1)|((0)&(1))
((1)|(0))&(1)
As you can see both expressions above have left and write part:
left: (1); right: ((0)&(1))
left: ((1)|(0)); right: (1)
I tried the following code, but it does not work correctly (see output):
// expression has type string
// result has type Array (ArrayList in Java)
function setParens(expression, result) {
if (expression.length === 1) return "(" + expression + ")";
for (var i = 0; i < expression.length; i++) {
var c = expression[i];
if (c === "|" || c === "^" || c === "&") {
var left = expression.substring(0, i);
var right = expression.substring(i + 1);
leftParen = setParens(left, result);
rightParen = setParens(right, result);
var newExp = leftParen + c + rightParen;
result.push(newExp);
}
}
return expression;
}
function test() {
var r = [];
setParens('1|0&1', r);
console.log(r);
}
test();
code output: ["(0)&(1)", "(0)|0&1", "(1)|(0)", "1|0&(1)"]
Assuming the input expression is not already partially parenthesized and you want only fully parenthesized results:
FullyParenthesize(expression[1...n])
result = {}
// looking for operators
for p = 1 to n do
// binary operator; parenthesize LHS and RHS
// parenthesize the binary operation
if expression[p] is a binary operator then
lps = FullyParenthesize(expression[1 ... p - 1])
rps = FullyParenthesize(expression[p + 1 ... n])
for each lp in lps do
for each rp in rps do
result = result U {"(" + lp + expression[p] + rp + ")"}
// no binary operations <=> single variable
if result == {} then
result = {"(" + expression + ")")}
return result
Example: 1|2&3
FullyParenthesize("1|2&3")
result = {}
binary operator | at p = 2;
lps = FullyParenthesize("1")
no operators
result = {"(" + "1" + ")"}
return result = {"(1)"}
rps = Parenthesize("2&3")
result = {"2&3", "(2&3)"}
binary operator & at p = 2
lps = Parenthesize("2")
no operators
result = {"(" + "2" + ")"}
return result = {"(2)"}
rps = Parenthesize("3")
no operators
result = {"(" + "3" + ")"}
return result = {"(3)"}
lp = "(2)"
rp = "(3)"
result = result U {"(" + "(2)" + "&" + "(3)" + ")"}
return result = {"((2)&(3))"}
lp = "(1)"
rp = "((2)&(3))"
result = result U {"(" + "(1)" + "|" + "((2)&(3))" + ")"}
binary operator & at p = 4
...
result = result U {"(" + "((1)|(2))" + "&" + "(3)" + ")"}
return result {"((1)|((2)&(3)))", "(((1)|(2))&(3))"}
You will have 2^k unique fully parenthesized expressions (without repeated parentheses) given an input expression with k binary operators.

sum of series AP GP clrs appendix A.1-4

I am trying to prove an equation given in the CLRS exercise book. The equation is:
Sigma k=0 to k=infinity (k-1)/2^k = 0
I solved the LHS but my answer is 1 whereas the RHS should be 0
Following is my solution:
Let's say S = k/2^k = 1/2 + 2/2^2 + 3/2^3 + 4/2^4 ....
2S = 1 + 2/2 + 3/2^2 + 4/2^3 ...
2S - S = 1 + ( 2/2 - 1/2) + (3/2^2 - 2/2^2) + (4/2^3 - 3/2^3)..
S = 1+ 1/2 + 1/2^2 + 1/2^3 + 1/2^4..
S = 2 -- eq 1
Now let's say S1 = (k-1)/2^k = 0/2 + 1/2^2 + 2/2^3 + 3/2^4...
S - S1 = 1/2 + (2/2^2 - 1/2^2) + (3/2^3 - 2/2^3) + (4/2^4 - 3/2^4)....
S - S1 = 1/2 + 1/2^2 + 1/2^3 + 1/2^4...
= 1
From eq 1
2 - S1 = 1
S1 = 1
Whereas the required RHS is 0. Is there anything wrong with my solution? Thanks..
Yes, you have issues in your solution to the problem.
While everything is correct in formulating the value of S, you have calculated the value of S1 incorrectly. You missed substituting the value for k=0 in S1. Whereas, for S, even after putting the value of k, the first term will be 0, so no effect.
Therefore,
S1 = (k-1)/2^k = -1 + 0/2 + 1/2^2 + 2/2^3 + 3/2^4...
// you missed -1 here because you started substituting values from k=1
S - S1 = -(-1) + 1/2 + (2/2^2 - 1/2^2) + (3/2^3 - 2/2^3) + (4/2^4 - 3/2^4)....
S - S1 = 1 + (1/2 + 1/2^2 + 1/2^3 + 1/2^4...)
= 1 + 1
= 2.
From eq 1
2 - S1 = 2
S1 = 0.

Convert boolean expression to 3 input NOR

I was taking a look at this link http://lizarum.com/assignments/boolean_algebra/chapter3.html to try and solve an equation I have. The original equation is:
H = MC + MC' + CRD + M'CD'
I simplified it to
H = M + CRD + M'CD'
Here is my attempt:
H = ((M + CRD + M'CD')')'
H = ((M)' * (CRD)' * (M'CD')')'
H = (((M)')' + ((CRD)')' + ((M'CD')')'
H = ((M')' + (C'+ R' + D')' + (M + C' + D)')'
Is that final equation a 3 input NOR equation? I have a feeling that I'm missing a step that makes the first parentheses into three variables.
As a first step, notice that M C + M C' = M
This simplifies your equation to
H = M + CRD + M'CD'
You can then leave out the M'. If it were false, M would be true and thus H.
H = M + CRD + CD'
This allows you to factor-out C:
H = M + C(RD + D')
Term D in the parentheses can be left out (same argument as above)
H = M + C(R + D')
The final result is:
H = M + CR + CD'
You could have arrived at this result using a Karnaugh-Veitch map.
Convince yourself asking WolframAlpha.

Reorganizing a formula containing Modulo

I have a formula that looks like
a = (b + 1 + c)%d
I want to express c in terms of rest, i.e. have "C" on the LHS.
Any suggestions ?
a = (b + 1 + c)%d
a + n*d = b + 1 + c
a -1 - b + n*d = c
For any integer n.

Resources