creating hyperbolic tangent function for matrix based on another matrix
9 views (last 30 days)
Show older comments
Kitt
on 22 May 2024
Edited: Abhishek Kumar Singh
on 4 Jun 2024
I'm having issues trying to create a hyperbolic tangent function for a probability distribution. Right now, I have a matrix based on two vectors subtracting, and then based on the sign of an element in that matrix, there's a probability of either 0.9, 0.5, 0.1
So this is what I originally had:
cap=15;
z=linspace(1,cap,cap)';
y=linspace(1,cap,cap)';
f=@(x,y) x-y;
M=f(z,y.');
p=zeros(size(M));
z1=z+1;
z1(z1>cap)=cap;
for j=1:15
for k=1:15
if M(z1(j),k)>0
p(z1(j),k)=0.9;
elseif M(z1(j),k)<0
p(z1(j),k)=0.1;
else
p(z1(j),k)=0.5;
end
end
end
Now what I'm trying to do it make p more of a curve instead of a step. I know you use the tanh, but I'm not sure how to make it with the p matrix being based on the matrix M. I also never want it to be 0, 1, or -1, I always want there to be a possibility that the individual goes to patch 2.
To put it into context of what I'm using this for, an individual has an estimation of quality between two patches (z and y) and matrix M represents the difference in estimation between the two patches. p represents the probability of going to patch 1 (z), so when M is positive (i.e. patch 1 estimation is higher than patch 2) the individual has a higher chance of going to patch 1. Right now it's just stepwise, but I'm wanting to make it a curve where, as the difference in estimation gets bigger, the probability as gets bigger/smaller.
0 Comments
Accepted Answer
Abhishek Kumar Singh
on 28 May 2024
Edited: Abhishek Kumar Singh
on 4 Jun 2024
I roughly understand that you want to create a mathematical model that uses hyperbolic tangent function to smoothly vary the probability of choosing between two options based upon the difference in their estimated qualities.
Since tanh outputs values in the range (-1,1), you can scale and shift its output to lie within the desired probability range, such as (0.1, 0.9). Then you can implement a scaling factor to control the steepness of the curve. Refer to the snippet below:
cap = 15;
z = linspace(1, cap, cap)';
y = linspace(1, cap, cap)';
f = @(x, y) x - y;
M = f(z, y.');
% Scaling factor for tanh to adjust steepness of the transition
k = 0.2; % Example value, adjust this as needed
% Scale and shift tanh output to fit within the desired range (0.1 to 0.9)
% The formula below transforms the output range of tanh to (0.1, 0.9)
p = (0.9 - 0.1) / 2 * tanh(k * M) + (0.9 + 0.1) / 2;
The direct assignment and loop are no longer needed because the entire matrix 'p' is calculated in one step.
I used MATLAB's imagesc function for a color-coded visualization:
figure; % Creates a new figure
imagesc(p); % Plots the matrix with color coding
colorbar; % Shows the color scale
title('Probability Distribution with tanh Function');
xlabel('Patch 2 Quality Estimation');
ylabel('Patch 1 Quality Estimation');
axis square; % Makes the plot square in aspect ratio
Hope it helps!
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices 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!