I want to implement Discrete Integration with Galois Fields in Matlab where the time step is not constant.
Assume that it is this:
My attempt
function [ int ] = integrate_matlab( YDataVector, a, b )
%integrate_matlab Calculate the discrete integral
% Discrete Matlab Integration
% int_1^N x(t_k) * (b-a)/N, where t_k = a + (b-a) k/N
%
% YDataVector - Galois vector (255 x 1 gf), this is signal,
% which values you can reach by YDataVector.x
%
% int - returns Galois vector (255 x 1 gf)
N = length(YDataVector);
for k=1:N
tk = a + (b - a) * k/N;
int = xtk(YDataVector, k) * (b - a) / N;
% How to implement the function xtk(YDataVector)?
end
and then the function xtk
function [ xtk_result ] = xtk( YDataVector, k )
%xkt Summary of this function goes here
% YDataVector - Galois vector (255 x 1 gf), this is signal
% xtk_result - Galois vector (255 x 1 gf)
% k - index, this must be here to be able calculate different xtk for different iterations
xtk_result = ; // I do not know what to fill here
end
I am confused by the mathematical series equation x(tk) for tk.
I know that I am doing now this wrong.
The writing x(tk) just confuses me, since I think it as a function that takes in the series.
I know that it is a signal at some time point, here the YDataVector, but how to implement it I have forgotten.
I should probably iterate the series first:
t_0 = a;
t_1 = a + (b - a) * 1/N;
This does not seem to help, since tk is not defined iteratively.
What am I thinking wrong when implementing the series x(tk)?
Assuming that t contains the time that corresponds to each element of x (stored in YDataVector.x). Then if I understood correctly your question you can get x_tk with something like this :
N = length(YDataVector.x) ;
k = 1 : N;
tk = a + (b-a)* k/N ;
x_tk = interp1(t,YDataVector.x,tk);
Related
I am working on a Matlab project and I want to make the gradient of the following function in Matlab:
f(x) = c^T * x - sum (log(bi - (ai ^ T) * x)).
Where ai^T are the rows of a random A matrix nxm , where n=2, and m=20
c is random matrix nx1, and x is also random nx1.
b is random matrix mx1.
I've done the following but the results i get don't seem to be right..
function gc0 = gc(x, c, b, A)
for k = 1 : length(A(:,1))
f1(k) = sum(log(b - A(k,:)'*x(k)));
end
gradient(-f1)
gc0 = c - gradient(f1)';
Any ideas? I'd appreciate your help, I'm newbie in Matlab..
It seems that your loop contains a mistake. Looking at the formula above,
I think that the function evaluation should be
f1 = c'*x;
for k = 1 : length(A(1,:))
f1 = f1 - log(b(k) - A(:,k)'*x)
end
A shorter and faster notation for this in Matlab is
f = c'*x - sum(log(b - A' * x)) ;
The function 'gradient' does not calculate the gradient that I think you
want: it returns the differences of matrix entries, and your function f
is a scalar.
Instead, I suggest calculating the derivatives symbolically:
Gradf = c' + sum( A'./(b - A' * x) );
I am creating a function that takes in data (x,y,z) and an anonymous function (M) as inputs. M's inputs are data (x,y,z) and a parameter (theta).
I need to determine the dimension of the parameter theta inside FUNC
EDIT: (To add some context)
I have data that follows a known data generating process (DGP). For example, I can generate data using a linear instrumental variable DGP with 1 endogenous variable (hence theta will be dimension 1):
n = 100; q = 10;
theta0 = 1; % true param value
e = randn(n, 1); % 2nd stage error
u = randn(n, 1); % 1st stage error
z = randn(n, q); % instrument
x = z * ones(q, 1) + u; % endog variable
y = x * theta0 + e; % dependent variable
Then I want to estimate theta0 using my own variation of generalized linear methods (FUNC)
M = #(x,y,z,theta) z' * (y - x * theta); % moment condition
thetahat = FUNC(M, x, y, z); % estimate theta0
and FUNC.m is
function out = FUNC(M, x, y, z)
k = ; % (!!!) <-- this is what I need to find out!
objFunc = #(theta) M(x, y, z, theta)' * M(x, y, z, theta);
out = fminunc(objFunc, ones(1, k)); % <-- this is where its used
end
In the above example, the DGP is a linear IV model. However, I should be able to use my function for any other DGP.
Other DGPs could, for example, define M as follows:
% E.g. 1) theta is dimension 1
M=#(x,y,z,theta) z' * (y - x * theta);
% E.g. 2) theta is dimension 2
M=#(x,y,z,theta) z' * (y - (x * theta(1))^theta(2));
% E.g. 3) theta is dimension 3
M=#(x,y,z,theta) z' * (y - (theta(1) + x * theta(2))^theta(3));
The (super bad) hack that I am currently using for (!!!) is:
for ktest = [3,2,1] % the dimension of theta will never be higher than 3
try
M(x, y, z, ones(1, ktest);
k = ktest;
end
end
Since you know already what the form and requirements of your function M will be when you pass it to FUNC, it doesn't make much sense to then require FUNC to determine it based only on M. It would make much more sense to pass flag values or needed information to FUNC when you pass it M. I would write FUNC in one of two ways:
function out = FUNC(M, x, y, z, k) % Accept k as an argument
...
end
function out = FUNC(M, x, y, z, theta0) % Pass the initial guess, of the correct size
...
end
If you really want to let FUNC do the extra work, then the answer from excaza is how I would do it.
Old answer below. not really valid since the question was clarified, but I'm leaving it temporarily...
I think you have two better options here...
Make M a cell array of anonymous functions:
You could make your input M a cell array of possible anonymous functions and use the number of values in theta as the index. You would pass this M to FUNC:
M = {#(x,y,z,theta) z' * (y - x * theta), ...
#(x,y,z,theta) z' * (y - (x * theta(1))^theta(2)), ...
#(x,y,z,theta) z' * (y - (theta(1) + x * theta(2))^theta(3))};
Then somewhere inside FUNC:
out = M{numel(theta)}(x, y, z, theta);
Make M a normal function instead of an anonymous one:
An anonymous function is good for quick, simple formulas. Add in conditional logic and you should probably just make it a fully-fledged function. Here's an example with a switch statement (good for if you have a number of different formulas):
function out = M(x, y, x, theta)
switch numel(theta)
case 1
out = z' * (y - x * theta);
case 2
out = z' * (y - (x * theta(1))^theta(2));
case 3
out = z' * (y - (theta(1) + x * theta(2))^theta(3));
end
end
And here's an example that sets some defaults for parameters (good for if you have one formula with different ways to set its parameters, like you seem to have):
function out = M(x, y, x, theta)
switch numel(theta)
case 1
p1 = 0;
p2 = theta;
p3 = 1;
case 2
p1 = 0;
p2 = theta(1);
p3 = theta(2);
case 3
p1 = theta(1);
p2 = theta(2);
p3 = theta(3);
end
out = z' * (y - (p1 + x * p2)^p3);
end
MATLAB doesn't store any information about the size of the inputs to an anonymous function. While a better idea would be to modify your code so you don't have to do these kinds of gymnastics, if your function definition is known to fit a narrow band of possibilities you could use a regular expression to parse the function definition itself. You can get this string from the return of functions.
For example:
function [nelements] = findsizetheta(fh)
defstr = func2str(fh);
test = regexp(defstr, 'theta\((\d+)\)', 'tokens');
if isempty(test)
% Assume we have theta instead of theta(1)
nelements = 1;
else
nelements = max(str2double([test{:}]));
end
end
Which returns 1, 2, and 3 for your example definitions of M.
This assumes that theta is present in you anonymous function and that it is defined as a vector.
Also note that MATLAB cautions against utilizing functions in a programmatic manner, as its behavior may change in future releases. This was tested to function in R2017b.
I have a vector x = [x_1 x_2 ... x_n], a vector y = [y_1 y_2 y_3] and a matrix X = [x_11 x_12 ... x_1n; x_21 x_22 ... x_2n; x_31 x_32 ... x_3n].
For i = 1, 2, ..., n, I want to compute the following sum in MATLAB:
sum((x(i) - y*X(:,i))^2)
What I have tried to write is the following MATLAB code:
vv = (x(1) - y*X(:,1))^2; % as an initialization for i=1
for i = 2 : n
vv = vv + (x(i) - y * X(:,i))^2
end
But I am wondering if I can compute that without for loop in order to potentially reduce the computational time especially if n is very high... So are there any other much more optimal possibilities to do that in MATLAB?
Any help will be very appreciated!
You do not need the loop at all,
for i = 2:n
y*X(:,i)
end
is the same as just y*X, so x(i) - yX(:,i) is simply x - yX so basically, its:
vv = sum((x - y * X).^2);
Thanks for #beaker for pointing the mistake.
Figure 1. Hypothesis plot. y axis: Mean entropy. x axis: Bits.
This Question is in continuation to a previous one asked Matlab : Plot of entropy vs digitized code length
I want to calculate the entropy of a random variable that is discretized version (0/1) of a continuous random variable x. The random variable denotes the state of a nonlinear dynamical system called as the Tent Map. Iterations of the Tent Map yields a time series of length N.
The code should exit as soon as the entropy of the discretized time series becomes equal to the entropy of the dynamical system. It is known theoretically that the entropy of the system, H is log_e(2) or ln(2) = 0.69 approx. The objective of the code is to find number of iterations, j needed to produce the same entropy as the entropy of the system, H.
Problem 1: My problem in when I calculate the entropy of the binary time series which is the information message, then should I be doing it in the same base as H? OR Should I convert the value of H to bits because the information message is in 0/1 ? Both give different results i.e., different values of j.
Problem 2: It can happen that the probality of 0's or 1's can become zero so entropy correspondng to it can become infinity. To prevent this, I thought of putting a check using if-else. But, the loop
if entropy(:,j)==NaN
entropy(:,j)=0;
end
does not seem to be working. Shall be greateful for ideas and help to solve this problem. Thank you
UPDATE : I implemented the suggestions and answers to correct the code. However, my logic of solving was not proper earlier. In the revised code, I want to calculate the entropy for length of time series having bits 2,8,16,32. For each code length, entropy is calculated. Entropy calculation for each code length is repeated N times starting for each different initial condition of the dynamical system. This appraoch is adopted to check at which code length the entropy becomes 1. The nature of the plot of entropy vs bits should be increasing from zero and gradually reaching close to 1 after which it saturates - remains constant for all the remaining bits. I am unable to get this curve (Figure 1). Shall appreciate help in correcting where I am going wrong.
clear all
H = 1 %in bits
Bits = [2,8,16,32,64];
threshold = 0.5;
N=100; %Number of runs of the experiment
for r = 1:length(Bits)
t = Bits(r)
for Runs = 1:N
x(1) = rand;
for j = 2:t
% Iterating over the Tent Map
if x(j - 1) < 0.5
x(j) = 2 * x(j - 1);
else
x(j) = 2 * (1 - x(j - 1));
end % if
end
%Binarizing the output of the Tent Map
s = (x >=threshold);
p1 = sum(s == 1 ) / length(s); %calculating probaility of number of 1's
p0 = 1 - p1; % calculating probability of number of 0'1
entropy(t) = -p1 * log2(p1) - (1 - p1) * log2(1 - p1); %calculating entropy in bits
if isnan(entropy(t))
entropy(t) = 0;
end
%disp(abs(lambda-H))
end
Entropy_Run(Runs) = entropy(t)
end
Entropy_Bits(r) = mean(Entropy_Run)
plot(Bits,Entropy_Bits)
For problem 1, H and entropy can be in either nats or bits units, so long as they are both computed using the same units. In other words, you should use either log for both or log2 for both. With the code sample you provided, H and entropy are correctly calculated using consistant nats units. If you prefer to work in units of bits, the conversion of H should give you H = log(2)/log(2) = 1 (or using the conversion factor 1/log(2) ~ 1.443, H ~ 0.69 * 1.443 ~ 1).
For problem 2, as #noumenal already pointed out you can check for NaN using isnan. Alternatively you could check if p1 is within (0,1) (excluding 0 and 1) with:
if (p1 > 0 && p1 < 1)
entropy(:,j) = -p1 * log(p1) - (1 - p1) * log(1 - p1); %calculating entropy in natural base e
else
entropy(:, j) = 0;
end
First you just
function [mean_entropy, bits] = compute_entropy(bits, blocks, threshold, replicate)
if replicate
disp('Replication is ON');
else
disp('Replication is OFF');
end
%%
% Populate random vector
if replicate
seed = 849;
rng(seed);
else
rng('default');
end
rs = rand(blocks);
%%
% Get random
trial_entropy = zeros(length(bits));
for r = 1:length(rs)
bit_entropy = zeros(length(bits), 1); % H
% Traverse bit trials
for b = 1:(length(bits)) % N
tent_map = zeros(b, 1); %Preallocate for memory management
%Initialize
tent_map(1) = rs(r);
for j = 2:b % j is the iterator, b is the current bit
if tent_map(j - 1) < threshold
tent_map(j) = 2 * tent_map(j - 1);
else
tent_map(j) = 2 * (1 - tent_map(j - 1));
end % if
end
%Binarize the output of the Tent Map
s = find(tent_map >= threshold);
p1 = sum(s == 1) / length(s); %calculate probaility of number of 1's
%p0 = 1 - p1; % calculate probability of number of 0'1
bit_entropy(b) = -p1 * log2(p1) - (1 - p1) * log2(1 - p1); %calculate entropy in bits
if isnan(bit_entropy(b))
bit_entropy(b) = 0;
end
%disp(abs(lambda-h))
end
trial_entropy(:, r) = bit_entropy;
disp('Trial Statistics')
data = get_summary(bit_entropy);
disp('Mean')
disp(data.mean);
disp('SD')
disp(data.sd);
end
% TO DO Compute the mean for each BIT index in trial_entropy
mean_entropy = 0;
disp('Overall Statistics')
data = get_summary(trial_entropy);
disp('Mean')
disp(data.mean);
disp('SD')
disp(data.sd);
%This is the wrong mean...
mean_entropy = data.mean;
function summary = get_summary(entropy)
summary = struct('mean', mean(entropy), 'sd', std(entropy));
end
end
and then you just have to
% Entropy Script
clear all
%% Settings
replicate = false; % = false % Use true for debugging only.
%H = 1; %in bits
Bits = 2.^(1:6);
Threshold = 0.5;
%Tolerance = 0.001;
Blocks = 100; %Number of runs of the experiment
%% Run
[mean_entropy, bits] = compute_entropy(Bits, Blocks, Threshold, replicate);
%What we want
%plot(bits, mean_entropy);
%What we have
plot(1:length(mean_entropy), mean_entropy);
I have this code for the Composite Simpson's Rule. However, I have been fiddling with it for quite a while and I can't seem to get it to work.
How can I fix this algorithm?
function out = Sc2(func,a,b,N)
% Sc(func,a,b,N)
% This function calculates the integral of func on the interval [a,b]
% using the Composite Simpson's rule with N subintervals.
x=linspace(a,b,N+1);
% Partition [a,b] into N subintervals
fx=func(x);
h=(b-a)/(2*N);
%define for odd and even sums
sum_even = 0;
for i = 1:N-1
x(i) = a + (2*i-2)*h;
sum_even = sum_even + func(x(i));
end
sum_odd = 0;
for i = 1:N+1
x(i) = a + (2*i-1)*h;
sum_odd = sum_odd + func(x(i));
end
% Define the length of a subinterval
out=(h/3)*(fx(1)+ 2*sum_even + 4*sum_odd +fx(end));
% Apply the composite Simpsons rule
end
Well for one thing, your h definition is wrong. h stands for the step size of each interval you want to estimate. You are unnecessarily dividing by 2. Remove that 2 in your h definition. You also are evaluating your function at the values of n not x. You should probably remove this statement because you end up not using this in the end.
Also, you are summing from 1 to N+1 or from 1 to N-1 for either the odd or even values, This is incorrect. Remember, you are choosing every other value in an odd interval, or even interval, so this should really be looping from 1 to N/2 - 1. To escape figuring out what to multiply i with, just skip this and make your loop go in steps of 2. That's besides the point though.
I would recommend that you don't loop over and add up the values for the odd and even intervals that way. You can easily do that by specifying the odd or even values of x and just applying a sum. I would use the colon operator and specify a step size of 2 to exactly determine which values of x for odd or even you want to apply to the overall sum.
You also are declaring x to be your n-point interval, yet you are overwriting those values in your loops. You actually don't need that x declaration in your code in that case.
As such, here's a modified version of your function with the optimizations I have in mind:
function out = Sc2(func, a, b, N)
h = (b – a) / N; %// Width of each interval
odd = 1 : 2 : n-1; %// Define odd interval
xodd = a + h*odd; %// Create odd x values
even = 2 : 2 : n-2; %// Create even interval
xeven = a + h*even; % Create even x values
%// Return area
out = (h/3)*(func(a) + 4*sum(func(xodd)) + 2*sum(func(xeven))+ func(b));
However, if you want to get your code working, you simply have to change your for loop iteration limits as well as your value of h. You also have to remove some lines of code, and change some variable names. Therefore:
function out = Sc2(func,a,b,N)
% Sc(func,a,b,N)
% This function calculates the integral of func on the interval [a,b]
% using the Composite Simpson's rule with N subintervals.
%// Define width of each segment
h = (b - a) / N; %// Change
%//define for odd and even sums
sum_even = 0;
for i = 2 : 2 : N-2 %// Change
x = a + i*h; %// Change
sum_even = sum_even + func(x);
end
sum_odd = 0;
for i = 1 : 2 : N-1 %// Change
x = a + i*h %// Change
sum_odd = sum_odd + func(x);
end
%// Output area
out = (h / 3)*(func(a) + 2*sum_even + 4*sum_odd + func(b)); %// Change
end