Calculated Field to get weighted sum - tableau-api

I m having trouble making calculated field to get Weighted window Sum.
It has to be in 12 months window size (for example, On 2023-02, it should be the sum of 2022-03 ~ 2023-02).
And, each value of the month is multiplied by index (like 1, 2, 3 ... 12)
So, the formula should look like,
For i = 1 ~ 12, Σ i * Xi (Xi is the value of the month)
I tried window_sum and running_sum ... but it didn't make the result i want...
WINDOW_SUM(SUM([Count]*INDEX(), -11, 0)
Thanks in advance!

Related

How can I loop through dates and store weight values in a matrix? [MATLAB]

Hi so I am new to MATLAB. I am trying to find the means of weight values for each month over five years and put these values into a matrix that will be 5x12 in size.
I am attempting to accomplish this with a loop but I'm having a little trouble, if anyone can push me towards the right direction that would be awesome, thanks. What I have so far is this:
weight_data = (10 weights per month for 10 years, 1200 weights total)
year = (years 2000-2010) %year 1-10 corresponds with the 1200 weights)
month = (months 1-12) %weights for all months (120 months, correspond with 1200 weights)
weight_vec = zeros([12, 5]);
for n = year(1:5)
weights = weight_data(n);
mean_weights = mean(weights);
end
This only gives me one number though, I assume the mean from the 5 years I'm trying to loop through. I also know I need to incorporate the months somehow but I'm just confused on how to do this.
Being your weight_data matrix (10 weights x 12 months x 10 years), the weight of the i-th year are located in weight_data(:,:,i).
w=weight_data(:,:,i)
w is a (10 x 12) which contains the 10 weights values of the 12 months.
You can use mean to compute the mean value of the weights of each month:
w=mean(weight_data(:,:,i))
Therefore you can setup a loop over the years:
for i=1:1:5
mean_weights(:,i)=mean(weight_data(:,:,i))'
end
(the ' in mean(weight_data(:,:,i))' is required to transpose the output of mean from a row-array to a column-array so that it fits in your output matrix which is (12 x 5)
Hope this helps,
Qapla'

calculate the x-axis average bar

I have a simple problem.
I have this code:
test = [30,40,60,30,20,10,5,5,3]
bar(test)
The bar represent the child number by family. For example, 30 family have 0 child and 10 family have 5 children.
I want to obtain the child average by family and of course I can't just use mean(test).
We can easily convert your data in a form where each entry of a vector represents an observation of a family with a value for the number of children. This way we generate a vector with length of the total number of families. Each entry is a number representing the amount of children of the observed family.
To do this we can use arrayfun and repmat to repeat the entries in cnt as many times as there are corresponding families in test. Since the output of arrayfun is a cell-array, we need to use cell2mat to convert it back to a 'normal' matrix. After that, obs is the above mentioned vector. Now we can simply use mean or var to calculate what you need.
Here is an example:
test = [30,40,60,30,20,10, 5 ,5, 3] % families
cnt = [ 0, 1, 2, 3, 4, 5, 6, 7, 8] % children per family in test
obs = arrayfun(#(s,c)repmat(c,1,s),test,cnt,'UniformOutput',false);
obs = cell2mat(obs);
mean(obs) % mean
var(obs) % variance
This is the result:
ans =
2.3103
ans =
3.2349
Note: I assume that there are 30 families with 0 children (as your text says) and not 30 families with one child (as your bar-plot says). Just adjust cnt to match your needs.
To get test and cnt back from obs, you can use hist like this:
[test,cnt] = hist(obs,unique(obs));
For the creation of your bar-plot, use bar with two arguments. This way you have the correct x-values (in this case cnt).
bar(cnt,test)
xlabel('Children per family')
ylabel('Number of Families')

How to make a plot

Write a script that asks for an integer (n) and then computes the following based on the value of the integer: While the value of n is greater than 1, replace the integer with half of its value (n/2) if the integer is even. Otherwise, replace the integer with three times its value, plus 1 (3*n + 1).
Make provision to count the number of values in (or the length of) the sequence that results.
Example calculation: If n = 10, the sequence of integers is 5, 16, 8, 4, 2, 1 and so the length is 6.
((Make a plot of the length of the sequence that occurs as a function of the integers from 2 to 30. For example, when n = 10, the length is 6 while for n = 15, the length is 17. Is there any pattern? Is there any integer for which the sequence does not terminate?))
Hi,
how I can plot this if I have a function(calculate) and the output is the length
for i=2:30
p = calculate(i)
plot(i,p)
end
is that correct ??
You should really just run it and see, but you have options to make it work:
Store each p generated in the loop, and plot after the loop.
Plot each point (not connected with lines) inside the loop with plot and hold on.
This is fundamental. Read the plot documentation, please.

Daily values to Monthly Means for Multiple Years Matlab

I have observed daily data that I need to compare to generated Monthly data so I need to get a mean of each month over the thirty year period.
My observed data set is currently in 365x31 with rows being each day (no leap years!) and the extra column being the month number (1-12).
the problem I am having is that I can only seem to get a script to get the mean of all years. ie. I cannot figure how to get the script to do it for each column separately. Example of the data is below:
1 12 14
1 -15 10
2 13 3
2 2 37
...all the way to 12 for 365 rows
SO: to recap, I need to get the mean of [12; -15; 13; 2] then [14; 10; 3; 37] and so on.
I have been trying to use the unique() function to loop through which works for getting the number rows to average but incorrect means. Now I need it to do each month(28-31 rows) and column individually. Result should be a 12x30 matrix. I feel like I am missing something SIMPLE. Code:
u = unique(m); %get unique values of m (months) ie) 1-12
for i=1:length(u)
month(i) = mean(obatm(u(i), (2:31)); % the average for each month of each year
end
Appreciate any ideas! Thanks!
You can simply filter the rows for each month and then apply mean, like so:
month = zeros(12, size(obatm, 2));
for k = 1:12
month(k, :) = mean(obatm(obatm(:, 1) == k, :));
end
EDIT:
If you want something fancy, you can also do this:
cols = size(obatm, 2) - 1;
subs = bsxfun(#plus, obatm(:, 1), (0:12:12 * (cols - 1)));
vals = obatm(:, 2:end);
month = reshape(accumarray(subs(:), vals(:), [12 * cols, 1], #mean), 12, cols)
Look, Ma, no loops!

representing the day and some parameters of a month

Could you please help me for this matter?
I have 3 matrices, P (Power), T (Temperature) and H (Humidity)
every matrix has 31 columns (days) and there are 24 rows for every column
which are the data for the March of year 2000, i.e.
for example, the matrix P has 31 columns where every column represents
a day data for Power through 24 hours and the same idea is for T and H
I tried to write a MATLAB program that accomplish my goal but
It gave me errors.
My aim is:
In the MATLAB command window, the program should ask the user the following phrase:
Please enter the day number of March, 2000 from 1 to 31:
And I know it is as follows:
Name=input (Please enter the day number of March, 2000 from 1 to 31:)
Then, when, for example, number 5 is entered, the result shown is a matrix containing the following:
1st column: The day name or it can be represented by numbers
2nd column: simple numbers from 1 to 24 representing the hours for that day
3rd column: the 24 points of P of that day extracted from the original P
(the column number 5 of the original P)
4th column: the 24 points of T of that day extracted from the original T
(the column number 5 of the original T)
5th column: the 24 points of H of that day extracted from the original H
(the column number 5 of the original H)
Any help will be highly appreciated,
Regards
Here is what you ask for:
% some sample data
P = rand(24,31);
T = rand(24,31);
H = rand(24,31);
% input day number
daynum=input('Please enter the day number of March, 2000 from 1 to 31: ');
[r, c] = size(P);
% generate output matrix
OutputMatrix = zeros(r,5);
OutputMatrix(:,1) = repmat(weekday(datenum(2000,3,daynum)),r,1);
OutputMatrix(:,2) = 1:r;
OutputMatrix(:,3) = P(:,daynum);
OutputMatrix(:,4) = T(:,daynum);
OutputMatrix(:,5) = H(:,daynum);
disp(OutputMatrix)
The matrix can be generated in a one line, but this way is clearer.
Is it always for March 2000? :) Where do you get this information from?