Using __attribute__ with typedef

Question: Apologies if the question seems too obvious or simple. Unfortunately, after going through a bunch of threads and googling about typedef coupled with attribute prefix, I am still not able to it figure out.
I have a following snippet of code in a (supposedly) portable app -
#ifdef WIN32
#define MY_PREFIX __declspec(dllexport)
#else
#define MY_PREFIX __attribute__((visibility("default")))
#endif

typedef MY_PREFIX bool some_func(void);

So my question is this -
1) What is that typedef exactly doing?
2) The code compiles fine on VS2008, but on G++ (gcc-4.1), I get a warning
   "'visibility' attribute ignored"
   Is there any way I can remove that warning? (Omitting -Wattributes is not an option)
Thanks!
Comment: @sskanitk: No. It is not a function pointer. It is a *function* type.
Comment: That doesn't look like a typedef that makes sense. Did you mean `typedef (MY_PREFIX *some_func)(void)`? (Also, `(void)` in C++ is an anachronism)
Comment: I understand that its trying to define a function pointer. And unfortunately, the weird looking typedef does compile on VS2008. :-/
Comment: @AndreyT, O OK - thanks. So a function type should still be able to use  __attribute__, rite?
Answer: AFAIK in GCC visibility attribute for function type cannot be "wrapped" into a typedef-ed type. The compiler assumes that this visibility attribute applies to the typedef-name itself. And GCC does not support visibility for typedef names (and it is not what you need anyway).
I'd say that instead of trying to wrap the declspec/attribute into the typedef, it should be specified explicitly at the point of function declaration. As in
#ifdef WIN32
#define MY_PREFIX __declspec(dllexport)
#else
#define MY_PREFIX __attribute__((visibility("default")))
#endif

typedef bool some_func(void);

MY_PREFIX some_func foo; // <- actual declaration

This will, of course, make is less clean, since instead of specifying MY_PREFIX once inside the typedef it should now be specified in every function declaration. But that's probably the only way to make it work, unless I'm missing something.
Comment: Ok - thanks for the explanation. Specifying attribute after the function declaration did not work - same warning is thrown again.
Comment: @sskanitk: Yes, I noticed that too. You can specify attributes before and after. Both ways are valid, with different meanings.
Comment: I'm surprised you can't specify visibility for typedefs, but you can for a struct/class.  It seems like a double standard that types have linkage in C++, but you can't specify linkage for types defined through typedefs.

Using __attribute__ with typedef


preg_match and my string

Question: I would like to have this kind of string:
basically a string of just letters a-zA-Z that can (or cannot) end with à or è or ì or ù.
I did this:
preg_match('/^[a-zA-Z]+[a-zA-Z]+$|^[a-zA-Z]+[àèìòù]?$/', $word)

and I still think is alright but for some reason it doesn't do the job!
EDIT:
There are some italian lastname that can end with àèìòù but some other ones just end with a letter. I want to get that the end of the string can end either with àèìòù or with a letter.
this is the full code
if ( preg_match('/^[a-zA-Z]+[àèìòù]?$/', $word) ) {
    echo "0";

} else {
    echo LASTNAME_ERROR;
}

but when I execute it, it gaves me the LASTNAME_ERROR
Comment: can or cannot? So you're saying either a string with any upper case or lower case letters in it, and potentially ending in the symbols or not?
Comment: I think this pattern is enough `^([a-zA-Z])+([àèìòù])?$`
Comment: Which character encoding is used in `$word` and which character encoding is used for the PHP script? (See as well my edit in http://stackoverflow.com/a/15383649/367456)
Answer: According to your description I would formulate the regular expression as followed:
/^[a-zA-Z]+[àèìòù]?$/

however from your question it is not clear where exactly your problem is. Your regex looks a bit verbose but not that faulty that it would explain your problem (at least not to me).
Edit: After re-reading your question I see one thing: The variable $word might contain UTF-8 encoded data. If that is the case you need to add the u (PCRE_UTF8) modifier to the regular expression:
/^[a-zA-Z]+[àèìòù]?$/u
                     ^
                     `--- UTF-8 modifier

This is also true the other way round: If you don't use UTF-8 yet for your application but the PHP files are encoded in UTF-8 the regular expression above is not valid, too.
So check the character encoding of both the string and your PHP file, that is one thing I could assume what can go wrong here.
Answer: Okay, let's just review some of your Regex, so you can see where you went wrong.
/^[a-zA-Z]+[a-zA-Z]

So one or more a-zA-Z's, followed by an a-zA-Z. Well that's really rather pointless:
/^[a-zA-Z]+ would suffice.
^[a-zA-Z]+[àèìòù]?$/

So a-zA-Z one or more times, followed by one or more of your symbols. Well, that's awfully similar to your original regular expression, so let's cut away and put it back together.
/^[a-zA-Z]+[àèìòù]?$/

So we've got a-zA-Z one or more times, followed by the symbols, 0 or more times at the end of the string. Just to note, Hakre devised this answer first. I just wanted to explain some of your mistakes.
Answer: This should be work
/((?!\(à|è|ì|ò|ù)$)[a-zA-Z])+/

(            # start a group for the purposes of repeating
 (à|è|ì|ò|ù) # negative lookahead assertion for the pattern à|è|ì|ò|ù
 [a-zA-Z]    # your own pattern for matching a URL character
)+           # repeat the group

preg_match and my string


Best module where you create a block and select a node from drop-down to render?

Question: It's been a short while since I played with Drupal 6 and now I'm getting my feet wet in Drupal 7.7.
I need to be able to create a simple block and in that block 'configure' area I want to be able to simply choose a node in the system from a drop-down list. When that block renders on XXXX page, it will then render the teaser for that node along with any other variables set.
I thought I could do this with Views 3.x by just creating a block view which only renders one result and takes a node title as an argument. The new Views 3.x interface has me a little confused though. Not quite sure how to set it so the node title argument is configured in the block admin area and that argument is then always passed with the block view renders.
I guess what I sort of was looking for was an exposed filter in the block admin edit page where the value is saved but that doesn't seem possible. Anyone have any module suggestions?
Answer: The Nodes In Block module seems to give you the desired outcome - albeit a different workflow to what you described. Here is an excerpt about it from the module's project page:

... makes it possible to add nodes into a block. A number of configurable blocks are generated which you can assign to a region. Visibility settings of this block are automatically set to 'Show on only the listed pages'. On the node content form, you define the weight and visibility per node thus making it easier for content administration as all your content is now in the same place. With this module, you don't have to explain your customers how blocks work but is still able to decide on which pages content must be rendered.


Remove load from server

Question: I'm having problem with my webserver which is currently running with very high loads. There are no cron jobs running and I can see any processes using the CPU. How can I find out what is going on?
Answer: From your top output it looks like the issue is high IO (RAM/CPU usage is low, IOWait% is high, load is high, and several httpd processes with state 'D'). What sort of application are you running? Are you using a database layer like MySQL? If so, what sort of load is the database experiencing and is it configured/optimized correctly?
You can try running iostat -x 5 when the IOWait% is high to see which drive is being used and whether it is read/write heavy. The next, more time consuming, step is to figure out what the source of the IO load is and how to fix it.
Answer: as i can see you are using apache prefork configuration and you have lots of clients on your server. start by trying to restart apache by typing sudo /etc/init.d/apache stop and then sudo /etc/init.d/apache start. you might consider switching to apache mpm configuration, so that you won't have so many process and load will be distributed across threads this way you might see the bottleneck processes that consume cpu time and kill them off or trace them out.
an alternative approach is to switch to lighthttpd or nginx web servers, they both consume less memory and can handle more simultaneous clients
Comment: or gatling (very fast)
Comment: Ok, thank you for the answer. I will try the solutions you described.
Comment: The nginx and lighttpd solutions as described in your answer is not complete, those applications cannot run PHP, but need fastcgi for it (for PHP, php-fpm is recommended)

Remove load from server


R: Find groups of vectors that have a > 80% overlap in their elements

Question: I have a list of vectors and I want to identify groups of vectors that share > 80% of their values. I would like the new groups to be named using the names of all the vectors included in the groups. And I want the new elements of the groups to be all possible elements from the grouped vectors. I am not sure how feasible this is.
Here is a simple example:
a <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14)
b <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
c <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,16)
d <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17)
e <- c(1,2,3,4,5,6)
f <- c(18,19,20,21,22,23,24,25,26)
g <- c(18,19,20,21,22,23,24,25)
h <- c(18,19,20,21,22,23,24)
i <- c(18,19,1,2,3,4,5,6,7)

So I think we would get two groups:
a-b-c-d-e-i
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
f-g-h
[1] 18 19 20 21 22 23 24 25 26

The values in the vectors do not have to be numeric, they could be strings. Let me know if there is anything else that I would need to specify. Thanks!
Answer: Assuming that the vectors are in a list V shown in the Note at the end of this answer, calculate an adjacency matrix adj such that adj[i,j] is 1 if the ith and jth vector have at least 80% of their elements in common and 0 otherwise.  We base this on the fraction of the shorter of the two vectors.  Then use the igraph package to convert adj to a graph g and compute its components.
library(igraph)

overlap <- function(u, v) length(intersect(u, v)) / min(length(u), length(v)) > 0.8
adj <- sapply(V, function(u) sapply(V, overlap, u)) + 0
g <- graph_from_adjacency_matrix(adj)
memb <- components(g)$membership
memb
## a b c d e f g h i
## 1 1 1 1 1 2 2 2 1

s <- split(V, memb)
groups <- lapply(s, function(x) unique(unlist(x)))
names(groups) <- lapply(s, function(x) paste(names(x), collapse = "-"))
groups

giving:
$`a-b-c-d-e-i`
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19

$`f-g-h`
[1] 18 19 20 21 22 23 24 25 26

We can also plot the graph:
plot(g)

Note:
V <- structure(list(a = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14), b = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
    c = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16),
    d = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
    16, 17), e = c(1, 2, 3, 4, 5, 6), f = c(18, 19, 20, 21, 22,
    23, 24, 25, 26), g = c(18, 19, 20, 21, 22, 23, 24, 25), h = c(18,
    19, 20, 21, 22, 23, 24), i = c(18, 19, 1, 2, 3, 4, 5, 6,
    7)), .Names = c("a", "b", "c", "d", "e", "f", "g", "h", "i"
))

Comment: That seems to work well to do the grouping of the vectors! I had also asked about having the new groups to named using the names of all the vectors included in the groups (i.e a-b-c-d-e-i and f-g-h), how could we do this?
Comment: Have added that.
Comment: Thanks! In addition, I mentioned that I was looking to get new vectors representing each group, where the elements would be all possible elements from the vectors making up the groups. Does that make sense?
Comment: Oh that is also included in the code that you just added right?
Comment: It's awesome by the way, the plots are a nice addition


Newton-Côtes closed formula for $\int_{0}^{2n} x^{2n+1}\cos(2{\pi}x) dx$

Question: Given $n \in \mathbb{N}$, I want to determine the value obtained by approximating the integral
$$ \int_{0}^{2n} x^{2n+1}\cos(2{\pi}x) dx $$
using the closed Newton-Côtes formula of $2n + 1$ points.
In other words, I want to find a closed formula for:
$$\int_{0}^{2n} P_{2n}(x) dx$$
where $P_{2n}(x)$ is the Lagrange interpolating polynomial of $f(x) = x^{2n+1}\cos(2{\pi}x)$ through the $2n + 1$ points $\{0, 1, ..., 2n\}$, which has degree less than or equal to $2n$.
For $n = 1, ..., 6$, the values of the integrals are:
$$4, \frac{2048}{3}, 209952, \frac{536870912}{5}, \frac{250000000000}{3}$$
but I don't see a pattern.

Note: I initially considered the problem for $f(x) = x^{2n}\cos(2{\pi}x)$. Since in this case, there are $2n + 1$ points and $f(k) = k^{2n}$ for $k = 1, ..., 2n$, the Lagrange interpolating polynomial of $f(x)$ (which is unique) must be $x^{2n}$, as it has degree less than or equal to $2n$. Then the value of the integral is trivial to compute ($\frac{(2n)^{2n+1}}{2n+1}$).
Answer: I don't know if this helps, but
$$x^{2n+1}-\prod_{k=0}^{2n}(x-k)=x^{2n+1}-(x)_{2n+1}$$
Is a polynomial of degree $2n$ that goes through all the $2n+1$ points, where $(x)_{2n+1}$ is the falling factorial. The first few integrals:
<code>       0  0.000000000000000000000000000000000E+0000
       1   4.00000000000000000000000000000000
       2   682.666666666666666666666666666667
       3   209952.000000000000000000000000000
       4   107374182.400000000000000000000000
       5   83333333333.3333333333333333333333
       6   91708461753490.2857142857142857145
       7   136122083613085696.000000000000000
       8   262353693492758067427.555555555558
       9   637411810819803908721868.800000012
      10   1906501818181818181818181818.18188
</code>
EDIT: Are you trying to form some sort of inductive hypothesis like
$$A_n=\frac{2^{2n-1}(n-1)^{2n}}n$$
EDIT: The situation is so much simpler than this. If we make the substitutions $y=2n-x$ and $j=2n-k$ then
$$\begin{align}\int_0^{2n}\prod_{k=0}^{2n}(x-k)dx&=\int_{2n}^0\prod_{k=0}^{2n}(2n-y-k)(-dy)\\
&=\int_0^{2n}\prod_{k=0}^{2n}(2n-y-k)(dy)\\
&=\int_0^{2n}\prod_{j=0}^{2n}(2n-y-2n+j)(dy)\\
&=(-1)^{2n+1}\int_0^{2n}\prod_{j=0}^{2n}(y-j)(dy)\\
&=-\int_0^{2n}\prod_{k=0}^{2n}(x-k)dx\\
&=0\end{align}$$
Because only zero is its own additive inverse. The $2n+1$-point Newton-Cotes formula also says this integral is $0$ because the integrand is zero at all the sample points. Thus the $2n+1$-point Newton-Cotes formula is exact for all polynomials of degree $2n+1$ because it's exact for all polynomials of degree $2n$ by construction and the difference between and degree $2n+1$ polynomial and some degree $2n$ polynomial is a multiple of $\prod_{k=0}^{2n}(x-k)$, which the $2n+1$-point Newton-Cotes formula evaluates exactly. That's an old theorem.
Thus if $f(x_k)=x_k^{2n+1}$ then a polynomial of degree $2n+1$ that goes through all $2n+1$ points is $f(x)=x^{2n+1}$ and because the $2n+1$-point  Newton-Cotes formula integrates this exactly, it spits out
$$\int_0^{2n}x^{2n+1}dx=\frac{(2n)^{2n+2}}{2n+2}=A_{n+1}$$
which is the same as our numerical results.
Comment: Thank you, you're absolutely right. How did you come up with the formula for $A_n$?
Comment: I just multiplied by the denominator pattern and factored the numerators. So it's just a pattern and not a theorem. It matched my results up through $A_{11}$. May be off by one compared with your definition because you listed $A_1$ through $A_6$, but there were only $5$ elements, so it's not clear whether that means $A_1$ through $A_5$ or $A_2$ through $A_6$.


Even Descartes numbers

Question: A Descartes number is defined as an odd number which would have been an odd perfect number, if one of its composite factors were prime. An example is:
$$
D = 3^2\times7^2\times11^2\times13^2\times22021,
$$
for which the divisor sigma function equals $2D$ when $22021$ is assumed prime (which it is evidently not).
If one removes the requirement of being an odd number, one can find several examples of "even Descartes numbers". For instance, one observes that
$$
D_e = 3\times4\times5 = 60
$$
is such that $\sigma(D_e)=2D_e$ if one assumes that $4$ is prime. Indeed:
$$
\sigma(3\times5)\times(4+1) = 120 = 2D_e.
$$
I have not found any mention of even Descartes numbers in literature. Are these interesting for any reason, have they been studied anywhere? Any references would be welcome.
Comment: Such numbers are certainly interesting, but in the case of odd numbers, there is a clear motivation to search for numbers somehow coming near to a perfect number because no single odd perfect number is known.
Comment: @Peter Thank you. Do you know any papers that have dealt with "even Descartes numbers"? Or any mention of them in literature?
Comment: bump... so there are no such references?
Comment: I have not checked this. But as said, in the case of even numbers, the motivation is small because the perfect numbers are completely classified by the Mersenne-primes.
Comment: @Peter Thank you!
Comment: @Peter Is it sill interesting if I can generate an infinity of even Descartes numbers?
Answer: Descartes numbers (from its original formulation in Banks, et. al's paper) are also known as spoof odd perfect numbers (as coined by Dittmer).
In your case, the correct search term to use is even spoof perfect number.
Google returns the following references:
https://oeis.org/wiki/Spoof_perfect_numbers#Even_spoof_perfect_numbers
Spoof odd perfect numbers by Dittmer
Problems and Puzzles on Spoof Perfect Numbers
Comment: You can also try searching for references related to the *freestyle sum of divisors function*, @PreservedFruit.
Comment: @Klangen: It *is* conjectured that there are only *finitely* many even completely spoof-perfect numbers, for a given number of composite factors.  Check the last link in my answer.
Comment: Thank you, that's exactly what I was looking for!
Comment: Quick follow-up question: do we know whether there are an infinity of spoof-perfect numbers (or **odd** spoof-perfect numbers)? I found no reference to the density of such numbers in any of the links.
Comment: @Klangen: There is only one odd spoof-perfect number that is currently known, and you already have that number in your question. Many more even spoof-perfect numbers are known, but I do not know if we can prove that there are infu
Comment: (Cont'd.) ... infinitely* many of them.
Comment: @JoseArnaldoBebitaDris Thank you.

Even Descartes numbers


Why should I be Bayesian when my model is wrong?

Question: Edits: I have added a simple example: inference of the mean of the $X_i$. I have also slightly clarified why the credible intervals not matching confidence intervals is bad.
I, a fairly devout Bayesian, am in the middle of a crisis of faith of sorts.
My problem is the following. Assume that I want to analyse some IID data $X_i$. What I would do is:

first, propose a conditional model:
$$ p(X|\theta) $$
Then, choose a prior on $\theta$:
$$ p(\theta) $$
Finally, apply Bayes' rule, compute the posterior: $p(\theta | X_1 \dots X_n )$ (or some approximation to it if it should be uncomputable) and answer all questions I have about $\theta$

This is a sensible approach: if the true model of the data $X_i$ is indeed "inside" of my conditional (it corresponds to some value $\theta_0$), then I can call upon statistical decision theory to say that my method is admissible (see Robert's "The Bayesian choice" for details; "All of statistics" also gives a clear account in the relevant chapter).
However, as everybody knows, assuming that my model is correct is fairly arrogant: why should nature fall neatly inside the box of the models which I have considered? It is much more realistic to assume that the real model of the data $p_{true}(X)$ differs from $p(X|\theta)$ for all values of $\theta$. This is usually called a "misspecified" model.
My problem is that, in this more realistic misspecified case, I don't have any good arguments for being Bayesian (i.e: computing the posterior distribution) versus simply computing the Maximum Likelihood Estimator (MLE):
$$ \hat \theta_{ML} = \arg \max_\theta [ p(X_1 \dots X_n |\theta) ] $$
Indeed, according to Kleijn, v.d Vaart (2012), in the misspecified case, the posterior distribution:

converges as $n\rightarrow \infty $ to a dirac distribution centered at a $\hat \theta_{ML} $
does not have the correct variance (unless two values just happen to be same) in order to ensure that credible intervals of the posterior match confidence intervals for $\theta$. (Note that, while confidence intervals are obviously something that Bayesians don't care about excessively, this qualitatively means that the posterior distribution is intrinsically wrong, as it implies that its credible intervals do not have correct coverage)

Thus, we are paying a computational premium (Bayesian inference, in general, is more expensive than MLE) for no additional properties
Thus, finally, my question: are there any arguments, whether theoretical or empirical, for using Bayesian inference over the simpler MLE alternative when the model is misspecified?
(Since I know that my questions are often unclear, please let me known if you don't understand something: I'll try to rephrase it)
Edit: let's consider a simple example: infering the mean of the $X_i$ under a Gaussian model (with known variance $\sigma$ to simplify even further).
We consider a Gaussian prior: we denote $\mu_0$ the prior mean, $\beta_0$ the inverse variance of the prior. Let $\bar X$ be the empirical mean of the $X_i$. Finally, note: $\mu = (\beta_0 \mu_0 + \frac{n}{\sigma^2} \bar X) / (\beta_0 + \frac{n}{\sigma^2} )$.
The posterior distribution is:
$$ p(\theta |X_1 \dots X_n)\; \propto\; \exp\!\Big( - (\beta_0 + \frac{n}{\sigma^2} ) (\theta - \mu)^2 / 2\Big) $$
In the correctly specified case (when the $X_i$ really have a Gaussian distribution), this posterior has the following nice properties

If the $X_i$ are generated from a hierarchical model in which their shared mean is picked from the prior distribution, then the posterior credible intervals have exact coverage. Conditional on the data, the probability of $\theta$ being in any interval is equal to the probability that the posterior ascribes to this interval
Even if the prior isn't correct, the credible intervals have correct coverage in the limit $n\rightarrow \infty$ in which the prior influence on the posterior vanishes
the posterior further has good frequentist properties: any Bayesian estimator constructed from the posterior is guaranteed to be admissible, the posterior mean is an efficient estimator (in the Cramer-Rao sense) of the mean, credible intervals are, asymptotically, confidence intervals.

In the misspecified case, most of these properties are not guaranteed by the theory. In order to fix ideas, let's assume that the real model for the $X_i$ is that they are instead Student distributions. The only property that we can guarantee (Kleijn et al) is that the posterior distribution concentrates on the real mean of the $X_i$ in the limit $n \rightarrow \infty$. In general, all the coverage properties would vanish. Worse, in general, we can guarantee that, in that limit, the coverage properties are fundamentally wrong: the posterior distribution ascribes the wrong probability to various regions of space.
Comment: When your model is misspecified in terms of using incorrect likelihood function, then both MLE and Bayesian estimates would be wrong...
Comment: @Tim: the MLE and Bayesian inference are not meaningless in the misspecified case: they both try to recover the parameter value $\tilde \theta_0$ which gives the best account of the data within the conditional models. More precisely, $\tilde \theta_0$ is the argmin of $ KL[p(X), p(X|\theta)] $ where KL is the Kullback Leibler divergence. Under mild assumptions, both MLE and Bayesian inference correctly identify this $\tilde \theta_0$ when provided with a sufficient amount of data
Comment: Well, Bayesian approaches regularize. That is something, to help against overfitting - whether or not your model is misspecified. Of course, that just leads to the related question about arguments for Bayesian inference against *regularized* classical approaches (lasso, ridge regression, elastic net etc.).
Comment: @amoeba I imagine hard-core Bayesian look and act like comandante Che
Comment: You might be interested in [this work](https://arxiv.org/abs/1412.3730) and its relatives.
Comment: @Tim, you probably missed this: *we are paying a computational premium (Bayesian inference, in general, is more expensive than MLE) for no additional properties*.
Comment: Regarding your edit: but credible intervals are not supposed to have correct coverage! If you want coverage guarantees, you should be doing frequentist statistics, this is what it's all about. Bayesian statistics does *not* generally promise you correct coverage, this is admitted and accepted (and even praised!) by the Bayesians. This seems to be a big confusion here.
Comment: I am confused by your second bullet point. You say that in the misspecified case, credible interval of the posterior will not match the confidence interval; but why should any Bayesian consider such "matching" as something desirable? Confidence intervals is a frequentist tool; hard-core Bayesians usually claim that CIs are misguided and should  not be trusted.
Comment: @GuillaumeDehaene: There is one place where Bayesian models completely break down and that is when you place zero prior probability somewhere where there should be nonzero probability (or vice-versa). For example, it doesn't matter how many data points (e.g votes) you gather in support of the idea that the speed of light is infinite; it simply isn't, even if 6 billion people say otherwise. It sounds stupid in hindsight when you already know the fact but it should make you pause when you're doing something similar about something you don't actually know.
Comment: @Mehrdad Your example begs the question of where folks with the privileged perspective got a prior belief that was somehow unavailable to 6 billion. It feels like you are inserting a non-Bayesian interpretation of probability into the example.
Comment: Credible intervals are defined so as to have a coverage property: if we sample from a hierarchical model, conditional on the data we observed, then the probability of $\theta$ falling inside a credible interval with level $\alpha$ should be equal to $\alpha$. Maybe the word "coverage" is incorrect here, I guess? Anyway, this property fails in the misspecified case
Comment: This is not correct. You don't need the concept of coverage to define a credible interval. If you're really concerned about model misspecification, you should use a Bayesian Nonparametric approach and understand the subtleties of how to control the support of your prior. I you do so, and care about, your Bayesian procedures will have good "what if"/frequentist properties. A good starting point is this paper: https://www.google.com.br/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=barron+wasserman+schervish
Comment: Maybe the word "coverage" is not the appropriate one (though I can't find an alternative). The point is that credible intervals have a single nice property in the correctly specified case which vanishes in the misspecified case. Thank you for the reference. It seems like they only prove the same result as Kleijn, Vaart: in the large-data limit, the posterior distribution converges to a (close equivalent to) a diract at the correct value. They fail to prove that the rate of contraction of the posterior around the truth has the correct properties. I'll check it out in detail.
Comment: They don't "fail" to prove. After the work of Lorraine Schwarz and others (Freedman, Diaconis etc), this BSW work is one of the pioneering papers in this field of research. Contraction rates have been studied extensively since then. Anyway, this is not something to be understood in 12 minutes (the time between my comment and yours).
Comment: Reorganized and expanded my answer to make better sense. I think it may be helpful to think of a toy domain which includes some of the complexity of the real-world problem (I feel that inferring the mean of a distribution is somewhat too simple). My proposal is that optimization is a good sandbox. I am interested in your thoughts about it.
Comment: MLE has assumptions as well. It is often used inappropriately. One Bayesian model may be vastly inferior in predictive ability than another, that is, if the model is less well matched to the data than an alternative model. If one model is not working properly, the first step is to search for a better model to the data, and only then apply the new model in a tailored, goal-oriented Bayesian context.
Answer: I consider Bayesian approach when my data set is not everything that is known about the subject, and want to somehow incorporate that exogenous knowledge into my forecast.
For instance, my client wants a forecast of the loan defaults in their portfolio. They have 100 loans with a few years of quarterly historical data. There were a few occurrences of delinquency (late payment) and just a couple of defaults. If I try to estimate the survival model on this data set, it'll be very little data to estimate and too much uncertainty to forecast.
On the other hand, the portfolio managers are experienced people, some of them may have spent decades managing relationships with borrowers. They have ideas around what the default rates should be like. So, they're capable of coming up with reasonable priors. Note, not the priors which have nice math properties and look intellectually appealing to me. I'll chat with them and extract their experiences and knowledge in the form of those priors.
Now Bayesian framework will provide me with mechanics to marry the exogenous knowledge in the form of priors with the data, and obtain the posterior that is superior to both pure qualitative judgment and pure data driven forecast, in my opinion. This is not a philosophy and I'm not a Bayesian. I'm just using the Bayesian tools to consistently incorporate expert knowledge into the data-driven estimation.
Comment: This is a general argument for Bayesian modelling, but how does it relate to the specific case of misspecified model? I do not see a connection.
Comment: @RichardHardy, I think my answer goes into the heart of OP's crisis of faith which is driven by the thought that if your conditional model is misspecified then it'll overpower the prior with increasing sample size and your posterior will be pushed towards the wrong model. In this case why bother about Bayesian to start with, why not just to straight MLE, he asks. My example is decidedly not philosophical, but practical: you deal often not with just finite, but small samples. So, your data will not drag the posterior too far away from the prior, which represents the exogenous knowledge.
Comment: Well, it does relate to my question: even in the misspecified case, bayesian inference does handle better (i.e: in a more principled fashion) qualitative information, via the prior, than MLE methods, which would have to work with regularizers. It's a form of empirical argument as to why bayesian inference is slightly better than MLE.
Comment: A very nice point. Bayesian inference does offer a framework for solving precisely a task like the one you have presented. Thank you.
Comment: @Aksakal, whether models are misspecified is besides the point. What I am concerned with is that you do not answer the question. (If the OP disagrees, then I think he has done a poor job in formulating the question.) But I see there has been a recent edit, so perhaps the question has been changed by now.
Comment: @RichardHardy In econometrics and finance I assume that all models are misspecified, pretty much. I'm sure that in social sciences and business the situation is even worse. This is not physics or chemistry.
Comment: (+1) for _"Note, not the priors which have nice math properties and look intellectually appealing to me. I'll chat with them and extract their experiences and knowledge in the form of those priors."_
Comment: Now the last sentence is *the* answer. It took some time to extract, but it was worth the effort.
Answer: A very interesting question...that may not have an answer (but that does not make it less interesting!)
A few thoughts (and many links to my blog entries!) about that meme that all models are wrong:

While the hypothetical model is indeed almost invariably and irremediably wrong, it still makes sense to act in an efficient or coherent manner with respect to this model if this is the best one can do. The resulting inference produces an evaluation of the formal model that is the "closest" to the actual data generating model (if any);
There exist Bayesian approaches that can do without the model, a most recent example being the papers by Bissiri et al. (with my comments) and by Watson and Holmes (which I discussed with Judith Rousseau);
In a connected way, there exists a whole branch of Bayesian statistics dealing with M-open inference;
And yet another direction I like a lot is the SafeBayes approach of Peter Grünwald, who takes into account model misspecification to replace the likelihood with a down-graded version expressed as a power of the original likelihood.
The very recent Read Paper by Gelman and Hennig addresses this issue, albeit in a circumvoluted manner (and I added some comments on my blog). I presume you could gather material for a discussion from the entries about your question.
In a sense, Bayesians should be the least concerned among statisticians and modellers about this aspect since the sampling model is to be taken as one of several prior assumptions and the outcome is conditional or relative to all those prior assumptions.
Comment: It's very nice to have your opinion on this. Your first point makes intuitive sense: if the model isn't too wrong, then the result of our inference should be ok. However, has anybody ever proved any result like that (or explored the question empirically)? Your last point (which I might have misunderstood) leaves me perplex: the sampling model is a critical choice. The fact that we also make also choices doesn't mean that errors in the choice of the sampling model can't taint the whole model.  Thank you for the references and the wonderful blog.
Comment: My main objection to non-parametrics is practical: they are more computationally expensive by several orders of magnitudes compared to simpler alternatives. Furthermore, don't we also run into trouble with non-parametrics, because its almost impossible for two prior distributions to have common support? That means that the prior would have a heavy influence and that it would be (almost) impossible for bayesian statisticians to agree when starting from different priors.
Comment: You seem to be suggesting that there is a decision-theoretic aspect of averaging model uncertainty versus picking only 'best' model. Surely it's always advantagous, ie helps make better decisions, to coherently incorporate all uncertainties, including model uncertainties.
Comment: @GuillaumeDehaene: Thanks. Wrt the last point, I mean that all Bayesian statements are conditional on the chosen Universe. They do not (dare to) say anything outside that Universe. From a Bayesian perspective, if one entertains the notion of a possibly wrong model, one should include a model for being in the wrong model. One partly satisfactory solution is to go non-parametric.
Comment: For point 1., why not Bayesian model averaging? Why just use the 'best' model?
Comment: @innisfree: it all depends on what you plan to do with the outcome, I have no religion about model averaging versus best model.
Comment: @innisfree: yes I do mean it depends on the loss function one chooses to evaluate one's actions. For a zero-one loss function, choosing the most likely model is the optimal Bayes decision.
Comment: @Xian returning to this as I took your book from library.  For a 0-1 loss function,  mode is indeed optimal. But wouldn't it be mode of $p (x) = \sum p (x|m)p(m) $, which is model averaged?
Answer: I only see this today but still I think I should chip in given that I'm kind of an expert and that at least two answers (nr 3 and 20 (thanks for referring to my work Xi'an!)) mention my work on SafeBayes - in particular G. and van Ommen, "Inconsistency of Bayesian Inference for Misspecified Linear Models, and a Proposal for Repairing It" (2014). And I'd also like to add something to comment 2:
2 says: (an advantage of Bayes under misspecification is ...) "Well, Bayesian approaches regularize. That is something, to help against overfitting - whether or not your model is misspecified. Of course, that just leads to the related question about arguments for Bayesian inference against regularized classical approaches (lasso etc)"
This is true, but it is crucial to add that Bayesian approaches may not regularize enough  if the model is wrong. This is the main point of the work with Van Ommen - we see there that standard Bayes overfits rather terribly in some regression context with wrong-but-very-useful-models. Not as bad as MLE, but still way too much to be useful. There's a whole strand of work in (frequentist and game-theoretic) theoretical machine learning where they use methods similar to Bayes, but with a much smaller 'learning rate' - making the prior more and the data less important, thus regularizing more. These methods are designed to work well in worst-case situations (misspecification and even worse, adversarial data) - the SafeBayes approach is designed to 'learn the optimal learning rate' from the data itself - and this optimal learining rate, i.e. the optimal amount of regularization,  in effect depends on geometrical aspects of model and underlying distribution (i.e. is the model convex or not).
Relatedly, there is a folk theorem (mentioned by several above) saying that Bayes will have the posterior concentrate on the distribution closest in KL divergence to the 'truth'. But this only holds under very stringent conditions - MUCH more stringent than the conditions needed for convergence in the well-specified case.
If you're dealing with  standard low dimensional parametric models and data are i.i.d. according to some distribution (not in the model) then the posterior will indeed concentrate around the point in the model that is closest to the truth in KL divergence.
Now if you're dealing with large nonparametric models and the model is correct, then (essentially) your posterior will still concentrate around the true distribution given enough data, as long as your prior puts sufficient mass in small KL balls around the true distribution. This is the weak condition that is needed for convergence in the nonparametric case if the model is correct.
But if your model is nonparametric yet incorrect, then the posterior may simply not concentrate around the closest KL point, even if your prior puts mass close to 1 (!) there - your posterior may remain confused for ever, concentrating on ever-different distributions as time proceeds but never around the best one.
In my papers I have several examples of this happening. THe papers that do show convergence under misspecification (e.g. Kleijn and van der Vaart) require a lot of additional conditions, e.g. the model must be convex, or the prior must obey certain (complicated) properties. This is what I mean by 'stringent' conditions.
In practice we're often dealing with parametric yet very high dimensional models (think Bayesian ridge regression etc.). Then if the model is wrong, eventually your posterior will concentrate on the best KL-distribution in the model but a mini-version of the nonparametric inconsistency still holds: it may take orders of magnitude more data before convergence happens - again, my paper with Van Ommen gives examples.
The SafeBayes approach modifies standard bayes in a way that guarantees convergence in nonparametric models under (essentially) the same conditions as in the well-specified case, i.e. sufficient prior mass near the KL-optimal distribution in the model (G. and Mehta, 2014).
Then there's the question of whether Bayes even has justification under misspecification. IMHO (and as also mentioned by several people above), the standard justifications of Bayes (admissibility, Savage, De Finetti, Cox etc) do not hold here (because if you realize your model is misspecified, your probabilities do not represent your true beliefs!).
HOWEVER many Bayes methods can also be interpreted as 'minimum description length (MDL) methods' - MDL is an information-theoretic method which equates 'learning from data' with 'trying to compress the data as much as possible'. This data compression interpretation of (some) Bayesian methods remains valid under misspecification. So there is still some underlying interpretation that holds up under misspecification - nevertheless, there are problems, as my paper with van Ommen (and the confidence interval/credible set problem mentioned in the original post) show.
And then a final remark about the original post: you mention the 'admissibility' justification of Bayes (going back to Wald's complete class thm of the 1940s/50s). Whether or not this is truly a justification of Bayes really depends very much on one's precise definition of 'Bayesian inference' (which differs from researcher to researcher...). The reason is that these admissibility results allow  the possibility that one uses a prior that depends on aspects of the problem such as sample size, and loss function of interest etc. Most 'real' Bayesians would not want to change their prior if the amount of data they have to process changes, or if the loss function of interest is suddenly changed. For example, with strictly convex loss functions, minimax estimators are also admissible - though not usually thought of as Bayesian! The reason is that for each fixed sample size, they are equivalent to Bayes with a particular prior, but the prior is different for each sample size.
Hope this is useful!
Comment: Welcome to CrossValidated and thanks for responding on this question. A minor note -- you can't rely on the answers being sorted in the same order as you see them; different people can sort in different orders (there's a choice of different sorting criteria at the top of the highest placed answer) and two of those criteria change over time. That is if you refer to them as "nr 3 and 20" people won't know which answers you mean. [I can only find ten answers as well.]
Comment: Thank you for a great answer Peter. I'm confused about your comment that Bayesian inference in the misspecified case requires very strong assumptions. Which assumptions are you explicitely referring to? Are you talking about the condition that the posterior needs to converge to a dirac distribution on the best parameter value? or are you talking about the more technical conditions on the likelihood which ensure asymptotic normality?
Comment: Guillaume - I'm updating the above to take your comment into account
Comment: Ok, thanks to Glen B (moderator) - I'll keep this in mind from now on.
Answer: Edits: Added reference to this paper in the body, as requested by the OP.

I am giving an answer as a naive empirical Bayesian here.
First, the posterior distribution allows you to do computations that you simply cannot do with a straightforward MLE.
The simplest case is that today's posterior is tomorrow's prior. Bayesian inference naturally allows for sequential updates, or more in general online or delayed combination of multiple sources of information (incorporating a prior is just one textbook instance of such combination).
Bayesian Decision Theory with a nontrivial loss function is another example. I would not know what to do otherwise.
Second, with this answer I will try and argue that the mantra that quantification of uncertainty is generally better than no uncertainty is effectively an empirical question, since theorems (as you mentioned, and as far as I know) provide no guarantees.
Optimization as a toy model of scientific endeavor
A domain that I feel fully captures the complexity of the problem is a very practical, no-nonsense one, the optimization of a black-box function $f: \mathcal{X} \subset \mathbb{R}^D \rightarrow \mathbb{R}$. We assume that we can sequentially query a point $x \in \mathcal{X}$ and get a possibly noisy observation $y = f(x) + \varepsilon$, with $\varepsilon \sim \mathcal{N}(0,\sigma^2)$. Our goal is to get as close as possible to $x^* = \arg\min_x f(x)$ with the minimum number of function evaluations.
A particularly effective way to proceed, as you may expect, is to build a predictive model of what would happen if I query any $x^\prime \in \mathcal{X}$, and use this information to decide what to do next (either locally or globally). See Rios and Sahinidis (2013) for a review of derivative-free global optimization methods.
When the model is complex enough, this is called a meta-model or surrogate-function or response surface approach. Crucially, the model could be a point estimate of $f$ (e.g., the fit of a radial basis network function to our observations), or we could be Bayesian and somehow get a full posterior distribution over $f$ (e.g., via a Gaussian process).
Bayesian optimization  uses the posterior over $f$ (in particular, the joint conditional posterior mean and variance at any point) to guide the search of the (global) optimum via some principled heuristic. The classical choice is to maximize the expected improvement over the current best point, but there are even fancier methods, like minimizing the expected entropy over the location of the minimum (see also here).
The empirical result here is that having access to a posterior, even if partially misspecified, generally produces better results than other methods.
(There are caveats and situations in which Bayesian optimization is no better than random search, such as in high dimensions.)
In this paper, we perform an empirical evaluation of a novel BO method vs. other optimization algorithms, checking whether using BO is convenient in practice, with promising results.
Since you asked -- this has a much higher computational cost than other non-Bayesian methods, and you were wondering why we should be Bayesian.
The assumption here is that the cost involved in evaluating the true $f$ (e.g., in a real scenario, a complex engineering or machine learning experiment) is much larger than the computational cost for the Bayesian analysis, so being Bayesian pays off.
What can we learn from this example?
First, why does Bayesian optimization work at all? I guess that the model is wrong, but not that wrong, and as usual wrongness depends on what your model is for. For example, the exact shape of $f$ is not relevant for optimization, since we could be optimizing any monotonic transformation thereof. I guess nature is full of such invariances.
So, the search we are doing might not be optimal (i.e., we are throwing away good information), but still better than with no uncertainty information.
Second, our example highlights that it is possible that the usefulness of being Bayesian or not depends on the context, e.g. the relative cost and amount of available (computational) resources. (Of course if you are a hardcore Bayesian you believe that every computation is Bayesian inference under some prior and/or approximation.)
Finally, the big question is -- why are the models we use not-so-bad after all, in the sense that the posteriors are still useful and not statistical garbage? If we take the No Free Lunch theorem, apparently we shouldn't be able to say much, but luckily we do not live in a world of completely random (or adversarially chosen) functions.
More in general, since you put the "philosophical" tag... I guess we are entering the realm of the problem of induction, or the unreasonable effectiveness of mathematics in the statistical sciences (specifically, of our mathematical intuition & ability to specify models that work in practice) -- in the sense that from a purely a priori standpoint there is no reason why our guesses should be good or have any guarantee (and for sure you can build mathematical counterexamples in which things go awry), but they turn out to work well in practice.
Comment: Awesome answer. Thank you very much for your contribution. Is there any review / fair comparison of Bayesian optimization vs normal optimization techniques that highlights that the Bayesian version is empirically better as you claim? (I'm quite fine with taking you at your word, but a reference would be useful)
Comment: Thanks! I think that the [probabilistic numerics](https://arxiv.org/pdf/1506.01326.pdf) call-to-arms contains several theoretical and empirical arguments. I am not aware of a benchmark that really compares BO methods with standard methods, but [*trigger warning: shameless plug*] I am currently working on something along these lines within the field of computational neuroscience; I plan to put some of the results on arXiv, hopefully within the next few weeks.
Comment: @IMA: Sorry, I don't think that I 100% get your point. I was taking black-box optimization as a _toy model_ of the scientific endeavor. I believe you can map many steps and problems of "science" to this simpler (but still incredibly complex) domain.

There is no need for the "Gaussian noise" assumption for my argument, it was just for simplicity. Real-world optimization problems (e.g., in engineering) may be corrupted by non-Gaussian noise, and that's something that needs to be dealt with. And Gaussian processes do not _need_ Gaussian observation noise (although it makes inference easy).
Comment: Indeed, at least their figure 2 has a clear comparison. Could you please add you work to your main question once it is out? I feel like it would be a valuable addition.
Comment: Yes -- that's their method for adaptive Bayesian quadrature, which is a pretty cool idea (in practice, its effectiveness depends on whether the GP approximation works; which is often near-equivalent to say that you have a sensible parameterization of your problem). I will add the link to the answer when my work is available, thanks.
Comment: I feel like the example / paper does not capture the (practical) problems of scientific endeavor. Even with a black box approach, you make sampling assumptions that almost never strictly hold for real data. We can not query several data points of a fixed DGP, and in particular this means that based on $x$, our function will not have a (Gaussian) additive error. $x$ will (in practice) not have sufficient information for this to hold.
So indeed, our model - in particular this GP one - is likely wrong. This is probably the number 1 problem we face, and one would ask if your ex. sidesteps this.
Comment: For example, if the assumptions would hold, we would be justified in throwing an arbitrarily complex neural network at the problem and call it a day.

But if we take our theory seriously, then there is a trade-off between our modeled process being "as right as possible" ( meaning general), and our estimation being identified. This sounds non-Bayesian, but it holds here as well.
Comment: And this is why, I think, the majority of papers with observational data move toward a non-Bayesian, (semi)non-parametric approaches trying to mimic experiments. When defending your empirical analysis, assumptions such as Gaussian errors are a non-starter. Only actual experiments would have data where this assumption makes sense. Instead, one is asked to exploit features in the data that do not rely on such "functional" assumptions. And when even these simple features of the true DGP are difficult to estimate, how heroic is it to think we can truly approximate the whole distribution?
Comment: So that being said, I fully agree when it comes to your comparison of fully specified models. In particular, the transparent process of deriving knowledge from (new) data is an advantage of Bayesian over other models even if all models are wrong.

But if all models are wrong, and in particular, wrong enough so that no GP can be assumed, the question is still valid - should we not use other statistical approaches? Is our knowledge of a completely wrong model, provided by Bayesian analysis, at all useful?
Comment: Sorry, then I misunderstand your point, because both in the paper and the example, it seems to me that you assume an additive noise term with a stable distribution (and indeed it seems to me that this is Gaussian).

I claim, I think that is quite accepted, that this is a heroic. Especially in nonexperimental sciences, where the amount of data we have, compared to factors generating $y$, is relatively small, it is already sort of misleading to think of it as noise. It is unobserved variation, as most significant issue. This then leads to to try to estimate "easier" things than posteriors etc.
Comment: Of course whether one assumes a Gaussian process, or a Gaussian noise term, is often not significant, as for many parameters of interest we can suitable decompose our process into conditionals and noise. My point remains, that the number one issue is to find a parsimonious model, simply because we can expect to be wrong with most assumptions we make, and our data is too scarce to identify complicated, but general models. This is what I would understand the main critique of Bayesian parametric estimation.
Comment: The assumptions in the post/paper are likely wrong/inexact; the question is whether they are useful. To my surprise, the answer has been "yes" (at least in this case). The referred optimization method, that uses a likely wrong/misspecified Gaussian process model for an arbitrary target function (which possibly does not meet such assumptions) ends up working well _empirically_. So, to me being _somewhat_ Bayesian pays off.
However, I agree that we want to have robust methods that take care of cases in which our models are horribly wrong (e.g., in the paper there is a model-free component).
Comment: @lacerbi "The simplest case is that today's posterior is tomorrow's prior." this can also be pointed as a disadvantage. In fact, the new posterior will depend on the amount of observable data and how this is made available. For a normal prior and new data which follows also a normal distribution, you'll find that the variance of the posterior will depend on the size of each block of new data, i.e. 10 blocks of 100 new data points will give you a different posterior than 1 block of 1000 new data points.
Comment: @lacerbi "quantification of uncertainty is generally better than no uncertainty" I agree although the uncertainty of the posterior bias is probably the most important and I'm not sure how do Bayesian methods address this.
Comment: @lacerbi "noisy observation y=f(x)+ε, with ε∼N(0,σ2)" the issue is that ε never has a zero mean....
Answer: Here are a few other ways of justifying Bayesian inference in misspecified models.

You can construct a confidence interval on the posterior mean, using the sandwich formula (in the same way that you would do with the MLE). Thus, even though the credible sets don't have coverage, you can still produce valid confidence intervals on point estimators, if that's what you're interested in.
You can rescale the posterior distribution to ensure that credible sets have coverage, which is the approach taken in:

Müller, Ulrich K. "Risk of Bayesian inference in misspecified models, and the sandwich covariance matrix." Econometrica 81.5 (2013): 1805-1849.

There's a non-asymptotic justification for Bayes rule: omitting the technical conditions, if the prior is $p(\theta)$, and the log-likelihood is $\ell_n(\theta)$, then the posterior is the distribution that minimizes
$-\int \ell_n(\theta) d\nu(\theta) + \int \log\!\Big(\frac{\nu(\theta)}{p(\theta)}\Big)d\nu(\theta)$
over all distributions $\nu(\theta)$. The first term is like an expected utility: you want to put mass on parameters that yield a high likelihood. The second term regularizes: you want a small KL divergence to the prior. This formula explicitly says what the posterior is optimizing. It is used a lot in the context of quasi-likelihood, where people replace the log-likelihood by another utility function.
Comment: Thank you for the Muller paper: I think it answers a lot of the questions which I have.
Answer: There is the usual bias-variance tradeoff. Bayesian inference assuming M-closed case [1,2], has a smaller variance [3] but in the case of model misspecification the bias grows faster [4]. It is also possible to do Bayesian inference assuming M-open case [1,2], which has a higher variance [3] but in the case of model misspecification the bias is smaller [4]. Dicussions of ths bias-variance tradeoff between Bayesian M-closed and M-open cases appear also in some of the references included in the references below, but there is clearly need for more.
[1] Bernardo and Smith (1994). Bayesian Theory. John Wiley \& Sons.
[2] Vehtari and Ojanen (2012). A survey of Bayesian predictive methods for model assessment, selection and comparison. Statistics Surveys, 6:142-228. http://dx.doi.org/10.1214/12-SS102
[3] Juho Piironen and Aki Vehtari (2017). Comparison of Bayesian predictive methods for model selection. Statistics and Computing, 27(3):711-735. http://dx.doi.org/10.1007/s11222-016-9649-y.
[4] Yao, Vehtari, Simpson, and Andrew Gelman (2017). Using stacking to average Bayesian predictive distributions.  arXiv preprint arXiv:1704.02030 arxiv.org/abs/1704.02030
Answer:
assume that the real model of the data $p_{true}(X)$ differs from $p(X|\theta)$ for all values of $\theta$

Bayesian interpretation of this assumption is that there is an additional random variable $\phi$ and a value $\phi_0$ in its range $\phi_0$ such that $\int p(X|\theta,\phi=\phi_0) \mathrm{d}\theta =0$. Your prior knowledge says $p(\phi=\phi_0)\propto 1$ and  $p(\phi\neq\phi_0)=0$. Then $p(\theta|X,\phi=\phi_0)=0$ which is not proper probability distribution.
This case corresponds to a similar inference rule in logic where $A, \neg A \vdash \emptyset$, i.e. you can't infer anything from a contradiction. The result $p(\theta|X,\phi=\phi_0)=0$ is a way in which bayesian probability theory tells you that your prior knowledge is not consistent with your data. If someone failed to get this result in their derivation of the posterior, it means that the formulation failed to encode all relevant prior knowledge. As for the appraisal of this situation I hand over to Jaynes (2003, p.41):

... it is a powerful analytical tool which can search out a set of propositions and detect a contradiction in them if one exists.  The principle is that probabilities conditional on contradictory premises do not exist (the hypothesis space is reduced to the empty set).  Therefore, put our robot to work;
  i.e. write a computer program to calculate probabilities $p(B|E)$ conditional on a set of propositions $E= (E_1,E_2,\dots,E_n)$  Even
  though no contradiction is apparent from inspection, if there is a contradiction hidden in $E$, the computer program will crash.
  We discovered this ,,empirically,'' and after some thought realized that it is not a reason for dismay, but rather a valuable diagnostic tool that warns us of unforeseen special cases in which our formulation of a problem can break down.

In other words, if your problem formulation is inaccurate - if your model is wrong, bayesian statistics can help you find out that this is the case and can help you to find what aspect of the model is the source of the problem.
In practice, it may not be entirely clear what knowledge is relevant and whether it should be included in derivation. Various model checking techniques (Chapters 6 & 7 in Gelman et al., 2013, provide an overview) are then used to find out and to identify an inaccurate problem formulation.
Gelman, A., Carlin, J. B., Stern, H. S., Dunson, D. B., Vehtari, A., & Rubin, D. B. (2013). Bayesian data analysis, Third edition. Chapman & Hall/CRC.
Jaynes, E. T. (2003). Probability theory: The logic of science. Cambridge university press.
Comment: Your answer is missing the point and considering a simpler situation. I do not consider a situation in which our model is so wrong that it is inconsistent with the data. I look at a situation in which our model is wrong, but not catastrophically so. For example, consider infering the mean of the $X_i$. You could use a Gaussian model of the $X_i$ for inference, even though the real model is Laplace. In this simple example, the model is wrong but won't "explode" like what you describe.
Comment: @GuillaumeDehaene Your question was whether there are some arguments for using bayes when model is missspecified. Clearly, catastrofically misspecified model is missspecified.  In addition you can't know apriori whether your model is catastrophically missspecified or just misspecified. In fact bayes can tell you precisely that, which makes it useful and my answer pointed that out.
Comment: If it's not catrastrophically wrong, then the coverage won't be so different from $1-\alpha$. You could write a simulation of this normal model with Laplacian data to check this. The conceptual benefits would always be present. Think about it: if you decide to throw your posterior out of the window, you wouldn't compute just the MLE, but also some confidence interval. But we know that the interpretation of the CI computed for ONE particular experiment is rubish. So relax and enjoy the bayesian beer. If you understand that the model is misspecified, use this information to buil a better one.
Comment: @GuillaumeDehaene Yes, my answer is not exhaustive. I gladly extend it to clarify not catastrophic cases, but You need to specify what You have in mind: do You mean  that $\int p(X,\theta|\phi=\phi_0) \mathrm{d}\theta =k$ where $k$ is some small number so that $p(X|\phi=\phi_0)$ is small? Or are You saying that there exists $\theta=\theta_0$ such that $p(\theta=\theta_0|\phi=\phi_0)=0$ yet $p(X,\theta=\theta_k|\phi=\phi_0)>0$ or something else? I agree with Zen that generally the posterior won't be affected much in these less severe cases, although one could construct a borderline case.
Answer: The MLE is still an estimator for a parameter in a model you specify and assume to be correct. The regression coefficients in a frequentist OLS can be estimated with the MLE and all the properties you want to attach to it (unbiased, a specific asymptotic variance) still assume your very specific linear model is correct.
I'm going to take this a step further and say that every time you want to ascribe meaning and properties to an estimator you have to assume a model. Even when you take a simple sample mean, you are assuming the data is exchangeable and oftentimes IID.
Now, Bayesian estimators have many desirable properties that an MLE might not have. For example, partial pooling, regularization, and interpretability of a posterior which make it desirable in many situations.
Comment: You don't need to assume IID for a mean to give meaning. It is enough to assume exchangeability (but, yes, that's still an assumption ...)
Comment: @kjetil b halvorsen Thank you, I've edited for clarity.
Answer: I recommend Gelman & Shalizi's Philosophy and the practice of Bayesian statistics. They have coherent, detailed and practical responses to these questions.

We think most of this received view of Bayesian inference is wrong. Bayesian methods are no more inductive than any other mode of statistical inference. Bayesian data analysis is much better understood from a hypothetico-deductive perspective. Implicit in the best Bayesian practice is a stance that has much in common with the error-statistical approach of Mayo (1996), despite the latter's frequentist orientation. Indeed, crucial parts of Bayesian data analysis, such as model checking, can be understood as 'error probes' in Mayo's sense.
We proceed by a combination of examining concrete cases of Bayesian data analysis in empirical social science research, and theoretical results on the consistency and convergence of Bayesian updating. Social-scientific data analysis is especially salient for our purposes because there is general agreement that, in this domain, all models in use are wrong – not merely falsifiable, but actually false. With enough data – and often only a fairly moderate amount – any analyst could reject any model now in use to any desired level of confidence. Model fitting is nonetheless a valuable activity, and indeed the crux of data analysis. To understand why this is so, we need to examine how models are built, fitted, used and checked, and the effects of misspecification on models.
...
In our view, the account of the last paragraph [of the standard Bayesian view] is crucially mistaken. The data-analysis process – Bayesian or otherwise – does not end with calculating parameter estimates or posterior distributions. Rather, the model can then be checked, by comparing the implications of the fitted model to the empirical evidence. One asks questions such as whether simulations from the fitted model resemble the original data, whether the fitted model is consistent with other data not used in the fitting of the model, and whether variables that the model says are noise ('error terms') in fact display readily-detectable patterns. Discrepancies between the model and data can be used to learn about the ways in which the model is inadequate for the scientific purposes at hand, and thus to motivate expansions and changes to the model (Section 4.).
Answer: I think you're describing an impact of model uncertainty - you worry that your inference about an unknown parameter $x$ in light of data $d$ is conditional upon a model, $m$,
$$
p (x|d, m),
$$
as well as the data. What if $m$ is an implausible model? If there exist alternative models, with the same unknown parameter $x$, then you can marginalize model uncertainty with Bayesian model averaging,
$$
p (x|d) = \sum_m p (x|d, m) p(m|d)
$$
though this is a functional of the models considered and their priors.
If , on the other hand,  the definition of parameter $x$ is intrinsically tied to the model $m$, such that there are no alternatives, it's hardly surprising that inferences about $x$ are conditional on $m$.
Comment: Model averaging can't save us: it is still foolish to assume that the true model somehow neatly falls inside the scope of our bigger model. With model comparison, we can determine which of several models gives the best account of the data, but this just returns a wrong model that is less wrong than the other models.
Comment: It can help you make inferences/estimates about an unknown quantity that coherently incorporate model uncertainty. It cannot invent new hypotheses for you, though. If there were a statistical machinery that invented models in light of data, e.g. science would be much easier.
Answer: How do you define what a "mis-specified" model is? Does this mean the model...

makes "bad" predictions?
is not of the form $p_{T}(x) $ for some "true model"?
is missing a parameter?
leads to "bad" conclusions?

If you think of the ways a given model could be mis-specified, you will essentially be extracting information on how to make a better model. Include that extra information in your model!
If you think about what a "model" is in the bayesian framework, you can always make a model that cannot be mis-specified. One way to do this is by adding more parameters to your current model. By adding more parameters, you make your model more flexible and adaptable. Machine Learning methods make full use of this idea. This underlies things like "nueral networks" and "regression trees". You do need to think about priors though (similar to regularising for ML).
For example, you have given the "linear model" as your example, so you have...
$$\text {model 1: }x_i =\theta + \sigma e_i $$
Where $e_i \sim N (0,1)$. Now suppose we add a new parameter for each observation....
$$\text {model 2: }x_i =\theta + \sigma \frac{e_i}{w_i} $$
Where $e_i \sim N (0,1)$ as before. How does this change things? You could say "model 1 is mis-specified if model 2 is true". But model 2 is harder to estimate, as it has many more parameters. Also, if information about $\theta $ is what we care about, does it matter if model 1 is "wrong"?
If you assume that $w_i\sim N (0,1) $ (like a "model 2a") then we basically have "cauchy errors" instead of "normal errors" and the model expects outliers in the data. Hence, by adding parameters to your model, and choosing a prior for them, I have created a "more robust model". However the model still expects symmetry in the error terms. By choosing a different prior, this could be accounted for as well...
Comment: And the more parameters you use, the more data you need. If the information in $x$ about $f(x)$ is scarce, then adding parameters will not help.

With new data, the DGP is even less constant, so you again need more parameters and so forth.

The more general your model (more parameters), the less likely it is "mis-specified", but the more data you need to estimate.

In contrast, the less you ask of your model, the less data you need.

But that means in reality, how "right" is likely the model if a full posterior versus, say, a conditional moment?


What does NH and <> mean on a tab?

Question: On a tab I'm reading, I see this:
<code>   NH
   H.
------------------|
------------------|
--<>12------------|
------------------|
------------------|
------------------|
</code>
What do they mean?
I know H. means a dotted half note and what 12 means.
Answer: NH and <> both mean the same thing. They're natural harmonics
Comment: @Tim the <> indicates it is a harmonic, but the letters above are telling you what kind of harmonic, it could be A.H (Artificial harmonic), P.H (Pinch harmonic), S.H (semi harmonic), T.H (Tap harmonic).. and sometimes the N.H are written like <12> (with the number inside the signs)
Comment: Why would anyone use two different signs for the same thing on the same sheet?
Comment: @Tim - Just in case one of them isn't understood, the redundancy might help. Man, I wish I knew what those B-like signs on the stems of notes of sheet music of Roland Dyens's *Libra Sonatine* were when transcribing its last movement, as it's not clear to me what they stand for simply by listening to recordings of that piece....
Comment: @Dekkadeci - so if one wasn't understood, how would that help? One gets played right, the other? Sounds like you need to pose a question...
Comment: @Tim - Since the redundant markings are seen in close proximity of each other, if only one of them is understood, the markings are still respected as a whole.

What does NH and <> mean on a tab?


What is that for a huge bridge on a Les Paul?

Question: I am really interested into the bridge of this Les Paul. So far I have not seen such a bridge. Unfortunately I could not find the name of the guitarist. The music video can be found here.

Click on the image to enlarge.
What are the features of this bridge and are there other guitars that have the same?
Answer: This bridge is a Washburn Wonderbar system, as used by Ace Frehley during his Washburn endorsee years. He subsequently had one fitted to his Les Paul during that time, my belief is this is a guitar owned by a fan looking to emulate his Les Paul - the 12th fret inlay is that of the Epiphone signature model from the 90s which Ace has never played live.
It has a cork screw spring within and the system can bolt onto the surface of any solid body guitar. No need for a traditional tremolo cut through. Perfect for retaining the sustain of a heavy solid Les Paul


Is it possible to see which modules grants access rights?

Question: Working on a Drupal 9 website the module Permissions by term gives an unexpected behaviour.
Trying to find the problem I got stuck. I think there might be another module overwriting the permissions. But I can't find out which one.
Is there a way to see which module grants what right to a node/user?
Or is there another step I should do to debug these node permissions?
Thanks a lot!
Comment: I guess looking at the `node_access` table in the database is not exactly helpful, but everything relating to grants is in there, including the grants from "Permissions by Term". If that doesn't help, I'd start digging through [NodeAccessControlHandler](https://api.drupal.org/api/drupal/core%21modules%21node%21src%21NodeAccessControlHandler.php/class/NodeAccessControlHandler/9.3.x), to understand where it goes wrong.
Comment: In case you didn't look at that yet, this might also help to understand the control flow and see what to watch out for: https://api.drupal.org/api/drupal/core%21modules%21node%21node.module/group/node_access/9.3.x
Answer: Thanks for your help it was super informative! I found out that the Permissions by Entity module was creating the problem. Since I wasn't using it I could just uninstall it and all is working like expected now.


Jacobian and local invertibility of function

Question: Following is a question in the text book:
Show by an example that the condition that the Jacobian vanishes at a point is not necessary for a function to be locally invertible at that point..... and the answer given is  f : R$^2$- R$^2$ f(x,y) = (x$^3$, y$^3$)
I am a bit confused by the question itself because my understanding is that  for local invertibility we check that Jacobian  is not zero....also for f(x,y) = (x$^3$, y$^3$), J works out to 9x$^2$y$^2$. So dont we say that this function is not invertible at either x=0 or y=0 since J at these values becomes 0? I am unable to understand the solution given in the textbook
I request help to understand this problem and the given solution
Answer: It looks like a typo in the text. It should say "...the condition that the Jacobian does not vanish...".
$f(x,y)=(x^3,y^3)$ is invertible. Its inverse is $f^{-1}(x,y)=(\sqrt[3]{x},\sqrt[3]{y})$. What happens is that the inverse is not differentiable.
Comment: Thank you very much. It does make sense if it reads "Jacobian does not vanish" and it appears a typo in textbook.. and that was the reason I was confused. But I have still not understood how the example in the textbook shows that Jacobian does not vanish is not a necessary for function to be locally invertible. May I request you to please amplify the explanation given by you? Can you please give another example to make it clear?
Comment: To show that non vanishing of the Jacobian is not a necessary condition for local invertibility, all you have to do is to exhibit a function that is invertible and whose Jacobian vanishes at some point; $f(x,y)=(x^3,y^3)$ is such an example. Another example (essentially the same): $f(x,y)=(y^5,x^7)$.


4 heads in 8 tosses

Question: If someone asked me the odds of getting 4 heads in 8 flips of a fair coin.
I would initially think to do something like this:
$\dfrac{2^8 - \left( \binom{8}{0} + \binom{8}{1} + \binom{8}{2} + \binom{8}{3} \right)}{2^8} = \dfrac{163}{256} \simeq 63.67\%$
In that there's $2^8$ total different outcomes of 8 flips, and of those (in the numerator) I subtract out the outcomes that have 0 heads, 1 heads, 2 heads, or 3 heads.  (Or equivalently, I could have subtracted out those with 8, 7, 6, or 5 tails).
However, I think this is more like asking "getting at least 4 heads in 8 fair flips"?  I assume if you were tossing these as soon as you hit 4 heads you'd be in a "win" state, so it wouldn't matter if the rest of the flips would result in greater than 4 heads for that particular outcome?
However, suppose that as soon as 4 heads were obtained, the flips would stop, so it would not be possible to get greater than 4 heads in any outcome.
So wouldn't that mean the total outcomes would be less?
Either 0 heads happen, 1 heads, 2 heads, 3 heads, or 4 heads:
$\binom{8}{0} + \binom{8}{1} + \binom{8}{2} + \binom{8}{3} + \binom{8}{4} = 163 $
Then, I only care about those that have 4 heads:
$\dfrac{\binom{8}{4}}{163} = \dfrac{70}{163} \simeq 42.94\%$
So why are the probabilities different? Shouldn't this be the same sort of question?
Answer: Imagine I watch you while you flip the coin. If you reach four heads before the eighth flip, I distract you by shouting, "Look! Your events are not equally likely!" While you are distracted, I steal your coin. Then I go where you can't see flip the coin enough times so that you and I have made eight flips altogether.
Now the coin has been flipped eight times, so we know how likely it has four heads altogether. And you have observed all the cases with heads. So yes, the probability you observe should be the same as the probability I observe.
Something to note is that in your second setup, you count the events HHHH and TTTTHHHH equally (i.e., each contributes $1$ to the numerator $70$). But the HHHH event is much more likely to occur than TTTTHHHH. Therefore your total probability does not compute correctly under the assumption that you have found equally likely outcomes.
In fact, any given unique result of a series of $n$ coin tosses has a probability
$2^{-n}$ to occur. For example, HHTHH is one unique result that can occur from the first five coin tosses, and it has probability $2^{-5} = \frac1{32}$.
The probabilities actually add up like this, dividing the "successful" outcomes
into five cases depending on how many times you toss the coin in order to achieve
four heads:
The 4th head occurs on the 4th toss: This can happen only one way: HHHH. The probability of that event is
$$P_4 = \frac1{16}.$$
The 4th head occurs on the 5th toss: This means there are $3$ heads in the first $4$ tosses, and the last toss is a head. There are $\binom{4}{3} = 4$ unique ways this can happen (for example, HHHTH). Each of these unique outcomes has probability $\frac1{32}$, so the probability that the outcome will be in this set is
$$P_5 = \frac4{32} = \frac18.$$
The 4th head occurs on the 6th toss: That is, $3$ heads in the first $5$ tosses, then a head. There are $\binom{5}{3} = 10$ unique ways this can happen. Each of these unique outcomes has probability $\frac1{64}$, so the probability that the outcome will be in this set is
$$P_6 = \frac{10}{64} = \frac5{32}.$$
The 4th head occurs on the 7th toss: There are $\binom{6}{3} = 20$ unique ways this can happen. Each of these unique outcomes has probability $\frac1{128}$, so the probability that the outcome will be in this set is
$$P_7 = \frac{20}{128} = \frac5{32}.$$
The 4th head occurs on the 8th toss: There are $\binom{7}{3} = 35$ unique ways this can happen. Each of these unique outcomes has probability $\frac1{256}$, so the probability that the outcome will be in this set is
$$P_8 = \frac{35}{256}.$$
The total probability of tossing $4$ heads is therefore
$$P_4 + P_5 + P_6 + P_7 + P_8 = \frac{163}{256} \approx 0.6367,$$
just like the first result.
Alternatively, consider the probability of failing to get four heads. Each unique outcome in this set requires you to toss the coin $8$ times, so it occurs with probability $\frac1{256}$. The total probability of all these cases is
$$P_\mbox{fail} =
\frac1{256} \left(\binom{8}{0} + \binom{8}{1} + \binom{8}{2} + \binom{8}{3} \right)
= \frac{93}{256}.$$
The probability that you do toss four heads is therefore
$$1 - P_\mbox{fail} = \frac{163}{256}.$$
Comment: It seems like solving the "at least" problem is a lot less work and ends up at the same answer.
Comment: I agree. The reason for the first approach is that it corresponds to one of the parts of the question, but shows explicitly how the probabilities _should_ be added up (thereby getting a correct answer instead of an incorrect one). The second approach actually is almost identical to the correct calculation in the original question (differing mainly in that it is more explicit about why the outcomes are treated as being of equal probability).
Answer: You are misinterpreting what the second probability, $70/163\approx42.94\%$, means.  It's actually a conditional probability:  It's the probability that you got exactly four heads, given that you got at least four heads.  (As DavidK points out in comments, it's also the conditional probability of getting exactly four heads given that you got no more than four heads, which is closer to the OP's derivation.  Both interpretations, however, require that the coin be tossed all eight times, which is counter to what the OP wants to do.  DavidK's answer shows the proper way to analyze things if you insist on stopping as soon as the fourth head appears.)
Incidentally, a quicker way to get the number $163$ is to consider that you either get your four heads within the first seven flips, or else you get the fourth head on the eighth flip.  After the first seven flips, you either have more heads than tails or more tails than heads; by symmetry each probability is $1/2$.  To get the fourth head on the eighth flip, you need to have gotten the other three in the first seven, and then flip a head.  In total, the probability is
$${1\over2}+{1\over2}\cdot{{7\choose3}\over2^7}={1\over2}+{35\over256}={163\over256}$$
Comment: Based on the way it was derived, I think the second probability actually represents the probability of exactly four heads, given that you get _no_ _more_ than four heads while flipping eight times (not stopping when you reach four heads). Coincidentally, it's the same value as the probability you described, because you're equally likely to get at _least_ four heads or at _most_ four. But look at the formulas applied and consider how they presumably would have been written if the original question had been "3 heads in 8 tosses" or "4 heads in 9 tosses".
Comment: +1 because I really like this derivation of the probability $\frac{163}{256}$ for the original question. Very nice use of symmetry!
Comment: @DavidK, you are quite right about the interpretation.  Thank you for pointing that out.
Answer: $\newcommand{\+}{^{\dagger}}
 \newcommand{\angles}[1]{\left\langle\, #1 \,\right\rangle}
 \newcommand{\braces}[1]{\left\lbrace\, #1 \,\right\rbrace}
 \newcommand{\bracks}[1]{\left\lbrack\, #1 \,\right\rbrack}
 \newcommand{\ceil}[1]{\,\left\lceil\, #1 \,\right\rceil\,}
 \newcommand{\dd}{{\rm d}}
 \newcommand{\down}{\downarrow}
 \newcommand{\ds}[1]{\displaystyle{#1}}
 \newcommand{\expo}[1]{\,{\rm e}^{#1}\,}
 \newcommand{\fermi}{\,{\rm f}}
 \newcommand{\floor}[1]{\,\left\lfloor #1 \right\rfloor\,}
 \newcommand{\half}{{1 \over 2}}
 \newcommand{\ic}{{\rm i}}
 \newcommand{\iff}{\Longleftrightarrow}
 \newcommand{\imp}{\Longrightarrow}
 \newcommand{\isdiv}{\,\left.\right\vert\,}
 \newcommand{\ket}[1]{\left\vert #1\right\rangle}
 \newcommand{\ol}[1]{\overline{#1}}
 \newcommand{\pars}[1]{\left(\, #1 \,\right)}
 \newcommand{\partiald}[3][]{\frac{\partial^{#1} #2}{\partial #3^{#1}}}
 \newcommand{\pp}{{\cal P}}
 \newcommand{\root}[2][]{\,\sqrt[#1]{\vphantom{\large A}\,#2\,}\,}
 \newcommand{\sech}{\,{\rm sech}}
 \newcommand{\sgn}{\,{\rm sgn}}
 \newcommand{\totald}[3][]{\frac{{\rm d}^{#1} #2}{{\rm d} #3^{#1}}}
 \newcommand{\ul}[1]{\underline{#1}}
 \newcommand{\verts}[1]{\left\vert\, #1 \,\right\vert}
 \newcommand{\wt}[1]{\widetilde{#1}}$
$$
{8 \choose 4}\pars{\half}^{8 - 4}\pars{\half}^{4}
={8\cdot 7\cdot 6\cdot 5 \over 4\cdot 3\cdot 2}\,{1 \over 2^{8}} = \color{#88f}{\Large{35 \over 128}}
\approx 0.2734
$$
Comment: That's the probability that in eight coin tosses (not stopping after four heads), you will get exactly four heads. What does this tell us with regard to the posted question?
Comment: I see that this is relevant to one of the other answers, and illustrates the fact that that answer is incorrect. Is that the point?
Comment: @DavidK Initially, the OP said there are $\tt exactly$ 8 flips. I understood the question in that way. Thanks.
Answer: If you want exactly 4 Heads at the 8th toss, that means you have 3 heads somewhere in the initial 7 tosses.
So $\binom 73 p^3 (1-p)^4$, then there is the final eighth trial which must be head.
so we get $\binom 73 p^3 (1-p)^4 p$
$$=\binom 73 p^4(1-p)^4$$
$$=\binom 73 \left(\frac{1}{2}\right)^8$$
I would suggest looking up the negative binomial distribution.  There are many ways of expressing it. Above, I have shown the version where we specifiy the number of desired successes, the paramater of success, $p$. Which means, the random variable in question examines the number of trials to make tha thappen:
$$P(X=k)=\binom{k-1}{r-1}p^r(1-p)^{k-r}$$
Where of course $k\geq r$, $r$ is number of successes.
This is a more general case for the geometric distribution:
NegativeBinomial(r=1,p) ~ Geometric(p)
Answer: The Question is stated as "If someone asked me the odds of getting $4$ heads in $8$ flips of a fair coin".
The probability is easily shown to be $\frac{35}{128}$ as some of you have correctly answered, and the odds would be $35$ to $93$.

4 heads in 8 tosses


Show that $ {S_n\over 2^.5(logn)} $ converges to 0 almost surely.

Question: Suppose ${X_n, n ≥ 1}$ are independent identical random variables with $E(X_n) =0 $ and $E(X^2_n) =1 $
 Show that $$
{S_n\over 2^.5(logn)}
$$ converges to 0 almost surely. where $ S_n =\sum_{i=1}^n X_i $.
I haven't got good idea how to solve it. I used SLLN. This doesn't work.
Answer: This is not true because
$$
\frac{S_n}{\sqrt{2}\ln{n}}=\frac{\sqrt{2n\ln{\ln{n}}}}{\sqrt{2}\ln{n}}\frac{S_n}{\sqrt{2n\ln{\ln{n}}}}
$$
and by the LIL
$$
\limsup_{n\to\infty}\frac{S_n}{\sqrt{2n\ln{\ln{n}}}}=1 \quad\text{a.s.,}
$$
while the first term in the product converges to $+\infty$.


Ruby - trying to make hashtags within string into links

Question: I am trying to make the hashtags within a string into links.
e.g. I'd like a string that's currently: "I'm a string which contains a #hashtag" to transform into: "I'm a string which contains #hashtag"
The code that I have at the moment is as follows:
<% @messages.each do |message| %>
    <% string = message.content %>
    <% hashtaglinks = string.scan(/#(\d*)/).flatten %>
    <% hashtaglinks.each do |tag| %>

<%= string = string.gsub(/##{tag}\b/, link_to("google", "##{tag}") %>

    <% end %>
<% end %>

I've been trying (in vain) for several hours to get this to work, reading through many similar stackoverflow threads- but frustration has got the better of me, and as a beginner rubyist, I'd be really appreciate it if someone could please help me out!
The code in my 'server.rb' is as follows:
get '/' do
    @messages = Message.all
    erb :index
end

post '/messages' do
    content = params["content"]
    hashtags = params["content"].scan(/#\w+/).flatten.map{|hashtag|
Hashtag.first_or_create(:text => hashtag)}
    Message.create(:content => content, :hashtags => hashtags)
    redirect to('/')
end

get '/hashtags/:text' do
    hashtag = Hashtag.first(:text => params[:text])
    @messages = hashtag ? hashtag.messages : []
    erb :index
end

helpers do

def link_to(url,text=url,opts={})
  attributes = ""
  opts.each { |key,value| attributes << key.to_s << "=\"" << value << "\" "}
  "#{text}"
end

end

Answer: Here is the code to get you started. This should replace (in-place) the hashtags in the string with the links:
<% string.gsub!(/#\w+/) do |tag| %>
  <% link_to("##{tag}", url_you_want_to_replace_hashtag_with) %>
<% end %>

You may need to use html_safe on the string to display it afterwards.
The regex doesn't account for more complex cases, like what do you do in case of ##tag0 or #tag1#tag2. Should tag0 and tag2 be considered hashtags? Also, you may want to change \w to something like [a-zA-Z0-9] if you want to limit the tags to alphanumerics and digits only.
Comment: Thank you, this worked! Hours of frustration finally relieved.


nodejs server connecting to mongodb when at the initialization: the server was not connected to internet

Question: Hello here is my problem,
I am working on a device that host a nodejs server.
this nodejs server is able to run smoothly without the wifi.
There is few rest point who need access to a database server (mongodb).
I use nodejs, mongoose, express.
my question is:
Is it possible to set up a connection to mongodb server after this initialization of the server (after a rest call for example) when at the initialization the server did not have wifi but during the before the rest call it has the wifi.
It is possible to do it when the server already has the wifi at the initialization.
But I wonder when it does not have the wifi at the initialization.
Answer: All official MongoDB drivers establish connections and monitor the deployment in the background and will automatically handle network outages. Client objects can be created when there is no network connectivity to the deployment, connectivity can be interrupted, etc. Queries themselves of course won't work when there is no connectivity but once connectivity reappears no actions are needed by the application for the driver to start working.
In some drivers like the node one this behavior must be explicitly requested for the time being for backwards compatibility reasons.
Comment: I can't speak for other drivers but the node.js driver [defaults](https://mongodb.github.io/node-mongodb-native/3.6/api/MongoClient.html#.connect) to try reconnecting 30 times with a 1000ms delay between retries so won't recover automatically after the 30 retries. The buffered queries are also lost at that point.
Comment: I assume this is the deprecated legacy behavior you get when you don't request the unified topology.
Comment: Yeah you're right, using the unified topology [deprecates them](https://github.com/mongodb/node-mongodb-native/blob/master/src/sdam/topology.ts#L69-L74)
Comment: I am not sure to fully understand, so is it a normal behavior that I can cant connect to mongoDB remote server after getting network connectivity when the node server have been launched with out network connectivity


Deleting Similar Rows using Excel VBA

Question: I am trying to write a VBA code for an Excel macro so that I can manually trigger the macro to do the following:
In the event that any two rows have:

Same value in column A
Same value in Column B
"apple" in Column C
Same value in Column D

Then I would like all of those rows to be deleted except the row with the highest value in column E.
As an example, if:

cell A1 = cell A2
cell B1 = cell B2
cell C1 and Cell C2 = "apple"
cell D1 = cell D2
Cell E1 = 5 and Cell E2 = 10

Then Row 1 gets deleted and Row 2 remains.
The overall goal is to delete similar rows.
Per a user's suggestions, this process can be aided/simplified by sorting range by c="apple",a,b,d so that rows can be analyzed consecutively.
Example of Code Outcome
I put together the following code, but I am unfamiliar with the delete row aspect and how to incorporate the highest value, but this was my best shot. The If and elseif statements are questionable.
Sub Macro()

Dim a As Range
Dim b As Range
Dim c As Range
Dim d As Range
Dim e As Range

For Each a In Range("A1:A9999")

For Each b In Range("B1:B9999")

For Each c In Range("C1:C9999")

For Each d In Range("D1:D9999")

For Each e In Range("E1:E9999")

If a.Offset(-1, 0) = a And b.Offset(-1, 0) And c.Offset(-1, 0) = c And d.Offset(-1, 0) = d And e.Offset(-1, 0) < e Then Range(a).EntireRow.Delete

ElseIf a.Offset(-1, 0) = a And b.Offset(-1, 0) And c.Offset(-1, 0) = c And d.Offset(-1, 0) = d And e.Offset(-1, 0) > e Then Range(a.Offset(-1, 0)).EntireRow.Delete

Exit For

Next a

Next b

Next c

Next d

Next e

End Sub

Comment: This might be easier with a helper column using MAXIFS.
Comment: Actually I realize the code I put together doesn't even make sense... I am working on revising it. The description I wrote is correct, though.
Comment: Your example compares **2 consecutive** rows  is this what you want??
Comment: @BigBen Hmm I am unaware of that - I will look into it!
Comment: @Gary'sStudent It compares any of the rows in the document. Not necessarily consecutive rows.
Comment: Temporarily sorting your range by c="apple",a,b,d would mean all your possible matches are grouped together and can be read once, comparing consecutively, rather than looping through it all over and over..
Comment: @CLR good tip! I will modify the post to state that would be an acceptable pre-step.
Comment: Okay - I added potential if statements that would better fulfill the objective. It doesn't work as is, but I have a feeling it is close.
Comment: Has the second row (linke in your example) always the max-value in column "D". In other words, do you want the max in col D from all rows where the a.m. conditions fit? Which Excel Version do you use, maybe you can use Grouping with PowerQuery i.o. VBA.
Answer: I hope it works.
Option Explicit

Sub RunMacro()
Dim i As Long, LastRow As Long, j As Long
Dim cellA, cellB, cellC, cellD, cellE
Dim Rng As Range
LastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To LastRow
cellA = Range("A" & i).Value
cellB = Range("B" & i).Value
cellC = Range("C" & i).Value
cellD = Range("D" & i).Value
cellE = Range("E" & i).Value

For j = LastRow To 2 Step -1
    If Range("A" & j).Value = cellA And Range("B" & j).Value = cellB Then
        If Range("C" & j).Value = cellC And Range("D" & j).Value = cellD Then

            If cellE > Range("E" & j).Value Then
                Range("E" & j).EntireRow.Delete
                LastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row

            End If

        End If
    End If
Next j
Next i
            LastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
            With ActiveSheet
            Set Rng = Range("A1", Range("E1").End(xlDown))
            Rng.RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5), Header:=xlYes
            End With

End Sub

Comment: Thank you for the input! From what I can tell, the only item that would need modified is for column C. The code needs to be specific for rows with the word "apple" in column C. I'm a little unsure how to modify your code to do this...would I change "Range("C" & j).Value = cellC" to "Range("C" & j).Value = "apple"" ?
Comment: yes, you can do that or if you want, you can fill the column C full of "Apple" :)
Comment: Unfortunately I do have to report back that it does not work as intended.... When I run this code once, there was a row that got deleted that had a unique value for column B violating the requirement that column B has to be equal. Also after running the code for a second time, I then lost one of the two remaining rows that had unique values for column D. I just uploaded a picture showing what the code should do to the excel sheet.
Comment: Sorry for delay, could you please try it?
Comment: It seems to be working on my simplified file! I will have a larger data set to test on in about 45 minutes and will report back! Thank you so much!
Comment: So I tested it on the larger file and for columns A-E this works perfectly, but there are additional columns in the larger document. As a result, this ends up mis-matching columns A-E with the remaining columns in the document. So the dilemma becomes how can the code be modified so that the "extra" columns are not mis matched? I can post a screenshot of what I mean in a bit.
Comment: I tested it once again on the bigger file and I seemed to have gotten it to work with a small code modification that was outside the purview of the post so for all intents and purposes, your work seems to be correct. I will test once more again today with another file to verify. For anyone reading, if you have additional colums, you simply change "E1" in Set Rng = Range("A1", Range("E1").End(xlDown)) to include your last column and add in each column numbers in "Rng.RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5), Header:=xlYes"

Deleting Similar Rows using Excel VBA


How to find the centroid of y=arcsin(x),

Question: I do not know how to continue this problem from what I have,
$$y=arcsinx, x=0, y=\frac{\pi}{2}$$ It is asking for the centroid in the region bounded by above.
This is what I have so far and after trying to get the answer i keep on getting it wrong.
$$Mx=p\frac{1}{2}\int_0^1(arcsinx+\frac{\pi}{2})(\frac{\pi}{2}-arcsinx)$$
$$My=p\int_0^1x(\frac{\pi}{2}-arcsinx)$$
$$M=p\int_0^1(\frac{\pi}{2}-arcsinx)$$ These are the equations I made to determine to get the centroids but it marks my answer to be wrong every single time i solve for them.
Comment: If $y=\arcsin x$ than $x=\sin y$ and using this is simpler.
Answer: Centriod is an intrinsic property. So conveniently interchange axes to get half-sine wave:
$$ y=\sin x, y=(0,1) ;  x=(0, \frac{\pi}{2}) $$
The surrounding rectangle has area $ \pi/2 $, area inside sine-wave is $A_1= 1$ and outside sine-wave is $ A_2 = (\pi/2-1)$
Compute $x_1,y_1$ in the usual way for areas inside the sine-curve
$$  \bar y = \frac{\int y dA }{\int dA},\bar x = \frac{\int x dA }{\int dA}. $$
Now find moments about x-axis and y-axis. For surrounding rectangle $( x_R,y_R) $ are known.
$$ x_1 A_1 + x_2 A_2 = x_R ( A_1 + A_2) $$
$$ y_1 A_1 + y_2 A_2 = y_R ( A_1 + A_2). $$