Scipy.stat.gamma.ppf returning inf value - scipy

I have a list of probabilities containing values between 0 and 1. When I tried to calculate the inverse of that using inverse of gamma function
by
scipy.stats.gamma.ppf but it returns inf value for value 1.
can we get some meaningful results instead of inf?
Thank you for the help
fit_alpha_z, fit_loc_z, fit_beta_z = st.gamma.fit(data1,floc = 0)
data2 = st.gamma.cdf(data1,a = fit_alpha_z, loc = fit_loc_z,scale = fit_beta_z)
st.gamma.ppf(data2,a = fit_alpha_x, loc = fit_loc_x,scale = fit_beta_x)

Related

Output a percentage of matrices of different sizes in Matlab

I am creating a function that takes matrixes of different sizes and converts the number of the total points scored into a percentage. I feel like I am missing a simple step that would complete this.
function weight = calculatepercentage(exams, homework, quizzes);
exams = ([60,60]./60);
homework = ([30,30,30]./30);
quizzes = ([10,10,10,10]./10);
eScore = exams*.60;
hScore = homework*.25;
qScore = quizzes*.15;
disp(eScore + hScore + qScore)
end
Here is a version that is probably doing the job. Your function was overwriting the variables that you hand over to the function.
final_score = calculatepercentage([60], [30 28], [5 5 5]);
function weight = calculatepercentage(exams, homework, quizzes)
% maximum points that can be obtained in each category
exam_maxpoints = 60;
homework_maxpoints = 30;
quizzes_maxpoints = 10;
% calc percentage per category
exams_percent = mean(exams./exam_maxpoints);
homework_percent = mean(homework./homework_maxpoints);
quizzes_percent = mean(quizzes./quizzes_maxpoints);
% calc percentage by weights for each category
eScore = exams_percent*.60;
hScore = homework_percent*.25;
qScore = quizzes_percent*.15;
% get final percentage
weight = eScore + hScore + qScore;
disp(weight)
end

problems with basic arithmetic in a function (Matlab)

I have defined a really basic function in matlab. It takes no input and returns an array of 10 floating point numbers.
The problem I have is that when I run the function to return the array I want I get incorrect values, however when I substitute in a value and simply print out the value from within the function I get the correct answer?!
I've posted samples from the code below:
% Calculate the terms in our expression
FirstTerm = sin(Alpha)*(atan(x+d)-atan(x-d));
SecondTerm = cos(Alpha)*0.5*log(((x+d).^2+h.^2)/((x-d).^2+h.^2));
% Combine and return result
Result = 2 * (FirstTerm - SecondTerm)
FirstTermTemp = sin(Alpha)*(atan(-8+d)-atan(-8-d));
SecondTermTemp = cos(Alpha)*0.5*log(((-8+d).^2+h.^2)/((-8-d).^2+h.^2));
ResultTemp = 2 * (FirstTermTemp - SecondTermTemp)
The array I want to calculate for starts at -8 so the results should match. Does anyone have any idea why they wouldn't?
Cheers
Jack
You have left off a . before your /
% //Calculate the terms in our expression
FirstTerm = sin(Alpha)*(atan(x+d)-atan(x-d));
SecondTerm = cos(Alpha)*0.5*log(((x+d).^2+h.^2)./((x-d).^2+h.^2));
% //Combine and return result
Result = 2 * (FirstTerm - SecondTerm)
Result =
Columns 1 through 7:
0.097944 0.133866 0.208270 0.425797 0.692904 -0.140347 -0.124798
Columns 8 and 9:
-0.095581 -0.076166

Generating random numbers...Faster way?

Using Run & Time on my algorithm I found that is a bit slow on adding standard deviation to integers. First of all I created the large integer matrix:
NumeroCestelli = 5;
lover_bound = 0;
upper_bound = 250;
steps = 10 ;
Alpha = 0.123
livello = [lover_bound:steps:upper_bound];
L = length(livello);
[PianoSperimentale] = combinator(L,NumeroCestelli,'c','r');
for i=1:L
PianoSperimentale(PianoSperimentale==i)=livello(i);
end
then I add standard deviation (sigma = alpha * mu) and error (of a weigher) like this:
%Standard Deviation
NumeroEsperimenti = size(PianoSperimentale,1);
PesoCestelli = randn(NumeroEsperimenti,NumeroCestelli)*Alfa;
PesoCestelli = PesoCestelli.*PianoSperimentale + PianoSperimentale;
random = randn(NumeroEsperimenti,NumeroCestelli);
PesoCestelli(PesoCestelli<0) = random(PesoCestelli<0).*(Alfa.*PianoSperimentale(PesoCestelli<0) + PianoSperimentale(PesoCestelli<0));
%Error
IncertezzaCella = 0.5*10^(-6);
Incertezza = randn(NumeroEsperimenti,NumeroCestelli)*IncertezzaCella;
PesoIncertezza = PesoCestelli.*Incertezza+PesoCestelli;
PesoIncertezza = (PesoIncertezza<0).*(-PesoIncertezza)+PesoIncertezza;
Is there a faster way?
There is not enough information for me to test it, but I bet that eliminating all the duplicate calculations that you do will lead to a speedup. I have tried to remove some of them:
PesoCestelli = randn(NumeroEsperimenti,NumeroCestelli)*Alfa;
PesoCestelli = (1+PesoCestelli).*PianoSperimentale;
random = randn(NumeroEsperimenti,NumeroCestelli);
idx = PesoCestelli<0;
PesoCestelli(idx) = random(idx).*(1+Alfa).*PianoSperimentale(idx);
%Error
IncertezzaCella = 0.5*10^(-6);
Incertezza = randn(NumeroEsperimenti,NumeroCestelli)*IncertezzaCella;
PesoIncertezza = abs((1+PesoCestelli).*Incertezza);
Note that I reduced the last two lines to a single line.
You calculate PesoCestelli<0 a number of times. You could just calculate it once and save teh value. You also create a full set of random numbers, but only use a subset of them where PesoCestelli<0. You might be able to speed things up by only creating the number of random numbers you need.
It is not clear what Alfa is, but if it is a scalar, instead of
Alfa.*PianoSperimentale(PesoCestelli<0) + PianoSperimentale(PesoCestelli<0)
it might be faster to do
(1+Alfa).*PianoSperimentale(PesoCestelli<0)

Applying grouping to values greater than average

I have a vector where I want to group based on the rolling average of the values in my vector. If the values are greater than average than I place them in group 1, if they are less they go in group 2.
What function can be used to give a group number to each to value within my vector based on whether or not its value is greater than the current average.
I don't think there is a function to assign "labels" to array entries.
Assuming v is your input vector, an easy approach would be to simply do:
v(v>mean(v)) %Group 1
v(v<mean(v)) %Group 2
If you intend on doing more with it of course, you could do the following:
avg = mean(v);
flag = zeros(size(v));
for i=1:numel(v)
if(v(i)>avg)
flag(i) = 1;
else
flag(i) = 2;
end
end
flag would contain your requisite grouping. Now if you want the elements of v in group 1, you can simply use:
v(flag==1)
If you want a rolling average though, it depends on how you compute it, but the same basic method should suffice.
There's no simple function that will do that. You'll need something like this:
N = length(vec);
[lo_group hi_group] = deal( NaN(ceil(N/2),1) );
[sum lo_ct hi_ct] = deal(0);
for i=1:N
v = vec(i);
sum = sum + v;
avg = sum/i;
if v>avg
hi_ct = hi_ct + 1;
hi_group(hi_ct) = v;
else
lo_ct = lo_ct + 1;
lo_group(lo_ct) = v;
end
end

Error: Output Argument Not Assigned During Call

Here's my code for a k-nearest neighbors algorithm:
function [preds, distances, indices] = knnfull(HandTrain,HandTest)
nn_value = 10; % how many nearest
inputs = HandTrain(:,2:end);
Y = HandTrain(:,1);
[preds, distances, indices] = knn_alg(inputs, y, HandTest, nn_value);
end
function [preds, D, I] = knn_alg(train_inputs, train_y, test_inputs, nn_value)
num_train_inputs = size(train_inputs,2);
num_train_examples = size(train_inputs,1)
num_test_inputs = size(test_inputs,2);
num_test_examples = size(test_inputs,1)
preds = zeros(size(test_inputs,1),1);
[D,I] = pdist2(train_inputs,test_inputs,'euclidean','Smallest',nn_value);
preds = mode(train_y(I'),2);
end
If you're asking why I have two separate functions, that's a good question. But regardless, I'm getting the errors:
Error in knnkaggle>knn_alg (line 16)
num_train_inputs = size(train_inputs,2);
Output argument "indices" (and maybe others) not assigned during call to
"C:...knn_alg".
Error in knnkaggle (line 10)
[preds, distances, indices] = knn_alg(inputs, y, HandTest, nn_value);
Can't figure out the issue.
It means that there are possible paths through your function which don't assign any value at all to the output argument.