Maximum likelihood estimates MATLAB - matlab

Hi i would like to make a MLE estimate of my parameters using the built in functions in matlab. Here is what matlab says:
phat = mle(data,'distribution',dist)
I don't know how to use the vector "data". Suppouse I have 340 observations giving 0 , 120 observations at 2 , and 90 observations at 10
so how should the vector look like? [340,0,120,0,0,0,0,0,0,0,90] ? i doubt it. I just want to know the "structure" of the vector

Seems that mle() function can only handle scalar (1-D) data.
So if you want to estimate the class conditional distribution Pr[X = x|Y = 0], Pr[X = x|Y = 2] and Pr[X = x|Y = 10], then you need to split the sample data into three groups and call mle() three times. And for each call, you put all data points into one vector as the first argument.

Related

How can I detect the minimum and maximum values every 50 rows

I'm trying to detect peak values in MATLAB. I'm trying to use the findpeaks function. The problem is that my data consists of 4200 rows and I just want to detect the minimum and maximum point in every 50 rows.After I'll use this code for real time accelerometer data.
This is my code:
[peaks,peaklocations] = findpeaks( filteredX, 'minpeakdistance', 50 );
plot( x, filteredX, x( peaklocations ), peaks, 'or' )
So you want to first reshape your vector into 50 sample rows and then compute the peaks for each row.
A = randn(4200,1);
B = reshape (A,[50,size(A,1)/50]); %//which gives B the structure of 50*84 Matrix
pks=zeros(50,size(A,1)/50); %//pre-define and set to zero/NaN for stability
pklocations = zeros(50,size(A,1)/50); %//pre-define and set to zero/NaN for stability
for i = 1: size(A,1)/50
[pks(1:size(findpeaks(B(:,i)),1),i),pklocations(1:size(findpeaks(B(:,i)),1),i)] = findpeaks(B(:,i)); %//this gives you your peak, you can alter the parameters of the findpeaks function.
end
This generates 2 matrices, pklocations and pks for each of your segments. The downside ofc is that since you do not know how many peaks you will get for each segment and your matrix must have the same length of each column, so I padded it with zero, you can pad it with NaN if you want.
EDIT, since the OP is looking for only 1 maximum and 1 minimum for each 50 samples, this can easily be satisfied by the min/max function in MATLAB.
A = randn(4200,1);
B = reshape (A,[50,size(A,1)/50]); %//which gives B the structure of 50*84 Matrix
[pks,pklocations] = max(B);
[trghs,trghlocations] = min(B);
I guess alternatively, you could do a max(pks), but it is simply making it complicated.

how to Average for every 10 values of 8200x1 array in matlab

i have data in .txt format and successfully imported data to a variable V which is 8200x1 matrix. Now I need to get average for every 10 values. Can any one help me with the code?
I think you are looking for colfilt. You can take average every 10 values as: [1,...,10] then [2,...,11] then [3,...,13] etc. as follows:
a=randi(10,[8200 1]);
b=colfilt(a,[10 1],'sliding',#(x) mean(x))
If you want to average over distinct blocks of 10 values as: [1,...,10],[11,...,20] etc., then just replace 'sliding' with 'distinct'.
You can do the same operation with blockproc and nlfilter but colfilt executes faster as stated in Mathworks colfilt documentation.
If you want the average of each separate block of size 10: reshape into a 10-row matrix and then average each column:
n = 10;
result = mean(reshape(V, n, []), 1);
If you want the average on a sliding window of length 10: use convolution:
result = conv(V, ones(1,n)/n, 'valid');

determine the frequency of a number if a simulation

I have the following function:
I have to generate 2000 random numbers from this function and then make a histogram.
then I have to determine how many of them is greater that 2 with P(X>2).
this is my function:
%function [ output_args ] = Weibullverdeling( X )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
for i=1:2000
% x= rand*1000;
%x=ceil(x);
x=i;
Y(i) = 3*(log(x))^(6/5);
X(i)=x;
end
plot(X,Y)
and it gives me the following image:
how can I possibly make it to tell me how many values Do i Have more than 2?
Very simple:
>> Y_greater_than_2 = Y(Y>2);
>> size(Y_greater_than_2)
ans =
1 1998
So that's 1998 values out of 2000 that are greater than 2.
EDIT
If you want to find the values between two other values, say between 1 and 4, you need to do something like:
>> Y_between = Y(Y>=1 & Y<=4);
>> size(Y_between)
ans =
1 2
This is what I think:
for i=1:2000
x=rand(1);
Y(i) = 3*(log(x))^(6/5);
X(i)=x;
end
plot(X,Y)
U is a uniform random variable from which you can get the X. So you need to use rand function in MATLAB.
After which you implement:
size(Y(Y>2),2);
You can implement the code directly (here k is your root, n is number of data points, y is the highest number of distribution, x is smallest number of distribution and lambda the lambda in your equation):
X=(log(x+rand(1,n).*(y-x)).*lambda).^(1/k);
result=numel(X(X>2));
Lets split it and explain it detailed:
You want the k-th root of a number:
number.^(1/k)
you want the natural logarithmic of a number:
log(number)
you want to multiply sth.:
numberA.*numberB
you want to get lets say 1000 random numbers between x and y:
(x+rand(1,1000).*(y-x))
you want to combine all of that:
x= lower_bound;
y= upper_bound;
n= No_Of_data;
lambda=wavelength; %my guess
k= No_of_the_root;
X=(log(x+rand(1,n).*(y-x)).*lambda).^(1/k);
So you just have to insert your x,y,n,lambda and k
and then check
bigger_2 = X(X>2);
which would return only the values bigger than 2 and if you want the number of elements bigger than 2
No_bigger_2=numel(bigger_2);
I'm going to go with the assumption that what you've presented is supposed to be a random variate generation algorithm based on inversion, and that you want real-valued (not complex) solutions so you've omitted a negative sign on the logarithm. If those assumptions are correct, there's no need to simulate to get your answer.
Under the stated assumptions, your formula is the inverse of the complementary cumulative distribution function (CCDF). It's complementary because smaller values of U give larger values of X, and vice-versa. Solve the (corrected) formula for U. Using the values from your Matlab implementation:
X = 3 * (-log(U))^(6/5)
X / 3 = (-log(U))^(6/5)
-log(U) = (X / 3)^(5/6)
U = exp(-((X / 3)^(5/6)))
Since this is the CCDF, plugging in a value for X gives the probability (or proportion) of outcomes greater than X. Solving for X=2 yields 0.49, i.e., 49% of your outcomes should be greater than 2.
Make suitable adjustments if lambda is inside the radical, but the algebra leading to solution is similar. Unless I messed up my arithmetic, the proportion would then be 55.22%.
If you still are required to simulate this, knowing the analytical answer should help you confirm the correctness of your simulation.

Constraining the Genetic Algorithm in MATLAB so that the solutions are all between 2 and 20, and are integers

How can I constrain the Genetic Algorithm in MATLAB so that the solutions are all between 2 and 20, and are integers?
I am using the function:
x = ga(#myFitnessfcn,nvars,A,b,[],[],LB,UB,nonlcon,IntCon)
Where myFitnessfcn takes two inputs and returns a scalar output.
However myFitnessfcn can only take integer inputs that are between 2 and 20.
How would I implement this?
My best attempt so far is:
A = [1, 1; -1, -1]
b = [20; -2]
IntCon = [1, 2]
LB = 2
UB = 20
nonlcon = []
But this just tried to evaluate myFitnessfcn with [4, 1872]
Here is the MATLAB page on ga
see InitialPopulation and PopInitRange in options of gaoptimset.
you can initialize a sequence of integers in the range 2 to 20 as your initial population.
then you might use IntCon.
OR
as a first statement in your myFitnessfcn
model=round(model);
if model > 20 || model < 2
fitness=1e20;
else
% evaluate the original fitness function
end
this way model parameters fed to your fitness function are always integers. and since any model with values less than 2 or more than 20 will be assigned really bad value of fitness (1e20 for example), this is essentially what Simon said, such models will be automatically removed from the population after 2-3 generations.
I am sorry I don't know Matlab, but in general in a GA, you can set a low or 0 fitness when the solution is outside that range, and some higher number when it is within the range.

Double interpolation in Matlab

I want to ask you somehting about interpolation in Matlab. I want to know if it's possible to do two different interpolations in the same line. I mean, for example, at the beginning do a linear interpolation and at the middle, approximately, do another kind of interpolation, like spline.
The main problem is that I've done a linear interpolation and it's perfect at the beginning but after some point, I think it would be better another type. If this is possible, how can I code where I want to change it? I've tried to check the documentation about Matlab and I couldn't find anything about modifying interpolations.
Thanks a lot in advance and greetings,
Allow me to elaborate on the comment I made on your post.
If you want to create an output array from an input array using 2 different functions with a split, you can use array index ranges like in the following code example
x = randn(20,1); %//your input data - 20 random numbers for demonstration
threshold = 5; %//index where you want the change of algorithm
y = zeros(size(x)); %//output array of zeros the same size as input
y(1:threshold) = fun1(x(1:threshold));
y(1+threshold:end) = fun2(x(1+threshold:end));
You could skip the preallocation of y if you like and just concatenate the additional data on to the end of the output. This is particularly useful if a function returns a different number of output elements relative to the number of input elements. The syntax for this is shown below.
y = fun1(x(1:threshold));
y = [y; fun2(x(1+threshold:end))];
EDIT:
In response to your post below, here is a full example . . .
clc; close all
x = -5:5; %//your x-range
y = [1 1 0 -1 -1 0 1 1 1 1 1]; %//the function to interpolate
t = -5:.01:5; %//sampling interval for output
xIdx = 5; %//the index on the x-axis where you want the split to occur
tIdx = floor(numel(t)/numel(x)*xIdx);%//need to calculate as it is at a different sample rate
out = pchip(x(1:xIdx),y(1:xIdx),t(1:tIdx));
out = [out spline(x((1+xIdx):end),y((1+xIdx):end),t((1+tIdx):end))];
%//PLOTTING
plot(x,y,'o',t,out,'-',[x(xIdx) x(xIdx)], [-1.5 1.5], '-')
legend('data','output','split',4);
ylim ([-1.5 1.5])
Which will give . . .