how to define the factorial if data set is non-integer
Show older comments
dear all
I have the data set as attached, and the formula as picture below.
I want to calculat the probabilty certain point using the formula as below.
My problem is how to define my factorial, while my data is non-integer.
clc
clear all
close all
%% TO READ DATA SET,
sz = [128 128 64];
fname = 'jaszak18092023tc10n1bckg10mbq10jutaf.a00';
fid = fopen(fname);
data = fread(fid,'*float'); % assuming uint
fclose(fid);
% this is blindly devectorized
% may still be transposed, but i can't tell due to symmetry
% note that data is unit-scale single-precision float
data = reshape(data,sz);
%% TO GET THE MEAN DATA SET
lamda = mean(mean(mean(data)));
% lamda = 0.0223; % lamda REPRESENT mean data set, I got from my data set
e = 2.718; % Euler number (exp)
k = 0.439173; % LET SAY my point to calculate the P (probability) (THE VALUE K FROM MY DATA SET, THE COORDINATE IS , [65 64 1])
j=factorial(k);
P = (lamda^k * exp(-lamda)/factorial(k));
Error using factorial
N must be an array of real non-negative integers.

ANYONE CAN HELP ME?
11 Comments
Bruno Luong
on 20 Sep 2023
Edited: Bruno Luong
on 20 Sep 2023
The Poisson probability is defined only for integer non negative number k, (it's generate a variable usually interpret as the number of some specific events at frequency lambda occurs in a fixed unit time interval.
If someone tells you "there is 0.439173 answer you your question between 9AM and 10AM" does ot make sense tou you?
Dyuman Joshi
on 20 Sep 2023
Note -
Use
lambda = mean(data,'all');
e = exp(1);
instead of
lamda = mean(mean(mean(data)));
e = 2.718;
mohd akmal masud
on 20 Sep 2023
Dyuman Joshi
on 20 Sep 2023
"If use function gamma, still the k value is non-integer"
I don't understand what you are saying.
k is the input to a function which does not modify k, so if k is non-integer, it's not because of the funciton being called.
k = 3; % an integer
kexc = gamma(k+1)
k % it's still the same
k = 3.1; % a non-integer
kexc = gamma(k+1)
k % it's still the same
dpb
on 20 Sep 2023
"LET SAY my point to calculate the P (probability)..."
The probability of what? As @Bruno Luong notes, the Poisson distribution is only defined for integer-valued k so it can't be appropriate for whatever it is you're trying to do.
We would have to know what the data represent and what it is that is a probablilty to compute in order to have a chance of selecting some continuous distribution that might be suitable. As is, there is no solution to the Q?
mohd akmal masud
on 20 Sep 2023
dpb
on 20 Sep 2023
Well, a number of photon(?) counts can't be non-integer, so that doesn't make any sense.
And, you've not told us what it is a probability of that you're trying to calculate...
Remember, we have zero context of what you're trying to do; you know all the background info, we don't know anything except what you tell us.
Don't be so cryptic; expand in full on the problem..."help us help you!"
Bruno Luong
on 20 Sep 2023
Edited: Bruno Luong
on 21 Sep 2023
The data is 128x128x64 show number between min=0 and max=0.4522. The mean is 0.0223414.
It looks like the data is from some photo detector that is proportional to the energy, thus number of photons, but not directly the number of photons that hits the pixel (which as @dpb remark must be whole number).
mohd akmal masud
on 21 Sep 2023
Dyuman Joshi
on 16 Oct 2023
Both the answers provided here do not understand nor acknowledge the problem with what OP is trying to do.
The comments above by Bruno and dpb have pointed out what the mistake is.
Accepted Answer
More Answers (1)
Francis Mike John Camogao
on 14 Oct 2023
Edited: Francis Mike John Camogao
on 14 Oct 2023
The error you are encountering is because the `factorial` function expects the input `k` to be a non-negative integer, but you are providing it with a floating-point value. To calculate the factorial of a floating-point number, you need to modify the code. You can use the `gamma` function, which calculates the factorial of a real number (including floating-point values). Here's how you can modify your code to fix the error:
clc
clear all
close all
%% TO READ DATA SET,
sz = [128 128 64];
fname = 'jaszak18092023tc10n1bckg10mbq10jutaf.a00';
fid = fopen(fname);
data = fread(fid,'*float'); % assuming uint
fclose(fid);
% this is blindly devectorized
% may still be transposed, but I can't tell due to symmetry
% note that data is unit-scale single-precision float
data = reshape(data, sz);
%% TO GET THE MEAN DATA SET
lamda = mean(mean(mean(data)));
% lamda = 0.0223; % lamda REPRESENT mean data set, I got from my data set
e = 2.718; % Euler number (exp)
k = 0.439173; % LET SAY my point to calculate the P (probability) (THE VALUE K FROM MY DATA SET, THE COORDINATE IS , [65 64 1])
% Calculate factorial using gamma function
j = gamma(k + 1);
P = (lamda^k * exp(-lamda) / j);
% Display the result
fprintf('P = %f\n', P);
I also got and implemented the ideas atop my response.
In this code, I replaced the `factorial` function with the `gamma` function to calculate the factorial of `k`, which is a floating-point number. This should resolve the error you were encountering. I as well modifed, added some lines.
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!