Creat Log-normal Random Variable in MATLAB [closed] - matlab

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have trouble with the probability density function (p.d.f) of a log-normal distribution. I really need your help. As defined by wikipedia:
http://en.wikipedia.org/wiki/Log-normal_distribution#Probability_density_function
The probability density function of a log-normal distribution is:
My problem is, how to define x variable in MATLAB? Thanks for your help!

If you have the Stats toolbox, you can just use lognpdf:
y = lognpdf(x,mu,sigma);
Though this is a very simple function - fully vectorized, it's effectively just:
y = exp(-0.5*((log(x)-mu)./sigma).^2)./(x.*sqrt(2*pi).*sigma);
But you may want to check that x > 0 and sig > 0. To create this plot on the Wikipedia article that you cited, you can do:
mu = 0;
sigma = [1;0.5;0.25];
x = 0:0.01:3;
y = lognpdf([x;x;x],mu,sigma(:,ones(1,length(x))));
figure; plot(x,y);
When your question asks about defining x, maybe you're actually looking for log-normally distributed random variables, i.e., you want to sample randomly from the log-normal PDF/distribution? In that case you can use lognrnd:
r = lognrnd(mu,sigma);

I'm confused, like you can do this in a one-liner,
fun = #(x,mu,sigma) (1./(x*sigma*sqrt(2*pi))).*exp( -(power((log(x)-mu),2))/(2*power(sigma,2)))
x is any value that satisfies x > 0, the pdf tells you via Wikipedia
In probability theory, a probability density function (pdf), or
density of a continuous random variable, is a function that describes
the relative likelihood for this random variable to take on a given
value.
So any value x given to the log-normal pdf tells you tel relative likelihood that a random variable could be that value.
Consider this toy example:
mu = 1;
sigma = 10;
x = logspace(-2,0,10);
plot( x, fun(x,1,10) )
From this plot as x gets closer to zero, it's relative likelihood of actually taking on that value increases. DISCLAIMER I just threw that function together, it needs to be checked for accuracy, the preceding was for illustration only.

Related

How to put trigonometric numbers as a power in matlab [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to make a graph with this function in matlab in space [-2,2]. Because I am new (really new actually) in matlab, I am struggling, especially with the first part of the function, the one with the "e":
So any help about the whole thing will be very useful.
Let's cover all the points in no particular order:
The e is known as Euler's constant, where e ~ 2.71828...
Something like ex, i.e. raising e to the power of x is known as the exponential function. While you could in theory compute e and then use the power operator (^ in matlab) to raise e to that power accordingly, this is in fact a less precise way of calculating the exponential function, and therefore matlab provides the exp function for that purpose. If you pass an array [x1, x2,...] to exp, it will perform this function "elementwise", and return [ex1, ex2,...] appropriately.
The sin function, similarly, if given an array of numbers, will calculate the sin for each of those numbers, and return an array of the same shape as its input.
You could use fplot with an anonymous function as Neo suggested, but I find that beginners find that confusing. So instead I would suggest you create an array of values between [-2,2], and obtain the value of y for each of them, which in matlab can be done as a single operation because it's good at working with arrays and performing such 'vectorized' operations:
x = [-2:0.1:2]; % Create an array of values from -2 to 2, with a step of 0.1
% Note: the ';' at the end suppresses output; if you want to
% see the contents of your array, remove it at the end.
Now that you have your array x you can perform operations on it. By convention, matlab uses "dot-operators" to denote "elementwise" operations, as opposed to 'undotted' ones denoting primarily "matrix" operations. Therefore to raise all elements of the array x to the power of 6, you would do x .^ 6
With that in mind, you can now calculate your f(x) for each point in the array x, by using elementwise operations:
y = exp( sin(x).^3 ) + x.^6 - 2*(x.^4) - x.^3 - 1;
The result is an array y of the same size as x.
You can now plot this using the plot command, which takes two arrays of the same size and plots all points in the first array against their equivalent points in the second array, as (x,y) pairs:
plot( x, y );
Output:
As you can see, matlab 'connects' the points by default. If you wanted to see just the individual points of your array instead, you can specify this as the third argument:
plot(x, y, 'o');
Type help plot at the matlab terminal to see more options for the plot command.
PS: The above plots were made in octave rather than in matlab, because I don't have matlab at home. Your own plots may look slightly different.
For a simple one-liner you could use:
fplot(#(x) exp(sin(x).^3) + x.^6 - 2*x.^4 - x.^3 - 1, [-2 2]);
See fplot.

Octave beginner help. Creating a function that takes a component matrix as a parameter and returns a matrix? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to create a function that takes a component matrix as a parameter and returns a matrix?
Apparently this function should normalise my data?
There are other instructions along with this step in my project such as:
Take the matrix and calculate the mean value along a certain column.
Calculate the difference between the measurement and this mean.
Subtract this difference from each measurement.
Return corrected matrix to the script.
Place corrected matrix in a variable within the script.
(I don't know if this is what the function is supposed to do or anything I'm completely lost and any help would be appreciated thanks!)
This is probably homework but I'll help you get started.
To create a function which takes a matrix and return a matrix:
function m_out = my_function(m_in)
%insert calculations here
end
To find the 2-norm of a matrix (which is the largest singular value):
the_norm = norm(my_matrix); % returns a scalar, 2-norm of matrix
To find the mean of a vector:
the_mean = mean(my_vector); % returns a scalar, mean of the vector
To access a specific column of a matrix:
my_col = my_matrix(:, col_number); % my_col is a vector
To access a specific row of a matrix:
my_row = my_matrix(row_num, :); % my_row is a vector
To subtract a scalar (single number) from a matrix:
new_matrix = old_matrix - single_number; % returns a matrix
To store a matrix into a variable (example):
my_matrix = [1,2,3;4,5,6;7,8,9];
Give it a try creating a function which puts this all together.

How to measure the second derivatives of an image [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I am at very first step in MATLAB programming and when I read an article about image processing, I see in most of them it has written that the first and/or second derivatives should be estimated.
How I can measure the second derivatives (Gxx,Gxy,Gyy) over the gradient?
Instead of applying successive differences, you could apply the second derivative kernel in each dimension:
Gx = [1 -2 1]; Gy = Gx'; %' y kernel is column vector
img = double(imread('cameraman.tif'));
Dxx = conv2(img,Gx,'same');
Dyy = conv2(img,Gy,'same');
If you were after a non-directional second derivative, you should use the Laplacian. A common kernel is:
L = [0 1 0;
1 -4 1;
0 1 0;] % fspecial('laplacian',alpha=0)
D2 = conv2(img,L,'same');
As in the comment above, you can use fspecial to get variations on the kernel that capture diagonal differences via the alpha parameter. Or you can use del2:
D2 = del2(img);
Look into imgradient from the Image Processing Toolbox. Two applications of that should give you what you want.
Something like this:
im = imread('cameraman.tif');
[Gx,Gy] = imgradientxy(im);
[Gxx,Gxy] = imgradientxy(Gx);
[Gyy,Gyx] = imgradientxy(Gy);
This uses a Sobel filter to compute derivatives. You can also use Prewitt, central differences or intermediate differences by passing this as a string to imgradientxy.
Hope that helps.

AX=0 and introducing error matrix [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm having a matrix of 10 rows and 5 columns, thatr I have called A
I would like to make AX=0 and determine X which contains 5 unknown parameters.
What I did is
null(A)
But it seems that it's considering what I did as a linear algebra.
I would like to introduce another matrix which contains a matrix of errors . which would give me a priori:
AX + E = 0
because the results are not accurate, indeed, but still, I would like to find the closest parameters of the unknown vector (X) and get an error matrix.
Can you help me with that?
You can do eigen value decomposition of A^T.
[Q, D] = eigen(A^T)
And take the eigen vectors that corresponds to the smallest eigen value.
That is take y vectors from the right hand side of the Q matrix.
y is the column number of X.
And then E = -AX
====edit=====
OK. actually you want to minimize E. But since E is a matrix I assume you want to minimize the sum of squares of all the elements in E.
That is:
||E||_F^2 = ||-AX||_F^2 = trace(X^T(A^TA)X)
You can do eigen decomposition of A^TA
[Q, D] = eigen(A^TA)
QA^TA = DQ => D = QA^TAQ^T
To make it smaller, X should be the columns of Qs that corresponds to the smallest eigen value in the diagonal of D.
If there is a eigen value equals to 0, then AX can be 0 and E=0

Random number in a certain interval with a exponential distribution [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'd like to generate a random number in a certain interval using an exponential distribution. My problem is that if I use exprnd I can't control the interval, I can only give a mean value, but that doesn't suit my needs.
Is there another function or is there some trick that I have to use?
Does this help? (or have I misunderstood the problem?)
%#Set the parameters
T = 2000; %#Number of observations to simulate
Mu = 0.5; %#Exponential distribution parameter
LB = 0; %#Lower bound on exponential distribution
UB = 1; %#Upper bound on exponential distribution
%#Validate the parameters
if LB < 0 || UB < 0; error('Bounds must be non-negative'); end
if Mu <= 0; error('Mu must be positive'); end
%#Determine LB and UB in terms of cumulative probabilities
LBProb = expcdf(LB, Mu);
UBProb = expcdf(UB, Mu);
%#Simulate uniform draws from the interval LBProb to UBProb
Draw = LBProb + (UBProb - LBProb) .* rand(T, 1);
%#Convert the uniform draws to exponential draws using the inverse cdf
X = expinv(Draw, Mu);
Exponential distribution is supported on [0,+\infty). You may want to remap it on [0,1) using some measurable invertible map f, so that Y = f(X) is a random variable supported on [0,1).
Problem: you have to build such an f.
My suggestion is
f(x) = 2/pi * arctan(x).
The function arctan maps (-\infty,\infty) to (-pi/2,pi/2). Because you are considering just positive samples (because your X goes exponentially) you will obtain samples in [0,pi/2); thus, you have to rescale by 2/pi. Moreover, because the MacLaurin expansion of arctan is x+o(x), you have samples that go exactly exponentially close enough to the origin.
Now, if you sample from whatever exponential (i.e. using possibly any value of \lambda - preferably small) and you evaluate f on the sample, you get samples that concentrate as you like (i.e. close to 0 and nearly exponential).
Here's a suggestion:
Sample from the exponential distribution with lambda=1, and reject any number outside of your intended interval. If your interval is [0,1], you have a probability of ~0.63 to get a number in that interval. That means a 99% probability of getting a "good" number after 10 samples.
Another possibility is to choose a high enough number n, such that the probability of sampling something over n is sufficiently small. For lambda = 1, n=1000 would suffice. Then you just sample from the exponential and transform it to your random sample by a+(b-a)*(sample/n)