Why is the exponential family so important in statistics?

Question: Why is the exponential family so important in statistics?
I was recently reading about the exponential family within statistics. As far as I understand, the exponential family refers to any probability distribution function that can be written in the following format (notice the "exponent" in this equation):

This includes common probability distribution functions such as the normal distribution, the gamma distribution, the Poisson distribution, etc. Probability distributions from the exponential family are often used as the "link function" in regression problems (e.g., in count data settings, the response variable can be related to the covariates through a Poisson distribution) - probability distribution functions that belong to the exponential family are often used due to their "desirable mathematical properties". For example, these properties are the following:

Why are these properties so important?
A) The first property is about "sufficient statistics". A "sufficient statistic" is a statistic that provides more information for any given data set/model parameter compared to any other statistic.
I am having trouble understanding why this is important. In the case of logistic regression, the logit link function is used (part of the exponential family) to link the response variable with the observed covariates. What exactly are the "statistics" in this case (e.g.. in a logistic regression model, do these "statistics" refer to the "mean" and "variance" of the beta-coefficients of the regression model)? What are the "fixed values" in this case?
B) Exponential families have conjugate priors.
In the Bayesian setting, a prior p(thetha | x) is called a conjugate prior if it is in the same family as the posterior distribution p(x | thetha). If a prior is a conjugate prior - this means that a closed form solution exists and numerical integration techniques (e.g., MCMC) are not required to sample the posterior distribution. Is this correct?
C) Is the third property essentially similar to the second property?
D) I don't understand the fourth property at all. Variational Bayes are an alternative to MCMC sampling techniques that approximate the posterior distribution with a simpler distribution - this can save computational time for high dimensional posterior distributions with big data. Does the fourth property mean that variational Bayes with conjugate priors in the exponential family have closed form solutions? So any Bayesian model that uses the exponential family does not require MCMC - is this correct?
References:

Exponential family
Sufficient statistic
Conjugate prior
Comment: I would argue they're not really ubiquitous at all; they're good approximations in some cases, but even in the physics examples that are often held up as "exact" for one or the other, they're clearly neither (e.g. clicks in a Geiger counter measuring radioactive decay was traditionally held up as an exemplar of a homogeneous Poisson process, but it literally can't be the case). Of course that doesn't diminish their usefulness as models of many real-world processes, and that would be an excellent reason.
Comment: @Glen_b clicks may not be but the radioactive decay itself is certainly exactly poisson. The optimal solution to Heisenberg's inequality equation is Gaussian etc
Comment: Your use of the word "certainly" is over-optimistic. Physical theories are refined over time (like Newton's laws of motion; they're great approximations but only that; if Newton had said "certainly " he'd be wrong); this is certain to happen again and again. We know GR and quantum mechanics don't fit together as is, for example. In any case, note that the amount of material available to decay changes with time; hence my explicit mention of "not homogeneous Poisson"; ... ctd
Comment: we wouldn't use the distributions just because they have "attractive" properties though, right? Normal and Poisson distribution are simply ubiquitous in nature, that's the main reason why we use them.
Comment: ctd.. even if I had a series of Poisson counts, with changing mean using a single Poisson to describe all of them would indeed be an approximation.
Comment: I've been saying the model could be a good approximation the whole time, so I don't have any dispute with "good model". I object to claims of exactness only.
Comment: I guess that the point of @Aksakal was that some distributions from the exponential family, like the Poisson distribution and the normal distribution, are not being used just because they have attractive easy properties, but also because they theoretically match with descriptions of nature. To counter this point with a discussion about whether it is 'exact' or whether 'certainly' is overoptimistic is besides the point. ...
Comment: @stats555 Obviously reasons B-D could not be compelling to a frequentist, but GLMs are very often used in that framework. However, in large data applications - and perhaps even moreso in online and distributed calculation frameworks - the benefits of (A) might be highly relevant.
Comment: @Glen_b the complexity of nature can deviate due to such things like a varying mean, but over short periods of time a Poisson distribution might be a good model. The same is true for the normal distribution. From Gauss's measurements of the position of celestial bodies till the discovery of the Higgs particle, these distributions are being used a lot as models for descriptions of nature.
Comment: @Glen_b, ok, I was under the impression that you also had a dispute with the models being ubiquitous. Also, I would not just consider the models just as useful, and there is more. Possibly the theory might be actually really true (I agree it is not certain), and it is only the realization in practice where the outcomes are not exactly described by ideal models because the conditions are not exact and as 'pretty' as in theoretical examples.
Comment: ... The fact is that the normal distribution, Poisson distribution, and many (if not most) others had been in use much before those attractive properties of the exponential distribution became studied. Those properties are an afterthought and not *the* reason why the exponential family is so important.
Comment: But maybe this becomes semantic now. What does the OP mean by important? Is the question 'why the exponential family is important' as in 'why do we use so often members from this family' or maybe it is more meant to be 'what is so important about the exponential family' as in what is so special about it to study the properties and why is the term 'exponential family' used so often? I guess the question is more about why the *concept* of exponential families is important and not about why 'exponential family' and it's members are important.
Comment: Oh, I misunderstood which part we were talking about. Well, I guess I would object to the word ubiquitous as well  "very commonly used" or "widespread" perhaps, but  actually *ubiquitous*? I wonder if we may be understanding the meaning of the word differently. I don't dispute an assertion of importance for the models, but I wouldn't claim for them properties I can't seem to justify.
Comment: IIUC, one point is that they are all effects of the combinatorial distribution, with sample n, on an infinite population, with various scale shifts (see Sterling's approximation of n!). Sort of a 'one more dice roll / coin flip' perception of distribution(s).
Answer: Excellent questions.
Regarding A:
A sufficient statistic is nothing more than a distillation of the information that is contained in the sample with respect to a given model. As you would expect, if you have a sample $x_i \sim N(\mu,\sigma^2)$ for $i \in \{1, \ldots, N\}$ and each independent, it is clear that so long as we calculate the sample mean and sample variance, it doesn't matter what the values of each $x_i$ are. In linear regression (easier to talk about than logistic in this context), the sampling distribution of the unknown coefficient vector (for known variance) is $N(\mathbf{X}^\top\mathbf{X})^{-1}\mathbf{X}^\top\mathbf{y}, \sigma^2\mathbf{X}^\top\mathbf{X})^{-1})$, so as long as these final quantities are identical, inference based thereupon while be too. This is the idea of sufficiency.
Note that in the $N(\mu,\sigma^2)$ example, the sufficient statistic comprises of just two numbers: $\hat{\mu}=\frac{1}{N}\sum_{i=1}^N x_i$ and $\frac{1}{N}\sum_{i=1}^N (x_i-\hat{\mu})^2$, no matter how big our sample size $N$ is (and assuming $N>2$). Likewise, the vector $(\mathbf{X}^\top\mathbf{X})^{-1}\mathbf{X}^\top\mathbf{y}$ is of dimension $P$ and $\sigma^2(\mathbf{X}^\top\mathbf{X})^{-1}$ of dimension $P\times P$ (here $P$ is the dimension of the design matrix), which are both independent of $N$ (though, technically, the matrix $\sigma^2(\mathbf{X}^\top\mathbf{X})^{-1}$ is just a constant under our assumptions). So in these examples, the sufficient statistic has a fixed number of values (not fixed values), or as I would put it, fixed dimension.
Let's note three more things. First, that there is no such thing as the sufficient statistic for a distribution, rather, there are many possible statistics which may be sufficient, and which may be of different dimension. Indeed, our second thing to discuss is that the entire sample itself, since it naturally contains all information contained in itself, is always a sufficient statistic. This is a trivial case, but an important one, as in general one cannot always expect to find a sufficient statistic of dimension less than $N$. And the final thing to note is model specificity: that's why I wrote with respect to a given model above. Changing your likelihood will change the sufficient statistics, at least potentially, for a given dataset.
Regarding B: What you're saying is correct, but additionally to allowing analytic posteriors in the univariate case, conjugacy has serious benefits in the context of Bayesian hierarchical models estimated via MCMC. This is because conditional posteriors are also available in closed form. So we can actually accelerate Metropolis-within-Gibbs style MCMC algos with conjugacy.
Regarding C: It's definitely a similar idea, but I do want to make clear that we're talking about two different distributions here: "posterior" versus "posterior predictive". As the name implies, both of these are posterior distributions, which means that they are distributions of an unknown variable conditioned on our known data. A "posterior" plain and simple usually refers to something like $P(\mu, \sigma^2| \{x_1, \ldots, x_N\})$ from our normal example above: a distribution of unkown parameters defined in the data generating distributions. In contrast, a "posterior predictive" gives the distribution of a hypothetical $N+1$'st data point $x_{N+1}$ conditional on the observed data: $P(x_{N+1}| \{x_1, \ldots, x_N\})$. Notice that this is not conditional on the parameters $\mu$ and $\sigma^2$: they had to be integrated out. It is this additional integral that is guaranteed by conjugacy.
Regarding D: In the context of Variational Bayes (VB), you have some posterior distribution $P(\theta|X)$ where $\theta$ is some vector of $P$ parameters and $X$ are some data. Rather than trying to generate a sample from it like MCMC, we are instead going to use an approximate posterior distribution that's easy to work with and pretty close to the true one. That's called a variational distribution and is denoted $Q_\eta(\theta)$. Notice that our variational distribution is indexed by variational parameters $\eta$. Variational parameters are nothing like the parameters we do Bayesian inference on, and are nothing like our data. They don't have a distribution associated with them and they don't have some hypothetical role generating the data. Rather, they are chosen as a result of an iterative optimization algorithm. The whole idea of variational inference is to define some measure of dissimilarity between the variational distribution and the true posterior and then minimize that measure with respect to the parameters $\eta$. We'll denote the result of that optimization process by $\hat{\eta}(X)$. At that point, hopefully $Q_{\hat{\eta}(X)}(\theta)$ is pretty close to $P(\theta|X)$, and if we do inferences using $Q_{\hat{\eta}(X)}(\theta)$ instead we'll get similar answers.
Now where does conjugacy fit in? A popular way to measure dissimilarity is this measure, which is called the reverse KL cost:
$$ \hat{\eta}(X) := \underset{\eta}{\textrm{argmin}}\, \mathbb{E}_{\theta\sim Q_\eta}\bigg[\frac{\log Q_{\eta}(\theta)}{\log P(\theta|X)}\bigg] $$
This integral cannot be solved in terms of simple functions in general. However, it is available in closed form when:

We use a conjugate prior to define $P(\theta|X)$.

We assume that variational distribution is independent, so in other words that $q_\eta(\theta)=\prod_{j=1}^P q_{j,\eta}(\theta_j)$.

We further restrict ourselves to a particular $q_{j,\eta_j}$ for each $j$ (which is determined by the likelihood).

So it's not that the variational posterior is available in closed form. Rather, it's that the cost function which defines the variational posterior is available in closed form. The cost function being closed form makes computing the variational distribution an easier optimization problem, since we can analytically compute function values and gradients.
Comment: 1)  Property 1 states that all statistics (e.g. mu from a normal distribution, beta coefficients from a regression model, etc.) based on distributions from the exponential family are always sufficient statistics. Does this mean that ALL statistics based  on distributions that are not from the exponential are NOT sufficient? Or can you still have sufficient statistics based on distributions not belonging to the exponential family? (e.g the uniform distribution :  https://math.stackexchange.com/questions/572867/non-exponential-family-probability-distributions-and-their-uses)
Comment: 2) This may sound like a trivial question - but is there any reason that non-sufficient statistics are considered so important? In the event that a statistic is considered non-sufficient, what exactly are we losing? For example, do non-sufficient statistics have very high variance? I am a bit confused: could it be that non-sufficient statistics might still be "good", but sufficient statistics are far "better"? For example - I heard there are some cases where it might be beneficial to use a biased estimator. Could there be any cases where it is beneficial to use a non-sufficient statistic?
Comment: 3) Property 2 states that if the prior and the posterior distribution are both from the exponential family and are conjugates, then closed form solutions for the prior distributions exist. This means that we are not required to use MCMC sampling. However, I have seen countless examples online of the prior distributions of beta coefficients from a simple Bayesian Linear Regression model being calculated using MCMC. Is this done as a learning example? Is this unnecessary since Property 2 guarantees the existence of closed form solutions ? Or is this just an exercise to familiarize beginners?
Comment: @ John Madden: Thank you so much for your answer! I was not expecting such a detailed answer! I will have to re-read it several times to fully understand it! As of now, I have the following questions based on your answer:
Comment: @stats555 Regarding 3)  Yes, these could be done as examples or because the priors for each of the coefficients come from some distribution NOT in the exponential family.
Comment: here is an example of MCMC being used to calculate the posterior distributions of beta-coefficients in a basic Bayesian Linear Regression model - when in theory, closed form solutions of these posterior distributions should exist (via the conjugacy property from property 2) : https://www.r-bloggers.com/2020/04/bayesian-linear-regression/
Comment: @stats555  That's fine, seems like an introductory example to MCMC anyway.  I'm not sure what you're getting caught up on.
Comment: @stats555 (1): It's not that *all* statistics that come from exponential families are sufficient. It's that if you have an exponential family distribution, then some sufficient statistic is guaranteed to exist that has dimension less than the full sample. In general, there may be no such reduction. But indeed there are distributions outside of the exponential family that also have "real" sufficient statistics (that is to so, they aren't just the whole sample), such as the uniform that you reference.
Comment: @stats555 (2): It turns out that sufficient statistics have special properties, and can be the first step towards developing MSE-optimal estimators using a technique called *Rao-Blackwellization*: see this page for more https://en.wikipedia.org/wiki/Rao%E2%80%93Blackwell_theorem
Comment: @stats555 (3): [you meant posterior, not prior right?] That's definitely a popular learning example. But I want to emphasize again that conjugacy gets you closed form *conditionals* too. So it's not just an academic exercise. If you're writing Gibbs-style MCMC and all your variables are conjugate, each individual sample will be a closed form conditional draw rather than a Metropolis-Hastings step (for example).
Comment: Nice answer. I think your definition of reverse KL cost should have something different on the left-hand side of the $:=$.
Comment: @ John Madden: thank you so much for your replies! I will have to read them more carefully to fully understand them. I posted another question here on trying to better understand why sufficient statistics are so important in statistics: https://stats.stackexchange.com/questions/552707/what-are-the-dangers-of-using-non-sufficient-statistics . maybe you could please take a look at it if you have time later? thank you so much!
Comment: @Nathaniel Good call; I had meant to put an "argmin" on the RHS but forgot.
Answer: Since I do not see the result mentioned on that thread so far, let me mention an often neglected if significant issue, namely that, concerning question A, exponential families are closely linked with the notion of sufficiency due to the Pitman-Koopman-Darmois lemma:

Suppose $X_n$ , $n = 1 , 2 , 3 , \dots$ are independent identically
distributed random variables whose distribution is known to be in some
family of probability distributions with fixed support. Only if that
family is an exponential family there is a sufficient statistic
(possibly vector-valued) $\displaystyle T(X_{1},\dots ,X_{n})$ whose
number of scalar components does not increase as the sample size $n$
increases. Wikipedia

In short, there is no fixed-support family outside the exponential families that enjoys a fixed dimension sufficient statistic.
And while I am at it, point B is not a compelling argument as closed-form expressions are not a strong argument for defining a prior distribution that reflects one's prior beliefs. Furthermore, there exist exponential families whose conjugates are not manageable, witness the Beta distribution. Conjugates are thus better seen as handy approximations in the spirit of question D.
Comment: @SingleMalt that's correct. As a converse example, the family consisting of uniform distributions over $[0,A]$, with $A$ as the parameter, doesn't have fixed support. However, it does have a conjugate prior (the Pareto distribution) and a sufficient statistic of fixed dimension (the maximum value of all samples observed). (The Pitman-Koopman-Darmois theorem is "only if" and not "if and only if")
Comment: What does fixed-support family mean here? I understand support as where the pdf is defined, or slightly more formally its domain.
Comment: It means that the support of the distributions within this family is *fixed* or *constant* or the same for all values of the parameter.
Comment: Got it. As an example the normal and exponential distributions have support of the reals and the non-negative reals. The normal support does not depend on mu nor sigma squared, and the exponential support does not depend on lambda. That is they both have fixed support, along with all the other exponential family distributions.
Comment: Distributions with parameter dependent supports bring much more information through the observations, as they allow for the sure exclusion of some values of the parameter. Hence, no surprise that they also allow for some form of sufficiency (and hence of conjugacy).
Comment: isn't the "number of scalar components does not increase as the sample size n increases" statement equivalent to being memoryless? (another 'get your head around it' term ;-). . . We have the current parameter estimate and can add one more sample and update the estimate, without needing to remember previous samples.
Comment: @PhilipOakley: The likelihood is not memoryless as it keeps track of all "previous" observations (when a time element is involved) through the sufficient statistic, e.g. $X_1+\cdots+X_{t+1}=X_1+\cdots+X_t+X_{t+1}$.
Answer: Great questions.  There are lots of ways to answer these. John Madden does an excellent job, but I'm going to crib a little bit from Ben's answer here regarding sufficient statistics.
The loss function for a Gaussian linear model (as Ben notes) is
$$ \ell_{\mathbf{y}, \mathbf{x}}(\boldsymbol{\beta}, \sigma)=-n \ln \sigma-\frac{1}{2 \sigma^{2}}\|\mathbf{y}-\mathbf{x} \boldsymbol{\beta}\|^{2}$$
In a lot of pedagogical material on fitting models, we would use the data to compute gradients of the loss and perform some sort of optimization routine.  Some code to do this might look like
<code>def compute_loss(beta, X, y):

    number_rows = len(X)
    loss = 0

    # Potentially expensive!
    for i in range(number_rows):
        loss += (y[i] - X[i]@beta)**2

    loss/=number_rows

    return loss
</code>
If we have lots of data, then this loop (and any additional iterations over the data to compute gradients, for example) might be expensive to compute.  But, because we can write down sufficient statistics for the exponential family, we can improve our computation drastically.
As Ben writes, the loss can be rewritten as
$$
\begin{aligned}
&=-n \ln \sigma-\frac{1}{2 \sigma^{2}} \mathbf{y}^{\mathrm{T}} \mathbf{y}-\frac{1}{2 \sigma^{2}}\left(2 \boldsymbol{\beta}^{\mathrm{T}} \mathbf{T}_{1}-\boldsymbol{\beta}^{\mathrm{T}} \mathbf{T}_{2} \boldsymbol{\beta}\right) \\
&=h(\mathbf{y}, \sigma)+g_{\beta}\left(\mathbf{T}_{1}, \mathbf{T}_{2}, \sigma\right)
\end{aligned}
$$
Where $\mathbf{T}_{1} \equiv \mathbf{T}_{1}(\mathbf{x}, \mathbf{y}) \equiv \mathbf{x}^{\mathrm{T}} \mathbf{y}$ and $\mathbf{T}_{2} \equiv \mathbf{T}_{2}(\mathbf{x}, \mathbf{y}) \equiv \mathbf{x}^{\mathrm{T}} \mathbf{x}$.  We can thus compute these quantities one time and fit our models, as opposed to recomputing them at each update step.
Let's compare on a simulated problem.  I will assume $\sigma=1$ and does not require estimation for simplicity. I'll vary the number of observations and the number of covariates in the model and compare the time to optimize the loss assuming when using sufficient stats and when using a naive approach.  Shown below is a plot of the expected time to completion plus/minus a standard deviation.  We can see that as the data become larger in size, using sufficient statistics is advantageous.

Now, this whole answer is a bit of a straw man.  I've not shown you the results when not using sufficient statistics and not using loops (so maybe leveraging some linear algebra).  But the point about sufficient statistics remains.  That we can represent all the information in a sample with a single number, computed once, is a very valuable property to have.
Code to reproduce experiments:
<code>import numpy as np
import pandas as pd
from scipy.optimize import minimize
from itertools import product
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_theme(style="darkgrid")

# First, simulate big data!

def make_data(N, p):
    X = np.random.normal(size = (N,p))
    beta = np.random.normal(2, 2, size = p)
    y = X@beta + np.random.normal(size = N)
    return X,y

# Next, set up a loss function to optimize using loops
def solve_naive(N, p):
    X, y = make_data(N, p)
    def loss_and_grad(w):

        number_rows, number_columns = X.shape
        grads = np.zeros_like(w)
        loss=0
        for i in range(number_rows):
            res= (y[i] - X[i]@w)
            loss+= res**2/number_rows

            grads+= -2*res*X[i]/number_rows

        return loss, grads

    r=minimize(loss_and_grad, x0=np.zeros(p), jac=True)
    return loss_and_grad

# Next, set up a loss function to opimize which only uses sufficient statistics
def solve_sufficient(N, p):
    X, y = make_data(N, p)

    T1 = X.T@y
    T2 = X.T@X
    const = y.T@y
    n = len(X)

    def loss_and_grad(w):

        loss = const - (2*w@T1 - w@T2@w)

        grads = -2*T1 + (T2@w + T2.T@w)

        return loss/n, grads/n

    r=minimize(loss_and_grad, x0=np.zeros(p), jac=True)
    return loss_and_grad

# A helper function to time the optimization for various datasets
def time_to_optimize(N, p):

    naive_optimization_times = %timeit -o -n 10 solve_naive(N, p)
    suff_optimization_times = %timeit -o -n 10 solve_sufficient(N, p)

    suff = pd.DataFrame({'times':suff_optimization_times.timings,
                         'type': 'Sufficient Statistics'})

    naive = pd.DataFrame({'times':naive_optimization_times.timings,
                         'type': 'Naive'})

    df = pd.concat((suff, naive))

    df['N'] = N
    df['p'] = p

    return df

if __name__ == '__main__':

    Ns = [1_000, 10_000, 100_000]
    ps = [10, 100, 250]

    prods = product(Ns, ps)
    frames = []

    for N, p in prods:

        frames.append(time_to_optimize(N, p))

    df = pd.concat(frames).reset_index(drop=True)

    fig, ax = plt.subplots(dpi = 240, figsize = (8, 5))
    grid = sns.lineplot(data=df, x='N', y='times', hue='type', style='p')

    grid.set(xscale="log", yscale="log", xlabel = 'Number of Observations', ylabel='Execution Time (Seconds)')
    grid.legend(loc='best', prop = {'size':6})

</code>
Comment: @ Demetri: Thank you so much for your answer! I will have to read it over a few times - I left some questions for @John Madden in the comment section. Could you please take a look at them if you have time? Thank you so much!
Answer: None of the properties that OP mentions were important when the most popular exponential family distributions were discovered or were put in use. That is not to say that the properties are irrelevant or not important. These are all interesting and useful features, but these are not the reasons for popularity of the distributions. Distributions such as Gaussian and Poisson became popular because they are ubiquitous in nature, and also approximate very well processes in engineering and the industry.
Poisson.
Here's one example: Poisson distribution. It and its related distributions, such as exponential distribution, are observed in radioactive decay phenomenon. Not only it is simply convenient to approximate the observed data with them, but they arise from the theory of radioactive decay in their exact form, and the observations are consistent with equations beyond any doubt with arbitrary precision limited only by the precision of our instrumentation. This has been discovered in early 20th century and ever increasing precision of measurements has not produced any deviation from the exact equations. At this point it's simply a fact that Poisson distribution explains the number of decaying nuclei in a given period of time, and that time between two events is from exponential distribution.
Gaussian. Gaussian distribution is observed in numerous natural phenomena, e.g. Brownian motion. I gave an exotic example in my comment to the question: Heisenberg's uncertainty principle (UP) from quantum mechanics (QM).
The first thing to note here is that physicists so far failed to find any deviation from QM finding. The precision with which it's confirmed to work is mind boggling and unprecedented in any field of study in sciences. So, you can safely accept UP as a fact of nature.
In its simple form it states that the uncertainties about the measurement of the moment and coordinate of a particle simultaneously has a lower limit:$$\Delta p\Delta x\ge h/4\pi$$
If you measure the coordinate very precisely, then the moment measurement will be very imprecise etc.
But when does the inequality become equality $\Delta p\Delta x= h/4\pi$? For a Gaussian wave packet!
We don't always have a choice. In the above examples we do not pick Poisson or Gaussian because they have some nice statistical properties. We use them because we must use them. That's how nature works, and if we want to describe the observed phenomena then we don't have choice but to use these distributions regardless of their convenience or elegance.
Answer: Regarding a point you raise in A: the sufficient statistic for the parameters $\beta$ of a logistic regression - that's a good question. There is no analytical solution to Logistic regression, only numerical. So, calling the final solution a function of the data $T(X,y)$ is a bit of a stretch to the definition of a function. It's true that you use the data in the fitting process - and technically you need only $X$ and $X^Ty$ - but there are other considerations: like the fitting algorithm (do you use Fisher-Scoring? Gradient Ascent? What threshold do you use? What learning step for the gradient?) Maybe others can answer if there is such a thing as a sufficient statistic for the logistic regression model.
Regarding point D: one of the main algorithms for Variational Inference (VI) / Variational Bayes - is called CAVI - Coordinate Ascent VI. It uses the "mean field" approximation (what John Madden called "Independent") $q(z)=\prod_j q_j(z_j)$. The CAVI update rule says that for each coordinate of the parameter, you update the variational distribution to be $q_j(z_j)\propto e^{\mathbb E_{-j}[\log p(z_j|z_{-j},x)]}$. This can be hard and require quite a lot of analytical work to find out what is this distribution, and what are it's parameters (take expectation on all other variational distributions, etc.). If you further assume that each "complete conditional" $p(z_j | x, z_{-j})$ is in the expo. family, the algorithm simplifies: the variational distributions have the same exponential family form as their complete-conditionals counterparts, and the update rule becomes simply updating the natural parameters of that family, $\theta_j$, by setting $\theta_j=\mathbb E_{-j}[\theta_{-j}]$ (where $\theta_{-j}$ will be a function of the conditioning set $x,z_{-j}$). For more information check sections 4.1 and 4.2 in "Variational Inference: A Review for Statisticians" here.


Prove that X = $\{n \in \mathbb{Z}\mid n+5$ is odd$\}$ is the set of all even integers.

Question: I was wondering if I am doing the prove to this question correctly..
My attempt: Pick Y to be set of all even integers, so then we have $Y = \{2m\mid m \in \mathbb{Z}\}$. Then I show that $X \subseteq Y$.
Let $a \in X$ where $a=2k+1$. Then $a+5 = 2k+1+5 = 2k+6 = 2(k+3)$. Here we see that $a$ must be even. Therefore, $X \subseteq Y$ holds.
Is this correct, or am I off a bit?
Comment: The question says "is the set of even integers", which means you must prove that $Y \subset X$. However, that is just as easy as what you've done so far (which is fine). Also, note  that $k \in \mathbb Z$ should be mentioned, otherwise this is incorrect.
Comment: .... but it'd be more efficient to prove $a$ is even if and only if $a+5$ is odd.  $a$ is even $\iff$ $a = 2k$ for some $k$ $iff$ $a+5 = 2k+5 = (2k+4)+1 = 2(k+2) + 1= 2m+1; m=k+2\iff a+5 $ is odd.  So then $X$ consists of precisely the even numbers.
Comment: Oh ok, I see. Thanks.
Comment: You are welcome. I hope that you found the reverse part just as easy, if not easier. Oh, +1.
Comment: Yeah it shouldn't be a problem lol
Comment: You don't know that $a$ is odd.  And if $a+5$ is even then $a$ is NOT in X.  Yo need so say that $a \in X$ so $a+5$ is odd (NOT $a$) and so $a+5 = 2k + 1$ and $a = 2k - 4 = 2(k-1)$ is even.  Then you have proven $X \subset Y$.  Now you must prove $Y\subset X$. That would be correct....
Answer: Let's define the subsets of $\mathbb Z$
even numbers like this  $\mathcal E:\begin{cases}0\in \mathcal E\\e\in\mathcal E\iff e+2\in\mathcal E\end{cases}\quad$  and odd numbers $\mathcal O=1+\mathcal E$

$0\in X$ because $0+5=1+(0+2+2)\in\mathcal O$
$x\in X\iff x+5\in \mathcal O\iff \exists e\in\mathcal E\mid x+5=1+e\overset{(*)}{\iff} (x+2)+5=\underbrace{1+\underbrace{(e+2)}_{\in\ \mathcal E}}_{\in\ \mathcal O}\iff x+2\in X$

So $X=\mathcal E$.
(*) to be thorough, we should also state $e\in\mathcal E\iff e-2\in\mathcal E$, I took a shortcut there... My purpose was to show that we don't necessarily need the $2n,2n+1$ stuff.


Making a exchange resource mailbox visible to certain users only

Question: Is there any way to make a resource mailbox visible to a specific user group only?
We've got a meeting room which should be only bookable and visible by a small group of persons.
Unfortunately it's not an option to just hide it in the adressbook because this way there is no possibility to check the rooms calendar.
Answer: Not easily.
You would have to create a new Address Book Policy for everyone - one policy that allows that account to be seen, one that doesn't. Then apply the appropriate policy to each mailbox.
If you lose the requirement to be visible, then restricting the use is easy enough - you can just permissions on who can view and book it. It will appear in the list, but correctly setup it will not allow anyone to book.


Significance test of the amplitude of sinusoidal regression

Question: I am working on a multi-level logistic regression model with a binary outcome and a circular predictor. Thanks to this post, I know that I have to add sine and cosine of the circular variable as a predictor and that I can calculate the amplitude $a$ and the phase shift $\phi$ from the regression weights $w_1$ and $w_2$ corresponding to the sine and cosine predictors, respectively.
$a = \sqrt{w_1^2 + w_2^2}$
$\phi = \tan^{-1}\frac{w_1}{w_2}$
Say I am not particularly interested in testing the significance of sine and cosine components individually, which is what the model provides. Instead, I would like to test the overall effect of the circular variable (i. e., the amplitude). My thought was to run a model comparison (in R, anova(m1, m2)) between an intercept-only model m1 and the model including the circular variable m2 to obtain a $\chi^2$ statistic. However, I would like to know if there is a cleaner way of testing the significance of $a$ and $\phi$ - and how the corresponding standard errors are calculated. Thanks in advance for your help!
Comment: What is the likelihood for your observations? How do you model your errors? I think you will need to be more explicit about the data and the model. Also, bear in mind that, if you have a clear model for your errors, you can simply simulate the data under null hypothesis, extract sampled $a$ from that, and extract confidence intervals and standard errors that way.
Comment: I am using R and am running `glmer(binary_outcome ~ sin + cos + (1|ID), family = "binomial", data)` where sin and cos stand for the sine and cosine predictor and ID is the subject identifier. The probability of a positive outcome (=1) is around 20 % in my data. I have not explicitly modeled error.
Comment: I am not an R user, so it does not give me enough information.
Answer: The relevant tests can be undertaken by converting them to tests on the underlying regression coefficients in linear form.  For the test of amplitude the relevant null hypotheses convert as follows:
$$\begin{matrix}
H_0: a = 0 \quad \quad \quad & & & H_A: a \neq 0 \quad \quad \quad \quad  \quad \\[6pt]
H_0: w_1=w_2=0 & & & H_A: w_1 \neq 0 \text{ or } w_2 \neq 0 \\[6pt]
\end{matrix}$$
You can test this hypothesis with a standard F-test for the subset of the two coefficients for the sine and cosine terms.  For the test of phase angle the relevant null hypotheses convert as follows:
$$\begin{matrix}
H_0: \phi = \phi_0 \ \ \quad \quad \quad & & & H_A: \phi \neq \phi_0 \ \ \quad \quad  \quad \\[6pt]
H_0: w_1 = w_2 \tan \phi_0 & & & H_A: w_1 \neq w_2 \tan \phi_0 \\[6pt]
\end{matrix}$$
This latter hypothesis test is effectively testing the ratio of the linear regression coefficients against a fixed value.  This would require a custom test for a ratio of coefficients, which is non-standard but should not be too difficult to construct.
Comment: Sorry, I had thought your post referred to linear rather than logistic regression.  Yes the appropriate test in this case is the likelihood ratio test using the chi-squared test statistic.
Comment: For the amplitude test, you would do a partial F-test on the two regression coefficients $w_1$ and $w_2$ together (not look at their individual p-values in t-tests) (see [here](https://stats.stackexchange.com/questions/157102/)).  The phase angle test would be a bit more complicated (see [here](https://stats.stackexchange.com/questions/569238/) for some preliminary thoughts).
Comment: It seems like the partial F-test is used for multiple regression and based on your reference, can be derived from the likelihood ratio test. The equivalent likelihood ratio test for mixed effects models seems to be [approximately $\chi^2$ distributed](https://www.ssc.wisc.edu/sscc/pubs/MM/MM_TestEffects.html). With this comment I want to clarify - for the amplitude test, do you refer to a model comparison procedure or did you suggest something different?
Comment: This is helpful. Could you elaborate on both tests? For the amplitude test, does that you would just check the p-value of each of the regression weights (representing sine and cosine) and call the amplitude significant if at least one of the regressors are significant?
Answer: The little I did understand from googling on R is that the model here is that we have a binary outcome $Y_{ij}$ some sort of bounded variable $\theta_{ij}$. Where $j$ is the group within which random intercept is kept constant. So the model is:
$$
\begin{align}
c_j&\sim N\left(\mu,\,\sigma^2\right) \\
\eta_{ij}&=c_j+w_1\sin\theta_{ij}+w_2\cos\theta_{ij}\\
\pi_{ij}&=logit\left(\eta_{ij}\right)\\
Y_{ij}&\sim Bernoulli(\pi_{ij})
\end{align}
$$
Where $\mu$, $\sigma$, $w_{1,2}$ are to be fitted
With some work you could write the likelihood for your data under this model and then use something like Fisher Information to extract the standard errors. It also, so happens that for Bernoulli, the algebraic expressions can be relatively simple
Instead, it may be easier to supplement this model with $\theta_{ij}\sim Uniform\left(0,2\pi\right)$ generate some $Y_{ij}$, and then use your current fitting procedure to get estimates of $w_{1,2}$. Their distribution, and distribution of their sum of squares will tell you the distributions of the statistics under null hypothesis.


How to prove that specific value (...5) is infinite in 10-adic

Question: I have watched: Veritasium
The idea behind is that there exist value that satisfies: $n^2 = n$.
That value is its own square.

We can create this value by:
$$
5^2 = 25
$$
$$
25 ^ 2 = 625
$$
$$
625^2 = 390625
$$
$$
0625^2 = ...
$$
$$
90625^2 = 8212890625
$$

How we can prove that we can go creating this value for indefinitely ?
Comment: If I said: "For fixed $N$, the residue class $5^{2^n} \pmod{10^N}$ is constant for every $n \geq N$."  Does that make sense to you?  I'm not sure how to interpret your question, but I believe that would be the answer.
Answer: Here's a rather abstract treatment of the phenomenon:
The $10$-adic numbers $\Bbb Z_{10}$ turn out to be the "ring-direct sum" of the $2$-adic numbers $\Bbb Z_2$ and the $5$-adic numbers $\Bbb Z_5$. We express this thus:
$$
\Bbb Z_{10}\cong\Bbb Z_2\oplus\Bbb Z_5\,.
$$
What's on the right side of the isomorphism sign is the set of all pairs $(u,v)$ for which $u\in\Bbb Z_2$ and $v\in\Bbb Z_5$. Addition and multiplication are performed coefficientwise, with no interaction across the $\oplus$-sign. The zero-element is $(0,0)$ and the unity element is $(1,1)$. They behave correctly, that is, $(0,0)+(u,v)=(u,v)$ and $(1,1)\cdot(u,v)=(u,v)$, where I'm using the dot to denote multiplication.
But there are two other "idempotents", i.e. objects $z$ for which $z\cdot z=z$, namely $\Bbb E_2=(1,0)$ and $\Bbb E_5=(0,1)$. You can get $\Bbb E_2$ by starting with a number $n$ that's $\>\equiv1\pmod2$ but $\>\equiv0\pmod5$: The starting value $n=5$ will do nicely. Then, recursively, you square each value of $n$ to get the next value of $n$.
As you saw, the first few values of $n$ are $5,\,25,\,625,\,390625,\,152587890625,\,\cdots$
But after a few repetitions, you're led to realize that at the $k$-th stage, you get stability in only the first $k$ digits: those to the left get changed. So, you might as well throw away all the digits to the left of the $k$-th digit. This makes your calculations more efficient. For instance, if you want the rightmost twelve digits of your magic number $\Bbb E_2$, you'll find that they are $\>\cdots918212890625\,$. And since $E_2+E_5=(1,1)$, which has ten-adic expansion simply $1$, you can get the expansion of $\Bbb E_5$ by writing out $1 - E_2$ ten-adically, to get $\>\cdots081787109376\,$. You can check that this is truly also an idempotent, to twelve $10$-adic places, and finally you can check that the leftmost twelve digits of $918212890625\cdot081787109376$ all are zeros, since $\Bbb E_2\cdot\Bbb E_5=(1,0)\cdot(0,1)=(0,0)$.


The go-compiler reports variable declared but not used when using pointers

Question: I have a snippet of code that i am unable to understand why the go compiler is reporting that a variable is declared but not used:
package main
import "fmt"
func main() {
    var StringSlice []*string

    MyStr0 := "Zero"
    StringSlice = append(StringSlice, &MyStr0)
    MyStr1 := "One"
    StringSlice = append(StringSlice, &MyStr1)
    MyStr2 := "Two"
    StringSlice = append(StringSlice, &MyStr2)

    var StrPtr *string
    for i, Value := range StringSlice {
        fmt.Println(Value)
        if i == 1 {
            StrPtr = Value
        }
    } // END for

With the Printf statement commented out, the go compiler claims »./prog.go:15:6: StrPtr declared but not used« - see the example in the go-playground at: https://go.dev/play/p/J3p4NDR6fBm
When the Printf is commented out, everything is fine and there's even the correct string-pointer stored in StrPtr…
Thank you very much in advance for your help!
Comment: Where is the Printf statement that you mention? I cannot see it in the code you posted.
Answer: The problem is that you are not using StrPtr. You are reassigning it, but it is never read. If you add that to a Printf statement the error will go away.
package main
import "fmt"
func main() {
    var StringSlice []*string

    MyStr0 := "Zero"
    StringSlice = append(StringSlice, &MyStr0)
    MyStr1 := "One"
    StringSlice = append(StringSlice, &MyStr1)
    MyStr2 := "Two"
    StringSlice = append(StringSlice, &MyStr2)

    var StrPtr *string
    for i, Value := range StringSlice {
        fmt.Println(Value)
        if i == 1 {
            StrPtr = Value
            fmt.Println(StrPtr) // <--- add this
        }
    } // END for
}

Comment: Thank you very much for your answer!
I didn't realize that a variable must be read in order to be "used" by the Go compiler, but that actually does make sense.


Windows Server 2008 R2 NTP ignoring SpecialPollInterval

Question: I've pointed the 2k8 machine to the NTP server and it updates fine on the original bootup.  But I want it to update every 30 seconds.  It's for a practical and I have to show NTP in action.  It keeps defaulting to 900 seconds, even after I've put "0x1" at the end of the NTP Server IP and set the specificpollinterval to 30.
This method words in XP just fine..but for some reason 2k8 seems to be ignoring this registry value.
Any ideas?
Comment: You say Specificpollinginterval, do you mean Special?  This is the regkey to change: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\ NtpClient\SpecialPollInterval
Comment: Yes I meant Special*.  That is the registry key I changed and it still defaults to 900.
Comment: Is the server part of a domain?  Is it a domain controller?
Answer: If I'm not mistaken the minimum poll interval is 64 seconds.  If you go anywhere below that number it defaults back to the recommended setting of 900 seconds.
Comment: Turns out my DNS records were messed up and it wasn't able to find the NTP server.  Fixed that and now it's working properly.  thanks for the help


what is the best way to display long text in xaml for windows phone app

Question: In my project .I need to display loog texts,it is some kind of book app project .i know how to read a txt file in c# but it doesn't look efficient to me there should be more easy and customable ways. do you know any other way to display long texts
Answer: You can use the WebBrowser control.
Here's the documentation for using it with a string.
XAML:
<phone:WebBrowser x:Name="webBrowser" />

C#:
string LongText = "blablabla";
webBrowser.NavigateToString(LongText);

Comment: i want to ask a question .when i read text into a string would i be able to change font with webbrowser control,i mean your string is longtext would you be able to change font size color etc from webrowser element
Comment: You can add a style with CSS: ``


Set a numerical field to perform Views Global Math Expression calculations

Question: Lets say I have three fields:

Field1 (a decimal)
Field2 (another decimal)
Global Math Expression

And in Global math expression I want to calculate [field1] + [field2]. How do I force Views to understand the replacement fields used in this global math expressions as numbers for calculation? As it is, it always returns the value 0.
Answer: Another solution that worked for me is to "Strip HTML tags":
Rewrite results --> [x] Strip HTML tags
Answer: As your field are coming as "strings" in the replacement pattern, you must output the both fields as numeric. Go to your individual fields and then to "rewrite results" and in rewrite results section, go inside replacement patterns. There must be a "raw field format" display option . Just output both fields as "raw". This will enforce the fields to display without any strings and they appear as raw numbers. Then exclude both fields from the display and then do calculation in Math Expression field.


In what sense is the interpretation of coefficients in a GLMM subject-specific?

Question: There is something I'm not quite understanding conceptually about the output from generalized linear mixed models. I have read that the target of inference in GLMMs is subject-specific. For example, the accepted answer to this question states that in a logistic GLMM the odds-ratios are conditioned on both the fixed and random effects. So, in a GLMM of pupils within classrooms, with random intercepts for classroom (i.e., the "subject" in this case), the odds-ratios will differ for each classroom as there will be many random intercepts. So far, this makes sense to me.
What I am confused about is that the typical output from the fixed effects part of such a model reports just one odds-ratio. For example, in the R example I provide below, the odds-ratio for the fixed effect of sex is .662. I have three questions:

How do I interpret this single fixed effect odds-ratio?
(Is it an odds-ratio ignoring the random effects? Is it an odds-ratio of the average random effect - in which case, isn't it a population average? Is it calculated assuming the random effect variance is zero?)
Is it possible to calculate a population average odds-ratio using the output from a GLMM?
I know this can be done using a GEE, but what about a GLMM?
How would I go about calculating the odds-ratio for a particular random effect (a particular classroom, lets say class 7 in the example below)?
Presumably this involves combining the fixed and random effect estimates somehow.

EDIT 1:
It seems after doing more reading (for example, this post), that since the fixed effect for sex in this example does not have its own random effect (e.g., a random slope), there will be no subject-level interpretation of this parameter. Does this mean that only the intercept term in the model below is subject-specific, while the sex term is a population average?
# dummy data:
set.seed(1)
dat <- data.frame(Y         = factor(sample(rep(c(0, 1), 100))),
                  sex       = factor(sample(rep(c("M", "F"), 100))),
                  classroom = factor(sample(rep(paste("class", 1:10), 20)))
)

# model:
library(lme4)
fit <- glmer(Y ~ sex + (1 | classroom), family=binomial, data=dat)

# summary(fit)
exp(fixef(fit))
# (Intercept)   sexM
#  1.229       0.662

Comment: One thing to note here is that the global null holds. In the data you generated, there are no fixed or random effects of anything.
Comment: You mean because the model with no predictors is not significantly worse than the model here? Yes, that's fair enough, but it's just an artifact of my not knowing how to simulate data very well.
Comment: It wasn't a criticism, @Alberto, please don't take it personally. It is hard to figure out how you are supposed to simulate data for something like this. By the time you've figured it out, you understand the model. But that inherent difficulty is part of why you are having trouble understanding the model output. & no, I don't mean just that things aren't significant; I mean in the population / underlying data generating process (your code) there are no fixed or random effects of anything.
Answer: I think I may have reached an understanding about this. The summary of the fixed effect coefficients from the model is (on the scale of the link function, here log-odds):
summary(fit)$coefficients

            Estimate Std. Error z value Pr(>|z|)
(Intercept)    0.206      0.222   0.927    0.354
sexM          -0.412      0.289  -1.426    0.154

These are the same estimates as when averaging over the random effects for intercept (the sex effect is constant). So, these fixed effect estimates (on the link scale) are population average effects:
coef(fit)

$classroom
         (Intercept)   sexM
class 1       0.1605 -0.412
class 10      0.3606 -0.412
class 2       0.2119 -0.412
class 3      -0.0294 -0.412
class 4       0.0281 -0.412
class 5       0.0855 -0.412
class 6       0.2237 -0.412
class 7       0.3090 -0.412
class 8       0.2119 -0.412
class 9       0.4990 -0.412

colMeans(coef(fit)$classroom)

(Intercept)   sexM
0.206         -0.412

However, when the coefficients are transformed to odds-ratios this is no longer the case:
sapply(coef(fit)$classroom, exp)

(Intercept)  sexM
1.174        0.662
1.434        0.662
1.236        0.662
0.971        0.662
1.028        0.662
1.089        0.662
1.251        0.662
1.362        0.662
1.236        0.662
1.647        0.662

colMeans(sapply(coef(fit)$classroom, exp))

(Intercept)  sexM
1.243        0.662

The intercept above (averaged over the exponentiated random effects) is not the same as the exponentiated fixed effect coefficient below, because exponentiation is not a linear transformation. But, the sex effect is the same, since this did not have a random effect associated with it. Therefore, the sex coefficient is a population average estimate, even as an odds-ratio:
exp(fixef(fit))

(Intercept)  sexM
1.229        0.662


Send mail if date in cell is 2 years older than the current date

Question: I have created a macro which lists the name, path, Creation date, modification date of all the files and subfolders in an excel worksheet, this macro is working absolutely fine.
However now I want to create a macro which can check the entire list of files and folders and if the Date creation date (Column F ) of any listed files/folders is greater than 2 years from the current date, it should send an automated mail giving the name of the file, its path and the creation and modification information (basically copy all the information of that particular row) to me.
Please find below the code in which I am getting the error (Run time Error) on
this line :
Sub send_files()
Dim OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
Dim cell As Range
Dim FileCell As Range
Dim rng As Range
Dim sDate As String

With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With

Set sh = ThisWorkbook.Sheets("Sheet1")
Set OutApp = CreateObject("Outlook.Application")
sDate = Date

For Each cell In sh.Columns("F").Cells
    Days = DateDiff("yyyy", Cells.Value, sDate)
    If Days > 2 Then
                Set OutMail = OutApp.CreateItem(0)

        With OutMail
            .to = cell.Value
            .Subject = "Testfile"
            .Body = "Hi " & cell.Offset(0, -1).Value
            .send

        End With
    End If
Next cell

Set OutApp = Nothing
With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With
End Sub

The error occurs on:
Days = DateDiff("yyyy", Cells.Value, sDate)

Can anyone assist with this error?
Answer: Try changing
Days = DateDiff("yyyy", Cells.Value, sDate)

to
Days = DateDiff("yyyy", cell.Value, sDate)

Comment: Looking at your code, you're reading **Every** cell in column F! You might want to check that the cell contains a date before you attempt to `DateDiff` it.
Comment: Now it gives error Runtime error 13 : Type mismatch
Comment: atleast it should give result for the cells which are filled.
Comment: Does the first cell contain a header? If so, it'll error there and stop. If not, you're right, it should work its way down until it does error. You could use error handling but that would be highly inefficient. Better to set your range up correctly in the first place and ignore empty cells if they can exist in the range.
Comment: I have modified the range but still its giving error:
Comment: bottomA = sh.Range("F" & Rows.Count).End(xlUp).Row: x = 2

  For Each cell In sh.Range("F1:F" & bottomA)
  Days = DateDiff("yyyy", cell.Value, sDate)
  If Days > 2 Then
Comment: Does cell F1 on sheet `Sheet1` contain a date. Is it stored as a date or text? Don't forget the `IsDate` function could be used here to check within the loop.
Comment: Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/139522/discussion-between-astha-and-clr).


Android Animar altura de objeto

Question: Tenho um objeto na minha view mas ele inicia com visibity=GONE e gostaria de animar ele para aparecer, tipo um slideDown e slideUp do JQUERY.
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#2f3d4c"
    android:paddingBottom="40dp"
    android:paddingTop="20dp">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/bt_mensagens"
        android:id="@+id/bt_mensagens"
        android:contentDescription="@string/content_bt_mensagens" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#000000"
        android:visibility="gone"
        android:id="@+id/box_buttons_msg"
        android:orientation="vertical"></LinearLayout>

</LinearLayout>

Answer: slide_up.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
    android:duration="1000"
    android:fromYDelta="100%"
    android:toYDelta="0" />
</set>

slide_down.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
    android:duration="1000"
    android:fromYDelta="0"
    android:toYDelta="100%" />
</set>

Java:
final Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down);
view.startAnimation(animation);

Android Animar altura de objeto


AWX execution environment pip error ResolutionTooDeep: 2000000

Question: I've been building an execution environment for my awx for a while now and never got any kind of problem, but now I am trying to recreate the same EE I created 1 month ago, and it does not work anymore. It used to take just 15 minutes to build the container, but now it takes 10 hours to fail with the error:
ERROR: Exception:
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/pip/_internal/cli/base_command.py", line 167, in exc_logging_wrapper
    status = run_func(*args)
  File "/usr/local/lib/python3.8/site-packages/pip/_internal/cli/req_command.py", line 205, in wrapper
    return func(self, options, args)
  File "/usr/local/lib/python3.8/site-packages/pip/_internal/commands/install.py", line 339, in run
    requirement_set = resolver.resolve(
  File "/usr/local/lib/python3.8/site-packages/pip/_internal/resolution/resolvelib/resolver.py", line 94, in resolve
    result = self._result = resolver.resolve(
  File "/usr/local/lib/python3.8/site-packages/pip/_vendor/resolvelib/resolvers.py", line 481, in resolve
    state = resolution.resolve(requirements, max_rounds=max_rounds)
  File "/usr/local/lib/python3.8/site-packages/pip/_vendor/resolvelib/resolvers.py", line 392, in resolve
    raise ResolutionTooDeep(max_rounds)
pip._vendor.resolvelib.resolvers.ResolutionTooDeep: 2000000
WARNING: You are using pip version 22.0.3; however, version 22.2.2 is available.
You should consider upgrading via the '/usr/bin/python3 -m pip install --upgrade pip' command.
[3/3] STEP 1/13: FROM quay.io/ansible/ansible-runner:stable-2.11-devel
Error: error building at STEP "RUN assemble": error while running runtime: exit status 2

pip._vendor.resolvelib.resolvers.ResolutionTooDeep: 2000000

This is my requirements.yml
---
collections:
  - name: amazon.aws
    version: 3.3.0
  - name: ansible.netcommon
    version: 2.5.0
  - name: ansible.posix
    version: 1.3.0
  - name: ansible.windows
    version: 1.9.0
  - name: awx.awx
    version: 21.4.0
  - name: check_point.mgmt
    version: 2.3.0
  - name: community.docker
    version: 2.7.1
  - name: community.hashi_vault
    version: 3.2.0
  - name: community.aws
    version: 3.3.0
  - name: community.general
    version: 5.5.0
  - name: community.vmware
    version: 1.18.0
  - name: community.windows
    version: 1.10.0
  - name: community.zabbix
    version: 1.8.0
  - name: google.cloud
    version: 1.0.2
  - name: kubernetes.core
    version: 2.3.2
  - name: netbox.netbox
    version: 3.8.0
  - name: openstack.cloud
    version: 1.6.0
  - name: ovirt.ovirt
    version: 1.6.6
  - name: redhatinsights.insights
    version: 1.0.5
  - name: theforeman.foreman
    version: 3.1.0
  - name: paloaltonetworks.panos
    version: 2.9.0
  - name: cyberark.pas
    version: 1.0.13
  - name: f5networks.f5_bigip
    version: 1.8.1
  - name: fortinet.fortios
    version: 2.1.7
  - name: community.mysql
    version: 3.5.1
  - name: community.postgresql
    version: 2.2.0

and this is my requirements.txt
urllib3
netaddr
rocketchat_API
cryptography==36.0.2
git+https://firstname.lastname@example.com
psycopg2-binary
PyMySQL

I always thought that using the versions would let me rebuild the container at any time, but that is not the case.
Any idea what is breaking that?
Answer: I've been having the same issue with ansible-builder and found that cryptography was causing the issue.
Was able to build the container after removing cryptography from requirements.txt.
It may also be worth removing the git declaration of ansible-builder from requirements.txt
Comment: I was also having this exact issue and resolved it by removing community.vmware from requirements.yml. Build time went from 10+ hours and failure to 10 minutes and success.


Percorrer todo o registro

Question: Gostaria de saber se tem como eu conseguir percorrer todo o registro (chaves e subchaves, não precisa o valor delas) do sistema operacional para encontrar o nome de uma chave em especifico?
Se sim, alguém poderia me dizer como fazer no python 3.x+ ou ao menos me indicar um caminho de como fazer isso?
Comment: Se você quer somente certificar-se que uma chave existe, porque percorrer todo o registro? em todo caso, você pode usar a função [`EnumKey`](https://docs.python.org/3/library/winreg.html#winreg.EnumKey) do módulo `Winreg` para enumerar as chaves.
Comment: Eu quero meio que fazer uma pesquisa pelo registro sem ter um caminho pré definido. Por exemplo: achar a chave x em todo o registro.
Comment: Entendi, você quer encontrar somente a primeira ocorrência ou continuar procurando? talvez recursão seja necessário neste caso.
Comment: A chave seria por acaso o PATH?
Answer: O Python dispõe de um módulo para acessar o registro do Windows. Ele vem junto na biblioteca padrão, não é preciso importar nada externo.
import winreg

Esse módulo possui uma função que recebe uma HKEY e um índice, e retorna o nome da subchave do HKEY naquele índice. Se o índice for inválido, ele libera um WindowsError. Assim é possível obter o nome de todas as subchaves imediatas de uma dada HKEY.
def obter_subchaves_imediatas(hkey):
    i = 0

    try:
        while True:
            yield winreg.EnumKey(hkey, i)
            i += 1
    except WindowsError:
        pass

Agora, usando a função acima, é fácil escrever uma rotina que navega recursivamente por toda a árvore de registros. É basicamente o algoritmo de navegação pre-order numa árvore qualquer.
def navegar_arvore(nome_chave_raiz):
    hkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, nome_chave_raiz)

    for nome_subchave in obter_subchaves_imediatas(hkey):
        nome_completo = f'{nome_chave_raiz}\\{nome_subchave}'
        yield nome_completo
        yield from navegar_arvore(nome_completo)

Um simples laço permite obter todos os caminhos a partir de uma certa raiz.
for chave in navegar_arvore('Control Panel'):
    print(chave)

Existem vários pontos que podem ser melhorados neste código, mas o que foi mostrado aqui é suficiente para resolver o problema.

Percorrer todo o registro


Picking hand technique and precision

Question: I started realizing that the picking hand was actually very important (probably as much as the fretting hand): it is always moving and it has to be extremely precise. I wanted to ask a question about some technique I saw in a video but I don't quite understand what's going on. In the video below starting at 1:57, the guitarist does some kind of "crunch/mute" sound in between the notes he's playing. I have a hard time figuring out how he's doing it. Is he doing that sound by using his right hand against the strings and then quickly lifting it off to play the notes or is he just doing that "crunch" mute sound with the left hand by just lifting his fingers off the strings between the notes? This seems to be related to his right hand doing a quick double movement (at 2:01), if someone could explain it, I would appreciate it because it seems that's how he's doing that sound.
Answer: This is mostly right hand muting (although I wouldn't be surprised if he also uses a quick left hand lift, as that helps kill the sound more quickly)
You have described it pretty accurately, he does quickly press his right hand against the strings between notes.
It's a very common thing to do, especially when using a lot of distortion and gain - it helps stop squeals and extra noise, and as he also has a compressor on this it helps drop the sound level quickly to give a sharper edge to the note. With practice it's almost automatic.
EDIT: what your updated question shows is actually the damping plus a quick pick to give a very short crunch/click noise. But the same process is true.

Picking hand technique and precision


Optional match extends query

Question: When I add an optional match to my already working query the selection is expanded.
I have a structure regarding players in games as follows
(player)-[got]->(result)-[in]->(game)
And as players oppose each other things will look like this in the final data
(player_1)-[got]->(result_1)-[in]->(game)<-[in]-(result_2)-[got]-(player_2)
Given a list of result_1 ids I try to find corresponding result_2
The basic query
MATCH (r:Result)-[:In]->(g:Game)<-[:In]-(or:Result)
WHERE r.id IN [30,32]
RETURN r, or, g, m

returns exactly what I expect:

(30)-(g1)-(or1)
(32)-(g2)-(or2)

But games can also be in an (optional) match and this query
MATCH (r:Result)-[:In]->(g:Game)<-[:In]-(or:Result)
OPTIONAL MATCH (g)<-[:Contains]-(m:Match)
WHERE r.id IN [30,32]
RETURN r, or, g, m

returns

(30)-(g1)-(or1)
(32)-(g2)-(or2)
(33)-(g3)-(or3)
(n)-(gn)-(orn)
Whatever else happens to match the structure p-r-g-r-p but with no regard to the list [30,32]

I do suspect that it has something to do with the mirrored nature of the data because if I remove r from the returned values I still get (30) and (32) back as or but I cannot figure out why and thus how to stop it.
I've tried to add a With before the optional but it makes no difference.
Answer: The WHERE clause modifies the immediately preceding [OPTIONAL] MATCH or WITH clause.
You need to move your WHERE clause so it is right after the initial MATCH, so that it will limit r as you intended. Like this:
MATCH (r:Result)-[:In]->(g:Game)<-[:In]-(or:Result)
WHERE r.id IN [30,32]
OPTIONAL MATCH (g)<-[:Contains]-(m:Match)
RETURN r, or, g, m

Comment: When knowing it it seems quite obvious really.

Optional match extends query


Hitting breakpoints in strange order with simple code

Question: I was working on some C++ code and noticed my breakpoints were getting hit in a strange order and I don't understand why.  I'm using Visual Studio 2022 with cl.exe version 19.35.32215 and link.exe version 14.35.32215.0.  As a simple reproduction I created a new C++ console app with the following code:
#include <vector>

std::vector<int> DoSomething()
{
  std::vector<int> results;
  results.push_back(55);
  return results;
}

int main()
{
  std::vector<int> results = DoSomething();
  return 0;
}

I put breakpoints on the following two lines:
  results.push_back(55);
  return results;

I was very surprised when the first breakpoint hit was "return results;".  If I continue code execution I bounce back to "results.push_back(55);" and then move on to "return results;" again.
I was expecting to hit my two breakpoints in their line ordering (i.e. "results.push_back(55)" first, then "return results;").  I've tried both x64 and x86 with a Debug build and both have the same breakpoint weirdness.
I also tried inspecting the disassembly and a box pops up asking me to "Resolve Ambiguity":
Resolve Ambiguity popup
I have not tried this same code with gcc or clang.
Can anyone help explain to me what's going on?  I feel like I'm missing something obvious, but I don't know what.
Thanks!
Comment: It is probably only because NRVO (named return value optimization) was applied, so that `results` and the result object of the function are the same object. Then the line initializing `results` is also initializing the return value of the function. I do not think choosing to mark the return statement is useful in that case, but that seems to be how the compiler decided to place the debug information.
Comment: You're either compiling with optimisation or (even if you are compiling without optimisation for debugging) your compiler is applying the NRVO
Comment: I'd suggest, in context of this question, that it doesn't really matter whether the compiler is required to apply the optimisation or not, since it evidently is.   Understanding  what is occurring, that it is not a major concern, and is mandatory in some cases but not others, is possibly sufficient.   Not all compilers conform with all mandatory requirements of the standard (e.g. a quality of implementation concern if they claim conformance, or they support an older standard) and some apply non-mandatory code transformations.
Comment: I thought about NRVO, but I wouldn't expect the debugger to jump around like this (optimizations are indeed off across the board).  The "ambiguity" box that pops up really bothers me too; it's like the compiler generated two copies of my function.

Edit: I also tried compiling the code with /std:c++14 (the default in VS2022), /std:c++17, and /std:c++20, all with the same result.
Comment: Aggressive optimizations -- even mandatory ones that must be executed due to required copy elision -- is a frequent cause of indigestion to debuggers, and to all the meatbags that use the debuggers. You will eventually get used to things like these, and they won't be much of a surprise.
Comment: @BigHands   Debuggers can behave in unexpected or counter-intuitive ways when optimisation is in play.   Sometimes those behaviours get fixed over time (particularly if the unexpected behaviour is a crash, or is grossly misleading) sometimes they don't.
Answer: If you use /permissive- in the compiler, then it applies the optional NVRO even in the Debug configuration. It allocates memory for a returned object on the caller stack frame and passes a pointer to a called function. It can confuse the debugger, that can see return results as object construction and initialization that of course should be performed before push_back(), but return is located in the code after push_back(), so it behaves like: user set the breakpoint on the line N, but I have jumped over on the line N+1, I must stop here.
You can disable NVRO with using /Zc:nrvo-.
Comment: What's happening is that `std::vector results;`, `return results;`, and the first half of `std::vector results = DoSomething()` are folded into a single logical operation that executes first -- a vector is constructed that will become the `results` vector in `main`.
Comment: /Zc:nrvo- worked great, thanks!  I think I'll keep this compiler flag set in Debug so my brain stops getting confused with all the jumping around.  Thanks for all the replies to this.


Access denied for admin page, after moving site from sub-folder to root folder

Question: Not able to access administration site after moving files from sub-directory to root folder.
With the help of google search, I have made below changes in settings.php under default folder, but that also didn't helped.
$base_url = 'http://example.com'; //Changed to site name
Actually base_url is commented in my settings.php file, I have tried enabling it.
Drupal version: 7
Answer: Got it! I was using url as ?q=admin instead of ?q=user url.


Find out what fonts are being sent to a printer

Question: I have an issue where two computers running XP and with identical print drivers have different behavior printing over parallel port to receipt printers. For one type of receipt, receipt printing is instant. For another kind printing is delayed by ten seconds on most machines but not on the other. This happens even if I swap out printers.
I believe the delay is because this computer has a different set of fonts installed. (It is used for graphic design.) The printers have built-in fonts, and if you do not use one of the built-in fonts the printer has to build up an image in memory rather than just spitting out its fonts. For a particular kind of receipt with special fonts on a particular computer the computer is sending a font which the receipt printer does not have built in.
My question is, is there a way to find out what fonts are being sent to the printer? This would let me narrow down what I need to modify in the Windows font folder. Thank you!
Answer: You could set up a jet-direct and then packet-sniff the output to the printer.
Personally, I'd copy all the fonts (.ttf or whatever) from the working machine to the misbehaving machine.
Finally, check on your Wordpad settings.  A lot of "general" printing output done in the Windows shell is by default routed through Wordpad.


What is the difference between push.default "matching" and "simple"

Question: I have been using Git for a while now, but I have never had to set up a new remote repo myself, and I have been curious to do so. I have been reading tutorials and I am confused on how to get git push to work.
If I simply use git push, it asks me to see up a "default branch" to point to. What is the difference between these two options it supplies me with?
git config --global push.default matching
git config --global push.default simple

The matching option just pushes whatever branches I have on my local repo, and if they don't match I have to then manually tell it to push whatever new local branches I have, correct? Is this best practice to use or is simple best?
Comment: possible duplicate of [Warning: push.default is unset; its implicit value is changing in Git 2.0](http://stackoverflow.com/questions/13148066/warning-push-default-is-unset-its-implicit-value-is-changing-in-git-2-0)
Comment: Now, if only `pull.default` is available for updating all those branches locally
Answer: git push can push all branches or a single one dependent on this configuration:
Push all branches
git config --global push.default matching

It will push all the branches to the remote branch and would merge them.
If you don't want to push all branches, you can push the current branch if you fully specify its name, but this is much is not different from default.
Push only the current branch if its named upstream is identical
git config --global push.default simple

So, it's better, in my opinion, to use this option and push your code branch by branch. It's better to push branches manually and individually.
Comment: I liked the `push.default current` from @UpAndAdam answer. Does not knew about it.
Comment: Note that `simple` is no longer an option. In `18.104.22.168` (and earlier?) it results in an error when you try to push. but `current` is still available
Comment: @sixty4bit No, it got included in a later Version of Git i don't know in which but (1.7) is old as hell though, even for 2016. I wouldn't recommend using such old Versions at all.
Comment: @sixty4bit: I am using git version 1.7.1.  I am using `tracking` -> push the current branch to its upstream branch.
Comment: Downvoted. Sorry, but the linked page's description of `simple` doesn't make sense, contradicts this answer, and is incorrect - which makes this answer confusing. The linked page says `simple` "will push branches one by one. Mostly connected with current branch." Does that mean it will push the branches sequentially as opposed to in parallel? What does "mostly connected" mean? Then, the description for `simple` goes on to quote the description for `matching`, which one would think means the description for `matching` also applies to `simple`. But obviously that is not true.
Comment: When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.

Since Git 2.0, Git defaults to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.
Comment: @sixty4bit as of git 2.21 `current` is still available. `tracking` is a deprecated synonym for `upstream`  Simple is like upstream with added safety that it only works if the names are the same.  This is really handy if you are 'forking' off an upstream branch and want to constantly keep synchronized with it by merges and rebases.
Comment: With this I still need to specify a branch for `pull`, even after `push` :(
Answer: From GIT documentation: Git Docs
Below gives the full information. In short, simple will only push the current working branch and even then only if it also has the same name on the remote. This is a very good setting for beginners and will become the default in GIT 2.0
Whereas matching will push all branches locally that have the same name on the remote. (Without regard to your current working branch ).  This means potentially many different branches will be pushed, including those that you might not even want to share.
In my personal usage, I generally use a different option: current which pushes the current working branch, (because I always branch for any changes).  But for a beginner I'd suggest simple

push.default
Defines the action git push should take if no refspec is explicitly given. Different values are well-suited for specific workflows; for instance, in a purely central workflow (i.e. the fetch source is equal to the push destination), upstream is probably what you want. Possible values are:

nothing - do not push anything (error out) unless a refspec is explicitly given. This is primarily meant for people who want to avoid mistakes by always being explicit.

current - push the current branch to update a branch with the same name on the receiving end. Works in both central and non-central workflows.

upstream - push the current branch back to the branch whose changes are usually integrated into the current branch (which is called @{upstream}). This mode only makes sense if you are pushing to the same repository you would normally pull from (i.e. central workflow).

simple - in centralized workflow, work like upstream with an added safety to refuse to push if the upstream branch's name is different from the local one.
When pushing to a remote that is different from the remote you
normally pull from, work as current. This is the safest option and is suited for beginners.
This mode will become the default in Git 2.0.

matching - push all branches having the same name on both ends. This makes the repository you are pushing to remember the set of branches that will be pushed out (e.g. if you always push maint and master there and no other branches, the repository you push to will have these two branches, and your local maint and master will be pushed there).
To use this mode effectively, you have to make sure all the branches you would push out are ready to be pushed out before running git push, as the whole point of this mode is to allow you to push all of the branches in one go. If you usually finish work on only one branch and push out the result, while other branches are unfinished, this mode is not for you. Also this mode is not suitable for pushing into a shared central repository, as other people may add new branches there, or update the tip of existing branches outside your control.
This is currently the default, but Git 2.0 will change the default to
simple.
Comment: I'm not sure I understand what you are getting at.  In ANY MODE if you say `git push origin master` it will do the same thing.  The point of the modes and defaults is generally what happens when you simply say `git push` and you don't tell it a remote or a branch.  What default setting? you mean the default setting of push.default? the default setting in which version of git... if you dont get it your comment is extremely vague.
Comment: yes, but I assume even with the push.default setting that if you do "$ git push *origin master*", it will only push the current branch to origin to the branch on origin with the same name...right? you should mention that there is also a default remote
Comment: 'push.default
Defines the action git push should take if no refspec is explicitly given'  if you say git push origin master you are giving it more information and it still might not do what you describe; depending on the refspec you set up.. https://git-scm.com/book/en/v2/Git-Internals-The-Refspec
Comment: you may want to also see the difference between `simple` and `current` here https://stackoverflow.com/a/23918418/5506988
Answer: As Adam's answer shows, push.default has many options now. If you find yourself looking back and forth between the descriptions to see the differences, try using this chart:

Behavior
simple1
upstream2
current
matching
nothing3

Push checked-out branch
✔
✔
✔
✔

Push other branches

✔

Upstream branch must be configured
✔
✔

Upstream branch name must match
✔

✔
✔

1 simple is the default setting value since Git v2.0, released in 2014.
2 upstream has a deprecated alias, tracking.
3 nothing requires all options to be specified on the CLI.

I have various repositories each set to simple, upstream, and current. I find matching to be a little too wild for me, but I can imagine circumstances where it's useful.
Comment: love the graph!
Answer: Git v2.0 Release Notes
Backward compatibility notes
When git push [$there] does not say what to push, we have used the
traditional "matching" semantics so far (all your branches were sent
to the remote as long as there already are branches of the same name
over there).  In Git 2.0, the default is now the "simple" semantics,
which pushes:

only the current branch to the branch with the same name, and only
when the current branch is set to integrate with that remote
branch, if you are pushing to the same remote as you fetch from; or
only the current branch to the branch with the same name, if you
are pushing to a remote that is not where you usually fetch from.

You can use the configuration variable "push.default" to change
this.  If you are an old-timer who wants to keep using the
"matching" semantics, you can set the variable to "matching", for
example.  Read the documentation for other possibilities.
When git add -u and git add -A are run inside a subdirectory
without specifying which paths to add on the command line, they
operate on the entire tree for consistency with git commit -a and
other commands (these commands used to operate only on the current
subdirectory).  Say git add -u . or git add -A . if you want to
limit the operation to the current directory.
git add <path> is the same as git add -A <path> now, so that
git add dir/ will notice paths you removed from the directory and
record the removal.  In older versions of Git, git add <path> used
to ignore removals.  You can say git add --ignore-removal <path> to
add only added or modified paths in <path>, if you really want to.


Como utilizar str_replace em aspas simples sem remover as que são necessárias?

Question: Estou mexendo em um banco de dados de um amigo o qual teve alguns dados inseridos errados ou meio confusos ou sou eu que estou dificultando.

Ex: Mariana "Gurizinha's"

Esse exemplo foi salvo no banco da seguinte maneira: \'Gurizinha\'s\'
Para exibir no website dele eu tentei criar uma função a qual substitui o caractere inicial por um " e o final também por ".
O Certo seria pegar um texto que foi salvo dessa forma e que possui 100 palavras ou mais e corrigir exibindo: "Gurizinha's"  ao invés de \'Gurizinha\'s\'.
Ai minha cabeça deu um nó pois se eu usar str_replace ele vai pegar todo o texto e o que tiver \' vai mudar para " então se for palavras que precisem Are's ele vai substituir por ".
function rt_especial($valor) {
  $i = 0;
  $palavras = explode(' ', $valor);

  foreach($palavras as $Arr) {
    $i++;
    $novotexto = stripcslashes($Arr);
    $i2 = 0;
    $palavras2 = explode("'", $novotexto);

    foreach($palavras2 as $Arr2) {
      $i2++;
    }

    for($i3 = 0; $i3 <= $i2; $i3++) {
      $str = '"';
      $str .= $palavras2[$i3];
    }
  }

  echo substr($string, -1);
  $novotexto = join(' ', $novotexto);

  return $novotexto;
}

Código que insere os dados no banco MySQL:
function db_executa($tabela, $dados, $acao = 'insert', $parametros = '') {
    reset($dados);
    if (strtolower($acao) == 'insert') {
      $consulta = 'insert into ' . $tabela . ' (';
      while (list($coluna, ) = each($dados)) $consulta .= $coluna . ', ';
      $consulta = substr($consulta, 0, -2) . ') values (';

  reset($dados);
  while (list(, $valor) = each($dados)) {
    switch ((string)$valor) {
      case 'now()':
        $consulta .= 'now(), ';
        break;
      case 'null':
        $consulta .= 'null, ';
        break;
      default:
        $consulta .= '\'' . db_entrada($valor) . '\', ';
        break;
    }
  }
  $consulta = substr($consulta, 0, -2) . ')';

} elseif (strtolower($acao) == 'update') {
  $consulta = 'update ' . $tabela . ' set ';

  reset($dados);
  while (list($coluna, $valor) = each($dados)) {
    switch ((string)$valor) {
      case 'now()':
        $consulta .= $coluna . ' = now(), ';
        break;
      case 'null':
        $consulta .= $coluna .= ' = null, ';
        break;
      default:
            $consulta .= $coluna . ' = \'' . db_entrada($valor) . '\', ';
            break;
        }
      }
      $consulta = substr($consulta, 0, -2) . ' where ' . $parametros;
    }
    return db_consulta($consulta);
  }

Resolução de parte do problema:
Não é uma maneira bonita e muito menos certa, porem foi a que consegui para resolver.
function rt_especial($valor){

        $string = $valor;
        $separa = explode(" ", $string); // quebra a string nos espaços
        $count = count($separa); // quantidade de separações

        $arrayok = array();

        for($i=0; $i<= $count; $i++)
        {
            // Pego toda palavra que começa com \' e substituo por "
            $string2 = ereg_replace("^([/\'])", '"',$separa[$i]);
            $string3 = str_replace("\',", '",', $string2);
            $string4 = str_replace("\',", '",', $string3);

            $string5 = ereg_replace('^([/""])', '"',$string4);
            $string6 = ereg_replace('([/""])$', '"',$string5);

            //Pego toda palavra que termina com \' e substituo por "
            $string = ereg_replace("([/\'])$", '"',$string6);
            $string7 = str_replace('"\'', '"', $string);
            $string8 = str_replace("\'\"", '"', $string7);
            $string9 = str_replace('\"', '"', $string8);

            $arrayok[$i] = $string9;

        }

        $ccp = implode(' ', $arrayok);

        return $ccp;
    }

Comment: @user Não inclua uma resposta, edite a pergunta.
Comment: Editei a pergunta e inseri o código que insere no banco.
Comment: Porque foi salvo no banco desta maneira `\'Gurizinha\'s\'`? qual foi o problema com o banco? extremamente não recomendo fazer uma gambi dessas porém se for preciso podemos ajudar... mas com certeza seria melhor arrumar o problema do banco ao invés de criar uma função dessas.
Comment: Ele usou um editor de texto javascript (JavaScript Swing) configurado incorretamente e o mesmo está fazendo isso com todo o texto inserido. Sendo que ele já tem mais de 1200 ID'S no banco.
Comment: Veja a configuração do Javascript Swing, e nos informe, pois dependendo, um editor de texto simples pode reverter isto.
Comment: Irei postar uma reposta contendo o código responsável por gravar no banco. O erro deve estar nele e não no editor. Perdão.
Comment: Os dados gravados são código javascript? Dá pra postar mais exemplos? O texto sempre começa **e** termina com aspas? Porque as primeiras aspas são **necessárias** e as demais não? Precisamos de um meio determinístico para saber quais aspas são "necessária" e quais não.
Comment: Realmente, deve ser nele mesmo :) achei estranho um editor de texto fazer isto...
Comment: esse é o código o qual os dados passam por ele ao clicar em salvar. o mesmo grava um texto como o de um blog que pode ou não conter HTML.
 `default:
            $consulta .= '\'' . db_entrada($valor) . '\', ';
            break;`
Comment: Acredito que o certo seria: `default: $consulta .= "'".db_entrada($valor)."'",'; break;`
Comment: Poderia postar o código completo para gravar?
Comment: Como sou novato, o stackoverflow não está me permitindo postar o código inteiro nem criar uma nova reposta.
Comment: Isto parece o tipo de problemas que as [magic quotes](http://www.php.net/manual/pt_BR/security.magicquotes.php) causam.
Comment: Pergunta editada.
Answer: No caso a sua pergunta não e nem voltado para PHP, mas sim MySQL.
Mas vamos lá:
Execute a query substituindo o nome da sua_tabela e sua_coluna
UPDATE sua_tabela
SET sua_coluna = REPLACE(sua_coluna, "\\'", '"')
WHERE sua_coluna LIKE "%\'%"

Answer: Uma forma alternativa é selecionar os registros que deseja corrigir.
SELECT

campo,
 ROUND (
        (
            LENGTH(campo)
            - LENGTH( REPLACE ( campo, '\'', '') )
        ) / LENGTH('\'')
    ) AS count

FROM

`regexp`

HAVING (
ROUND (
        (
            LENGTH(campo)
            - LENGTH( REPLACE ( campo, '\'', '') )
        ) / LENGTH('\'')
    )
) % 2 = 1

Nesse exemplo, a consulta retornará todos que possuírem single quote (') e a soma de suas ocorrências por row, sendo que quantidade de ocorrências deve ser número ímpar.
Qual a lógica em fazer isso?
Se existir números ímpares, provavelmente há ocorrências como \'Gurizinha\'s\'.
Com isso, eliminaria a necessidade em olhar uma boa parte dos resultados.
Para todos os outros resultados com soma resultante em pares, apenas aplique um replace geral, trocando o single quote escapado por um double quote sem o caracter de escape.
Ainda assim não resolverá 100%, mas ao menos terá uma lista de todos os IDs dos rows que precisam ser corrigidos.
Há outras formas com REGEXP, porém, não há como usar REGEXP com REPLACE. Teria que usar um UDF. Mas acho que pode resolver de forma mais simples sem precisar de UDFs.
Comment: Muito boa Lógica.
Vou tentar aplicar estudando UDF.
Answer: Procure sobre expressões regulares com PHP, não sou experiente ainda com elas, mas acredito que o exemplo abaixo resolve seu problema:
<?php
    function ModificarTXT($str)
    {
        // Pego toda palavra que começa com \' e substituo por "
        $string = ereg_replace("^([/\'])", '"',$str);

        //Pego toda palavra que termina com \' e substituo por "
        $string = ereg_replace("([/\'])$", '"',$string);
        return $string;
    }

    echo ModificarTXT("\'Gurizinha\'s\'"); // Teste da funcao
?>

Comment: Acredito que o problema não estaria em retirar as aspas simples, porém o autor está com dificuldades para expressar o problema, leia os comentários da pergunta.
Comment: Obrigado.
já fez um efeito diferente. o resultado foi o seguinte: `\'Valentine\'s Day\"`
Comment: Muito obrigado. Usei parte do seu código para criar um função que resolvesse parte do meu problema.
Answer: O problema está na sua conexão com o banco.
Use PDO que suas informações serão armazenadas exatamente da forma que passou, sem ter que remover ou substituir caracteres especiais, aspas, etc. E quando você recuperar, estará da mesma maneira que inseriu. PDO também aceita declarações parametrizadas (Preparement Statement) que vai lhe proteger de injeções SQL.

Para conectar:
// Dados da conexão
$host = 'localhost';
$port = '3306';
$user = 'root';
$pass = '123456';
$dbname = 'meubanco'

// Configuração para garantir a codificação correta caso esteja usando utf8
$config = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8");

// Realiza a conexão
$pdo = new PDO("mysql:host=$host;port=$port;dbname=$dbname", $user, $pass, $config);

Exemplo de INSERT parametrizado:
// Recebe variáveis
$codigo = $_POST['codigo'];
$nome = $_POST['nome'];

// Define a query usando '?' no lugar das variáveis
$query = "INSERT INTO table1 (codigo, nome) VALUES (?,?)";

// Prepara
$stmt = $pdo->prepare($query);

// Seta os parametros definidos como '?' na query de acordo com a ordem
$stmt->bindValue(1, $codigo, PDO::PARAM_INT);
$stmt->bindValue(2, $nome, PDO::PARAM_STR);

// Executa query
$stmt->execute();

// Pega o total de linhas afetadas
$total = $stmt->rowCount();

// Verifica se inseriu
if( $total > 0 )
    echo "Inseriu!";
else
    echo "Não Inseriu!";

Exemplo de SELECT parametrizado:
// Recebe variáveis
$codigo_minimo = $_POST['codigo_minimo'];

// Define a query usando '?' no lugar das variáveis
$query = "SELECT * FROM table1 WHERE codigo > ?)";

// Prepara
$stmt = $pdo->prepare($query);

// Seta os parametros definidos como '?' na query de acordo com a ordem
$stmt->bindValue(1, $codigo_minimo, PDO::PARAM_INT);

// Executa query
$stmt->execute();

// Muda de acesso as colunas para o modo pelo nome
$stmt->setFetchMode(PDO::FETCH_ASSOC);

// Usando fetchAll você pega todos resultados em um array
$result = $stmt->fetchAll();

var_dump($result);

var_dump($result) iria imprimir algo assim:
array (size=3)
   0 => array (size=2)
       'codigo' => 1
       'nome' => "bla bla bla"
   1 => array (size=2)
       'codigo' => 2
       'nome' => "ble ble ble"
   2 => array (size=2)
       'codigo' => 3
       'nome' => "blu blu blu"

Para desconectar:
// basta setar como NULL
unset($pdo);

Para mais informações sobre como utilizar o PDO, acesse este link.
Comment: Opa, muito bom. Obrigado. irei levar um tempo para adaptar o sistema do mesmo para PDO. Mais já dei inicio. Assim que adaptar o mesmo completo para PDO posto o resultado aqui.
Comment: Faça a adaptação por módulos. Crie uma classe de conexão em PDO. Escolha uma classe, a mais simples para começar, e mude-a. Após isso, sempre que puder modifique uma classe de persistência e vá mudando. :)
Comment: Preciso primeiramente aprender como funciona para poder saber aplicar. O material que tenho ainda são poucos. Mais já estou correndo atrás.
Answer: Use uma expressão regular para substituir ' por " no início e final de palavras inteiras:
preg_replace_callback('/(?<=\s|\t|\r|\n| )(\'.+\')(?=\s|\t|\r|\n|\.|,| )/muU', function($match) {
    return '"' . substr($match[0], 1, -1) . '"';
}, $x);

Você pode usar esta função (udf) para substituír todos os registros que já estão no banco de dados:
REGEXP_REPLACE(col, '/(?<=\s|\t|\r|\n| )\'(?=.*\'[\s\t\r\n\., ])/', '"');
REGEXP_REPLACE(col, '/(?<=[\s|\t|\r|\n| ]\'.*)\'(?=[\s\t\r\n\., ])/', '"');

Ou criar um script para selecionar e atualizar todas as colunas:
$rows = $dbh->query("SELECT id, col FROM tabela")->fetchAll(\PDO::FETCH_ASSOC);
$sth = $dbh->prepare("UPDATE tabela SET col = ? WHERE id = ?");
foreach($rows as $row) {
    $str = preg_replace_callback('/(?<=\s|\t|\r|\n| )(\'.+\')(?=\s|\t|\r|\n|\.|,| )/muU', function($match) {
       return '"' . substr($match[0], 1, -1) . '"';
    }, $row['col']);
    if($str == $row['col']) continue;
    $sth->execute(array( $str, $row['id'] ));
    $sth->closeCursor();
}

Comment: Opa, muito bom. Obrigado. irei levar um tempo para adaptar o sistema do mesmo para PDO. Mais já dei inicio. Assim que adaptar o mesmo completo para PDO posto o resultado aqui.


Namespaces e Use quando usar e para que servem?

Question: O que são namespaces e use e quando é recomendado usar eles numa aplicação ?
Comment: Acho que isso responde bem a pergunta http://pt.stackoverflow.com/a/16690/3635 mesmo sendo sobre outra linguagem, agora se quer saber como implementar namespaces com "pacotes" recomendo ler isto: http://pt.stackoverflow.com/a/88039/3635 e http://pt.stackoverflow.com/a/91512/3635
Answer: Os namespaces são semelhantes em diversas linguagens de programação, tal como esta resposta explica Como funciona namespaces no C#? (mesmo o assunto sendo uma linguagens diferente)
Faço das palavras do Maniero as minhas:

Ele não encapsula nada
Este entendimento mostra que apesar da visão que os programadores têm sobre o namespace ser um módulo, uma caixa de tipos (como indica a primeira função), na realidade ele funciona como um sobrenome para para os tipos. Um sobrenome funciona como uma forma de nomear uma família. Assim você pode ter dois Ricardos no mesmo ambiente sem confusão, porque um é Oliveira e o outro é Silva.

Tendo entendido qual a utilidade, vamos entender como funciona no PHP. Em linguagens como Java, Actionscript e C# os namespaces estão associados aos arquivos "nativamente", já no PHP isso não ocorre, você tem que usar require ou include manualmente, ou seja tenho que criar algo assim:
foo.php
<?php

namespace Foo\Bar;

class Baz
{
     public function hello()
     {
          echo 'Olá mundo!';
     }
}

index.php
<?php

use Foo\Bar\Baz;

require 'foo.php';

$test = new Baz;

O uso do "use"
O use não faz nada, mesmo com spl, no PHP ele serve somente para criar apelidos, por exemplo suponha que você tem duas classes que o nome é o mesmo, se fizer isto:
<?php

use Aa\Bb\MinhaClasse;
use Xx\Yy\MinhaClasse;

Na hora de usar MinhaClasse ele iria conflitar, então você pode usar assim:
<?php

use Aa\Bb\MinhaClasse;
use Xx\Yy\MinhaClasse as MinhaClass2;

$a = new MinhaClasse;
$b = new MinhaClasse2;

Ou seja no momento que usa isto:
<?php

use Foo\Bar\Baz;

$test = new Baz;

Você está apenas criando um "apelido rápido".
Existe também a possibilidade de usar diretamente:
<?php

$a = new \Aa\Bb\MinhaClasse;
$b = new \Xx\Yy\MinhaClasse;

Note que dentro de namespaces você pode adicionar outras, como funções e acredito que até executar algumas coisas, exemplo de funções:
<?php

namespace Foo\Bar;

function file($path) { ... }

Desta maneira não irá conflitar com a função nativa do php chamada file:
file('oi.txt');
\Foo\Bar\file('oi.txt');

Aproveite e leia sobre as funções nativas e namespaces aqui:
É necessário barra "\" no começo de funções nativas quando usamos namespace?

O SPL
O SPL é um conjunto de funções e classes nativas do PHP que foram criadas para resolver uma série de situações, como o PHP não carrega nativamente, mas é possivel usar o spl_autload, tal como expliquei aqui O que é spl_autoloader_register em PHP?, desta maneira os arquivos ficarão associados por divisão de pastas aos namespaces, de maneira semelhante as linguagens C#, Java e Actionscript3.
Extras
Como o @WallaceMaxters explicou em https://pt.stackoverflow.com/a/104479/3635, isso foi implementado desde o PHP5.3, ou seja bem antes do PHP7
O @rray também deu uma dica de como "escrever menos" (somente para o PHP7), assim você pode agrupar as classes de um namespace https://pt.stackoverflow.com/a/104426/3635, exemplo:
use Foo\Bar\DB\{Relaciona, NoSQL, LDAP};

Comment: Muito obrigado, lendo umas respostas aqui e na net, consegui entender bem o conceito.
Answer: Conforme a documentação oficial do PHP sobre namespaces, é basicamente uma forma de encapsular itens.
Na prática, serve para organizar melhor o seu código, agrupando classes e evitando o conflito de nomes, como por exemplo, com classes de terceiros.
Você pode utilizá-lo para separar módulos, grupos de classes ou da forma que você entender que irá melhor organizar e encapsular o seu código.