MCMC to handle flat likelihood issues

Question: I have a quite flat likelihood leading Metropolis-Hastings sampler to move through the parameter space very irregularly, i.e. no convergence can be achieved no matter what the parameters of proposal distribution (in my case it is gaussian). There is no high complexity in my model - just 2 parameters, but it seems that MH cannot handle this task. So, is there any trick around this problem? Is there a sampler that would not produce Markov chains moving very far to the posterior tails?
Update of the problem:
I will try to reformulate my question giving more details. First of all I will describe the model.
I have a graphical model with two nodes. Each node is governed by an auto-Poisson model (Besag, 1974) as follows:
$$p\left ( X_{j} |X_{k}=x_{k},\forall k\neq j,\Theta \right )\sim Poisson\left ( e^{\theta _{j}+\sum _{j\neq k}\theta _{kj}x_{k}} \right )$$
Or, since there just two nodes and assuming equal global intensities:
$$p\left ( X_{1} |X_{2}=x_{2},\theta, \alpha \right )\sim Poisson\left ( e^{\theta+\alpha x_{2}} \right )$$
$$p\left ( X_{2} |X_{1}=x_{1},\theta, \alpha \right )\sim Poisson\left ( e^{\theta+\alpha x_{1}} \right )$$
Since it is a Markov field, the joint distribution (or likelihood of realization $ X=[x_{1},x_{2}] $) is as follows:
$$ p\left ( X \right )=\frac{exp\left ( \theta \left ( x_{1}+x_{2} \right )+2 x_{1}x_{2} \alpha\right )}{Z\left ( \theta, \alpha \right )}=\frac{exp\left ( E\left ( \theta, \alpha, X \right ) \right )}{Z\left ( \theta, \alpha \right )} $$
Since I assumed flat priors for $\alpha$ and $\theta$, posterior is then proportional to
$$\pi(\theta, \alpha |X)\propto \frac{exp\left ( E\left ( \theta, \alpha, X \right ) \right )}{Z\left ( \theta, \alpha \right )}$$
Since $Z(\theta, \alpha)$ in general is very hard to evaluate (lots of lots of summations) I am using auxiliary variable method due to J. Moller (2006). According to this method, first I draw a sample of data ${X}'$ by Gibbs sampler (since conditionals are just poisson distributions) then I draw a proposal from Gaussian distribution and calculate accordingly the acceptance criteria $H({X}',{\alpha}',{\theta}'|X, \alpha, \theta)$.
And here I get a wild Markov chain. When I impose some boundaries within which the chain can move, the sampler seems to converge to some distribution, but once I move at least one boundary, resulting distribution also moves and always shows trancation.  I think that @Xi'an is wright - the posterior might be improper.
Comment: Just let me clarify first that, if the likelihood is flat, you do not really want that your sampler do not "move very far to the posterior tail". What is desired is to sample properly from the distribution (both, tails and centre). When using a MH algorithm with Gaussian proposals, you need to choose scale parameters/covariance matrix that determine the length of the steps. These have to be chosen for 1. Sampling properly from the distribution and 2. Getting a reasonable acceptance rate.
Comment: A possibility is to use a larger scale parameter in order to get larger steps. You might be interested on the R package `mcmc` and the command `metrop` as well. You will probably need an adaptive sampler. [This sampler](http://projecteuclid.org/DPubS?service=UI&version=1.0&verb=Display&handle=euclid.ba/1340218339) (the twalk) can be used in this kind of cases given that it is adaptive (perhaps just as a "second opinion"). It is implemented in R, C and Python. The codes can be downloaded from [one of the author's webpage](http://www.cimat.mx/~jac/twalk/).
Comment: @Procrastinator Can you elaborate more about what do you mean by "larger scale parameter"? Do mean to use larger variance parameters for proposals?
Comment: if you only have two parameters then numerical integration is probably a better alternative
Comment: there is something wrong with the joint likelihood expression. If you try and sum out $x_1$ you get $p(x_2|\alpha\theta)=g(x_2)\sum_{x_1=0}^{\infty}\exp(x_1[\theta+2\alpha x_2])=\infty$.  so the likelihood is improper as currently written.
Comment: this is of course for the case that $\theta+2\alpha x_2$ is non negative.
Comment: The two parameter case is just for exploration of the methods - when the dimension of X grows it is simply impossible to evaluate the $Z()\theta, \alpha$ function. So, this leads to so called doubly intractability in Bayesian framework.
As with the correctness of the likelihood, it is valid as long as the parameter $ \alpha $ is negative (see the Besag 1974)
Comment: Thanks for clarifying.  You should probably include the range of the parameters as part of the model description.
Comment: actually there are terms in $Z(\alpha,\theta)$ that depend on $x_i!$ which I overlooked.  these terms make the joint likelihood proper for all parameter combinations.
Answer: I find it surprising that a flat likelihood produces convergence issues: it is usually the opposite case that causes problems! The usual first check for such situations is to make sure that your posterior is proper: if not it would explain for endless excursions in the "tails". If the posterior is indeed proper, you could use fatter tail proposals like a Cauchy distribution... And an adaptive algorithm à la Roberts and Rosenthal.
If this still "does not work", I suggest considering a reparameterisation of the model, using for instance (i.e. if there is no other natural parametrisation) a logistic transform,
$$
\varphi(x) = \exp(x)/\{1+\exp(x)\}
$$
(with a possible scale parameter), which brings the parameter into the unit square.
Regarding the earlier answers, Gibbs sampling sounds like a more likely solution than accept-reject, which requires finding a bound and scaling the t distribution towards the posterior, which did not seem feasible for the more robust Metropolis-Hastings sampler...
Comment: @Xian thanks for the feedback on the downvote. Is there actually any situation where you would favor accept-reject over MH?
Comment: @gui11aume: if you can produce an accept-reject algorithm with a small enough bound to ensure a reasonable acceptance rate, then accept-reject is undoubtedly preferable to Metropolis-Hastings. However, this is unlikely to happen with (a) large dimensions and/or (b) complex, possibly multimodal, targets...
Answer: Can you write down the distribution of your first parameter conditional on your second parameter and vice-versa?  If so, Gibbs sampling would be a viable option.  It's only a couple of lines of code and it can mix almost instantly in many cases.
Answer: EDIT: See the answer of @Xi'an and the discussion after it to see the issues with the following approach.
If Metropolis-Hastings fails and your model is relatively simple, you could think of using the accept-reject algorithm with Student's $t$ distribution with a low degree of freedom (1-6) for the proposals.
If you use R, you can easily simulate a Student's $t$ with rt(). If you do not have an easy way to generate $t$ variables with your software but you can simulate a $\Gamma$, then drawing the variance of a Gaussian from a $\Gamma$ at each step and simulating a Gaussian with that variance is equivalent.

MCMC to handle flat likelihood issues


Defining Devel generation rules for custom nodes

Question: I have Devel module installed, I use it mostly for generating content.
I wanted to know if it was possible to define some custom rules to generate content for my custom nodes.
For example: I want the node titles to be fetched from a php array or another source instead of the automatic Lorem Ipsum fragments.
I've done some google researches but I can't find a starting point to do this.
Thanks
Comment: Yes, it's possible. To get a proper answer though, please see the [FAQ] and edit the question into something a bit more meaty and useful for future visitors. Adding what you've already tried (including code examples and error messages/etc) would be the simplest way to do that
Comment: Thanks for the advice @Clive, the problem is I don't have any code yet as I don't know where to start.
Answer: I usually create a simple custom module for migratig data from an external data source and no need to use devel for this kind of tasks.
you have a very good example here :
http://timonweb.com/how-programmatically-create-nodes-comments-and-taxonomies-drupal-7
you can simply adapt the code and make a foreach loop on your array.
Comment: I think I'll do it this way, it's a more time saving solution.
Thanks a lot @Sicca


How to create a new column in data frame which each row of new column is multiplication of all previous rows

Question: I have a data frame like the one shown below:
ref_inf <- c(2,3,1,2.2,1.3,1.5,1.9,1.8,1.9,1.9)
ref_year<- seq(2001,2010)
inf_data <- data.frame(ref_year,ref_inf)

   ref_year ref_inf
1      2001     2.0
2      2002     3.0
3      2003     1.0
4      2004     2.2
5      2005     1.3
6      2006     1.5
7      2007     1.9
8      2008     1.8
9      2009     1.9
10     2010     1.9

What I want to do is to create a new column "Final Inflation" and each number in the new column should be calculated by multiplying all previous numbers in ref_inf column, so for example, if I want to calculate Final Inflation for the year 2005 I should do this:
Final inflation= (1+1.3/100)*(1+2.2/100)*(1+1.0/100)*(1+3.0/100)*(1+2.0/100)

or as another example, Final inflation for the year 2003 would be
Final inflation= (1+1.0/100)*(1+3.0/100)*(1+2.0/100)

I should do this calculation for each row of the data frame
How can I do that using dplyr in R?
Comment: I think your calculation for 2003 is different from the values in the input
Comment: That's right. I'll fix it. Thanks akrun
Comment: That would be `(1+1/100)*(1+3/100)*(1+2/100)`
Answer: We could use cumprod
library(dplyr)
inf_data %>%
    mutate(new = cumprod(1 + ref_inf/100))

-output
#  ref_year ref_inf      new
#1      2001     2.0 1.020000
#2      2002     3.0 1.050600
#3      2003     1.0 1.061106
#4      2004     2.2 1.084450
#5      2005     1.3 1.098548
#6      2006     1.5 1.115026
#7      2007     1.9 1.136212
#8      2008     1.8 1.156664
#9      2009     1.9 1.178640
#10     2010     1.9 1.201035


Check Files Existance from Shared Folder using Angular17 & ASP.NET Core v2.1

Question: I used to have this AngularJS project where I used to call my API at this way:
$http.get(`${uriApi}api/PDS/CheckDirectoryExistance?path=` + encodeURIComponent(dirPath))
             .then(response => {
             if (response.data.ok) {
             docFound = true;
             LoadRootLevel(dirPath);
             Promise.resolve()
        }
         /*My Code Continues*/
    })
    .catch(error => {
        if (!alertShown) {
           docFolderFound = false;
           console.error("Error checking directory existence:", error);
           alert("There's no process documentation created for  '" + $scope.partNo + "'\nPlease verify that this is correct or ask your supervisor to provide the documentation." +
                 "Evaluating folders: \n" + dirs2 + "\n\nNo existe documentacion creada para el numero de parte '" + $scope.partNo + "'\nPor favor verifique que esto sea correcto o solucite a su supervisor la creacion de la documentacion"
            );
            alertShown = true;
        }
    });

But now I'm migrating my app to use Angular17 and ASP.NET Core.
The thing is that, my API didn't change basically.
As well here's my API Code:
[HttpGet("CheckDirectoryExistance/{path}")]
public ActionResult CheckDirectoryExistance(string path)
{
    try
    {
        bool directoryExists = Directory.Exists(path);
        return Ok(new { ok = directoryExists });
    } catch(Exception ex)
    {
        return NotFound(ex);
    }
}

And here is my new TS code calling that API:
let dirs = [];
let dirPath = this.path + this.workSpaceInfo.userCell + "\\" + this.itemsInfo.partNo;
let alertShown = false;
console.log("DirPath? ", dirPath);
  try {
    let response = await this.http.get(`api/PDSWorker/CheckDirectoryExistance/` + encodeURIComponent(dirPath)).toPromise();
    if (response == true) {
    docFound = true;
    this.LoadRootLevel(dirPath);
    console.log("directory found from response");
    }
    /* My code Continues*/
  } catch (error) {
    console.error("Error checking directory existence general catch:", error);
  }

The problem is:
I'm getting this error and I cannot find where's my error:

GEThttps://localhost:44359/api/PDSWorker/CheckDirectoryExistance/%5C%5CSharedFile01%5CPDS%5CX405%5CSt6%5C284A1733-170 404 (Not Found)
Error checking directory existence general catch:
message
:
"Http failure response for https://localhost:44359/api/PDSWorker/CheckDirectoryExistance/%5C%5CSharedFile01%5CPDS%5CX405%5CSt6%5C284A1733-170: 404 OK"
name: "HttpErrorResponse"
ok: false
status: 404
statusText: "OK"
url:"https://localhost:44359/api/PDSWorker/CheckDirectoryExistance/%5C%5CSharedFile01%5CPDS%5CX405%5CSt6%5C284A1733-170"

I have already tried next:

I check the parameter passed in my API to be right and it is, it exist.
Added a breakpoint in my API to get in but never happend.
Changed API site (I changed from one controller to another).
I have another API's in my Controller that actually works, but this one does not work.
Answer: Looks like your api expects the raw format without encoding, so the simplest solution for you is replace
let response = await this.http.get(`api/PDSWorker/CheckDirectoryExistance/` + encodeURIComponent(dirPath)).toPromise();

with
let response = await this.http.get(`api/PDSWorker/CheckDirectoryExistance/${dirPath}`).toPromise();

Comment: yeah, this part `?path=` mean that the `path` must be taken from query params not wrom the URL itself. I'm glad that you finally solved it.
Comment: also I'd recoment to use syntax with `Observable` instead if converting to `Promise` and `await`. But it is not related to the issue, just better practice.
Comment: Thanks for the comment, but it doesn't worked as expected. Instead I got same error:
url: `https://localhost:44359/PDS/api/PDSWorker/CheckDirectoryExistance///SharedFile01/PDS/X405/St6/284A1733-170`
Comment: Could you show how does the request look like in a working version of the app. Inspect the network tab in chrome devtools and check the request itself and its headers. If you say backend hasn't changed, that means that request if different from the original one. Knowing the difference we will be able to identify the problem's reason.
Comment: In previous code the call used to be like this:
`var dirPath = path + userCell + "\\" + $scope.partNo;`
`$http.get(`${uriApi}api/PDS/CheckDirectoryExistance?path=` + `encodeURIComponent(dirPath))`
 `.then(response => { `

In my current one i tried to encode same as it was but seems like the call is taking the path as the API path and that's why I'm getting the error.
But in the headers tab this is what I see:
Request URL:`https://localhost:44359/api/PDSWorker/CheckDirectoryExistance/X405%5CSt6%5C284A1733-170`
Request Method:GET
Status Code:404 Not Found
Answer: After a long research I've noticed what my problem was:
My directory path has to be encoded being like this:
let dirPath = this.path + this.workSpaceInfo.userCell + "\\" + this.itemsInfo.partNo;
let encondedURL = encodeURIComponent(dirPath);

And I add that new variable to my API call:
let response = await this.http.get<any>(`api/PDSWorker/CheckDirectoryExistance?path=` + encondedURL).toPromise();

But the change was in my API actually:
    [HttpGet("CheckDirectoryExistance")]
    public ActionResult CheckDirectoryExistance([FromQuery] string path)
    {
        try
        {
            bool directoryExists = Directory.Exists(path);
            return Ok(new { ok = directoryExists });
        } catch(Exception ex)
        {
            Console.WriteLine("Error while Verifying the directory existance");
            return BadRequest(ex);
        }
    }

Notice that I removed the "{path}" from the route and I added "[FromQuery]". This because I was passing the string as a query parameter and I should use it as a route parameter.


Martingale-like properties of extreme value process?

Question: Imagine I have a sequence of i.i.d. random variables $x_1,x_2,...,x_n,...$ and let $M_n = \max(x_1,...,x_n)$. Is there something I can say about the expectation $\mathbb{E}[M_n | M_{n-1}]$?   What if $x_1,...,x_n$ have a known distribution, such as the exponential distribution or an extreme value distribution?
Answer: Since the cumulative maximum will only increase if the next $X_n$ is greater than the current value of the maximum, we have  $$E(M_n\mid M_{n-1}) = P(X\le M_{n-1})M_{n-1} + P(X> M_{n-1})E(X\mid X> M_{n-1}).$$
In particular, for an exponential with mean $\theta$, this works out to $$ (1-e^{-M_{n-1}/\theta})M_{n-1} + e^{-M_{n-1}/\theta}(M_{n-1}+\theta)=M_{n-1} +\theta e^{-M_{n-1}/\theta}$$ where we used the memorylessness property to get the conditional expectation.
Answer: We also know $M_n$ is a sub-martingale since $M_{n+1} \geq M_n$ almost surely, so
$$\mathbb{E}[M_{n+1}|M_1,\ldots,M_n] = \mathbb{E}[M_{n+1} | M_n] \geq M_n.$$


A problem about lub and glb of matrix

Question: For any matrix $A\in \mathbb{C}^{n\times n}$, define $$lub_K(A):= \inf\{\alpha\geq 0: AK\subset \alpha K\},$$ and $$glb_K(A):= \sup\{\alpha\geq 0: \alpha K\subset AK\},$$ where $K$ is a equilibrated convex body.
Prove for any two matrices $R$ and $S$, if there exist $x$, not zero, such that $Rx=Sx$, then $glb_K(R)\leq lub_K(S)$.
Answer: If $\mathrm{glb}_K(R)=0$, there is nothing to show. Otherwise, let $y:=\eta_*x$ where $\eta_*:=\sup\{\eta:\,\eta y\in K\}$ (note that $\eta_*>0$ since $K$ contains interior points). Now if $\alpha K\subset RK$ for some $\alpha>0$, we have $\alpha y\in RK$. In addition, $\alpha y\in SK$ so for any $\beta>0$ such that $SK\subset\beta K$, $\alpha y\in \beta K$ and hence $\alpha\beta^{-1}y\in K$. But since $y$ is the largest scalar multiple of $x$ which "fits" into $K$, $\alpha\beta^{-1}\leq 1$ so we have $\alpha\leq\beta$ (that is, if $\alpha\beta^{-1}>1$, $\alpha\beta^{-1}y\not\in K$ by the definition of $y$).

A problem about lub and glb of matrix


Why are CFB polls so week-to-week compared to NFL's power ranking style?

Question: Disclaimer**
I tried to make this question as unbiased and as unopinionated as possible.  I notice a discrepancy in how teams are ranked in college football polls vs how they are ranked in NFL power rankings, but I was not able to use solely facts and figures to explain what I notice.  Rather, I tried to explain what I observe and why I observe it that way.  If this does not meet StackExchange guidelines, please advise me to clarify instead of simply closing.
How I understand NCAA football polls...
Voters seem to vote fairly rigidly based on game outcomes, something to the effect of "Games X, Y, Z ended in scenario A, B, and C, so we must rank the teams accordingly."  Watching teams move up and down in the polls, combined with the analysis I watch/hear, rankings seem to have the following significant factors:

Win/Loss
Good wins
Bad losses
Margin of victory
Strength of schedule
Strength of conference

This all sounds completely logical, but many times I'll see an upset or a fluke ending to a game that will totally mix up the polls, and overestimate or underestimate the true quality of a team (per 2014 week 7 polls, it seems unlikely that voters believe Ole Miss is truly the 3rd best team in the country).  I see a lot of week-to-week volatility in the polls that doesn't seem to accurately reflect who is truly the best, second best, third, etc.  It seems the voters use the above criteria to rank teams based on who deserves specific rankings, instead of truly who is the better team.
On November 18, 2006 #1 Ohio State edged out #2 Michigan 42-39, and Michigan fell from #2 to #3 in both the AP and Coaches polls.  The rankings suggests that after this loss, the voters no longer thought Michigan was the second best team in the country.  But if the second best team loses to the best team by only 3 points, there is little else that could be done to assert that team as the second best.  This just doesn't make sense to me.
I also see these scenarios often, which don't seem "fair":
1) Team A starts the season ranked #1, loses in week 2 and goes to rank #10.  They go undefeated the rest of the season, while teams ahead of them lose, and Team A works its way back to #1 by the end of the season.
2) Team B starts the season ranked #1 and wins its first 11 games, still ranked #1.  They lose the 12th game and fall to #10 and finish the season at this spot.  It seems the voters methods allow a team that loses early in the year a better opportunity to finish high in the polls than a team that loses at the end of the year.
How I understand NFL power rankings...
NFL power rankings seem to be a less "by-the-book" assessment of teams' quality, but more about taking an overall look at a team's quality and applying some consistent estimate to their quality.
Observe ESPN's 2014 Week 3 power rankings.  The 1-1 Patriots were ranked 5th, ahead of the 2-0 Cardinals ranked 9th.  The 0-2 Colts were ranked 15th ahead of the 1-1 Cowboys ranked 22nd.  It seems most NFL power rankings take a broader view of a team's quality, and do not let individual games affect a team's rank as much as it would in college football.
Why do college football polls tend to rank teams more according to who deserves the position, as opposed to who are truly the better teams?

I understand college plays fewer games, so each has more impact (33.3% more)
I understand there are more college teams and less parity

These 2 factors just don't seem big enough to justify the discrepancies in ranking I have outlined above.
Comment: NFL rankings have no impact on the postseason.  Also it is much easier to determine the better teams as there are a lot of team to team comparisons.  A team will play 13 other teams in a season.  Leaving only 18 they haven't played.  The teams they have played however will play those other teams in some combination.  It makes it easier to evaluate teams wins and losses compared to the rest of the league.
Comment: @diggers3 and it generally doesn't unless teams ahead of it lose. It's pretty rare to see two teams who both win early season games by large margins switch places in the rankings (though it does happen).
Comment: @waxeagle not true. There are many cases of 1 or 2 loss teams getting the go ahead over teams with better records because of losing to tough (highly ranked opponents), especially in close games.
Comment: For what it's worth, my opinion is that this question is along the lines of being [subjective to generate interesting content](http://meta.sports.stackexchange.com/questions/317/should-we-look-to-relax-too-subjective-to-generate-more-and-more-interesting-q) (also see [good subjective, bad subjective](http://blog.stackoverflow.com/2010/09/good-subjective-bad-subjective/)). Others may differ, but that's where I stand.
Comment: To comment on your question, as asked, college football appears to have significance toward rankings to determine bowl games, including the championship game (or starting this season, the playoff system). The "power rankings" of the NFL do not.
Comment: As for the NCAA voting, this is partly why people don't like the preseason and early season polls.  A SEC team beating a mid major school shouldn't really have any impact on their ranking.
Comment: @waxeagle true.  You usually see teams rewarded for playing a tough schedule in the two polls.
Comment: @diggers3 only if they win. Losing to tough opponents doesn't net them anything.
Comment: @waxeagle Alabama didn't drop after as far after losing to Ole Miss as Oregon did in losing to Arizona this past weekend.
Answer: College rankings sort of matter. NFL power rankings don't at all.
There are a number of factors here, but let's examine what the two are, so we can make sure we know how apples to oranges this comparison really is.
Collegiate Rankings:
These are official polls done by major new organizations. The two major polls are the AP poll and the USA Today poll.
In the AP poll members of the media rank their top 25 teams.
In the USA Today poll, NCAA I-A coaches vote for their top 25 teams.
These lists are compiled, and posted on major sports websites. Until this year, these polls were factored into the computer rankings that determined which two teams would compete for the national championship. This year, four teams will be selected to compete, and these polls may or may not factor into the decision making process.
Power Rankings:
Anyone can put together a power rankings. Generally the major ones are a single, or small set of journalists ranking the best teams.
ESPN, SI, YSports and many other organizations put out their own Power Rankings. These rankings have absolutely no importance whatsoever to determining league championships.

Obviously this is apples to oranges. Aggregated polls versus very small sample analysis  means that the results really aren't comparable between the two functions.
As to why they differ dramatically in how they move teams about, it actually probably has a lot to do with their intent, but also how the post season in each league is structured.
With respect to intent, College football polls are used to rank teams, and have long held importance in both determining who is who in the league, and in generally determining who is the better team. Power rankings have never had much importance and at best are the stuff of Sunday afternoon BSing in bars.
The post season structure in the leagues is the real money here though. Poll results have for decades played a huge roll in who was, or got to play to be the national champion. It used to be that at the end of the season the AP #1 was declared champ. Then the BCS came into being and the AP poll became a factor among others in determining who would play in the title game (there were BCS standing released based on computer formulas). The winner of that game was declared #1 team in the final AP poll of the season. Other post season games also take poll placement into consideration in college, bowl selection is a big deal, and bowls by contract get to choose teams in a specific order from various conferences. The polls help them decide who to choose.
Also, the way that college football post season is structured, lends itself heavily to week to week adjustments. If there are four top conference undefeated teams, and a bunch of good one loss teams, those 4 top conference undefeated teams are going to the playoff, not those good one loss teams. In contrast, top NFL teams usually lose 2-4 games a season, so power rankings can afford to be less capricious. NCAA rankings have to be harsh, because there are usually a lot of good, comparable teams, and losses are much more magnified (since the best teams go undefeated or lose at most 2 games).
The NFL does not use polls, but instead uses actual standings (which is more fair in the NFL where teams play consistent schedules year to year, and have approximately the same difficulty schedule...or at least within an order of magnitude compared to collegiate schedules). Since they have no polls, none of the rankings actually have any consequence in the NFL game and can thus be more capricious.
last thing to reiterate is that generally NCAA polls are decided by large bodies and NFL power rankings are often just a guy, or maybe a small team. They aren't really comparable.
Comment: +1 for the following statement alone: "College rankings sort of matter. NFL power rankings don't at all."
Comment: I think this `Also, the way that college football post season is structured, lends itself heavily to week to week adjustments.` just about sums up what I was looking for...the idea that there could be something that necessitates voters to create more weekly flux directly tied to wins and losses.


ActiveDirectory domain service wizard: Prerequisites check for domain controller upgrade fails

Question: I want to test my software's SSO, so I am trying to configure ActiveDirectory on Windows Server 2012, following this tutorial.
At "prerequisites step", the window is supposed to look like:

but unfortunately I get an error:

Sorry for the Japanese, it says:
x The prerequisites check for the domain controller upgrade failed.
   The certificate server is installed.
i The prerequisites check is over.
x One of the above failed. Please correct and retry.

It seems that ActiveDirectory is already installed, but not configured.
Is there another way to enter the forest/domain name details, apart from this wizard?
Or should I uninstall ActiveDirectory, then restart the tutorial?
Answer: If you have already added the Active Directory Certificate Services before promoting it to a Domain Controller, remove the Certificate Services role, and then add the AD DS role again.
If the "Add Roles and Features" wizard does not allow you to remove Active Directory Certificate Services" (checkbox is greyed out), then you can remove it using the following PowerShell command:
Remove-WindowsFeature -Name AD-Certificate

See also: https://serverfault.com/a/717841/239616
Comment: Here is my understanding of your answer, is it correct? 1) Remove AD CS, 2) Promote to domain server, 3) Add AD CS
Comment: Exactly! Remove ADCS role, add ADDS role, add ADCS role again
Answer: The removal of certificates worked in my case too for clearing off the pre-requisite failure.
I'm pasting a powershell command in case the GUI does not work
PS C:\WINDOWS\system32> Remove-WindowsFeature -Name AD-Certificate

Answer: I really like the answer by Mathias R. Jessen.
Remove-WindowsFeature -Name AD-Certificate

as a PowerShell command; however, if the option Network Device Enrollment Service is already installed, so the above command will be not enough before uninstalling this feature:
PS C:\> Uninstall-AdcsNetworkDeviceEnrollmentService -Force

Now, retry Mathias's solution and would work now ;-)


When are '$form_state['values'] copied to '$form' on rebuild?

Question: as far as I understand it whenever you submit and rebuild a form (e.g. 'Add another item' on fields with infinite cardinality) something like this happens:

User submits a form with filled values
User input is copied to $form_state
Element validation happens
Form validation happens
drupal_get_form is called (form is being rebuild)
??? old user input from $form_state is copied to every $element[#value]
Individual elements of the form are created in $element[#process] callbacks
Rebuild form is shown to a user

My question is, when and how is user input from the old form transferred into the new one?
From what I've been able to understand by reading the documentation it must happen somewhere in drupal_build_form before the element #process callback is called. But when exactly that I haven't been able to find.
//I know about this FAPI flowchart if anyone could point me in the right direction on it I'd appreciate it.
Comment: `old user input from $form_state is copied to every $element[#value]` is it? Never noticed. [By the book](https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#value) `#value` is used only by button, hidden, image_button, submit, token, value. Where exactly you managed to `var_dump` or `kpr` your form and see `#value`s set for elements that wasn't set by you? It is not a rhetorical question. First, you are looking for something and I want to know it really happens. Second, knowing when they are for sure set and when they are not will tell us where to look.
Comment: @Mołot I'm talkind about $element['#value'], not FAPI #element property. And that one has to be and according the flowchart is set in form_builder function right before the 'process callback' (that actually renders the element FAPI array) is called.
Answer: Nothing like "??? old user input from $form_state is copied to every $element[#value]" happens. Simply $form_state['input'] is kept so that when the form is built the values are filled in based on that.
Comment: When you submit a form with user input, the input gets stored in $form_state['input'] and later (after validation) in $form_state['values']. But when you, then, rebuild the form, the values are (and AFAIK have to be) are ?somehow? given as parameters to element process functions (that create the FAPI arrays of elements).
Comment: Because $form_state['input'] is kept and then values is computed from them same as normal submit.
Comment: Petroll, if `When you submit a form with user input, the input gets stored in $form_state['input'] and later (after validation) in $form_state['values']` is true, so why do we use `$form_state['values']` in validation function ?


When to use cylindrical coordinate and when to use spherical coordinate?

Question: So I was told that any kind of $3$ space Cartesian coordinate volume question can be solved using rectangular coordinate, cylindrical coordinate and spherical coordinate. Here is the thing, by using one of the coordinate systems you can solve the question easily compare to the other coordinate system.
My question is only about cylindrical coordinate and spherical coordinate, what kind of geometry or how do we know which coordinate system we should use to easily solve a geometry problem.
Thanks.
Answer: Basically it makes things easier if your coordinates look like the problem.  If you have a problem with spherical symmetry, like the gravity of a planet or a hydrogen atom, spherical coordinates can be helpful.  If you have a problem with cylindrical symmetry, like the magnetic field of a wire, use those coordinates.  There are many other systems possible.  It is not required that the axes be orthogonal, though you get some new terms in your equations.  If you are studying certain crystals, you might want to align your axes with the crystal axes, even if they are not orthogonal.
Comment: thanks for the explanation.


Learn Python the Hard Way Ex 25: local variable/object assignment in functions

Question: I am learning the basics of python2 by studying Zed Shaw's book "Learn Python The Hard Way". On exercise 25 the author gives a few functions, like:
def break_words(sentence):
    words = sentence.split(' ')
    return words

def print_first_word(words):
    word = words.pop(0)
    print word

I shortened the functions like this:
def break_words(sentence):
    return sentence.split(' ')

def print_first_word(sentence)
    print break_words(sentence).pop(0)

And wanted to know if there is a reason the author assigns .split() & .pop() to words/word?
Thank you
Comment: Why on earth are you learning Python 2? It will reach its official End of Life in 2020. You should be learning Python 3. And then you can learn Python 2 if you need it to work on (or read) legacy code.
Comment: FWIW, the SO Python Chat room regulars [do not recommend LPTHW](http://sopython.com/wiki/LPTHW_Complaints). If it's working for you, that's great, but please be aware that there are several problems with this book. And we aren't very impressed with Zed's severe anti-Python 3 stance in that book. FWIW, he has now done a Python 3 version, but he's still not a big fan of Python 3.
Comment: Note that the original will leave you with a shortened `words` list, while your version will always discard the rest of the words.
Comment: Only for clarity.
Comment: @PM2Ring because the author suggests to first learn Python 2 ... you got me confused and frustrated with this one :)
Comment: That advice may have been good several years ago, but it doesn't make much sense these days: it's like telling someone who can't speak English that they should first learn Shakespeare's English.
Answer: I think the only reason is for readability (in their view).
For many just returning them directly would be just as clear, but it is up to whoever is writing the code or whoever the code is being wrote for.
Your method of shortening them is what I personally would go with, but it is a matter of preference.
Taking guidance from the ZEN of Python:

The Zen of Python
  Beautiful is better than ugly.
  Explicit is better than implicit.
  Simple is better than complex.
  Complex is better than complicated.
  Flat is better than nested.
  Sparse is better than dense.
  Readability counts.
  Special cases aren't special enough to break the rules.
  Although practicality beats purity.
  Errors should never pass silently.
  Unless explicitly silenced.
  In the face of ambiguity, refuse the temptation to guess.
  There should be one-- and preferably only one --obvious way to do it.
  Although that way may not be obvious at first unless you're Dutch.
  Now is better than never.
  Although never is often better than *right* now.
  If the implementation is hard to explain, it's a bad idea.
  If the implementation is easy to explain, it may be a good idea.
  Namespaces are one honking great idea -- let's do more of those!

As a sidenote, there is an easter-egg whereby this is printed to the console by running: import this.


Problema ao acessar dados de campos gerados dinamicamente ASP.NET

Question: Estou desenvolvendo um site onde tem um administrador para os formulários de contato, ou seja, o cliente pode criar campos para esse form. Até aí tudo bem, o cadastro funciona bem e eu consigo desenhar o formulário corretamente na tela, conforme código abaixo:
frmContato.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="frmContato.aspx.cs" Inherits="frmContato" EnableEventValidation="false" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

            <asp:Label CssClass="lblNovo" ID="lblCurr" runat="server" Text="Anexe seu currículo: "></asp:Label>

            <asp:FileUpload ID="fuCurriculo" runat="server" AllowMultiple="false" />

            <asp:Label CssClass="lblNovo" ID="lblFoto" runat="server" Text="Anexe sua foto: "></asp:Label>

            <asp:FileUpload ID="fuFoto" runat="server" AllowMultiple="false" />

<asp:Button ID="btnEnviaEmail" runat="server" Text="Enviar" OnClick="Button1_Click" />

Abaixo, está o code-behind do form:
frmContato.aspx.cs
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class frmContato : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["tpSite"] != "3")
    {
        fuCurriculo.Visible = false;
        fuFoto.Visible = false;
        lblCurr.Visible = false;
        lblFoto.Visible = false;
    }

    if (!Page.IsPostBack)
    {
        SessaoAberta();
        SetInitialRow();
    }
}

private void SessaoAberta()
{
    try
    {
        if (Session["tpSite"] == null || Session["tpSite"] == "")
        {
            Response.Redirect("index.aspx");
        }
    }
    catch (Exception)
    {
        Response.Redirect("index.aspx");
    }
}

 private void SetInitialRow()
{
    string tpSite = Session["tpSite"].ToString();

    string tpcampo;
    string nomecampo;
    string assuntos;

    TextBox tx;
    RadioButton rdb;
    CheckBox chk;
    DropDownList ddl;
    Label lbl;

    string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

    string sql = "  SELECT idCampo,nomeCampo, tipoCampo, assuntos FROM [dbo].[camposFormulario] WHERE idTpSite = " + tpSite + " AND status = 1 ORDER BY ordem";

    SqlConnection cn = new SqlConnection(conString);
    SqlCommand cmd = new SqlCommand(sql, cn);
    SqlCommand cmd2 = new SqlCommand(sql, cn);

    cn.Open();

    SqlDataReader dreader = cmd.ExecuteReader();

    forms.Controls.Add(new LiteralControl("
"));

    while (dreader.Read())
    {
        tpcampo = dreader["tipoCampo"].ToString();
        nomecampo = dreader["nomeCampo"].ToString();
        assuntos = dreader["assuntos"].ToString();

        if (tpcampo == "1") //Textbox
        {
            lbl = new Label();
            lbl.Text = nomecampo;
            lbl.Style.Add("font-family", "Arial");

            tx = new TextBox();
            tx.ID = "txt";
            tx.Style.Add("font-family", "Arial");
            tx.Width = 300;

            forms.Controls.Add(new LiteralControl("
"));
            forms.Controls.Add(new LiteralControl(""));
            forms.Controls.Add(lbl);
            forms.Controls.Add(new LiteralControl("	"));
            forms.Controls.Add(new LiteralControl(""));
            forms.Controls.Add(tx);
            forms.Controls.Add(new LiteralControl("	"));
            forms.Controls.Add(new LiteralControl("
"));

        }
        else if (tpcampo == "2") //Radio
        {
            string s = dreader["assuntos"].ToString();
            string[] itens = s.Split(',');

            lbl = new Label();
            lbl.Text = nomecampo;
            lbl.Style.Add("font-family", "Arial");

            rdb = new RadioButton();
            rdb.ID = "rdb";

            forms.Controls.Add(new LiteralControl("
"));
            forms.Controls.Add(new LiteralControl(""));
            forms.Controls.Add(lbl);
            forms.Controls.Add(new LiteralControl("	"));
            forms.Controls.Add(new LiteralControl(""));
            foreach (string item in itens)
            {
                rdb = new RadioButton();
                rdb.Text = item;
                rdb.Style.Add("font-family", "Arial");
                forms.Controls.Add(rdb);
                forms.Controls.Add(new LiteralControl("
"));
            }
            forms.Controls.Add(new LiteralControl("	"));
            forms.Controls.Add(new LiteralControl("
"));
        }
        else if (tpcampo == "3") //Check
        {
            string s = dreader["assuntos"].ToString();
            string[] itens = s.Split(',');

            lbl = new Label();
            lbl.Text = nomecampo;
            lbl.Style.Add("font-family", "Arial");

            chk = new CheckBox();
            chk.ID = "chk";

            forms.Controls.Add(new LiteralControl("
"));
            forms.Controls.Add(new LiteralControl(""));
            forms.Controls.Add(lbl);
            forms.Controls.Add(new LiteralControl("	"));
            forms.Controls.Add(new LiteralControl(""));
            foreach (string item in itens)
            {
                chk = new CheckBox();
                chk.Text = item;
                chk.Style.Add("font-family", "Arial");
                forms.Controls.Add(chk);
                forms.Controls.Add(new LiteralControl("
"));
            }
            forms.Controls.Add(new LiteralControl("	"));
            forms.Controls.Add(new LiteralControl("
"));
        }
    }
    forms.Controls.Add(new LiteralControl("
"));
cn.Close();
}

O form está desenhando certinho da maneira como eu preciso, a minha dúvida é como fazer para capturar esses dados após a geração. Tentei fazer com um foreach pelos controls mas não deu muito certo.
Qualquer auxílio eu agradeço!
Comment: Estou sem tempo agora, mas existe uma coisa com o asp.net que faz com que controles criados dinamicamente não estejam presentes em um postback, tenque dar uma pesquisada sobre isso... amanhã eu posto uns links relevantes.
Answer: Com foreach deveria funcionar:
foreach (Control c in forms.Controls)
{
    if (c is TextBox)
    {
        TextBox textBox = (TextBox)c;
        textBox.Text = "Texto"; //acesse aqui
    }
    else if (c is RadioButton)
    {
        RadioButton radioButton = (RadioButton)c;
    }
    else if (c is CheckBox)
    {
        CheckBox checkBox = (CheckBox)c;
    }
}

Caso seja necessário identificar os controles individualmente, pode ser feito definindo a propriedade Name (ou mesmo ID) exclusiva pra cada um deles.
Comment: Tentei fazer como você colocou, mas no if ele está retornando sempre false, será que estou deixando passar alguma coisa?
Comment: Talvez ao fazer a referência ao forms, tente mudar pra `this.Controls` no `foreach`.
Comment: Tentei, mas também não deu...acho que estou deixando passar algo, estava lendo sobre o ciclo de vida de páginas asp.NET mas n sei se tem muito a ver com isso...quando uso o foreach ele simplesmente traz o campo como null


Preprocess Field: CSS Style per page

Question: Trying to set up a preprocess_field_hook() to send a different css style depending on the node that is loaded for simpler theming. Will not work and been beating my head.
Have this set inside my template.phpand have my tpl pages named correctly (exam: node--node_page_one.tpl.php), which is loading fine with other functions already working. I feel like I need to have it point to the actual element somehow? Or should I be doing this somewhere else like in the preprocess_node_hook()?
Sorry for my noobness.
 function mytheme_preprocess_field(&$variables, $hook) {
      $variables['fieldclass'] = '';
      switch ($variables) {
        case 'node_page_one':
          $variables['fieldclass'] = 'tiny-blue';
          break;
        case 'node_page_two':
          $variables['fieldclass'] = 'large-green';
          break;
        default:
          $variables['fieldclass'] = 'black';
          break;
      }

Answer: Rather than $variables['fieldclass'] use $variables['classes_array']
Based on the docs
Comment: After taking this into account and tinkering with what I had. Got it working!

`($vars['element']['#bundle'])`

For future theming, what is the best/easiest method to discovering the classes array?

Thanks again for the help!

Preprocess Field: CSS Style per page


C++ Memory Leak Error (definitely lost: 280 bytes in 2 blocks)

Question: I have Memory Leak in my code and not sure how to diagnose it. There are 3 files and the main.cpp is the test file therefore cannot be altered. The program is using a Mem Checker and it displays that ==159804== Memcheck, a memory error detector, definitely lost: 280 bytes in 2 blocks.
Main.cpp (Cannot be altered)
#include<iostream>
#include<cstring>
#include"Basket.h"
#include"Basket.h" //intentional

using namespace std;
using namespace sdds;

void printHeader(const char* title)
{
    char oldFill = cout.fill('-');
    cout.width(40);
    cout << "" << endl;

    cout << "|> " << title << endl;

    cout.fill('-');
    cout.width(40);
    cout << "" << endl;
    cout.fill(oldFill);
}

int main()
{
    sdds::Fruit fruits[]{
        {"apple",  0.65},
        {"banana", 1.25},
        {"pear",   0.50},
        {"mango",  0.75},
        {"plum",   2.00},
    };

    {
        printHeader("T1: Default Constructor");

        Basket aBasket;
        cout << aBasket;

        // conversion to bool operator
        if (aBasket)
            cout << "Test failed: the basket should be empty!\n";
        else
            cout << "Test succeeded: operator said the basket is empty!\n";

        cout << endl;
    }

    {
        printHeader("T2: Custom Constructor");

        Basket aBasket(fruits, 2, 6.99);
        cout << aBasket;

        // conversion to bool operator
        if (aBasket)
            cout << "Test succeeded: operator said the basket has content!\n";
        else
            cout << "Test failed: the basket should NOT be empty!\n";

        cout << endl;
    }

    {
        printHeader("T3: += operator");

        Basket aBasket;
        aBasket += fruits[2];
        (aBasket += fruits[0]) += fruits[4];
        aBasket.setPrice(12.234);

        cout << aBasket;
        cout << endl;
    }

    {
        printHeader("T4: Copy Constructor");

        Basket b1;
        Basket b2(b1);

        cout << "Basket #1 -> " << b1;
        cout << "Basket #2 -> " << b2;

        b1 += fruits[3];
        b1.setPrice(3.50);

        Basket b3(b1);
        cout << "Basket #3 -> " << b3;
        cout << endl;
    }

    {
        printHeader("T5: Copy Assignment");

        Basket b1, b2, b3(fruits, 5, 19.95);

        b1 = b2;
        cout << "Basket #1 -> " << b1;
        cout << "Basket #2 -> " << b2;

        b1 = b3;
        cout << "Basket #1 -> " << b1;

        b3 = b2;
        cout << "Basket #3 -> " << b3;
    }

    return 0;
}

Basket.h
#ifndef Basket_h
#define Basket_h

#include <stdio.h>
#include <iomanip>
#include <iostream>

namespace sdds{

struct Fruit
{
  char m_name[30 + 1];
  double m_qty;
};

class Basket{
private:
    Fruit *m_fruits;
    int m_cnt;
    double m_price;
public:
    Basket();
    Basket(Fruit* fruits, int cnt, double price);
    Basket(Basket &d);
    Basket& operator = (Basket &d);
    ~Basket();
    void setPrice(double price);
    operator bool() const;
    Basket& operator+=(Fruit d);
    friend std::ostream& operator << (std::ostream& output, Basket test);
};

}
#endif /* Basket_h */

Basket.cpp
#include "Basket.h"

using namespace sdds;

namespace sdds {

Basket::Basket(){
    m_fruits = nullptr;
    m_cnt = 0;
    m_price = 0;
}

Basket::Basket(Fruit* fruits, int cnt, double price){
    if (cnt > 0 && fruits != nullptr) {
        m_cnt = cnt;
        m_price = price;
        m_fruits = new Fruit[cnt + 1];
        for (int i = 0; i < cnt; i++) {
            m_fruits[i] = fruits[i];
        }
    }else{
        m_fruits = nullptr;
        m_cnt = 0;
        m_price = 0;
    }
}

Basket::Basket(Basket &d){
    m_price = d.m_price;    // Shallow Copying
    m_cnt = d.m_cnt;
    m_fruits = new Fruit[m_cnt + 1];
    for (int i = 0; i < m_cnt; i++) {   // Deep Copying
        m_fruits[i] = d.m_fruits[i];
    }
}

Basket& Basket::operator = (Basket &d){
    m_price = d.m_price;
    m_cnt = d.m_cnt;
    m_fruits = new Fruit[m_cnt + 1];
    for (int i = 0; i < m_cnt; i++) {
        m_fruits[i] = d.m_fruits[i];
    }
    return *this;
}

Basket::~Basket(){
    delete [] m_fruits;
}

void Basket::setPrice(double price){
    m_price = price;
}

Basket::operator bool() const {
    // returning true if the Basket is valid
    return m_fruits != nullptr;
}

Basket& Basket::operator+=(Fruit d){
    Fruit* tmp = new Fruit[m_cnt + 1];
    for (int i = 0; i < m_cnt; i++) {
        tmp[i] = m_fruits[i];
    }
    tmp[m_cnt++] = d;
    delete [] m_fruits;
    m_fruits = tmp;

    return *this;
}

std::ostream& operator << (std::ostream& output, Basket test){
    if (test.m_cnt == 0 || test.m_price == 0 || test.m_fruits == nullptr) {
        output << "The basket is empty!" << std::endl;
    }else{
        output << "Basket Content:" << std::endl;
        std::cout << std::fixed;
        std::cout << std::setprecision(2);
        for (int i = 0 ; i < test.m_cnt; i++) {
            output << std::setw(10) << test.m_fruits[i].m_name << ": " <<test.m_fruits[i].m_qty << "kg" << std::endl;
        }
        output << "Price: " << test.m_price << std::endl;
    }
    return output;
}

}

Comment: What happens to the thing that `m_fruits` already points to, when you run `operator=`?
Comment: Unrelated: [a simple and nearly foolproof way to implement an assignment operator](https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom). It's not always the best solution, but it makes a great place to start and you'll find that most of the time you'll stay there.
Comment: @Daboss2182 Your copy assignment operator could have been written very simply and correctly by using copy / swap.  It takes about a minute to get it working, instead of duplicating the code that's in the copy constructor.
Comment: Glad to see a course teaching "real" C++ though, with namespaces and such like (presumably vectors come later)
Comment: As for how to diagnose it ... dunno, just look at all your `new`s and look at all your `delete`s, and try to spot the missing one I guess :) Memcheck _should_ tell you _what_ was lost...
Comment: @AsteroidsWithWings I have created a destructor to delete m_fruits, shouldnt that take of all the news ?
Comment: @Daboss2182 Assignment does not invoke the destructor first.
Answer: Your assignment operator leaks memory, since you failed to delete[] the original data when doing the assignment.
The easiest way to get a working assignment operator is to use the copy/swap idiom:
#include <algorithm>
//...
Basket& Basket::operator = (const Basket &d)
{
    if ( &d != this)
    {
       Basket temp(d);
       std::swap(temp.m_price, m_price);
       std::swap(temp.m_cnt, cnt);
       std::swap(temp.m_fruits, fruits);
    }
    return *this;
}

Your original assignment operator had multiple flaws:

Did not delete[] the old memory.

Did not do a check for self-assignment, thus Basket a; a = a; would fail.

Changed member variables before issuing a call to new[], thus an exception thrown would corrupt your object.

All three issues are taken care of with the code shown above.
In fact, item 2) need not be done for copy / swap to work (but done in the example code above, just to show what your original code was missing).


How to apply the right amount of rosin to a bow?

Question: I was wondering if I was applying a bit too much rosin to my violin bow.
I noticed that my violin strings are turning white,
with white powder specks on the fingerboard and around the F holes.
So I have the following questions:

What signs would show that I have applied too much rosin to a bow?
What signs would show that I have applied too little rosin to a bow?
What is a "normal" frequency for applying rosin to a bow?
Every day before practice? Once a week?
Answer: Well,  Rule Number One is to make sure you wipe down your strings and your instrument after every session. Don't let rosin -- or anything else-- cake up on your axe.
What little observation I've done suggests that beginners tend to over-rosin, but you can't really judge by the rate of dust or buildup on the strings. For one thing, different rosins have differing rates of powdering.
My personal method, and please don't take this as the one-and-only way, was to play until the bow clearly was skating on the strings, then rosin up very lightly until I got a good "feel."  Then I played until the bow started to skate again, and set my rosining frequency to one day less than that :-) .  Basically, it comes down to having played enough to recognize the feel of the bow on the strings and applying just enough rosin to keep that feel going.
Comment: As a bowmaker, I'll second all that, especially the part about cleaning the rosin off the violin.  Rosin is chemically not all that different from some violin varnishes, and it's very hard to clean off after it's been on the instrument for a while- it melds with the varnish.  Keep it clean.


Migrate from Ubercart 3 to Commerce

Question: I have been trying to understand how to accomplish this for quite some time.  And I'm not getting anywhere. The info I'm finding seems a bit dated.
Can anyone tell me where to start to get this accomplished?
I am looking to migrate only products.  Nothing else.  The new site will be a fresh start other than product/inventory.  And with some 1500 products, I really need a migration process.
Is there a template for Commerce Ubercart Migration that I am missing?
Thanks.
Comment: Probably easier to query and dump results to a CSV file, and use that as the data source in the migration?
Comment: Have you looked into [Commerce migrate](https://www.drupal.org/project/commerce_migrate) it has support for importing from ubercart.
Comment: I have.  I installed it.  But have no clue where to start.  I have found videos that are more than a couple of years old and not relevant.  And it seems that all I am finding in docs is not helping me understand how to do this.  So I am starting to feel that this is just beyond my ability.
Answer: Our official migration path lives in Commerce Migrate and contains migrations for Commerce 1.x, Ubercart 2.x (D6) and Ubercart 3.x (D7). Improvements are released monthly.
Comment: The module has its own docs at https://www.drupal.org/docs/8/modules/commerce-migrate
Comment: Is there any recent documentation on this?  I haven't found anything that explains how to get there from here, as it were.

Migrate from Ubercart 3 to Commerce


Como importar a biblioteca HttpClient?

Question: Onde devo colocar a library HttpClient? Em todos os tutoriais é possível encontrar apenas exemplos de códigos, mas ninguém realmente explica que depois de baixar, devemos colocar onde?
http://hc.apache.org/downloads.cgi
Existem dois tipos, um com arquivos httpclient-4.3.6.jar e outro com arquivos Java, onde coloco os arquivos para em seguida importar, exemplo: org.apache.http.client.methods.HttpPost
Comment: Que IDE você está usando? Se não estiver usando nenhuma, o que você faz para (tentar) compilar a aplicação?
Comment: @Victor Estou usando Eclipse
Answer: Você tem que especificar as bibliotecas que você está utilizando no seu classpath. Contudo dependendo do tipo de projeto que você está desenvolvendo, existem algumas "convenções".
Por exemplo, se você estiver criando um projeto java web com o eclipse, o local aonde você teria que colocar os JARs, seria em: WebContent/WEB-INF/lib.
Se você estiver fazendo um projeto 'free-style', você pode verificar como importar a biblioteca nesse link: http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-(Java)
Se for um projeto com o Maven, você não precisa baixar o JAR, basta adicionar a dependência no pom.xml e por aí vai.
Comment: voce poderia botar um exemplo na sua resposta? lembrando que voce [edit] as suas perguntas/respostas sempre que quiser
Comment: Muito obrigada, no mesmo dia que fiz essa pergunta, acabei aprendendo a fazer isso que você mencionou.

Como importar a biblioteca HttpClient?


AWS Load Balancing without Certificate?

Question: I currently have my NextJS site and my domain hosted with Vercel, which provides a SSL certificate for my domains.
My web application is on mydomain.com
and my backend domain is backend.mydomain.com
Since my backend is in EC2, I tried to set up a AWS Load Balancer to make HTTPS requests from my frontend to backend.
Should I set up a HTTP/HTTPS load balancer, or a TCP load balancer?
AWS keeps asking for my certificate, but I do not have it, since the details are hidden from my by Vercel.
Will I have to contact them to get more details?
Comment: I'm confused. What / where is your front end - is this a static single page or hosted somewhere? How does the Vercel SSL cert come into this? Typically you would just create a certificate with AWS Certificate Manager, associate it with an ALB, and point the ALB at your EC2 instances.
Comment: The frontend is a NextJS application hosted by Vercel. I thought the Vercel SSL would be related because the backend.mydomain should have a Vercel cert already and I wouldn't need to use one for the ALB.... right?
Comment: Ok. The front end hosting and back end hosting are completely independent.
Answer: From what you've said your front end is hosted by Vercel, your back end by EC2. They're completely independent from a load balancing / certificate point of view.
I suggest:

Create a certificate for your back end server using AWS Certificate Manager (ACM). Alternately you can import an existing SSL cert into ACM, but then it won't re-issue automatically.
Create an AWS Application Load Balancer, and associate it with the new ACM cert
Ensure your load balancer directs traffic to your EC2 instance. You can use auto scaling or do it manually, your call.

In comments you mentioned you can't use ACM because you already have a cert issued elsewhere. You can have multiple CAs issue a certificate for the same domain. If you're having a problem with certificate issuing please create another question that's much more detailed.
Comment: You should be able to issue a cert from ACM even if another place has issued a cert. It's not like having a cert done blocks another being done. If your core problem is issuing the certificate suggest you ask a new question and give a lot more technical detail including environment and details of the error message. You can also import an external certificate into ACM, answer updated in a minute.
Comment: I see what you mean. However, I am unable to create the certificate with ACM because backend.mydomain.com already has a certificate created by Vercel already. The request fails with a "CAA Error"
Comment: My frontend is hosted by Vercel and my backend is by EC2. However, the backend.mydomain.com is managed by Vercel and that's been throwing me in a loop since I don't know where I am supposed to reuse my Vercel Certificate or not
Answer:
You need to host your site on Ec2
You need to add your SSL certificate to your site.
Then use HTTP/HTTPS Load balancer.
Comment: I don't think that's right. Hosting on Vercel shouldn't be the issue. I think the way to proceed is to create a certificate for ALB but I'm not sure what to fill it when it asks for my domain
Comment: That's an imprecise answer which I'm not sure is correct.

AWS Load Balancing without Certificate?


What is the difference between Variational Inference and Variational EM?

Question: I have been reading about variational inference and it is relation to Bayesian regression. It seems there are two versions
The first version is discussed here.
The second version is discussed here.
What is the difference between the two versions?
I know that variational EM is closer to the EM algorithm. However, how is this different to variational inference?
Answer: What's Variational Bayes?
Variational Bayes is a general technique for approximating posteriors in Bayesian inference.
Say that we have a posterior $P(\theta|X)$ that doesn't turn out to be a nice simple distribution, and we're interested in some aspect of that distribution, like its mean or some quantiles.
The prevailing technique is to use MCMC to generate a sample from that distribution, then to build sample estimators of the mean or quantiles.
Variational Bayes offers an alternative.
Instead of sampling from $P$, we're going to specify some family of distributions $\mathcal{Q}$ that are easy for us work with.
And we're going to pick some distribution in that family as a surrogate for $P(\theta|X)$.
For now, let's assume that we can parameterize the distributions in $\mathcal{Q}$ by some variational parameters $\eta$.
The word variational means "of, or related to optimization", and indeed the plan is to pick which $q_\eta\in\mathcal{Q}$ we want by solving an optimization problem:
$$ q_{\eta^*(X)}(\theta)= \underset{Q_\eta\in\mathcal{Q}}{\min}  d[Q_\eta(\theta),P(\theta|X)]$$
where $d$ is some measure of dissimilarity between distributions.
Almost universally as of today, $d$ is the reverse KL loss, which is a divergence you can learn about by searching this site.
The idea is that if this distribution is a good enough approximation for the actual posterior, we could just ask it whatever questions we were going to ask $P(\theta|X)$. (This is only a profitable approach if $\mathcal{Q}$ contains distributions that are easy to work with wrt the posterior quantities of interest).
So what's Variational EM? Is it an EM algorithm in the style of Dempster? (No)
Historically, variational bayes has been popular in applications that involve latent variables.
These latent variables are treated identically to parameters in both Bayesian and variational Bayesian inference: we place a variational distribution over each latent variable just as we did parameter.
Let's call the latent variables $\mathbf{z}$; they have associated variational parameters $\eta_z$ (and we'll call those variational parameters associated with $\theta$ as $\eta_\theta$).
Many methods proposed for variational inference on latent variable problems alternate between optimizing $\eta_z$ for fixed $\eta_\theta$ and then vice versa, what are known in optimization as block coordinate descent methods (and actually oftentimes just plain-ol' coordinate descent, but let's leave that story for another day).
This reminded people of the Expectation Maximization algorithm which is/was prevalent for maximum likelihood based inference in latent variable models. We seem therefore to be cursed with the name Variational EM, despite the fact that there is no Expectation step: it's actually two "Maximize" steps (minimize if we consider KL divergence and maximize if we consider evidence lower bound) as we are taking descent steps on the variational distance between the current variational approximation and the true posterior in both steps.
(Oh, yes, to further confuse things, it turns out that under certain assumptions, the solution for our M steps may be expressed as an expectation...)
Comment: It seems like "Variational Bayes" and "Variational Inference" are used interchangeably. Just so I am crystal clear, do these mean the same thing?
Comment: @k3thomps Variational inference is the act of approximating some probability distribution by optimizing a surrogate to be close to it. If the thing that youre approximating is a Bayesian distribution of some kind, like a posterior, we would call it variational Bayes. It is conceivable to use VI on things other than posteriors, which would not reasonably be called "variational bayes".
Comment: Ah, I see. Thanks for the clarification!


$E(X^r) = r!\{(-1)^r+(1/2)^r\}/2\;\;$ and Its PDF

Question: Let r-th moment $E(X^r) = r!\{(-1)^r+(1/2)^r\}/2\;\;$ where $r \in \Bbb N\cup\{0\} $
Then moment generating function $M(t)$ is
$M(t) =\sum_{r=0}^\infty \dfrac{E(X^r)}{r!}t^r= \dfrac{1}{2}\left[\sum_{r=0}^\infty(-1)^rt^r+\sum_{r=0}^\infty\ ({1\over 2})^rt^r\right] = \dfrac{1}{2}\left[\dfrac{1}{1+t} + \dfrac{1}{1-{1\over 2}t}\right] = \int_0^\infty e^{tx}\cdot{1\over 2}e^x+ [?]$
I can't figure out which pdf would the last term will be.
I need to evaluate the proper pdf corresponds to above $E(X^r)$
Any hint?
Comment: or the special case of an exponential distribution if you're more familiar with that
Comment: Hint: the mgf of a [Gamma distribution](https://en.wikipedia.org/wiki/Gamma_distribution) looks like one of your terms.
Comment: @whuber any other approach not using the cocept of Gamma distribution? I am at little bit before the specific distributions
Comment: Since the answer necessarily involves Gamma distributions, it will be hard to avoid using them!
Comment: thx will check it
Answer: Consider independent but not necessarily identically distributed random variables $X_1, X_2, \ldots, X_n$, with densities $$f_{X_1}(x), f_{X_2}(x), \ldots, f_{X_n}(x),$$ and weights $a_1, a_2, \ldots, a_n \in [0,1]$ such that $\sum_{i=1}^n a_i = 1$.  Then the mixture distribution with density $$f_Y(x) = \sum_{i=1}^n a_i f_{X_i}(x)$$ is clearly a density.  The MGF of $Y$ is expressible as the weighted sum of the MGFs of the $X_i$s; i.e., $$M_Y(t) = \int_{y\in\Omega} e^{ty} f_Y(y) \, dy = \sum_{i=1}^n a_i \int_{y\in\Omega} e^{ty} f_{X_i}(y) \, dy = \sum_{i=1}^n a_i \operatorname{E}[e^{tX_i}] = \sum_{i=1}^n a_i M_{X_i}(t)$$ for the continuous case.
Since you know that $M_{X_1}(t) = (1+t)^{-1}$ corresponds to the MGF of the density $$f_{X_1}(x) = \begin{cases} e^x, & x < 0 \\ 0, & x \ge 0, \end{cases}$$ (that is to say, $-X_1 \sim \operatorname{Exponential}(\lambda = 1)$), and $M_{X_2}(t) = (1-t/2)^{-1}$ corresponds to the MGF of the density $$f_{X_2}(x) = \begin{cases} 2e^{-2x}, & x > 0 \\ 0, & x \le 0, \end{cases}$$ and the weights are simply $a_1 = a_2 = 1/2$, we find that the random variable characterized by the given MGF has the density $$f_X(x) = \begin{cases}e^{-2x}, & x > 0 \\ e^x/2, & x < 0. \end{cases}$$  The discontinuity at $x = 0$ simply means the CDF is not smooth at this point, but the CDF is unique and well-defined everywhere.


How to programmatically read the display name of a CCK field from the allowed list?

Question: For example if a CCK text field is configured to have the following allowed values:
US|United States
UK|United Kingdom

The node has the corresponding field:
node->country

and its value can be found by accessing:
node->country[0]['value']

which will return 'US' or 'UK'. What is best way to return the display name which was specified in the allowed list (i.e "United States" or "United Kingdom")?
Answer: You can use the following code.
$field = content_fields($field_name);
$function = $field['module'] . '_allowed_values';
$values = array_values(function_exists($function) ? $function($field) : (array) content_allowed_values($field));

This code is similar to the code used by optionwidgets_options(); the difference is that optionwidgets_options() returns also the value for the empty choice.
The output of content_allowed_values($field) is an array that in your case is the following one.
array(
  'US' => 'United States',
  'UK' => 'United Kingdom',
);

As you are only interested to the options visible in the user interface, I passed the result of content_allowed_values($field) to array_values().
Answer: Install the devel module and go to a node with this field and click the devel tab. If its available directly it will show in devel. If not you may have to do a custom db query.
Comment: It's not available. I thought there was an API for that?


An interval in the rationals will contain an irrational number

Question: I am trying to figure out that the set of rationals are open or not.
My problem is that I do not see how would the density of irrationals help me.
so the density of irrationals mean that an interval in the real numbers contain an irrational number, does this mean that an interval in the rationals will contain an irrational number?
I appreciate any help!
Comment: This has no sense, or well, it's trivially false: "an interval in the rationals" (or, what I think you're meaning) is a set of the form (for example) $\{x\in \mathbb{Q}: a\leq x \leq b\}$ with $a,b$ rationals. This is a subset of $\mathbb{Q}$ so it does not contain any irrational number.
Answer: For $\mathbb{Q}$ to be open, every point would need to be an interior point. Since any open interval $(a,b)$ with $a,b\in \mathbb{Q}$ contains an irrational, no point is interior. So $\mathbb{Q}$ is not open.
Comment: @Hanna7777 You are not far off. Recall that a set $M$ is dense in a set $X$ if the closure of $M$ is $X$ (i.e. $cl(M)=X$, $\bar{M}=X$, or whatever notation you use).

The argument here is that the irrationals are dense in $\mathbb{R}$. It should be proven that if a set $M$ is dense in $\mathbb{R}$ then the intersection of any interval with $\mathbb{R}$ contains a point of $M$.
Comment: The definition of an open set is that every point is interior, (or equivalently, a set $U$ is open if $\forall x\in U$ there is an open neighborhood of $x$, $N_x$, contained in $U$). In $\mathbb{R}$ open sets are equivalent to the union of open disks with some radius. So suppose $x\in \mathbb{Q}$ and consider an open neighborhood (an open set of $\mathbb{R}$) of $x$. Every open neighborhood of $x$ is either a disk or the union of disks, so it is sufficient to consider the former.
Comment: But an open disk of $\mathbb{R}$ is just an interval which, by the first comment, must contain an irrational point. Thus, any open neighborhood of a rational point contains an irrational point. This shows that no open neighborhood of $x\in \mathbb{Q}$ can be contained in $\mathbb{Q}$ or, equivalently, no point of $\mathbb{Q}$ is interior. Therefore, $\Bbb{Q}$ is not open.
Comment: when you say that " any open interval (a,b) with a,b∈Q contains an irrational" would this mean that the irrationals are dense in the rationals? Or am I far off?
Comment: Rationals are not a subset of irrationals, therefore $\mathbb Q$  couldn't be  dense in irrationals.


Member initialization in constructors

Question: Can someone please tell, in this code how to declare the constructor such that when the object is instantiated, height is initialized with passed value while width is always default (2 in the case below).
class rectangle{
    int width, height;
public:
    //  rectangle(int w = 1, int h = 1): width(w), height(h){}
    rectangle(int w = 2, int h=1): width(w) {height = h;}
    int getW(){return width;}
    int getH(){return height;}
};
int main()
{
    rectangle r1(1);
    rectangle r2(2);
    rectangle r3(4);
    rectangle r4(5);
    cout << "w = " << r1.getW() <<" h = " << r1.getH() << endl;
    cout << "w = " << r2.getW() <<" h = " << r2.getH() << endl;
    cout << "w = " << r3.getW() <<" h = " << r3.getH() << endl;
    cout << "w = " << r4.getW() <<" h = " << r4.getH() << endl;
}
Output with above code:
w = 1 h = 1
w = 2 h = 1
w = 4 h = 1
w = 5 h = 1

Can someone tell me how to declare the constructor so that the output is like below (I want to declare the object with only one parameter)?
w = 1 h = 1
w = 1 h = 2
w = 1 h = 4
w = 1 h = 5

Answer: The wording in your question is a bit unclear. It sounds like you want to completely ignore the width argument, and just make the width 2, while the height is optional, and defaults to 1. If that is the case, then you can simply do this:
rectangle(int, int h=1) :width(2), height(h) {}

But my mind reading skills tell me that is not really what you want (mainly because it's a stupid thing to do). I have a hunch that you simply worded your question wrong, and that you actually want something like this:
rectangle(int w, int h) :width(w), height(h) {} // handles 2 arguments
rectangle(int h=1) :width(2), height(h) {}      // handles 0 or 1 arguments

This configuration allows three call signatures.

2 arguments, first goes to width, and the second goes to height.
1 argument, which goes to height, and width becomes 2
0 arguments, width becomes 2, height becomes 1
Comment: Thanks Benjamin, you are correct, my wording was confusing. I am learning from an online lecture series and really wanted to understand how members are initialized, so this is mostly a theoretical exercise. Broadly speaking, What I wanted to learn is when there are more than 1 arguments, how to initialize the 1st argument (width in this case) with default value. All my constructor combinations seemed to pass the value to width, leaving height at default. Does that make sense?
Comment: @kaushal98: Yeah, that makes sense. It is not possible though, not with just one function. You can, however, simulate it with multiple overloads, which is what my answer demonstrates.
Comment: @Banjamin: sorry accidentally deleted my earlier comment. I guess this is not possible, or even stupid but I'd still like to confirm. Can we define the construct such that when instantiated like rectangle r1(1), r2(2), r1 sets w and r2 sets h?
Comment: @kaushal98: Sure. Just use an `if` statement inside the constructor: `if (parameter == 1) width = 1; if (parameter == 2) height = 2;` -- But what would you want to do in case of the other ~4 billion possible values?
Comment: :) my bad, I meant we can somehow specify that this time I want to pass the value to W, and this time to H, I just used 1, 2 as examples, it could be anything. But, now I know for sure, it's just not possible  to select W or H if there only one argument.
Comment: @kaushal98: Read about the Named Constructor Idiom: http://www.parashift.com/c++-faq/named-ctor-idiom.html

Member initialization in constructors


A smooth path through the set of all positive definite matrices

Question: Let $\mathfrak S(n)$ be the set of all positive definite $n\times n$ (real) matrices. I am no expert in topology but stumbled across this question: Does there exist a smooth path through $\mathfrak S(n)$?
I know already that $\mathfrak S(n)$ can be made to a topological space by equipping the set with the topology inherited from the canonical inner product for $n\times n$ matrices $A$, $\Vert A\Vert = \sqrt{\mathrm{trace}(A'A)}$. But what are the requirements for a smooth path to exist?
Comment: You mean, like a space filling curve?
Comment: The set of positive definite matrices is convex.
Answer: For any $A, B \in {\mathfrak S}_n$, $(1-t) A + t B$, $0 \le t \le 1$, is a smooth path in ${\mathfrak S}_n$ from $A$ to $B$.


eclipse and websphere 8.0 - cannot change the context root

Question: I have the IBM plugin for eclipse that allows me to develop on websphere 8.0 servers.  I'm using eclipse (Juno Build).
Normally, in the project explorer, I can change the context root of an application simply by right-clicking >> properties >> Web Project Settings >> and setting the context root right there.  I can literally set that context root to anything, and it doesn't change anything about it.  When I check the admin console, it displays as being 'AppName', rather than anything I've set in the web project settings.  That and I'm unable to change it right there in the console, since the only button I can click is "cancel".  All the text fields are there for me to change it, but there's no save button anywhere...
Has anyone else had this issue?  What did you do to fix it, if at all?
Answer: There are 2 places you can set a Context Root in WebSphere.
The first is in the module definition inside the application.xml file of the EAR (That file is not mandatory in WAS 8 but it is possible to use it)
The second is in the ibm-web-ext.xml file inside the WAR.
Is it important to keep in mind the application.xml settings override the ibm-web-ext.xml settings.
When you set the context root in the UI it is setting the ibm-web-ext.xml file, if you got another definition in application.xml you will see no change, as you experiencing.
My suggestion is, if you are using application.xml always set the context root there.
Comment: That did it.  Sorry to get back so late... was making my rounds, and realized I forgot to mark this as my solution.  Thank you for your help :)


What is the idea behind minimal dynamical systems?

Question:
A topological dynamical system $(X,f)$ is called minimal if whenever there is a closed $Y \subseteq X  $ s.t. $f(Y)\subseteq Y$ then either $Y = \emptyset$ or $Y=X$.

James R. Brown writes in his book Ergodic Theory and Topological Dynamics (p. 46):

The essential idea of minimality is that everything worth knowing about the system can be determined from the present and future, or from the past, present, and future, situation of a single point under the action of $f$.

What is that "everything worth knowing"? How could it be determined? Could someone elaborate on the quote or share his own understanding of minimal dynamical systems?
Answer: A bit unfortunate this "everything worth knowing", but probably Brown is only giving a brief rough motivation at that point (his book is very good!).
It also works, say, without changes, for an ergodic measure or, in case you are not so happy with this example, with any dynamics that is uniquely ergodic (note that unique ergodicity is invariant under topological conjugacy, as is minimality). Moreover, many dynamics are not minimal but still have plenty dense orbits (example: any transitive hyperbolic toral automorphism has uncountably many dense orbits).
Better something along these lines:
Agreeing that the most basic objects of a dynamics are the smallest possible invariant objects, namely orbits, when an orbit is dense one could hope to be able to obtain all the information from the topological point of view from that single orbit.
Well, I wrote "hope". For example, to be dense doesn't say anything about the density of the orbit...