How is finding the centroid different from finding the mean?

Question: When performing hierarchical clustering, one can use many metrics to measure the distance between clusters. Two such metrics imply calculation of the centroids and means of data points in the clusters.
What is the difference between the mean and the centroid? Aren't these the same point in cluster?
Answer: As far as I know, the "mean" of a cluster and the centroid of a single cluster are the same thing, though the term "centroid" might be a little more precise than "mean" when dealing with multivariate data.
To find the centroid, one computes the (arithmetic) mean of the points' positions separately for each dimension. For example, if you had points at:

(-1, 10, 3),
(0, 5, 2), and
(1, 20, 10),

then the centroid would be located at ((-1+0+1)/3, (10+5+20)/3, (3+2+10)/3), which simplifies (0, 11 2/3, 5). (NB: The centroid does not have to be--and rarely is---one of the original data points)
The centroid is also sometimes called the center of mass or barycenter, based on its physical interpretation (it's the center of mass of an object defined by the points). Like the mean, the centroid's location minimizes the sum-squared distance from the other points.
A related idea is the medoid, which is the data point that is "least dissimilar" from all of the other data points. Unlike the centroid, the medoid has to be one of the original points. You may also be interested in the geometric median which is analgous to the median, but for multivariate data. These are both different from the centroid.

However, as Gabe points out in his answer, there is a difference between the "centroid distance" and the "average distance" when you're comparing clusters. The centroid distance between cluster $A$ and $B$ is simply the distance between $\text{centroid}(A)$ and $\text{centroid}(B)$. The average distance is calculated by finding the average pairwise distance between the points in each cluster. In other words, for every point $a_i$ in cluster $A$, you calculate $\text{dist}(a_i, b_1)$, $\text{dist}(a_i, b_2)$ , ... $\text{dist}(a_i, b_n)$ and average them all together.
Comment: Under what conditions the centroid and the medoid be identical? And also why the centroid is a good representative of a set of points?
Comment: @dkr, You might want to ask this as a new question to get more (and more in-depth) responses. That said, the difference boils down to two things: 1) the thing to be minimized (squared distance/L2 norm for the centroid, absolute distance/L1 norm for mediod) and 2) Whether the output can be any point (centroid) or must be in the data set (mediod). You can imagine cases where they'll be the same, but in general, they will not.

The centroid is "good" for the same reasons the mean is (smallest sum-squared distance to the points) and also has similar drawbacks (e.g., not robust against outliers).
Answer: The above answer may be incorrect see this video: https://www.youtube.com/watch?v=VMyXc3SiEqs It seems that average adds up all the combinations of distances between the elements of cluster 1 and cluster 2 - that is n^2 distances added together and then divides by n^2 to the average.
Centroid method first computes the average of each cluster within itself. Then it calculates one distance between those average points.
Comment: Hi Gabe! I think you're talking about [this part](https://youtu.be/VMyXc3SiEqs?t=395) of the video?

As far as I know, the centroid and mean of a single cluster are the same thing but, as you pointed out, the centroid distance and average distance *between* two clusters are different measures. I thought the OP was asking about the former, but I just edited in a bit about the latter too.

Thanks for pointing that out (+1) and welcome to Cross Validated!
Answer: In general, the mean (to be precise, the average) distance (between all pairs of points) is larger than the distance between the centroids of the clusters. So usually, they are different. Here is a mathematical proof:
Let $x_1,\dots ,x_n\in \mathbb{R}^d$ and $\{C_1,C_2\}$ a partition of $\{1,\dots,n\}$. Let $d$ be a metric in $\mathbb{R}^d$, positive homogeneous (for instance, the euclidean distance)
Define $\alpha := d(\frac{1}{|C_1|}\sum_{i\in C_1}x_i,\frac{1}{|C_2|}\sum_{j\in C_2}x_j)$ and $\beta := \frac{1}{|C_1|}\frac{1}{|C_2|}\sum_{i\in C_1}\sum_{j\in C_2} d(x_i,x_j)$
Claim: $\alpha \leq \beta$
Proof:
The function $\phi:= d(\cdot ,\frac{1}{|C_2|}\sum_{j\in C_2}x_j)$ is convex (this follows by the triangle inequality of the metric + positive homogeneity). Therefore, by Jensen's Inequality
$$ \alpha = \phi(\frac{1}{|C_1|}\sum_{i\in C_1}x_i) \leq  \frac{1}{|C_1|}\sum_{i\in C_1}\phi(x_i)$$
For every fixed $x_i$ the function $\psi_i := d(x_i , \cdot )$ is also convex. Replacing above, we get
$$ \alpha \leq  \frac{1}{|C_1|}\sum_{i\in C_1}\psi_i(\frac{1}{|C_2|}\sum_{j\in C_2}x_j)$$
Using one more time Jensen's Inequality we get
$$ \alpha \leq  \frac{1}{|C_1|}\sum_{i\in C_1}\frac{1}{|C_2|}\sum_{j\in C_2}\psi_i(x_j) = \beta$$
Answer: centroid is average of data points in a cluster, centroid point need not present in the data set whereas medoid is the data point which is closer to centroid,medoid has to be present in the original data


JSF Tag disables css preferences for Log Out button

Question:
<h:form styleClass="logoutbuttonform"><h:commandLink action="#{loginController.logout}">Log Out</h:commandLink></h:form>

So my Problem is, I just implemented the functionality of the Logout Button on my Software. But when i add "h:form" the button gets tight and seems to ignore the CSS Preferences of . Logout Button with Tag
When I remove the JSF Tag, the Logout Button looks normal. why is it that the JSF tag disables the CSS Preferences?
Comment: Have you tried my suggestion?
Answer: It should work when you add a styleClass attribute to your commandLink (just as you did in your form tag) and change the style of the class in your CSS.
Something like this:

  <h:form styleClass="logoutbuttonform">
    <h:commandLink action="#{loginController.logout}" styleClass="yourClass">

      Log Out
    </h:commandLink>
  </h:form>

CSS:
.yourClass {
  /* Your CSS */
}

References:

TutorialsPoint reference
This thread here

Hope this helps


Getting Unique 1:1 results with many to many is possible

Question: Ok hope that title wasn't misleading
I have (among other columns) the following info
PO_NUMBER                    PO_TRACKING_NO
-------------------------------------------
321A                            C100
321A                            C100
321A                            C102
321B                            C100
321B                            C012
321C                            C012

I need to write code so that PO_NUMBER and PO_TRACKING_NO match up ONCE, any other results are eliminated...but the PO_TRACKING_NO can still reference other PO_NUMBER and PO_NUMBER can reference other PO_TRACKING_NO...so with the above table, I would want the below results
PO_NUMBER                  PO_TRACKING_NO
-----------------------------------------
321A                          C100
321A                          C102
321B                          C100
321B                          C012
321C                          C012

all must be done via queries, I am grabbing this info from views, I am not allowed to edit/change the tables/views (though in this case I don't think that would have been a problem)
EDIT: sorry text not coming out in a neat form, trying to format it :( [ I'm pretty new here]
Comment: if you wan't your code to look clear, just highlight the code and click the `{ }` from the editor control.
Comment: I'm using oracle, but the SQL cannot include and Oracle SQL, because the query will be going into a 3rd party program that reads SQL Server only....so i can't store variables
Answer: if the are only two columns in your table, DISTINCT is here to help you,
SELECT DISTINCT PO_NUMBER, PO_TRACKING_NO
FROM tableName

Comment: indeed, that was what i tried first, however if i do that than the PO_NUMBER that has two different tracking numbers, one of them is eliminated
Comment: i take back what i said, for whatever reason, yea it absolutely worked

but how do i add more columns to this query that are NOT part of the distinct, whenever i try something i get an error
Answer: A DISTINCT should work, but a GROUP BY will just give you a bit more info:
SELECT
 [PO_NUMBER],
 [PO_TRACKING_NO],
 COUNT(*) AS [Occurrences]
FROM [VW_TRAFFIC_PO_SIDE]
GROUP BY
 [PO_NUMBER],
 [PO_TRACKING_NO]
ORDER BY
 [PO_NUMBER],
 [PO_TRACKING_NO]

Comment: If you add columns to the SELECT-statement, you should add them to the GROUP BY-clause too.
Comment: thanks! absolutely worked, would give you +up but my rep isn't high enough :(
Comment: It's not about earning rep, it's about solving problems. Though, you should be allowed to mark this answer as the solution.
Comment: @user1955517: now your rep is high enough :-)
Comment: Here's my next question (follow up rather)...whenever i use these group by functions and everything, if i try and grab more columns, it gives me a "not a GROUP BY expression"
Answer: Just use DISTINCT like so:
SELECT DISTINCT PO_NUMBER, PO_TRACKING_NO
FROM YourTable


Inference with only left-censored data

Question: Suppose I have a data set that is only left-censored data, ex:

<5, <5, <5, <10, <10, <10

A technique to handle left-censored data is the Kaplan Meier estimate, see page 5 of this excellent article:
One problem I will run into is that the KM method relies on some data to have been fully observed. For by above dataset, I have only censorships, so the KM method will return $\hat{F(t)} = 1, \forall t$.
This feels wrong to be, as I feel my intuition can give a better inference. For example, looking at the above data, I know that if an individual enters the study, the have more than a 50% chance of having the event of interest occur before time 5.
What statistical options do I have if all my data is left censored?
Additional thoughts
Conversely, if all my data is right-censored, I can't make a similar argument as above. The above argument works because I know $T \in [0,5]$.
Comment: Article of OP could be found at https://pdixon.stat.iastate.edu/stat505/Chapter%2011.pdf with interesting stuff at https://pdixon.stat.iastate.edu/stat505/
Comment: The fact that it's positive-but-left censored effectively means it's censored [in an interval](http://en.wikipedia.org/wiki/Censoring_%28statistics%29#Types). It would be like right censored data that you nevertheless knew was less than some upper limit, $M$. It could be that approaches related to ['case 1' interval censoring](https://www.stat.washington.edu/jaw/JAW-papers/NR/jaw-huang-97LNS-SBS.pdf) may help.
Comment: Hmm, I mentioned $T \in [0,5]$ because the subject was only susceptible after being born ($t=0$). Am I interpreting left-censorship wrong? Is there no natural lower-bound?
Answer: How much you can conclude depends on the assumptions you are willing to make about the underlying distribution.  Even with no assumptions, though, you can still conclude something, although it might not be a whole lot.  For example, the sample data are not plausibly consistent with an underlying distribution whose mean is $100$ and standard deviation is $10$: Chebyshev's inequality tells us these data must have extremely low likelihood in that case.
The most general answer will be given in the form of a p-box.  This is a simple device to describe a range of distribution functions.  It consists of two graphs, one lying above the other, which thereby determine a region $\mathcal P$ in Cartesian coordinates.  Any CDF whose graph would lie entirely within $\mathcal P$ is an element of that p-box.
As an example, let's construct a (nonparametric) 90%-confidence p-box for the sample data, $(\lt 5, \lt 5, \lt 5, \lt 10, \lt 10, \lt 10)$.  This p-box should be as "small as possible" (in some sense) while containing any distribution law $F$ for which the probability of the data, given $F$, is at least $1 - 90/100 = 1/10$.  Let us (temporarily) call these the "plausible laws."
"As small as possible" can be rigorously defined.  Let $\mathbb{P}$ be the set of all distribution functions.  Associated with any subset $\Omega\subset \mathbb{P}$ is the set $C(\Omega)$ consisting of all points $(x,y)$ such that $x$ is a real number and there exist $F,G\in \Omega$ with $F(x) \le y \le G(x)$.  Letting $\Omega$ be the set of plausible laws, we take the p-box to be $C(\Omega)$.
These data, which have no quantified results at all, tell us just two things about the plausible laws.  When a sample $X_1, X_2, \ldots, X_6$ is taken from such a law $F$, then (1) it should be unlikely that one or more of the $X_i$ equal $10$ or greater and (2) it should be unlikely that four or more of the $X_i$ should equal $5$ or greater.  These are binomial events governed by the parameters $p=F(10)$ and $q=F(5)$, respectively.  Interpreting "unlikely" as being a chance of $100-90 = 10\%$ or less, these criteria uniquely determine $p$ and $q$.
For these data, $p = (1/10)^{1/6} \approx 0.681$ and $q \approx 0.201$.
The p-box therefore contains the graphs of all distribution laws $F$ for which (1) $q \le F(5) \le 1$ and (2) $p \le F(10) \le 1$.  A part of this p-box (between $x=0$ and $x=15$) is shaded in gray:

Within this p-box I have drawn the graphs of some distributions that come close to its boundary.  All three are two-parameter Exponential distributions.  The dark red one is determined by the two corners it passes through.  The other two (dashed gray ones) show a distribution with a large standard deviation and one with a small standard deviation.
These illustrations should make it clear that if you wished to construct a parametric p-box encompassing only two-parameter Exponential distributions, then it would exclude the region between the thick red curve and the lower line segment between $x=5$ and $x=10$, but it would have to include everything else.  We don't necessarily achieve a lot by making a strong parametric assumption in this example.
What happens when the amount of data increases?  Suppose as you collect more data that all results continue to be either $\lt 5$ or $\lt 10$ and that their proportions remain roughly 1:1.  Then the value of $p$ approaches $1.0$--there can't be much of a chance of observing anything greater than $10$--and the value of $q$ approaches $1/2$, the proportion of $\lt 5$s in the population.  Making an Exponential distribution assumption allows you to conclude a little more: the chance of observing anything above $5$ grows vanishingly small, too.  Here is the situation upon observing $3000$ values of "$\lt 5$" and $3000$ values of "$\lt 10$":

Nevertheless, there is a huge set of possible distributions consistent with such data, even when the dataset becomes enormous.  Intuitively this should be clear: for example, any distribution supported on a set of numbers less than $5$ will be consistent with such data and its graph will fit within this p-box.
This approach to data analysis is not as powerful as, say, a Bayesian analysis.  However, it requires minimal assumptions: we only suppose that the data are identically and independently distributed.  There is no need to adopt a prior and justify your choice of it, for instance.  (Such checking is rather difficult when all the data are censored!)  If the p-box is sufficiently narrow to let you draw a firm conclusion, then you're done and you know your conclusion is extremely robust.  Otherwise, the p-box can be a useful tool to help you explore the impact of potential additional assumptions on your conclusions.

Here is some R code to play with.
k1 <- 3; k2 <- 3     # Counts
x1 <- 5; x2 <- 10    # Quantitation limits
confidence <- 90/100
#
# Find p and q.
#
x <- c(rep(x1, k1), rep(x2, k2))
n <- length(x)
alpha <- 1 - confidence
p <- uniroot(function(p) pbinom(n-1, n, p) - confidence, c(0,1))$root
q <- uniroot(function(p) pbinom(k1-1, n, p) - confidence, c(0,1))$root
#
# Plot the p-box.
#
plot(c(0, 15), c(0, 1), type="n", bty="n", xlab="X", ylab="Probability")
rect(0, 0, x1, 1, col=gray(.975), border=NA)
rect(x1, q, x2, 1, col=gray(.975), border=NA)
rect(x2, p, 15, 1, col=gray(.975), border=NA)
#
# Show a parametric solution.
#
F.inv <- qexp; F <- pexp            # Exponential distribution family
F.p <- F.inv(p); F.q <- F.inv(q)
sigma <- (x2 - x1) / (F.p - F.q)    # Scale parameter
mu <- x2 - sigma*F.p                # Offset parameter
curve(F((x - mu)/sigma), add=TRUE, col="Red", lwd=2)
f <- stepfun(c(x1, x2), c(0, q, p)) # Lower limit of the p-box
plot(f, add=TRUE, verticals=FALSE)
curve(1 + 0*x, add=TRUE)            # Upper limit of the p-box

Comment: Neat answer, thanks @whuber. I have a few followup questions if you don't mind. 1. Is there a name for this censorship inference method? 2. What would incorporating observed values look like? You could treat them the same way your treat censorships, but this loses information.
Comment: 1. I think this often goes by "interval statistics."  The p-box terminology has been developed and advocated by S. Ferson and V. Kreinovich, so you can find useful material by Googling something like "ferson interval statistics". 2. Incorporating observed values can start getting computationally challenging, but the same concepts and principles apply: you construct a p-box containing distributions that are not too inconsistent with the observed data.
Answer: I can think of one solution, but I'm open to others.
In the paper linked, the use the formula:
$$F(Z_i) = \prod_{i=1}^j \frac{R_i - n_i}{R_i} $$
The term being multiplied, $\frac{R_i - n_i}{R_i}$, is a pointwise estimate of the rate at time $i$. For low $R_i$ size, or when $n_i = R_i$ or $n_i = 0$, this produces somewhat pathological results (see my complaints above). Instead we can treat this estimation as a Bayesian beta-binomial problem:  Given prior $Beta(\alpha, \beta)$, we observe $n_i$ deaths in $R_i$ opportunities and update our prior to $Beta(\alpha + n_i, \beta + R_i - n_i)$. (I suggest keep $\alpha, \beta$ very low as to not introduce a strong bias in the final product).
We then sample from each posterior conversion rate and form the product:
$$F(Z_i) = \prod_{i=1}^j (1 - S_i) $$
where $S_i \sim Beta(\alpha + n_i, \beta + R_i - n_i)$. Doing this many times, we can reconstruct the posterior CDF.
Why does this work? Even if $n_i = 0$, $S_i$ is still slightly greater than 0, though not too much greater. If I have time later this week, I'll do some simulations.

Inference with only left-censored data


What is the rationale of solving SDE by Ito's formula?

Question: When solving a SDE by Ito's formula, we have to find a function $f(t, X_t)$ of index $t$ and the process $X$ to be solved for.

I was wondering  what is the criterion of choosing $f$? Is it to
make the integrands not contain $X$?
How is $f$  chosen in principle?

Thanks and regards!
Answer: It depends on the particular SDE. There is no systematic approach.
One main idea is striving for some cancelation to simplify the drift/volatility coefficients (eg.https://math.stackexchange.com/questions/823785/stochastic-differential-equation-with-trigonometric-functions). For example, in Geometric Brownian motion
$$ dS_t = \mu S_t\,dt + \sigma S_t\,dW_t$$
one observes the $S_{t}$ term would cancel out with the derivative of $f(x)=log(x)$.
Another idea is to help transfer some ODE techniques such as integration-factor see here.
Solution to General Linear SDE


Slice html template into Magento theme

Question: I have HTML Template. I want to integrate that template to Magento theme. I have searched over google as well as here(Stack-Over-Flow). But not getting proper documentation for that. Any help will appreciated. Thanks in advance.
Comment: You are asking whole bunch of magento in one question and asking for a single documentation.You will not get any. You have to full understand magento to do so.
Comment: The question you are asking is too broad.You have to be specific because different pages are rendered from different template file .So be specific in which page you want your template to be implemented.
Comment: I want to integrate whole html template as Magento theme. Which includes home-page, product-page and everything that HTML-TEMPLATE have. @aton1004
Comment: I am not asking for single doc. I`m not getting a GOOD or a SINGLE document over integrating html template to MAGENTO theme. Even not getting document over WHERE to START from. By the way, I have found the MagentoDesignGuide.pdf which is good to START. @aton1004
Comment: Learning about Zend Framework and Model2 is a good place to start, then you'll be ready to understand how the view works. You'll be very frustrated when you try to access things the view shouldn't have access to ;)
Comment: I think you did not get the question. @NickM
Comment: @MaulikM.Dodia as a veteran Magento EE/CE developer, I did get the question, and pointed out that to understand the theming of a Model2 framework, you need to understand first how the view works. Otherwise you are going to make mistakes that will cost both you and clients large sums of money in the future. Learn how Model2 works, then read the Magento PDF you have, and it will be much clearer. For instance, hardly ANY logic should ever be done in the phtml files. Like zilch, zero, none. This is contrary to most entry-level PHP devs experience prior to Magento or other frameworks.
Comment: So, First of all I have to learn Model2 framework. Than I can start learning Magento. Can you provide some good documentation over Model2 framework. @NickM
Answer: Ok You want to integrate the static HTML template to the magento theme. So let's divide things you need to learn and how to learn efficiently.
Magento can be basically divided into three parts:

Admin panel: As you know magento is a CMS so most of the basic things could be managed from backend.For example static pages and Home pages could be managed from "CMS Pages" from admin panel.
Next is theming: You have mentioned about the MagentoDesignGuide.pdf this could be the start. I recommend you to have a look for the youtube channel of leveluptuts for the theming(layouts).This is very important for structuring your pages and site.You can follow other also but i prefer that because i also learnt from it and was very helpful.
Another is the module development portion.In some cases you need to develop custom modules .So this would be the last part.There are great tutorial provided by Inchoo and Smashing Magzine for developing custom module.

All of these things are required to fully develop a magento site. If you understand this than you will do great in magento.
Hope this answer provides you with the idea of how to proceed further and also helps you to divide the way of learning Magento.
Happy Coding.
Comment: Thanks for understanding my question. I have understand very well as what you have told.I will take a look at your reference links. Thanks for helping me. If you dont mind, Can I ask you a question? @atom1004
Comment: Have you removed the previous answer to this question? @atom1004
Comment: Which previous answer there is only one answer to this question in which we are commenting right now.There are no any other answer.
Comment: There was another answer which have just a link. Anyway thanks for help. @atom1004

Slice html template into Magento theme


Visual Studio 2015 Remote Debugger 'not implemented'

Question: I'm getting crazy with Visual Stusio 2015 remote debugger.
What I want:
I have running a .Net console application on a Azure VM.
I want to remote debug my application and make some performance measure.
What I've done so far:
Application was compiled in debug mode and is running on the remote machine.
Over the Server-Explorer of VS2015 I added the Remote Debugger extention to the VM. Firewall on the VM is down for testing. Tested on two devices with the same result.
What I get:
I select my VM over the Server-Explorer and choose 'Attach debugger'.
I select my process of my running application (yes all processes running are shown).
Then after a few seconds I get the great error window 'Not implemented'!?
Where I need help:
Does anybody have a similar problem? Where can I find an error log to get more information on whats going on?
Further
I also created a Connect entry for this (Thanks for the hint @Vova). So if you have the same problem you can upvote it. Microsoft Connect
I'm thanksfull for every help. Greetings Steffen
Comment: Did you look through this link? https://msdn.microsoft.com/library/azure/ff683670.aspx#Anchor_3
Comment: Probably a bug in Visual Studio or Azure Debugging tools. Try to post this to Microsoft Connect...
Comment: @Vova yes thats all the steps I'm followed up.
Comment: did you actually publish your Azure project in the Debug build configuration? This isn't the default option, and you definitely *cannot* attach the remote debugger to an app in release build configuration.....
Comment: It is not publish as Azure Service. It's a simple console application running on a Azrue VM. But yes ist compiled in debug mode.
Answer: According to Microsoft Connect guy (Eliver) its a bug. It only effects Visual Studio 2015 with display language other then english.
Solution 1
Wait for next Azure SDK release with fix inside
Solution 2
Install english language pack from here and switch ths VS language to english from option dialog.
Update - 2015-11-21
I just wanted to let you know that the Azure SDK 2.8 has been released and this bug I'am facing have been resolved in this.
https://azure.microsoft.com/en-in/blog/announcing-the-azure-sdk-2-8-for-net/


If $U \subseteq W^{\perp}$ and $V = W+U$ so $U=W^{\perp}$.

Question: Let $V$ a vector space over field $\mathbb{K}$ with inner product and let $U$ and $W$ subspaces of $V$ so that $U \subseteq W^{\perp}$ and $V = W+U$. Show that $U=W^{\perp}$.
I try this approach:
Let $w \in W+U$ so $w \in U$ and $\langle w,v \rangle = 0$ for all $v \in W$. In particular $\langle w,w \rangle = 0$ implies $w=0$ and direct sum $V = W \oplus U$.
My question is, can I compare $V = W\oplus U$ with decomposition $V = W\oplus W^{\perp}$ and conclude $U=W^{\perp}$?
Answer: Vector space decompositions do not "care" about inner products, so in general $V = W_1 \oplus W_2$ and $V=W_1 \oplus W_3$ does not imply $W_2 = W_3$.  For example, if $W_1= <(1,0)>, W_2 = <(0,1)>$, and $W_3 = <(1,1)>$, then $\mathbb{R}^2 = W_1 \oplus W_2$ and $\mathbb{R}^2 = W_1 \oplus W_3$.
Answer: As you are supposed to prove $U=W^{\perp}$, and you know $U\subseteq W^{\perp}$, one would expect you to try to prove $W^{\perp}\subseteq U$:
Proof: Let $v\in W^{\perp}$. Write $v=w+u$ with $w\in W, u\in U$, i.e. $w=v-u$. Let's calculate $\langle w,w\rangle$:
$$\langle w,w\rangle=\langle v-u,w\rangle=\langle v,w\rangle-\langle u,w\rangle=0-0=0$$
which implies $w=0$, i.e. $v=u\in U$.
Decomposition: As for the decomposition $V=W\oplus W^{\perp}$: it can be done in finite-dimensional vector spaces, and this can lead to a valid (dimensional) argument. If $V$ is finite-dimensional, you will know that $\dim W+\dim U=\dim (W+U)+\dim(W\cap U)\ge\dim V=\dim W+\dim W^{\perp}$, i.e. $\dim U\ge\dim W^{\perp}$ (which with $U\subseteq W^{\perp}$ gives $U=W^{\perp}$).
The same argument is not valid in infinite-dimensional vector spaces, because in general it is not possible to prove that $W+W^{\perp}=V$ at all. For an example, see e.g. Non-closed subspace of a Banach space. (Further to that, the dimensional argument, obviously, won't work.)


Group By / Sum Aggregate Query with Parse Cloud Code

Question: I have Inventory table in my Parse database with two relevant fields: productId and quantity. When a shipment is received, a record is created containing the productId and quantity.  Similarly, when a sale occurs, an inventory record is made with the productId and quantity (which will be negative, since the inventory will decrease after the sale).
I would like to run a group by/ sum aggregate query on the Inventory table with Parse Cloud Code that outputs a dictionary containing unique productIds as the keys and the sum of the quantity column for those Ids as the values.
I have seen a number of old posts saying that Parse does not do this, but then more recent posts refer to Cloud Code such as averageStart in the Cloud Code Guide: https://parse.com/docs/cloud_code_guide
However, it seems that Parse.Query used in averageStars has a maximum limit of 1000 records. Thus, when I sum the quantity column, I am only doing so on 1000 records rather than the whole table.  Is there a way that I can compute the group by/ sum across all the records in the inventory table?

For example:
Inventory Table
productId     quantity
Record 1: AAAAA 50
Record 2: BBBBB 40
Record 3: AAAAA -5
Record 4: BBBBB -2
Record 5: AAAAA 10
Record 6: AAAAA -7
Output dictionary:
{AAAAA: 48, BBBBB: 38}
Comment: http://stackoverflow.com/questions/32840272/parse-query-aggregations
Answer: You can use Parse.Query.each(). It has no limit. If your class has too many entries it will timeout though.
See docs
e.g.:
var totalQuantity = 0;
var inventoryQuery = new Parse.Query("Inventory");
inventoryQuery.each(
    function(result){
        totalQuantity += result.get("quantity");
    }, {
        success: function() {
            // looped through everything
        },
        error: function(error) {
            // error is an instance of Parse.Error.
        }
    });
});

If it times out, you have to build something like this.
Comment: Thanks!!! It works perfectly.  I modified the code slightly to output the dictionary.
Answer: In case you want to see the code with the dictionary:
Parse.Cloud.define("retrieveInventory", function(request, response) {
  var productDictionary ={};
  var query = new Parse.Query("Inventory");
  query.equalTo("personId", request.params.personId);
  query.each(
     function(result){
        var num = result.get("quantity");
        if(result.get("productId") in productDictionary){
             productDictionary[result.get("productId")] += num;
        }
        else{
             productDictionary[result.get("productId")] = num;
        }
    }, {
        success: function() {
            response.success(productDictionary);
        },
        error: function(error) {
            response.error("Query failed. Error = " + error.message);
        }
    });
});


How to group List elements in Hashmap by a List of key

Question: I am trying to group a List of events into dates with a HashMap but i'm having problem achieving it. I want the dates to be the key while the events will be the values. So every events that matches a key will be stored inside a List in the HashMap. So far i have a list of events and a list dates.....my code is show below, thanks in advance.
code
public class TestMap {

public static void main(String[] args){
    TestMap test = new TestMap();

    List<Date> mDays = new ArrayList<Date>();
    SimpleDateFormat mFormat = new SimpleDateFormat("EEEE d MMMM yyyy");
    GregorianCalendar mCalendar = (GregorianCalendar)GregorianCalendar.getInstance();
    mCalendar.setTime(new Date());
     for (int i = 0; i < 7; i++){
            mDays.add(mCalendar.getTime());
            mCalendar.add(Calendar.DAY_OF_WEEK, 1);
           System.out.println("Date: " + mFormat.format(mDays.get(i)));

      }

    List<Events> mEvents = new ArrayList<Events>();
    mEvents.add( new Events("1", "boat party", "Sunday 7 December 2014"));
    mEvents.add( new Events("2", "church meeting", "Monday 8 December 2014"));
    mEvents.add( new Events("3", "shopping", "Saturday 13 December 2014"));
    mEvents.add( new Events("4", "clubbing", "Friday 12 December 2014"));
    mEvents.add( new Events("5", "visiting", "Thursday 11 December 2014"));
    mEvents.add( new Events("6", "tour", "Wednesday 10 December 2014"));
    mEvents.add( new Events("7", "work", "Tuesday 9 December 2014"));
    mEvents.add( new Events("1", "beckie birthday", "Sunday 7 December 2014"));
    mEvents.add( new Events("2", "camping", "Monday 8 December 2014"));
    mEvents.add( new Events("3", "gym", "Saturday 13 December 2014"));
    mEvents.add( new Events("4", "working", "Friday 12 December 2014"));
    mEvents.add( new Events("5", "farming", "Thursday 11 December 2014"));
    mEvents.add( new Events("6", "attend sales", "Wednesday 10 December 2014"));
    mEvents.add( new Events("7", "work", "Tuesday 9 December 2014"));

    for(Events evt: mEvents){

        System.out.println("ID: " +evt.getEventId()+ " Title: "+evt.getEventTitle()+ "   Date:
  "+evt.getStartDate());
    }
}

}
Events class:
class Events{

private String eventId;
private String eventTitle;
private String startDate;

public Events(String eventId, String eventTitle, String startDate) {
    super();
    this.eventId = eventId;
    this.eventTitle = eventTitle;
    this.startDate = startDate;
}
public String getEventId() {
    return eventId;
}
public void setEventId(String eventId) {
    this.eventId = eventId;
}
public String getEventTitle() {
    return eventTitle;
}
public void setEventTitle(String eventTitle) {
    this.eventTitle = eventTitle;
}
public String getStartDate() {
    return startDate;
}
public void setStartDate(String startDate) {
    this.startDate = startDate;
}

}

Answer: You could use a HashMap<Date,List<Events>> to achieve this.
You get your dates, and for every date object you will get a List<Events> object. After getting this object (or NULL if it does not exist) you will be able to add / remove events and then store them again using put method.
Hope it helps
EDIT
You will have something like this declared in your class:
HashMap<Date,List<Events>> my_events = new HashMap<Date,List<Events>>();

Lets put some examples.
1 - Adding an event to a certain date:
 //suppose that you already have a date in which you want to add the new event

 if(my_events.contains(given_date))
 {
     List<Events> aux = my_events.get(given_date);
     aux.add(new_event);
     my_events.put(given_date,aux);
 }
 else
 {
    List<Events> new_list = new ArrayList<Event>();
    new_list.add(new_event);
    my_events.put(given_date,new_list);

 }

2 - You want to get all the events in a given date:
return my_events.get(given_date); // this will return null if the given date (key) does not exists

Comment: thanks zozelfelfo, it helps a bit, but can you pls add a code snippet for more clarification.
Comment: Put the answer as correct if this solved your problem, comment anything you need otherwise @mish :)
Answer: Suppose you are grouping the items by the ID field,
// create a map which relates String-List of Event mapping
HashMap<String, List<Event>> listMap=new HashMap<String,List<Event>>();

// loop through the events
for(Events evt: mEvents){
    if(!listMap.containsKey(evt.getID())){
        listMap.put(evt.getID(), new ArrayList<Event>());
    }
    listMap.get(evt.getID()).add(evt);
}

// if you want to get a list of all events with the same ID,
Collection<Event> eventWithSpecificId=listMap.get("certain ID");

Comment: Please add some explanations.


Mark links as read with a hook

Question: I would like to mark the links the already read with a hook. Unread links should not display the hook.
The rule is the visited link, and can differ only in color.
Would it be the fine to put the hook of the unread link transparent?
Or is there a better solution?
a.readed {color: rgba(0, 255, 0, 1)}
a.readed:visited {color: rgba(0, 255, 0, 0)}

 goto ✓

Comment: Did you try using `::after{ content: ' ✓'}` ?
Comment: The Hook should only dispalyed if the Link are readed (visited). Sorry i edit my question.
Comment: You should have a look at this : https://hacks.mozilla.org/2010/03/privacy-related-changes-coming-to-css-vistited/
Answer: Instead of adding additional link, just put it as CSS content of :after/:before

a:after {
  content: '✓';
  display: inline;
}
Some link
Comment: i found: http://stackoverflow.com/questions/12342516/combine-avisited-with-aafter-or-chaining-pseudo-classes-with-pseudo-elements
Comment: The Hook should only displayed if the link are readed. The link himself should be allways visible.
Comment: a.visted:after {
  content: '✓';
  display: inline;
}
coud work and Looks smarter. I will tried.
Comment: oh it must a:visited:after but ist wrong how can i write that both together works?
Answer: I would do something like change the color of the link to see what already was clicked.

a {
  color: blue;
}

a[tabindex]:focus:after {
 color: red;
 outline: none;
 content: 'insert hook here';
 display: inline;

}
Test

But your way is also good so if it works it doesn't really matter I guess.
EDIT:
didnt answer your question.
so i tried some things this is the closest i got to displaying the hook
only problem is after u click out range it hides again so fiddle arround with this.
Comment: You are right it is not a 100% solution. But it is only a "nice to have" and not importend for a process to display the marked hook. Like a Cookie solution are to much. Thx
Comment: No problem just do as you feel how it has to be done. it can be done in multiple ways.

Mark links as read with a hook


Cluster Analysis: effectiveness of k means results and alternative methods

Question: I have to separate 425 observations based on certain variables numbering 32.
1)I used PCA to reduce the dimensionality of Data, which gave me 32 components out of which 5 components accounted for 75% of the variance.
2)I used Elbow plot to determine no of clusters that is 6. I further used these 5 components as variables for kmeans clustering. when plotted using clusplot() Clusters are not well separated.

Does this mean that clustering was not successful.(between_SS / total_SS =  67.9 %). Is there any other way to determine effective ness of clustering? Can somebody suggest a better way to obtain better clusters may be PAM, Hierarchical clustering ...?
Comment: Notice that your choice of the number of cluster does not seems obvious for me according to the elbow graph. If you do this on the fly, why not considering 7 clusters? There seems to be a small drop around 10 and stabilizing as well. For each of these values you can calculate `between_SS/total_SS`
Comment: **Don't** do factor-cluster analysis: http://ro.uow.edu.au/cgi/viewcontent.cgi?article=1820&context=commpapers.
Comment: There is **no elbow in the elbow plot**. Either there are *no* clusters, or you did not preprocess the data well.
Answer: First, let me state that I'm highly experienced in building cluster solutions in applied research contexts. Therefore, I have a strong, biased POV that does not agree with the objections to factor-cluster approaches raised in the pdf cited by Fg Nu. In other words, DO use factor-cluster methods in developing a cluster solution.
Among the reasons cluster analysis is considered "soft" and a-scientific by many has to do with the plethora of subjective decisions that are required in generating solutions. These include ground truth considerations such as the selection of inputs, whether one does a PCA or CFA, orthogonal vs oblique solutions and rotations to simple structure, not to mention the blizzard of arbitrary heuristics available for selecting the number of factors, the number of clusters, the impossibility of developing unique and optimizable solutions, and so on. My point is that adopting or acting on the recommendations in this pdf will not make any resulting solution more rigorous or "scientific."
The objections to factor-cluster analysis in the pdf cited by Fg Nu are primarily academic, red herring concerns that can be applied to any and all dimension reducing techniques. The irony is that the authors of this pdf are marketers writing about consumer segmentations in tourism, an applied context. That said, I am agnostic about whether or not their observations might have relevance in  more truly "scientific" contexts such as writing a PhD dissertation, in cladistics and speciation -- the assignment of species to groups -- or in machine learning situations leveraging automatic approaches to k-means, etc.
Given that, the pdf's specific concern with whether or not the resulting solution is evaluated or profiled based on the factors from the transformed space or with the original variables is a purely academic issue. Transformations of any nature exact a price, whether it be in reshaping the latent geometric space as in mean standardization, in rescaling, whatever. Being cognizant of the tradeoffs is the key, not tossing an approach out because there are "problems." In an applied context where understanding and insight is the focus, this is an irrelevant concern.
Next, their observation about "throwing away" 40% of the original variance in using FA is true but so what? It's an artificial loss or number, in the first place. In the next place, motivate the logic behind why I should care. That wasn't done, at least not to my satisfaction.
Finally, objection #3 that factor-cluster solutions perform "worse" relative to the "correct" structure on known, artificial data is the biggest red herring of all. The fact is that the "correct structure" of data in real world analysis is never known. Next, the point of cluster analysis in applied contexts is not precision, its directional, strategic insight that is riddled with error.
Cluster solutions are well-known to be highly sensitive to redundancy. The strongest argument in favor of factor-cluster approaches is that factorization reduces or even removes redundancy in the inputs. This is the real beauty of factorization -- the fact that the resulting solution absorbs information about all of the inputs, making the selection of specific variables moot. This is not accomplished without a price, of course. Next, variable selection is a highly subjective, arbitrary step about which every statistician and their brother has a working paper and solution. Arriving at agreement on the "correct," raw inputs to use in the absence of factorization can and has derailed many clustering projects.
Ok...enough of my polemic. I want to note that, based on the scatterplot of dim1 by dim2, the solution looks pretty crisp and good to me.
One way to cross-validate the results would be to split your data into "train and test" sets, calibrate a solution on the training set, score the test data with that solution and then see how well you can recover the test solution based on a misclassification rate using something like a discriminant analysis based on LOOCV or jacknifing. One can do this CV in two ways: first, based on the "full" set of variables used in developing the factors/clusters.  And, second, leveraging a "reduced" set of information that would be comparable to the poor quality and incomplete information available from huge data vendors such as Experian. The resulting misclassification rates should generate intuition as well as confidence (or not) about how well the cluster solutions are performing once they are projected and used based on the scoring of external information.
Comment: Pedantically, or otherwise, speciation is what the organisms do; it is not the part of taxonomy whereby biologists distinguish species. More importantly, you almost make me want to read the paper you criticise to see how bad it really is.... Please don't use "academic" as a catch-all; if you want to say silly, wrong, confused, irrelevant, .... and explain why you say so, then that is naturally fine. Using "academic" abusively is too easy, and FWIW does not appeal to academics.
Comment: "Pedantic" was a reference to the correction that followed it: I am pointing out that speciation is not a kind of classification. No more.
Comment: I feel that you might want to open a special thread about `cluster analysis after dimensionality reduction (PCA, FA)` - its pros and cons. There you could present your arguments as an answer to own question. And people could add their answers. As for currently, your (quite interesting) account is more about the article once mentioned in a comment, not so much about the OP's question.
Comment: Speciation is not pedantic since it's evolutionary. Classification *is* or can be pedantic. I think that's my point. And you are correct about my use of *academic*. I will monitor that in future.
Comment: @ttnphns  My post is a bit of a diatribe that I had completely forgotten about. Thanks for the suggestion. I will give it some thought.
Answer: Consider: http://cran.r-project.org/web/packages/densityClust/
It was described here: http://www.sciencemag.org/content/344/6191/1492.short
and has been implemented in R: https://github.com/thomasp85
Comment: The big miracle is: how did they get THAT accepted?!? I guess they don't have any clustering experts in the reviewers board... kind of like the MIC coefficient which was on Nature or Science, and all the statistics community went WTF?
Comment: @Anony-Mousse Have not read the Science paper -- care to elaborate?
Comment: It's ignoring most of the state of the art.
Comment: @Anony-Mousse I'd actually find it extremely valuable if you took the time to explain (perhaps with reference to specific concepts used or not used in the paper itself) (as I think @T C asks) . I definitely am very under-educated on this and I would love to learn (for eg. it's not immediately obvious to me what one should criticize or review when reading literature of the sort). Thanks ahead of time.


preventing OpenJPA N+1 select performance problem on maps

Question: When I have an entity that contains a Map, e.g.
@Entity
public class TestEntity {
    @ElementCollection(fetch = FetchType.EAGER)
    Map<String, String> strings = new HashMap<String, String>();
}

and I select multiple entities (SELECT z FROM TestEntity z), OpenJPA 2.0 performs one query for each TestEntity to fetch the map, even though I used FetchType.EAGER. This also happens when the Map value is an entity and I use @OneToMany instead of @ElementCollection. In principle this can be done more efficiently with one query that selects all the map entries for all returned TestEntities. For Collection-valued fields OpenJPA already does this by default (openjpa.jdbc.EagerFetchMode" value="parallel") but it seems to fail on this simple entity. (Same problem with value="join").
Could I be doing something wrong? Is there an easy way to tell OpenJPA to not perform a query per entity but only one?
Or is there already any work planned on improving this (I filed it under https://issues.apache.org/jira/browse/OPENJPA-1920)?
It is a problem for us because we wish to fetch (and detach) a list of about 1900 products which takes almost 15 seconds with OpenJPA. It takes less than a second with my own native query.
Having to write only one native query wouldn't be much of a problem but the map we use is inside a reusable StringI18N entity which is referenced from several different entities (and can be deep in the object graph), so native queries are a maintenance headache.
Any help getting performance up is greatly appreciated.
EDIT: explicitly using JOIN FETCH does not help either:
"SELECT z FROM TestEntity z JOIN FETCH z.strings"
OpenJPA's TRACE still shows that it executes one SQL statement for each individual TestEntity.
Comment: Problem look the same using Hibernate 4.2.
Comment: using the @BatchSize annotation might solve the issue in Hibernate
Comment: Already tried, but no, but in my case I think this is a @OneToOne issue when loading my actual MultilingualText entity... I'll continue to search!
Comment: Interesting, I was actually using the map for the same goal in my StringI18N entity. I ended up hardcoding a String field for each of the languages we use. (Not exposed to clients of StringI18N, they still use a set(String, Locale) method). It's ugly but I think it is acceptable in our case because the languages never change.
Comment: Yes, same usage but in my case I was using another method before and it was a pain (creating custom queries each time). I've posted a question, hope an answer soon!
Answer: It might be a pain (correction: I know it'll be a pain) but have you tried actually mapping your 2-field TestEntity as a full JPA-persisted @Entity?
I know that Hibernate used to treat @ElementCollections rather differently to @OneToManys for example - OpenJPA could well be doing something similar.
Comment: Thank you for your suggestion. Actually I have posted on the mailing list and they say it is expected behavior at least they don't really seem to see this as a bug.
Comment: Actually I also found out that if you have a structure Company - Employee - Project and with a ManyToOne field Company.ceo, selecting companies fetches the ceo's projects with 1 query per ceo instance. Does NOT happen with OneToMany (List). No reaction of OpenJPA on the mailing list. (My original problem was a String in multiple languages which I solved the ugly way by having a hardcoded String field for each language)


How to detect if ajax error is Access-Control-Allow-Origin or the file is actually missing

Question: My question is NOT about how to solve the Access-Control-Allow-Origin issues. This errors will happen sometimes when performing requests, and other times the url's might be outdated. But I want to print different messages for the user depending on the different errors.
Currently I have the following code:
$.ajax(
{
    url: link,
    type:'HEAD',
    timeout: 2000,
    error: function(request, status, message)
    {
        console.log('ajax error');
        console.log(request);
        console.log(status);
        console.log(message);
        openPopUp("There was an error accessing the image. It can be because the address is invalid, "+
            "or because the server where the image is stored is not allowing direct access to the images.");
    },
    success: function()
    {
        // More stuff here
    }
});

Looking at the console it's easy to see if the file was actually missing, or if it was an Access-Control problem. But i'd like to print out two different messages to the user saying exactly what the problem was. Looking at the variables in error: function(request, status, message) they do not change, both cases result in a 404 error. Is there some other was to do this so that I can know what the problem was?
Thank you in advance for the attention.
Answer: Your browser console shows you

XMLHttpRequest cannot load http://www.google.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://mysite.com' is therefore not allowed access.

but you cannot access this information yourself with JavaScript alone. When a browser detects a CORS violation, it will discard the header information as a matter of protocol.

One solution that works is to inspect the response headers using server-side code and pass the results back to your client page. For example, if the ajax request fails, you could call this script (let's call it cors.php) and know for certain if it contains "Access-Control-Allow-Origin" or not.
Example:

cors.php?url=http://ip.jsontest.com
  cors.php?url=http://www.google.com

returns

Access-Control-Allow-Origin: *
  None

Thus, you can do the following in your JavaScript code:
$.ajax({
  url: "http://www.google.com",
  timeout: 4000,
  statusCode: {
    404: function() {
      // Simple not found page, but not CORS violation
      console.log(this.url + " not found" );
    }
  }
})
.fail(function(jqXHR, textStatus) {
   // Empty status is a sign that this may be a CORS violation
   // but also check if the request timed out, or that the domain exists
   if(jqXHR.status > 0 || jqXHR.statusText == "timeout") {
      console.log("Failure because: "+jqXHR.status+" "+jqXHR.statusText+" error");
      return;
   }

   // Determine if this was a CORS violation or not
   console.log("Checking if this is a CORS violation at - " + this.url);
   $.ajax({
      url: "http://myserver.net/cors.php?url=" + escape(this.url),
   })
   .done(function(msg) {
      // Check for the Access-Control-Allow-Origin header
      if(msg.indexOf("Access-Control-Allow-Origin") >= 0) {
        console.log("Failed bacause '" + msg + "'");
      } else {
        console.log("Failed bacause of CORS violation");
      }
   });
})
.done(function(msg) {
  // Successful ajax request
  console.log(msg);
}); /* Drakes, 2015 */

Customize this PHP script for your own needs:
<?php
/* cors.php */

$url = $_GET["url"];
if(isset($url)) {
    $headers = getHeaders($url, "Access-Control-Allow-Origin");
    header("Access-Control-Allow-Origin: *"); // Allow your own cross-site requests
    echo count($headers) > 0 ? $headers[0] : "None";
}

// Get the response headers, only specific ones
function getHeaders($url, $needle = false) {
    $headers = array();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD'); // Only get the headers
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($curl, $header_line) use(&$headers, $needle) {
        if(!$needle || strpos($header_line, $needle) !== false) {
            array_push($headers, $header_line);
        }
        return strlen($header_line);
    });
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_exec($ch);
    return $headers;
} /* Drakes, 2015 */

Comment: +1 for "When a browser detects a CORS violation, **it will discard the header information** as a matter of protocol.". This was driving me nuts.
Answer: You should be able to read the response header from the request object:
var acao = request.getResponseHeader('Access-Control-Allow-Origin');
then output the appropriate error based on whether the header exists and whether your url is in the value.
Comment: That would seem a good way to go but unfortunately when the problem is the Access-Control-Allow-Origin I don't have access to the headers. Even using getAllResponseHeaders() I get an empty list.
Comment: What changes in the console? Could you post a couple of examples of the different cases? Seems like if you can see the difference in the console it should be available to you somewhere...
Comment: Sure. When I try to access an image that exists but i can't access i get the error:
XMLHttpRequest cannot load http://mylink. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://mysite' is therefore not allowed access. If I try to open a wrong link I get: HEAD http://wronglink net::ERR_NAME_NOT_RESOLVED


Swift Combine publisher won't trigger in flatMap after finished event

Question: I'm trying to create a refresh event using CurrentValueSubject while making a network call. So whenever network request fails I can press the button to make the request again, but can't make it work as failure event will terminate publisher and it won't work again.
import Combine
import SwiftUI

struct ContentView: View {

    @ObservedObject var viewModel: TestViewModel = TestViewModel()

    var body: some View {
        Button("Test", action: viewModel.test)
        Button("Refresh", action: viewModel.refresh)
    }
}

class TestViewModel: ObservableObject {

    var bag = Set<AnyCancellable>()
    private let changeSubject = CurrentValueSubject<Void, Never>(())

    func test() {
        changeSubject
            .flatMap { self.networkPublisher }
            .sink(
                receiveCompletion: {
                    switch $0 {
                        case .failure:
                            print("Failure")
                        case .finished:
                            print("Finished")
                    }
                },
                receiveValue: {
                    print("Value: \($0)")
                }
            )
            .store(in: &bag)
    }

    func refresh() {
        changeSubject.send(())
    }

    var networkPublisher: AnyPublisher<String, Error> {
        var url = URLRequest(url: URLComponents(string: "www.google.com")!.url!)
        url.httpMethod = "GET"
        return URLSession.shared
            .dataTaskPublisher(for: url)
            .tryMap { _ -> String in "Result" }
            .receive(on: DispatchQueue.main)
            .eraseToAnyPublisher()
    }
}

To fix this I could use .tryCatch { _ -> Just<String> in Just("Error") } to catch the error and that would prevent the publisher from terminating. But why it wouldn't work when publisher terminates? And how can I make work after finish event?
Comment: https://stackoverflow.com/a/64448364/341994
Answer: The pattern to not terminate a pipeline on error is to capture the error with a flatMap:
changeSubject
   .flatMap {
      networkPublisher
         .catch { _ in Empty() }
   }
   .sink {
      print("Value: \($0)")
   }
   .store(in: &bag)

As you can see, .sink only receives an Output of String and an Error of Never, because the error is handled fully inside .flatMap
Comment: This is pretty much same as I explained in my question that you could do this while using tryCatch. But it's not what I want. I would like to use error instead of never and just, in general, understand why the new instance of network request wouldn't work.
Comment: @kosas, the whole idea of an error is to signal a *completion* of a chain with a failure state, i.e. the subscription (the one stored in `bag`) is done and shouldn't receive any more values. You say "I would like to use error" - use how exactly?
Comment: I thought it's possible to keep publisher alive after an error is emitted similar like .retry() does. In my case, I would like to keep it alive till I remove it from the bag or completion, but not when an error is emitted.
Comment: @kosas, `.retry` resubscribes to upstream. Ok, so your use case can be exactly satisfied with a `Never` failure type... i.e. even if an error occurred, the subscription is still alive, until cancelled
Answer: Rather than mapping to a single instance of your publisher, you should be mapping to a new instance.  Your persisted networkPublisher fires off its data task and completes, but you continue mapping the same completed stream in your flatMap.
var networkPublisher should be a computed value instead, such that you are returning a new publisher (and a new data task) on every pass.
var networkPublisher: AnyPublisher<String, Error> {
        var url = URLRequest(url: URLComponents(string: "www.google.com")!.url!)
        url.httpMethod = "GET"
        return URLSession.shared
            .dataTaskPublisher(for: url)
            .tryMap { _ -> String in "Result" }
            .receive(on: DispatchQueue.main)
            .eraseToAnyPublisher()
}

Have a look at this sample code from Apple.  The data publisher itself is created within the closure of the flatMap, which emphasizes the fact that it is a new instance every time.
Comment: Actually, I had it as a computed variable in my project, but that doesn't work either.


Listar grupo apenas que contenham itens

Question: E um exemplo de model:
public class Grupo
{
   public int Id {get;set;}
   public string Nome {get;set;}

   public ICollection<Item> Itens {get;set:}
}

E os itens
public class Item
{
  publit int Id {get;set;}
  public int GrupoId {get;set;}
  public string Nome {get;set;}
}

Existe alguma forma de eu filtrar via EF apenas os grupos que contenham Itens ?
Answer: Primeiramente tens de dar referencia na class Item para a Grupo:
public class Item
{
  publit int Id {get;set;}
  public int GrupoId {get;set;}
  public string Nome {get;set;}
  public virtual Grupo Grupo { get; set; } // <-

}

Depois basta fazer uma consulta em Grupo para verificar se tem Item:
var listaGrupos = db.Grupo.Where(a => a.Itens.Count > 0).ToList();

Comment: Como Itens é uma ICollection, acredito que não tenha a propriedade `Count` e o Método `Count` pode ter um custo muito acima do aceitavel nesta situação.

Uma Opção seria o Método `Any`, porem ao meu ver este ainda um custo elevado, a melhor solução ao meu ver seria um Contains, isto se ele estiver a usar EF5 ou superior.
Comment: @TobyMosque, como disseste e bem, tanto o Contains ou o Any dá. Agora a nível de melhor solução já não consigo ter um bom feedback
Answer: Caso esteja a usar EF5 ou superior, acredito que a melhor solução envolva um Contains.
var gruposID = db.Itens.Select(item => item.GrupoId).Distinct().ToList();
var listaGrupos = db.Grupos.Where(grupo => gruposID.Contains(grupo.GrupoId));

Listar grupo apenas que contenham itens


jQuery iterate through table column

Question: I'd like to iterate through a tables columns. Given I have a cell Id, I then want to get the values of the rest of the cells in that column.
The reason for this is, I want to add some validation so that 3 cells in a column, can't all contain the same class.
I'm open to ideas using both jQuery or PHP.
If you require more information, please comment.
Answer: In JQuery to get an item's column:
var column = $('#myID').index();

To parse through the table by the nth column:
var columnNth = $('#myID').index() + 1;
var items = [];

$('#tblID tbody tr td:nth-child('+columnNth +')').each( function(){
   //add item to array
   items.push( $(this).attr('class'); );
});

jQuery iterate through table column


How can I set the ACL of a CA programmatically?

Question: When launching the CA console (certsrv.msc), I can right-click on my CA, select Properties and then I can modify the ACL of my CA in the Security tab. When I modify it, the changes are applied to the AD object at:
CN=MY-CA,CN=Enrollment Services,CN=Public Key Services,CN=Services,CN=Configuration,DC=contoso,DC=com

When I view it in the ADSI editor, the modified ACL entries are not inherited, but directly applied (as expected). But when I change the ACL of this object programmatically, e. g. by:
Import-Module ActiveDirectory
$path = 'AD:CN=MY-CA,CN=Enrollment Services,CN=Public Key Services,CN=Services,CN=Configuration,DC=contoso,DC=com'
$acl = Get-Acl -Path $path
$acl.AddAccessRule(...)
Set-Acl -Path $path -AclObject $acl

Then I can see those changes in the ADSI editor, but I cannot see those changes, when I view the ACL of my CA via the CA console.
So, am I doing something wrong? Do I have to modify other objects programmatically as well?

What I tried:

After the change, I forced the AD to sync with: repadmin /syncall /Aed
I restarted the CA service certsvc

But it does not seem to be a "sync" problem...
Answer: These ACLs are somewhat different. The ACL on AD object affects only AD entry and they are not propagated down to CA.
The ACL you see in CA Management console, are stored in CA configuration, in registry.
I'm not sure if there is an easy way to edit permissions on CA using PowerShell natively (without 3rd party tools). However, I would recommend to use the PowerShell PKI module to simplify the task. The module contains several ACL-related commands:

Connect-CertificationAuthority
Get-CertificationAuthorityAcl
Add-CertificationAuthorityAcl
Remove-CertificationAuthorityAcl
Set-CertificationAuthorityAcl

p.s. I'm the author of this PSPKI module
Every command has built-in and online help with useful examples on how to use each command.
Comment: You have to restart CA service after writing to registry. CA reads configuration only on service startup and don't look for changes until next start.
Answer: Thanks to the pointers of Crypt32 and some additional sources1, I came up with the additionally needed changes that have to be performed in the registry of the CA:
# Get binary security descriptor of the CA from the registry
$sd_bin = Get-ItemPropertyValue -Path 'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\CertSvc\Configuration\MY-CA' -Name 'Security'

# Create security descriptor object of it
$sd = New-Object -TypeName System.Security.AccessControl.CommonSecurityDescriptor -ArgumentList @($false, $false, $sd_bin, 0)

# Modify the DACL in place
$sd.DiscretionaryAcl.AddAccess([System.Security.AccessControl.AccessControlType]::Allow, "<SID>", <INT32_PERMISSIONS>, [System.Security.AccessControl.InheritanceFlags]::None, [System.Security.AccessControl.PropagationFlags]::None)

# Convert the security descriptor back to binary form
$sd_bin_new = [System.Byte[]]::CreateInstance([System.Byte], $sd.BinaryLength)
$sd.GetBinaryForm($sd_bin_new, 0)

# Write it back to the registry
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\CertSvc\Configuration\MY-CA' -Name 'Security' -Value $sd_bin_new

# Restart the certificate services
Restart-Service -Name CertSvc

1Further reading:

TechNet: Setting Certificate Authority Security Permissions with Powershell
SO: Powershell SDDL modification
CommonSecurityDescriptor Class
(Use PowerShell to Convert SDDL to Binary Format)
(Win32_SecurityDescriptorHelper class)


Calculating variance of estimated intercept parameter, $\hat\beta_0$

Question: I have the following sample :
$$
\begin{array}{c|lr}
X&80&100&120&140&160&180&200&220&240&260\\
\hline
Y & 70 &65&90&95&110&115&120&140&155&150 \\
\end{array}
$$
Now i have to calculate
$\mathbb V(\hat\beta_0)=\sigma^2[\frac{1}{n}+\frac{\bar X^2}{\sum(X_i-\bar X)^2}]$
here,
X is independent  and Y response variable.
$\beta_0$ intercept parameter
$\sigma^2$ population variance
But i have my sample data.
How can i calculate $\sigma^2$ ?
Answer: You can't calculate the population variance $\sigma^2$, but you can estimate it with $$\widehat{\sigma^2} = \frac{\Sigma (Y_i - \hat Y_i)^2}{n-2}.$$
Here, $\hat Y_i= \beta_0 + \beta_1 X_i$ denotes the predicted value of $Y$ given $X_i$.
Comment: Why is not the formula $\mathbb V(\hat\beta_0)=\hat\sigma^2[\frac{1}{n}+\frac{\bar X^2}{\sum(X_i-\bar X)^2}]$ ? that is , why not it considering the estimated variance in place of population variance ?
Comment: It depends on your model assumptions: If you start with a known population variance $\sigma^2$ of your error $\varepsilon$ in $Y=\beta_0 + \beta_1 X + \varepsilon$, you can calculate the proper variance of the BLUE $\hat \beta$. If you don't start with a known population variance, you need to estimate it.
Comment: Thank you. Now it is clear


Azure Logic App - Loop to write to SharepointList

Question: I have a bit of a challenge here!
I receive data from a Form Response trigger as shown in the code below.
I have quite complex flow behind it already, but in the end, I want it to write data to a sharepoint list. The challenge is that it would need to sometimes loop based on the number of answers given in the form response.
For example, when someone replied "Product Company" (as in example) the Input in the Sharepoint should be as follows :

ReportName
UserRole
FilterValue
User

PTS - WOL Campaign Report
Product Company
HOP
email@example.com

PTS - WOL Campaign Report
Product Company
ITH
firstname.lastname@example.com

How should I start?? I have no clue.. :/
{
    "statusCode": 200,
    "headers": {
        "Pragma": "no-cache",
        "Transfer-Encoding": "chunked",
        "Vary": "Accept-Encoding",
        "Content-Length": "493"
    },
    "body": {
        "responder": "JaneDo@XXXcom",
        "submitDate": "1/20/2022 4:32:08 PM",
        "ra22de8ef4d2744f49662d98f618bb614": "",
        "rb6faae7228c4454d91422175db06cfde": "PTS - WOL Campaign Report",
        "rc456e0fddc9440a09a67cbf979173354": "Product Company",
        "rec611b6290ae48179ad62f7d4f0c7a50": "[\"HOP\",\"ITH\"]",
        "r958deb1b0a9c411a8d8a9ccbed256689": "",
        "r80943943042b414c80c3d56b6cb669e8": "Myself",
        "r35825e12ace649ec964d24a86d851762": "Testing"
    }
}

Comment: But in the example JSON you've given, there is no array, so what are you wanting to loop through? Does that JSON structure ever look different?
Comment: Hi, well.. that is the struggle!
I think the 2 values to loop are pretty clear right? the 6th value of the body
Comment: Nah, wasn't clear to me but then I had a boys looks.  Does this change ...  "rec611b6290ae48179ad62f7d4f0c7a50" ... response on response?  And by change, I mean, does that GUID change in the response?  I understand the values may differ but I'm keen to know if that key does.
Comment: No that guid will not be changing, just the values.
Answer: Try this ...!  Insert a Parse JSON action using your payload to generate the schema.  There's a button there that says Generate from sample.
The next action after that should be another Parse JSON step with this as the schema ...
{
    "type": "array",
    "items": {
        "type": "string"
    }
}

... then you want to assign your array property as the content that came from the previous Parse JSON action ...

Underneath that, define a variable that will hold the individual value from the inner array.

... now underneath that, loop through each item in the newly created array.

Result
This is the end result, the loop traverses over the two objects that were in the sample you provided.
Within that loop, you can get all of your other properties and create as many new list items as the array has items.
Item 1

Item 2
Comment: @Robin666 "Hero" might be a stretch but you're welcome.  :-)
Comment: My hero ! thanks alot


Find Fourier series of Indicator function

Question:
Find Fourier series of $f(x)$:
  $$
f(x)=\begin{cases}
1,\ \ a\leqslant x\leqslant b\\
0\ \ \ \text{otherwise}
\end{cases};\ \ \ \ \ [a,b]\in[-\pi;\pi]
$$

Here is my attempt:
$$
\begin{aligned}
&f(x)\sim a_0+\sum_{n=1}^\infty \left(a_n\cos nx+b_n\sin nx\right)=S(x)\\
&a_0=\frac{1}{2\pi}\int\limits_a^b 1\cdot dx=\frac{b-a}{2\pi}\\
&a_n=\frac{1}{\pi}\int\limits_a^b\cos nx dx=\frac{1}{\pi n}(\sin bn-\sin an)\\
&b_n=\frac{1}{\pi}\int\limits_a^b\sin nx dx=\frac{1}{\pi n}(\cos an-\cos bn)\\
&\Rightarrow S(x)=\frac{b-a}{2\pi}+\frac{1}{\pi}\sum_{n=1}^\infty\frac{1}{n}((\sin bn-\sin an)\cos nx+(\cos an-\cos bn)\sin nx)=\\
&=\frac{b-a}{2\pi}+\frac{1}{\pi}\sum_{n=1}^\infty\frac{1}{n}(\sin(n(b-x))-\sin(n(a-x)))
\end{aligned}
$$
The question is can I simplify it even more? Or should my last expression be the final answer?
Answer: $$\sin(A) - \sin(B) = 2 \sin(A/2 - B/2) \cos(A/2 + B/2)$$
$A = nb - nx$ and
$B = na - nx$
$$A/2 - B/2 = \left( n \frac{b-a}{2} \right)$$
$$A/2 + B/2 = \left( n x \right)$$
Would be a more dense writing.


Show that $\lim_{n\rightarrow \infty}\frac{\mathsf {Var}[Y_1 + \dots + Y_n]}{n^2} = 0.$

Question: Let $(X_n)_{n=1}^{\infty}$ be a sequence of pairwise independent, identically distributed random variables with finite mean.
Let $Y_n = X_n\mathbf1\{{|X_n| \leq n\}}$, then
$$ \sum_{n=1}^{\infty} P(Y_n \neq X_n) = \sum_{n=1}^{\infty} P(|X_n| >n)  = \sum_{n=1}^{\infty} P(|X_1|>n) \leq \mathbb{E}[|X_1|] < \infty. $$
Hence from Borel-Cantelli lemma $\lim_{n\rightarrow \infty}\frac{X_1 + \dots + X_n}{n} = \mathbb{E}[X_1]~a.s$  if and only if $\lim_{n\rightarrow \infty}\frac{Y_1 + \dots + Y_n]}{n} = \mathbb{E}[X_1]~a.s.$
Now I have to show that $\lim_{n\rightarrow \infty}\frac{\mathsf {Var}[Y_1 + \dots + Y_n]}{n^2} = 0$, unfortunately I don't know how.
I would really appreciate any hints or tips.
Comment: By the way, I don't get your "iff" statement on the Var. Could you copy the full and precise statement of  your problem please ?
Comment: From Borel-Cantelli lemma and Toeplitz lemma we have that $ \lim_{n\rightarrow \infty}\frac{X_1 + \dots + X_n}{n} = \mathbb{E}[X_1] a.s $ iff $ \lim_{n\rightarrow \infty}\frac{Y_1 + \dots + Y_n]}{n} \mathbb{E} = [X_1] $ a.s. For that reason I assumed also that $\lim_{n\rightarrow \infty}\frac{\mathsf {Var}[X_1 + \dots + X_n]}{n^2} = 0$ iff $\lim_{n\rightarrow \infty}\frac{\mathsf {Var}[Y_1 + \dots + Y_n]}{n^2} = 0.$ But now I'm not sure is it true.
Comment: Suppose that the $X_i$ have a finite second moment. Since they are pairwise independent $V(\sum_{i=1}^n X_i)=\sum_{i=1}^n V(X_i) = nV(X_1)$ and the conclusion follows. Why are you interested in $V(\sum_{i=1}^n Y_i)$ ?
Comment: The interest of this kind of truncation methods is precisely to bypass these kind of  assumptions ! Nice (but tough) exercise, where does it come from ?
Comment: Could you give some elements on my question please ?
Answer: I come after the battle, but here it is : yet another answer,
\begin{align*}
Var(Y_1+\ldots+Y_n)
& =\sum_{i=1}^n Var(Y_i) \\
&=\sum_{i=1}^n Var(X_i 1_{|X_i|\le i}) \\
&=\sum_{i=1}^n Var(X_1 1_{|X_1|\le i}) \\
&\le \sum_{i=1}^n \mathbb E[X_1^2 1_{|X_1|\le i}] \\
& =  \mathbb E[X_1^2 \sum_{i=1}^n 1_{|X_1|\le i}] \\
& \le n  \mathbb E[X_1^2 1_{|X_1| \le n}]
\end{align*}
Therefore :
$$ \frac{Var(Y_1+\ldots+Y_n)}{n^2} \le \mathbb E\Big[\frac{X_1^2}{n} 1_{|X_1| \le n}\Big] $$
Now we have the bound
$$\frac{X_1^2}{n} 1_{|X_1| \le n} \le |X_1|$$
the left hand side converges to $0$ a.s., while the right hand side is an integrable bound (assuming $X_1$ has a finite expectation)
that allows to apply Dominated Convergence Theorem to the effect that :
$$\mathbb E\Bigg[ \frac{X_1^2}{n} 1_{|X_1| \le n}\Bigg] =o(1)$$
Comment: Why does $\frac{X_1^2}{n} 1_{|X_1| \le n}$ converge a.s to $0$ ?
Answer: For this proof we need $E|X|<\infty$ (it seems that this is the case).
Notice that
$$E(Y_k^2)\leq \int_0^k 2tP(|X|>t)dt. $$
Hence, by Fubini's theorem
\begin{align}
\sum^\infty_{k=1}\frac{E(Y_k^2)}{k^2} &\leq \int_0^\infty \left(\sum^\infty_{k=1}\frac{1_{(t\leq k)}}{k^2}\right)2tP(|X|>t)dt\\
&  \int_0^\infty\left( \sum^\infty_{k=t}\frac{1}{k^2}\right)2tP(|X|>t)dt\\
& \leq \int_0^\infty \left( \sum^\infty_{k=t}\frac{2}{k(k+1)}\right)2tP(|X|>t)dt\\
& = \int_0^\infty \left( \sum^\infty_{k=t}\frac{2}{k} - \frac{2}{k+1}\right)2tP(|X|>t)dt\\
& \leq \int_0^\infty \frac{2}{t}2tP(|X|>t)dt = 4E|X|<\infty
\end{align}
So, we proved that
$$\sum^\infty_{k=1}\frac{E(Y_k^2)}{k^2} $$
converges. Then, by Kronecker's lemma,
$$\lim_{n\rightarrow \infty}\frac{1}{n^2}\sum^n_{k=1}E(Y_k^2) =0.$$
Additional thoughts: After reading the comments, I think this may help you.
You can use Kolmogorov's maximal inequality (or a Martingale in $L_2$ convergence Theorem) to prove that if
$$ \lim_{n\rightarrow \infty}\sum^n_{k=1}\frac{\text{Var}(Y_k^2)}{k^2}$$
converges, then
$$\lim_{n\rightarrow \infty}\sum^n_{k=1}\frac{Y_k -EY_k}{k} $$
converges a.s..
Hence, by Kronecker's lemma,
$$\lim_{n\rightarrow \infty}\frac{1}{n}\sum^n_{k=1}(Y_k -EY_k)=0. $$
Using Dominated Convergence Theorem, it's easy to show
$$E Y_k \rightarrow EX. $$
Therefore, by Cesàro's Lemma,
$$\frac{1}{n}\sum_{k=1}^n Y_k = EX,\ a.s. .$$
Now, you can use your Borel-Cantelli Argument to prove that the above expression implies
$$\lim_{n\rightarrow \infty}\frac{1}{n}\sum^n_{k=1}X_k=EX,\ a.s.. $$
This is basically the proof for the Strong Law of Large Numbers in the case i.i.d. with $E|X|<\infty.$
Answer: Since the $Y_i$ are independent, it is enough to show that $\frac{1}{n^2}\sum_{k=1}^n{Var(Y_k)} \rightarrow 0$.
Now, $\mathbb{E}[Y_k^2]=\int_0^{k^2}{P(Y_k^2 > a)\,da}=\int_0^k{2uP(k > |X| > u)\,du} \leq \int_0^k{2uP(X > u)\,du}$.
Denote as $F(u)=\int_u^{\infty}{P(|X|>a)\,da}=\mathbb{E}[|X|1(|X| > u)]$, then $uP(X>u)=F(u)-\frac{d}{du}(uF(u))$.
As a consequence, $\mathbb{E}[Y_k^2] \leq 2\int_0^k{F(u)\,du}-kF(k) \leq 2\int_0^k{F(u)\,du}$.
Note that $F$ is non-increasing and goes to $0$, so $\int_0^k{F(u)\,du}=o(k)$, hence $Var(Y_k) =o(k)$, which ends the proof.
Comment: Very nice proof ! +1


Locally nilpotent derivations closed under isomorphism?

Question: I have been struggling to prove or find a counterexample to the following:
Fix a field $k$ of characteristic $0$. Let $R:= k[x_1,...,x_n]$ and $A$ be the ring of differential operators on $R$ (so $A$ is isomorphic to the $n$th Weyl algebra; in particular, it is just a ring of polynomials in the operators $\hat{x_1}, ..., \hat{x_n}, \partial_1,...,\partial_n$, where $\hat{x_i}$ is the multiplication by $x_i$ operator and $\partial_i$ is the ith partial derivative operator). We say an operator $D \in A$ is locally nilpotent if for all $f \in k[x_1,...,x_n]$ there exists a positive integer $n$ so that $D^n(f) = 0$. Let $\phi: A \to A$ be an automorphism of $A$. If $D$ is a locally nilpotent $k$-derivation in $A$, is the same true of $\phi(D)$? If this is true, does the statement hold in general when $R$ is a finitely generated integral domain over $k$?
Any thoughts would be appreciated.
Answer: No, this is not true - in fact, automorphisms of $A$ need not even send derivations to derivations. Let $n=1$ so that the Weyl algebra is $k\langle x,\partial\rangle/(\partial x- x\partial=1)$. Then $\partial$ is locally nilpotent: if $p$ is a polynomial of degree $n$, $\partial^{n+1}p=0$, while $x$ acts injectively on $k[x]$. But there is an automorphism sending $\partial\mapsto x$ and $x\mapsto -\partial$.
Comment: Thank you, I overlooked this case in posing the question, and had in mind the case that a given derivation in $A$ is the image of a derivation in $A$ under an automorphism. Maybe there is a simple counterexample in this setting as well?
Comment: @user450138 If there is a simple counterexample, I don't see it right now. I'm even a little unsure about what the conditions one would need to place on $\phi$ to get that $\phi(D)$ is a derivation - the classification of automorphisms might be the next place to look (Dixmier, 1960s, I think?) to see what one of these things has to do.
Comment: Thanks, I will accept your answer as it was my fault for not phrasing the question correctly. I had the following particular family of automorphisms in mind: let $m <n$ be a positive integer and for $i \in \{1,...,n\}$, if $i \leq m$ $f_i$ be a polynomial in the variables $x_{m+1},...,x_n$ and otherwise set $f_i = 0$. The automorphisms I had in mind are given by $x_i \mapsto x_i +f_i$ and $\partial_i\mapsto\partial_i-\sum_{k=1}^n \partial f_k/\partial x_i \cdot \partial_k$. Local nilpotence for derivations is seemingly preserved (in general, I don't understand the automorphism group of $A_n$).
Comment: I'm sorry that this question did not lead to a better answer. For what it's worth, I don't really understand the whole automorphism group either.


Cast webview android studio

Question: Estou fazendo um app para meu site, que é só uma webview, alguém pode me dizer se é possível colocar para transmitir nas TV Smart? se sim, pode me indicar onde dar uma estudada sobre? ou até me ajudar por aqui mesmo, só tenho a WEBVIEW até agora.
Answer: Recentemente eu estava dando suporte ao meu app de download de vídeos justamente ao suporte de cast, eu encontrei as seguintes opções:
Google cast SDK
Com ele você pode transmitir vídeo, áudio ou a dela de algum app ou jogo, ele está disponível para android, ios e chrome, está bem documentado mas para você aprender vai ter que se esforçar um pouco, dependendo do que você tem em mente você vai ter que registra esse app no console do google cast que custa 5 dólares, mas se não deseja customizar ui do receptor ou algo semelhante pode usar o player padrão que é de graça, creio que o google cast só da suporte a tvs com android a aparelhos do google cast como o Chromecast.
Samsung Smart View SDK
Este sdk visa televisores com o sistema operacional da Samsung, o Tizem. Mas o próprio site já recomenda que você use em conjunto com o google cast para um maior suporte, existe um guia de integração no site.

Cast webview android studio


Definition of completion of a measure space

Question: On a measure space $(\Omega,\mathcal{A},\mu)$ a completion of a measure is defined as:
$\{A: A_1\subset A\subset A_2$ with $A_1,A_2\in\mathcal{A}$ and $\mu(A_2\backslash A_1)=0\}$
I'm trying to show that this set is equivalent to:
$\{A\cup N:A\in\mathcal{A}$, and $N\subset B\in\mathcal{A}$ for some $B$ with $\mu(B)=0\}$
and
$\{A\triangle N:A\in\mathcal{A}$, and $N\subset B\in\mathcal{A}$ for some $B$ with $\mu(B)=0\}$
where $\triangle$ is the symmetric difference.
To show that the first definition is equivalent to the second, I showed that some $A_a$ in the first set is in the second set (which was pretty easy since $A_a\in\mathcal{A}$ implies that it is in $\mathcal{A}\cup N$. However, I am having trouble going the other direction.
For the third set, I'm having a hard time seeing the picture.
Thanks for your help.
Answer: I call these families $\mathcal{A}_1,\mathcal{A}_2,$ and $\mathcal{A}_3$.
$\mathcal{A}_1\subset\mathcal{A}_2$:
Let $A_1\subset A\subset A_2$ with $\mu(A_2\backslash A_1)=0$. Then $A=A_1\cup N$ where $N=A\cap (A_2\backslash A_1)$. So $A\in\mathcal{A}_2$.
$\mathcal{A}_2\subset\mathcal{A}_3$:
Take $A\cup N$, where $N$ is a subset of a measurable set $B$ of measure $0$. Let $N'=N\backslash A\subset B\backslash A$. Clearly, $\mu(B\backslash A)=0$. Now $$A\triangle N'=(A\backslash N')\cup (N'\backslash A)=A\cup N'=A\cup N\backslash A=A\cup N.$$
$\mathcal{A}_3\subset\mathcal{A}_1$:
Take $A\triangle N$, where $N$ is a subset of a measurable set $B$ of measure $0$. Let $A_1=A\backslash B$ and $A_2=A\cup B$. Now $A_1=A\backslash B\subset A\backslash N\subset A\backslash N\cup N\backslash A=A\triangle N\subset A\cup N\subset A\cup B=A_2$. Now $A_2\backslash A_1=(A\cup B)\backslash (A\backslash B)=B$, so $\mu(A_2\backslash A_1)=0$.
Comment: Hi, Thank you so much for your concise proof. I have two questions. First, when you prove, say, $ \mathcal{A}_1 \subset \mathcal{A}_2$, do you change the expression of $\mathcal{A}_2$ in terms of $\mathcal{A}_1$?
Comment: I added a light more details in the first one by $ \mathcal{A}_1 \subset \mathcal{A}_2$: let $A_1 \subset A \subset A_2 $ with $A_1, A_2 \in \mathcal{A}, \mu(A_2\backslash A_1)=0$, then $A=A_1 \cup N$, where $N= A \cap (A_2 \backslash A_1) \subset (A_2\backslash A_1)=B$ having $\mu(B)=0$ and $A_1\in \mathcal{A}$. Not sure if I am doing it correctly.
Comment: Also, if my understanding is correct., I am a bit confused with your second proof where you showed $N \backslash A \subset B \backslash A , \mu(B \backslash A)=0$. Should't it be enough to show $N' \subset N \subset B$?
Comment: @JoZ If I understand the first part of your comment; these are essentially three independent proofs of implications that jointly imply that all conditions are equivalent. I'm to sure what you ask about the second proof; the goal is to show that a set of the form $A\cup N$ can be written as a set of the form $A\Delta N'$.
Comment: Thank you I think I got the point!
Answer: Another way of approaching this is to prove that each of these families ($\mathcal A_1$, $\mathcal A_2$ and $\mathcal A_3$) is equal to $\sigma(\mathcal A \cup \mathcal N)$ which is the sigma algebra generated by $\mathcal A \cup \mathcal N$ where $\mathcal N$ is the collection of subsets of null sets in $\mathcal A$. This approach, which can be more work than the direct approach of simply showing $\mathcal A_1 \subset \mathcal A_2 \subset \mathcal A_3 \subset \mathcal A_1$,, has a benefit of making one see motivations for each of the three definitions.
Let's start with $\mathcal A_1$. To show that this is equal to $\sigma(\mathcal A \cup \mathcal N)$, one only needs to show that $\mathcal A_1$ is a sigma algebra containing $\mathcal A \cup \mathcal N$ and that each element of $\mathcal A_1$ can be obtained by applying countable union, countable intersection, and complement several times to some elements in $\mathcal A \cup \mathcal N$.
It is easy to show that each element of $\mathcal A_1$, $\mathcal A_2$ and $\mathcal A_3$ can be obtained by applying countable union, countable intersection, and complement several times to some elements in $\mathcal A \cup \mathcal N$. Therefore, each of $\mathcal A_1$, $\mathcal A_2$ and $\mathcal A_3$ is a subset of $\sigma(\mathcal A \cup \mathcal N)$.
It is also easy to show that each of $\mathcal A_1$, $\mathcal A_2$ and $\mathcal A_3$ contains $\mathcal A \cup \mathcal N$.
Now it only remains to show that they are sigma algebras. For that, read the following thread first:
Completion of a measure space
The question in that thread is concerned with a collection that you can easily see is equal to $\mathcal A_3$ (Equality follows from $A \Delta B = C \iff B \Delta C = A \iff C \Delta A = B$).
Michael Greinecker's answer in that thread contains a proof of the fact that $\mathcal A_2$ is a sigma algebra.
Jisang Yoo's answer in that thread contains a proof of the fact that $\mathcal A_3$ is closed under finite union, but you can easily generalize the proof to show that it is closed under countable union as well.
As for how to prove that $\mathcal A_1$ is closed under countable union, let $(A_i)$ be a sequence of elements in $\mathcal A_1$. For each $i$, there are $A_i'$ and $A_i''$ in $\mathcal A$ that approximate $A_i$ from below and above in the sense that $A_i' \subset A_i \subset A_i''$ and $\mu(A_i'' \setminus A_i') = 0$.
You'd expect that the union $\bigcup_i A_i$ would be approximated by $\bigcup_i A_i'$ and $\bigcup_i A_i''$ in the same sense. Is it true that $\mu(\bigcup_i A_i'' \setminus \bigcup_i A_i') = 0$ holds? Yes because the final total exception set $\bigcup_i A_i'' \setminus \bigcup_i A_i'$ must be covered by the union of the exception sets we started with, namely $A_i'' \setminus A_i'$, but these are null sets.


Help with integral with parameter.

Question: I have this integral with parameter:
$$g(y)=\int_{0}^{\frac{\pi}{y}}\frac{\tan^2(xy)}{2x}dx$$
Also, $y>0$
So i want to calculate $g'$.
How can i do this?
Any help would be really helpful.
Comment: I believe the integral is divergent for all $y$ so that the $g$ is ill-defined.
Answer: Hint. Use the Leibniz integral rule. The first step is
$$g'(y)=\frac{\tan^2((\pi/y)\cdot y)}{2(\pi/y)}\cdot \frac{d}{dy}\left(\pi/y\right)
+\int_{0}^{\pi/y}\frac{d}{dy}\left(\frac{\tan^2(xy)}{2x}\right)dx.$$
Answer: Hint: This one is tricky, but the general idea is that if $$g(x, y) = \int_{a}^{x}f(t, y)\,dt\tag{1}$$ then $$dg = f(x, y)\,dx + \left(\int_{a}^{x}\frac{\partial }{\partial y}f(t, y)\,dt\right)dy\tag{2}$$ Apply this general rule on your integral.
Explanation: If $g(x, y)$ is a function of two variables then we know that $$dg = \frac{\partial g}{\partial x}\,dx + \frac{\partial g}{\partial y}\,dy$$ Here the function $g(x, y)$ depends on $x$ which is upper limit of integral (and hence we get $\partial g/\partial x$ via Fundamental Theorem of Calculus) and it also depends on $y$ which is a parameter under integral (and hence $\partial g/\partial y$ requires differentiation with respect to parameter $y$ under the integral sign).
Answer: $\newcommand{\angles}[1]{\left\langle\,{#1}\,\right\rangle}
 \newcommand{\braces}[1]{\left\lbrace\,{#1}\,\right\rbrace}
 \newcommand{\bracks}[1]{\left\lbrack\,{#1}\,\right\rbrack}
 \newcommand{\dd}{\mathrm{d}}
 \newcommand{\ds}[1]{\displaystyle{#1}}
 \newcommand{\expo}[1]{\,\mathrm{e}^{#1}\,}
 \newcommand{\half}{{1 \over 2}}
 \newcommand{\ic}{\mathrm{i}}
 \newcommand{\iff}{\Longleftrightarrow}
 \newcommand{\imp}{\Longrightarrow}
 \newcommand{\Li}[1]{\,\mathrm{Li}_{#1}}
 \newcommand{\mc}[1]{\mathcal{#1}}
 \newcommand{\mrm}[1]{\mathrm{#1}}
 \newcommand{\ol}[1]{\overline{#1}}
 \newcommand{\pars}[1]{\left(\,{#1}\,\right)}
 \newcommand{\partiald}[3][]{\frac{\partial^{#1} #2}{\partial #3^{#1}}}
 \newcommand{\root}[2][]{\,\sqrt[#1]{\,{#2}\,}\,}
 \newcommand{\totald}[3][]{\frac{\mathrm{d}^{#1} #2}{\mathrm{d} #3^{#1}}}
 \newcommand{\ul}[1]{\underline{#1}}
 \newcommand{\verts}[1]{\left\vert\,{#1}\,\right\vert}$

Lets consider a 'more' general case: $\ds{\,\mrm{G}\pars{y} \equiv \int_{0}^{\pi/y}{\mrm{f}\pars{xy} \over 2x}\,\dd x}$ for 'some'
  $\ds{\,\mrm{f}}$ such that $\ds{\mrm{G}\pars{y}}$ converges. Indeed,
  $\ds{\,\mrm{G}\pars{y}}$ is $\ds{y}$-independent:

\begin{align}
\mrm{G}\pars{y} & =
\int_{0}^{\pi/y}{\mrm{f}\pars{xy} \over 2x}\,\dd x
\,\,\,\stackrel{x\ \equiv\ t/y}{=}\,\,\,
\int_{0}^{\pi}{\mrm{f}\pars{t} \over 2t}\,\dd t\quad\imp\quad
\color{#f00}{\mrm{G}'\pars{y}} = \color{#f00}{0}
\end{align}

As pointed out by $\texttt{@Jack Lam}$, the "OP-original integral" diverges because
  $\ds{\tan^{2}\pars{t} = {1 \over \tan^{2}\pars{\pi/2 - t}} \sim
{1 \over \pars{t - \pi/2}^{2}}}$
  as $\ds{t \to \pi/2}$.
Comment: But that integral doesn't converge so..... *flies out the window*
Comment: @JackLam Thanks for your remark. I changed my answer to point out the original idea. It was my fault I didn't pay attention to the divergence.

Help with integral with parameter.


How to estimate the change of rate of COVID-19 infections when accounting for vaccine effectiveness and increased transmissibility of mutated strains?

Question: The world has reasonably accurate numbers of the transmissibility of the strains of COVID-19 that have ravaged humankind.
Now, two changes are afoot:

Some wealthy regions have access to limited quantities of vaccines (although most poor regions continue to be without any access to vaccines).  Each vaccine has its own scientific estimation of efficacy.

New mutated strains of COVID-19 have been discovered with significantly higher levels of transmissibility.

What is the correct way to take into account those two changes when projecting case rates?
For example, if you have a vaccine that is 80% efficacious against a new strain, but that new strain is 80% more transmissible than the original strain (and that new strain becomes dominant, which is scientifically most likely), what is the estimated net outcome in case rates (removing other variables)?  Is it zero?  To keep things simple, let us assume that the new strain will account for all new cases.
As another example, with the same conditions and removal of variables as above, if the vaccine is 65% efficacious against the new strain, but that new strain is 80% more transmissible than the original strain, what is the estimated net outcome in case rates?
Comment: Purely statistical methods are a (spectacularly) poor choice for addressing such questions: you need an *epidemiological model* of transmission, infection, vaccination efficacy, and case identification.
Comment: I am very open to those possibilities -- but I also am aware of so many of those "shades of gray," and have so much experience with this, that I'm trying to help you out by saving you some wasted effort (or worse, developing and communicating misleading results).  But I appreciate that you don't value that and therefore won't continue my efforts to help you.  I do, however, want to make sure other readers of this thread are not similarly misled.
Comment: Are you really interested in just case transmission rates?  What if someone gets the virus, but has no symptoms and incurs no costs as a result?  Have you defined "case" more carefully?
Comment: @StatsStudent Yes, I am really interested in transmission rates.  If someone gets the virus, but has no symptoms or financial costs, that is still an infection case.
Comment: @whuber I partially agree, except that I'm not interested in publishing results.  I'm only interested in this estimation for personal understanding.
Comment: A purely statistical model is not going to advance your understanding.  The understanding comes when your model incorporates meaningful, interpretable aspects of the situation.
Comment: @whuber Let's not let ***perfection*** stand in the way of ***good enough***.
Comment: Let me be perfectly clear, then: the direction you are pursuing has no chance of being "good enough" unless "good enough" is extraordinarily untrustworthy and uninformative.
Comment: @whuber Are you open to the possibility that you see things in terms of black and white and are unable to see things in shades of gray?  Are you open to the possibility that sometimes you are mistaken and are unable to see the forest for the trees?  I think it would be good if you move on, and let others help to the best of their abilities.  Thanks for your understanding.
Comment: @whuber Thank you for (almost) moving on.  No one is being misled by anyone, despite your conclusions based on inaccurate assumptions and your choice to try to somehow cast blame and/or create the shadow of doubt.  Have a pleasant night/day.
Answer: You are not gonna get an answer
Exactly predicting the influence of effects such as mutated strains is extremely difficult. Even governments with lot's of mathematicians and big computers working for them are not well capable of making good predictions (or even bad predictions).
Predicting the course of Covid is like predicting the weather more than 14 days ahead of time but with very inaccurate models and inaccurate measurements.

Models that are inaccurate about details. Like

How do people move around, where do contacts take place, what is the influence of news and politics, etc.
In which ways does the virus transmit? What are effects of measures on this (masks, distancing, ventilation, hand-washing, vitamin-D and exercise, etc.)

Measurements of the current weather (state of the virus) that are inaccurate

What are the current epidemiological parameters? How do they vary as function of weather (effects of temperature, humidity, wind), age of the people, health of the people, immunity due to vaccines, immunity to having had the disease (and how long this lasts), location (variations in population density, human behavior, state of health, age, partial herd immunity).

What is different for the new strain? People believe that several strains have a selective advantage, but this is mostly based on simple measurements about the total cases (and theory about the changes in the proteins).
How is the strain better or more fit? Does it spread easier or faster? Is less viral load needed or does it create a high viral load? Does it survive better in some environments? Is it having some (partial) resistance to immunity in some people (how much and in which people?)? Is its incubation time shorter or is it's infectious phase longer? Etc.

You are gonna get an answer
Intuition of the mechanics by means of a game
If your goal with this question is just to gain some insight into the underlying theoretical considerations then you might get some improvements in your intuition.
In this part of the question, I am gonna give some intuition by replacing viruses with dice. I am actually currently making a board game that includes the aspect of virus mutations, and now even my 5-year-old son "understands" the aspects of the mechanics and aspects of probability for an epidemic.

The game mimics transmission by rolling several dice. When you roll a six then an infection occurs. When you roll 2/3 or more sixes then the newly infected person gets a headache as well (2 when it's an adult, 3 for kids).
Let's see what the probability is for rolling at least 1 six.

Note: It is not linear! Reducing transmissibility rate by $x \%$ (changing the number of dice, the viral load) may not be the same reduction in transmissibility probability per contact.
In this game, the virus variant will increase the number of dice used in the infection by 'two dice' (in a real-world the variant might also increase the rate of infection, the viral load, in some way).
Already for this very basic game, it is very difficult to compute the effect of the variant (the two extra dice). There are several ways in which the effect of higher infectivity will have different effects that vary due to inhomogeneities.

When you change the number of dice from 10 to 12 then the effect (probability increases a bit from 84% to 89%) is a lot different from when you change the number of dice of 1 to 3 (Increase from 17% to 42%, more than doubled).
There are several ways in which increased infectivity will have inhomogeneous effects. The effect of increased infectivity is for this game the largest when the infectivity is low (I believe the same will be true for the real world). When the probability of infection is high, for instance inside families, then people are gonna get sick anyway (at least with high probability).
(The same is true for the interventions/measures like mask-wearing that are in the opposite direction as the virus variant; For these as well the effects will be variable and depend on the situation. There is another question here where this came across as well.)

The types of spread and the population are inhomogeneous. Some areas are more crowded than others, have more complex networks with bigger knots, etc. Because of these, you may have different effects of the increased transmissibility of the variant (the increase of dices).

It is wrong to think of some virus mutation to increase the R(t) value (the reproduction number) or the transmissibility rate or transmissibility probability.
Those figures eventually are just based on an estimated/observed growth rate that is rescaled to express the reproduction scale.
The growth rate might is not the same as transmissibility, and transmissibility is not the same as what changed in the virus mutant. The underlying mechanisms are complex, inhomogeneous, and change from place to place. The growth rate, or other related statistics, is just what is observed at the surface, but it is something inside the machinery that has changed.

In short
The effect of a virus mutant is not easily determined.
Let alone that the effect can be predicted/extrapolated. But what people (politicians and media) do is just extrapolate the observed effects of the mutant to other populations (without considering the mechanics and the idea that the effects may be different for different situations).
For example, in South East England scientists observe some increase of a variant. Subsequently, it became the dominant strain in London.
Then people start to use the same figures (like R value increases such and such) and plug it into their own simple model as if this effect on the surface (a particular increase of the R value) is gonna be exactly the same.
I believe that this is not the right way to handle the lack of information. The correct way is to gather more/richer/better data (and that is what people did afterward, and they found in many other places that the variant is not as strongly fast-spreading, or sometimes they found no effect at all.).

a vaccine that is 80% efficacious against a new strain, but that new strain is 80% more transmissible
...
what is the estimated net outcome in case rates
...
Is it zero?

Typically the vaccine/immunity is gonna have more effect.
In the basic SIR model, the effective reproduction number is often compared with the basic reproduction number along with the fraction of people that are not immune (non-susceptible) (the ratio of the total susceptible people $S$  over the total number of people $N$)
$$R(t) = R_0 \cdot S/N$$
So vaccination is gonna reduce that number $S$ and mutations are gonna increase $R_0$. However, the number $R_0$ does not increase linearly with an increase of transmissibility, it will be less.


Is /... or /abc/... a valid unix path?

Question: I read somewhere that /... is a valid unix path. But when I try it out, I get an error.
$ cd /...
-bash: cd: /...: No such file or directory

Can someone clarify?
Comment: you can try `/..` i read somewhere is a valid path, without error ...
Answer: It is a valid path, but "no such file or directory" with that path exists - this doesn't make the path inherently invalid.
For instance, it is trivial to create such directory and then change to it (although using such a name should generally be discouraged):
mkdir ...
cd ...

Perhaps you were thinking of . and ..?

Now, is /.. a valid path? Yes, it is a valid absolute path (because it starts with /).
In this case, /, /. and /.. all refer to the same thing - the root directory. (The parent of a root directory is itself.)
ls -ldi / /. /..

Comment: Right. `.` and `..` are special. `...` is just another name.
Comment: Ah yes, I was thinking of relative paths and ignored the fact that it can be a filename. Thanks :)

Is /... or /abc/... a valid unix path?