how to group by added columns in python

Question: Previously, I was able to read an excel file to python as a df which contains a column called date in the format like 2011-02-01, and I wanted to get the quarter of each date and group by the data in quarter. I have added a column to the df called quarter, but when I tried
df.groupby('quarter')

it showed me that
No axis named quarter for object type class'pandas.core.frame.DataFrame'

Is it possible to do the thing I wanted? Or is there any other way to do that?
Comment: Yes.  It's completely possible. You need to provide more information about what the dataframe looks like and the code you're using to add the quarter column.
Comment: Show your code and your excel file
Comment: @piRSquared The dataframe is a big one with thousands of rows of date and other information which I want to groupby. I first converted the date to a datetime type using         df['Date'] = pd.to_datetime(df['Date']), because it was as a series type originally. Then I got the month of each date by  df['month'] = df['Date'].dt.month,     and got the quarter by quarter=(month-1)//3+1 and just added it to the df by df['quarter']=quarter, and I just tried the code I posted above which did not work.
Comment: I think it would have been better to calculate your `quarter` column like this: `df['quarter'] = df['Date'].dt.quarter` and then maybe trying your groupby again.
Comment: @Jarad It works now, thanks!
Comment: @Jarad I didn't know the `dt` accessor had a `quarter` property, thanks.
Answer: Jarad's answer (in the comments) is very good, but here is another option, which avoids creating a new column:
df.groupby(pd.TimeGrouper(key='Date', freq='Q'))

This option is also more flexible. It can be used to group by non-standard periods, e.g. two weeks:
df.groupby(pd.TimeGrouper(key='Date', freq='2W'))

how to group by added columns in python


Is there a Node Reference URL Widget module for entity references?

Question: I just learned that there is the nice Node Reference URL Widget module.  Basically, it allows you to add a add a A link in the node view pages of content type B, if there is a node reference A->B.  When you are viewing a B node, clicking on the add a A link would lead you to the node creation page of content type A, with the A->B field populated with the B node you were just viewing.  That way the user do not need to figure out "which node B am I adding this node A to?"
Our sites mainly use Entity Reference.  Is there a module that provides similar functionality for entity references?
Answer: OK.  I see there is Entityreference prepopulate.

Allow prepopulating any Entity reference field via URL

Going to try it now.
Comment: Exactly what we are looking for.


CSS - Issue with inline-block and transform rotate

Question: I'm trying to build a menu where the link items run at a 90degress angle. You'll see what I mean when you load the codepen
https://codepen.io/anon/pen/BbQEPb
The problem I'm having is; even though I set the navigation drawer to "block" and the v-list items as "inline-block", They are still side by side, not stacking at that 90 degree angle down the navigation drawer.

This is the CSS I've used to try and emulate that menu, but I just can't get the stack right and my CSS skills aren't that grand yet.
<code>  .v-navigation-drawer .v-list {
    transform: rotate(90deg) translateX(-100%);
    transform-origin: bottom left;
    display: block;
  }
  .v-list > div {
    display: inline-block;
    vertical-align:  center;
    text-align: center;
  }
</code>
As usual, I more look for pointers in the right direction rather than completed answers, but happy with either.
Thanks in advance y'all.
Answer: It turns out the parent needed to be set with nowrap on whitespace
I was simply searching for the wrong answers.
This stackoverflow issue sorted me out Stacking Divs side by side in CSS


How to get name, price and image of product using EntityFieldQuery()

Question: Right now i am using this which doen't give me the name of the product or the price or location of the product image.
<code>    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'commerce_product');
    $query->fieldOrderBy('commerce_price', 'amount', 'ASC');
    $result = $query->execute();
</code>
which give this result
<code>Array
(
    [commerce_product] => Array
        (
            [56] => stdClass Object
                (
                    [product_id] => 56
                    [revision_id] => 55
                    [type] => product
                )

            [50] => stdClass Object
                (
                    [product_id] => 50
                    [revision_id] => 49
                    [type] => testcamera
                )

            [28] => stdClass Object
                (
                    [product_id] => 28
                    [revision_id] => 28
                    [type] => cameras
                )
</code>
etc...
Answer: You can now load the product using commerce_product_load($product_id); Then you will see price, fields, etc. You could also put all the product_id's in an array, then load them all with commerce_product_load_multiple($product_ids);
Comment: [Read the docs](https://www.drupal.org/node/1343708) @Krish. Your current approach of just trying to hack together a solution won't work with Drupal, it's too complicated. You need to understand how the entity API works to make any sense of Drupal Commerce, there's a steep learning curve ahead if you're just starting out with Drupal (trust me, been there, got the t-shirt)
Comment: Do i just pass the $result to commerce_product_load($result); or do i use the id from the array?


.val() is returning undefined .text returning random code

Question: I am having an issue with .val() returning an unexpected output I am using an item in an array to create a selector for an ID of a number input and then retrieving its value.
The input this should point to:

    Strength
    <input class="attributes form-control" id="strength" type="number" min="1" max="300" value="50" />

javaScript / jQuery:
$.each(traitName, function (key, value) {

if (binConv(traitRaces[key], 12).charAt(race) + binConv(traitClasses[key], 4).charAt(baseClass) == 00 && $("#startrunes li[data-value='" + key + "']").length <= 0) {

  $("#startrunes")
   .append($("

")
   .text(value));

}
else if (binConv(traitRaces[key], 12).charAt(race) + binConv(traitClasses[key], 4).charAt(baseClass) != 00) {

  $("#startrunes li[data-value='" + key + "']").remove();

}

if (traitRequireStat0[key].length > 0) {

  alert(parseInt($('#' + traitRequireStat1[key]).val(),10));
  if ($('#'+traitRequireStat0[key]).val() < traitRequireValue0[key]) {
      $("#startrunes li[data-value='" + key + "']").remove();
  }
}
else if (traitRequireStat1[key].length > 0) {
  if ($('#' + traitRequireStat1[key]).val() < traitRequireValue1[key]) {
      $("#startrunes li[data-value='" + key + "']").remove();
  }
}

The alert in the jQuery return NaN if I alert the val without parse int it comes back as undefined and if I use .text() I get random code all the arrays in here are equivalent length arrays and I don't believe them to be the root of the issue I am trying to get the current user input value not the value attribute.
Edit: forgot to mention the environment is jQuery / Bootstrap / jQuery UI
Comment: can you add a fiddle ?
Comment: http://www.tacticalkaos.com/CharacterBuilder.aspx I decided to provide the entire project here
Answer: You have to use .text() - cos it functions
Comment: @WilliamDunn ok, give me more html, cos u use key in each and i dont know what array do u have
Comment: coolm what i have to click or change? and what i have to get, but dont get cos of errors?
Comment: ack sorry the code I am concerned about activates after race and class are selected it will then activates the alert above
Comment: currently its set to parseInt so returns Nan instead of the value of the associated number input
Comment: so, u get trouble in getting value inside each, right?
Comment: yes when i try to get the val of the number inputed (strngth,dexterity) within that .each I get NaN,undefined, or random code if I use .text()
Comment: I tested the pointer if I have it just alert traitRequireStat1[key] it returns the proper point in the array
Comment: w8 a sec pls, u have a huge amount of code, its a little bit hard to understand all) i try now
Comment: traitRequireStat1 first - it is array woth only one  elem and other is "" - so thats why u get undefined, cos u have `$('#').val()`
Comment: u have to add a condition on if elem.exist
Comment: `if ($('#' + traitRequireStat1[key]).length > 0 ){
       alert(parseInt($('#' + traitRequireStat1[key]).val(),10));}`
Comment: @WilliamDunn i soon will be afk, try now pls
Comment: @WilliamDunn u have to check it not only in alert
Comment: ur trouble in select undefined element
Comment: this is odd because that value should always be greater than 0
Comment: if .length > 0 - it means that element exist and u can get .val() and .text()
Comment: u have empty elements in arrays - and its triggered erros
Comment: except if the element in the array is empty the alert shouldnt trigger its checking for it if (traitRequireStat0[key].length > 0)
Comment: so, its logical, cos it wierd - call methods for undefined


What is the meaning of upward and downward arrows on double note in guitar tab?

Question: It is a part of Desperado song guitar tab. I'm wondering what does upward and downward arrows mean in the guitar tab?
Comment: Which version is this from? Can't remember a triplet feel in Desperado.
Comment: @Tim I've found it on [youtube](https://m.youtube.com/watch?v=xywiN34xCzI)
Comment: It seems worth noting, that, though this definitely *means* upstroke/downstroke, the actual technique shown in the video doesn't do that.
Answer: The arrows show the direction of strum, either up or down. I find it a  little strange, though, as with a triplet pattern, I'd use down, up, down, followed by the second beat of up, down, up, to keep the strumming hand/arm in rhythm, which isn't shown in this version.
Comment: I believe the down/up is reversed, based on the string direction of the tab. Also, only first 2 of 3 triplets are being played.
Comment: @JohnBelzaguy - what I meant was playing as writ, the strumming hand would stutter, and not be as easy to keep in time as if there was a ghost strum on the 3rd triplets (which I just don't hear in Desperado).
Comment: Off the top of my head I don't recall any triplets in the song either. It also doesn't specify, Eagles or Linda version, both are very well known. I get your point but considering the slow tempo I would personally be inclined to do down-up to keep the consistency between beats. Either way would work fine, after all it's only a 2 note strum.
Comment: @JohnBelzaguy - turns out it's a red ferret - another Desperado..!
Answer: It means upstroke and downstroke. It wants you to mimic strumming the two notes. The flamenco feeling loves strumming anything from 2 to 6 notes at a time. It is very stylistically accurate.


Leitura de arquivo de texto

Question: Preciso lidar com um arquivo de 3 linhas com a seguinte estrutura:
nome
data
horário

Para fazer a leitura desse método eu uso os seguintes comandos:
BufferedReader leitor = new BufferedReader(new  FileReader("dados/dados.txt"));
ArrayList<String> dados = new ArrayList<>();

String linha = "";

while((linha = leitor.readLine()) != null){
    dados.add(linha);
}

leitor.close();

Mas ao final da leitura, a primeira linha do arquivo fica corrompida com o valor null, quando na verdade era pra continuar com o valor do campo nome.
Como posso fazer para ler o arquivo sem corrompê-lo?
Comment: Fiz exatamente o que você fez e o resultado saiu perfeito. Não parece ser problema de código. Tentou fazer alguma alteração?
Comment: Relacionada: [Como ler dados de arquivos txt usando Java?](http://pt.stackoverflow.com/questions/74972/como-ler-dados-de-arquivos-txt-usando-java?rq=1)
Comment: To achando que pode ser problema em um outro método que serve para verificar se o arquivo esta vazio ou não
Comment: nesse método eu uso File arquivo = new File("dados/dados.txt"); apenas
Comment: @Roney Se possível poste o código desse método.
Answer: Verifica o encoding que você está usando. Os problemas que ocorrem com arquivos corrompidos costumam ser relacionados com o encoding utilizado para a leitura do arquivo. Olha o seguinte post e faz a mudança indicada: https://stackoverflow.com/questions/12096844/how-to-read-a-file-in-java-with-specific-character-encoding.
Então no seu caso ficaria da seguinte forma:
String fileName = "dados/dados.txt";
FileInputStream is = new FileInputStream(fileName);
InputStreamReader isr = new InputStreamReader(is, Charset.forName("UTF-8"));
BufferedReader leitor = new BufferedReader(isr);
ArrayList<String> dados = new ArrayList<>();

String linha = "";

while((linha = leitor.readLine()) != null){
  dados.add(linha);
}

leitor.close();

Troca o charset UTF8 se estiver utilizando outro encoding na tua aplicação.
Comment: Apesar do link ser do SOEn, é recomendado que você adicione um exemplo aqui, para que o link seja apenas para referencia e consulta.
Comment: Entendi. Vou colocar então! Obrigada ^^

Leitura de arquivo de texto


How to stop Visual Studio 2017 v15.7.2 opening debugging session in a new browser window?

Question: Since installing Visual Studio update v15.7.2, clicking "Start Debugging" in a .Net project now opens a new browser window instead of adding a tab to my existing browser window, as it had done previously. How do I get this to stop and go back to adding a new tab to my existing browser window?
In my scenario; I have an existing browser window open (Google Chrome, my preferred development browser) and I click Debug > Start Debugging. Result: It opens a new Chrome window. Preferred Result: It adds a new tab to my existing browser window.
Note: This is not related to the option for "Enable JavaScript debugging for ASP.NET (Chrome, Edge and IE)" as previous posts point to separately - as this option is already disabled for me. I'm assuming there is another toggle somewhere that I don't know about and need to change.
Any help/pointers much appreciated.
Comment: https://blogs.msdn.microsoft.com/webdev/2016/11/21/client-side-debugging-of-asp-net-projects-in-google-chrome/
Comment: @EricHerlitz That link is for a similar but different problem that occurs when upgrading to VS2017. As pointed out in the original post.
Comment: Possible duplicate of [Visual Studio Do not open new browser instance](https://stackoverflow.com/questions/42095299/visual-studio-do-not-open-new-browser-instance)
Comment: @EricHerlitz thanks but that is not the issue I am having, which I stated in the question it's something different.
Comment: @TomGlenn 's answer below worked for me, thanks. I will accept this as the correct answer
Answer: I had the same issue since upgrading to 15.7.1+
The solution is a bit obscure and hidden in the Tools -> Options menu.
To revert to the previous functionality, where it opens a new debug session in an already open browser window with a new tab, do the following:
Tools -> Options -> Projects and Solutions -> Web Projects -> Untick Stop debugger when browser window is closed
Reference:
https://developercommunity.visualstudio.com/content/problem/247841/web-debug-always-opens-new-browser-window-and-clos.html
Answer: Go to list where you choose the browse with options(IIS Express,Web Browser,Browse with etc.),there is an option script debugging, disable it and you are done.
Comment: Thanks but this is not the problem I am having as I stated in the question - it is already disabled. Appreciate your help. Tomglenn's answer above worked for me.


Finding a Recurrence Relation and Solving

Question: I am trying to find a recurrence relation for the following question:
For integer n ≥ 1, let h(n) be the number of length $n$ words consisting of A's and B's, that contain either at least an "AA", or at least an "ABB". Find a recurrence relation satisfied by $h(n)$ (with necessary initial conditions) and solve it.
So far, what i have come up with is $h(n) = h(n-1)+h(n-2)+2^{n-2}+2^{n-3}$, but when I try to solve that, I end up with non-whole number eigenvalues. Then, I tried the recurrence relation $h(n) = h(n-1)+2^{n-2}+2^{n-3}$, but when I solved I did not get an equation that matched the cases I computed by hand. Is my recurrence relation wrong or or am I solving wrong? Any help would be great!
Answer: This is an odd problem, because it seems easier to find a closed expression for $h(n)$ than a recurrence relation!
After all, how many words of length $n$ don't meet the criteria?  Such a word can start with any number of B's.  But once it hits the first A, it needs to alternate between A and B to avoid having either an AA or an ABB.  Since there are $n+1$ words of length $n$ that satisfy that (starting with between $0$ and $n$ B's), $$h(0)=h(1)=0\\h(n)=2^n-n-1\quad\text{for }n\ge2$$

So, let's work backwards from here and come up with a recurrence relation.  To find $h(n+1)$, we can start with a term of $2h(n)$ since every $n$ letter word that contains an AA or an ABB can either have an A or a B at the end and will still obviously contain that substring.  So how many $n+1$ letter words wouldn't meet the criterion if you took off its last letter?  Just $n$ of them.  For instance, the six 7-letter words of that type are BBBBBAA, BBBBABB, BBBABAA, BBABABB, BABABAA, and ABABABB.  You can see how we formed them by thinking about the previous paragraph.  so a recurrence relation would be $$h(1)=0\\h(n+1)=2h(n)+n\quad\text{for }n\ge1$$
As to how you'd come up with that before the closed form, your guess is as good as mine.  ^_^
Comment: Why would it been avoiding AA and ABB?
Comment: If you follow an A with another A, then there's an AA, and if you follow that AB with a B, then you have an ABB.  So every A has to be followed by a B, and every B after you've seen an A has to be followed by an A.
Comment: Ok I understand that!  However, do you think there is a way to devise a recurrence relation rather than an expression?
Comment: @cpks18 I added a recurrence relation part to my solution.


Linearity Assumption in OLS with Dummy Variables

Question: Let's say that I have a continuous response variable and have constructed a regression model with multiple predictors. Most of my predictors are continuous but I have one which is a dummy variable. How do I determine whether the ols assumption of linearity is met or violated? And if the linearity assumption is violated, how would I go about transforming the dummy variable?
Comment: A dummy variable takes values of either 0 or 1. How might it exhibit a non-linear relation to the response?
Comment: Linearity does not mean that the independent variables have to be continuous; it merely means the response is a linear function of whatever values the independent variables happen to take on, as in, for example, the linear function $3x$, which makes no assumptions about $x$.
Comment: @amathew: I was starting to list the errors at that link, but there are too many - just forget about it & read something else.
Comment: @Scortchi If there were only a few errors on that page we should be generous in our assessment, because anyone who writes about stats is bound to make some mistakes. However, these errors seem like fundamental misconceptions. I am pleased that there are already good answers on CV dealing with them all. The writer claims he "[has gained statistical and mythological experience](http://www.dissertationwritingservices.info/statisticssolutions-com-review/)" in helping people write their dissertations. As always on the Web, *caveat lector,* especially when mixing mythology and stats. :-)
Comment: @whuber The use of that quoted phrase would be enough to make me wary of the rest of what they said. Even before reading the particulars in this case, statements of such a flavor are usually associated with deep misunderstandings.
Comment: so in the following link, where the author claims that "linear regression needs the relationship between the independent and dependent variables to be linear"....that's wrong? or am I thinking about relationship wrong by assuming it's meaning a 'scatterplot' relationship. http://www.statisticssolutions.com/academic-solutions/resources/directory-of-statistical-analyses/assumptions-of-linear-regression/
Comment: I see that error mentioned over and over at so many places. So linearity is not about the relationship, just that 'the linear function makes no assumption about x," meaning that we don't make any transformations to x. Of course, there are times when we can transform data (3x^2) and use that within a linear model. The question then becomes, when is a 'linear model' inadaquete and when is it appropriate to use data transformation or a nonlinear model.
Comment: @whuber: Fundamental mythconceptions, perhaps.
Answer: It's time this question had an answer.

I have a continuous response variable and a regression model with multiple predictors.
  Most of my predictors are continuous

The continuity or otherwise of a predictor makes no no difference; a relationship only need be linear over the domain where it's defined, for it to be linear.

How do I determine whether the ols assumption of linearity is met or violated?

With a 0/1 predictor ($d$ say) there are only two possibilities:
$E(Y|d=0,\underline{x})=E(Y|d=1,\underline{x})$ or
$E(Y|d=0,\underline{x})\neq E(Y|d=1,\underline{x})$
(where $\underline{x}$ is all the other predictors)
In either case, linearity is satisfied, since the population coefficient is the difference in expected values in either case.
It is, however, possible that the size of the coefficient depends on the values of the other parameters (i.e. interactions between other predictors and $d$ may exist).

And if the linearity assumption is violated, how would I go about transforming the dummy variable?

The (conditional) linearity of $y$ in terms of $d$ is automatically satisfied -
fortunately, because we can't gain anything substantive here by attempting to transform a dummy.
Answer: Adding to the excellent answer by Glen_b, it is worth noting that adding a categorical variable into a regression cannot cause problems with the linearity assumption in and of itself.  If there is an issue of non-linearity in the model it will be due to some underlying relationship that is not captured by the specified model form, but the inclusion of a categorical variable cannot cause this problem.
To see this more clearly, consider a regression with an arbitrary categorical variable $h = 1,...,k$ with $k \geqslant 2$ possible outcomes, and one or more other terms encapsulated by $\mathbf{x}.$  The categorical variable is represented in a regression by $k-1$ indicator variables for the categories (with one base category).  So the relevant terms in the model will look like this:
$$\mathbb{E}(Y_i|\mathbf{x},\mathbf{h}) = \beta_0 + \sum_{\ell=1}^{k-1} \beta_\ell \cdot \mathbb{I}(h_i = \ell) + \text{Other terms for } x_i.$$
The linearity assumption in regression requires that the regression equation be linear with respect to the coefficient parameters.  The presence of the categorical variable $h$ in the regression adds the parameters $\beta_1,...\beta_{k-1}$ which apply across categories.  Each individual indicator term simply adds a term for the difference in conditional expectation based on whether the categorical variable is in that category or not.  The addition of such terms cannot constitute non-linearity.


Define object's prototype in a self executing function

Question: Im building an app's namespace and it looks as follows:
var app = {};
app.utils = {
    Expiration: function(){ // contructor
    ... // some code
    },
    init: (function(){
         app.utils.Expiration.prototype = function(){....}
      ())
};

But i get an error: TypeError: Cannot read property 'Expiration' of undefined, and actually it's true beacuse utils is still being defined, i know that i can define the prototype outside of the scope of app, my question is: can i define it inside app or app.utils with an self executing function or by any other means, thanks.
Comment: @user1108631 - well, Javascript doesn't allow it so you can't do it regardless of how much you want it to look that way.   `app.utils` is not properly defined until AFTER that entire declaration is done.
Comment: Why exactly don't you want to just define it after the initial `util` definition?
Comment: It might sound pedantic, but i just want my code to look complex.
Comment: that's what i mentioned at the END on my post, but it's always good to read what others have to say about the internals of js.
Answer: You cannot refer to your own variable name in a static declaration.  Your self executing function causes that to get evaluated and executed at the time of static declaration.  Thus app.utils is not yet defined.
There are alternatives.  Here's one:
var app = {};
app.utils = {};

app.utils.Expiration = function(){ // contructor
    ... // some code
    };

app.utils.Expiration.prototype = ...

Since each of these successive statements adds another level of properties, these statements have to get executed one after the other in this order so that the previous elements are in place.


Why are $M$-estimators NOT scale equivariant?

Question: Consider the following location model.
$$
x_i = \mu + u_i, (i = 1,\dots, n),
$$
where $u_i$ are $i.i.d.$ with density function $f_0$. Hence, $x_i$ are $i.i.d.$ with density function $f_0(x-\mu)$. It usually is of interest to find the estimator of $\mu$. One example is the Maximum-Likelihood-Estimator for location $\mu$ (a special case of $M$-estimators) defined to be
$$
\widehat\mu(x_1, \dots, x_n) := \arg\min_\mu \sum_{i=1}^n -\log f_0(x_i-\mu).
$$
I read that this estimator is NOT scale equivariant. That is, for a constant $c \in \mathbb R$,
$$
\widehat\mu(cx_1, \dots, cx_n) \neq c\widehat\mu(x_1, \dots, x_n). (*)
$$
I do not understand why this is the case. I suppose whether $(*)$ holds depends on the function $f_0$, doesn't it? Could anyone explain this to me, please? Some examples are appreciated. Thank you!
UPDATE: As an example, let us consider the normal distribution $N(\mu, \sigma^2)$. The MLE for $\mu$ (regardless of $\sigma$) can be shown to be
$$
\widehat\mu(x_1, \dots, x_n) = \frac{\sum_{i=1}^n x_i}{n}.
$$
Moreover, if you multiply $x_i$ by $c$, it is also straightforward to show that
$$
\widehat\mu(cx_1, \dots, cx_n) = \frac{c\sum_{i=1}^n x_i}{n} = c\widehat\mu(x_1, \dots, x_n).
$$
Also note in all these derivation one did not use $\sigma$ at all. Hence, I do NOT see why this estimator is not scale equivariant. Could anyone give me an example where the identity
$$
\widehat\mu(cx_1, \dots, cx_n) = c\widehat\mu(x_1, \dots, x_n).
$$
does NOT hold, please? Thank you!
Answer: Because you need it to be a location and scale family, $f((x - \mu)/\sigma)$, and the estimation method needs to include a data-based estimator of $\sigma$ as well, e.g., the MAD (median absolute deviation of the residuals). For most but the normal case, the scale parameter makes a difference in the log of the PDF.
Addendum
Now that I'm not distracted by the NCAA basketball finals, I can add some info, hopefully with fewer mistakes...
The next step (for sufficiently smooth densities) in the question's second displayed equation (with $\arg\min$) is to differentiate and set to zero -- i.e., solve the equation
$$ \sum \psi(x_i - \hat\mu) = 0 $$
where $\psi(z) = -\frac d{dz}\log f_0(z)$. If $f_0(z) = \frac1{\sqrt{2\pi}\sigma}\exp\{-\frac12z^2/\sigma^2\}$, we have $-\log f_0(z) = \frac12\log(2\pi)  + \log\sigma + \frac12z^2/\sigma^2$, so that $\psi(z) = z/\sigma^2$ -- and thus we need to solve the equation $\frac1{\sigma^2}\sum(x_i-\hat\mu) = 0$. Since $\sigma\ne0$, we can just canel it out and it plays no further role in solving the equation. Thus, $\hat\mu$ is scale equivariant.
However, suppose that $f_0(z) = \frac1{\pi\sigma}\cdot\frac1{1 + z^2/\sigma^2}$ is the Cauchy density with $\sigma$ as a scale parameter. We now have $-\log f_0(z) = \log{\pi\sigma} + \log(1+z^2/\sigma^2)$, so that $\psi(z) = \frac{2z/\sigma^2}{1+z^2/\sigma^2} = \frac{2z}{\sigma^2+z^2}$. In solving $\sum\psi(x_i-\hat\mu)=0$, $\sigma$ does not cancel out, and its value affects the solution! That is, with this Cauchy density as the basis for the M estimation, $\hat\mu$ is not scale equivariant.
The gist of my original answer is that you should include the scale parameter in the equation:
$$ \sum \psi\{(x_i - \hat\mu)/\hat\sigma\} = 0 $$
The classic M-estimation example uses Huber's $\psi(z) = \mbox{sign}(z)\cdot\min(|z|,c)$ where $c$ is a suitable tuning constant, e.g. 1.34. A popular scale estimator is $\hat\sigma = \mbox{med}|x_i - \mbox{med}_j\{x_j\}|$, then obtain $\hat\mu$ as an iteratively weighted mean with weights $w_i = \frac{\psi\{(x_i - \hat\mu)/\hat\sigma\}}{(x_i - \hat\mu)/\hat\sigma}$ (and with, say, the median as the initial $\hat\mu$. This estimator is scale equivariant because $\hat\sigma$ is scaled proportionally to the scale of the data.
Comment: Thanks. However, I still do not get what you are saying (see my update). Could you give an specific example to illustrate your point, please?
Comment: Here are some examples for you to try. Let f(x) be the normal density and g(x) be the Cauchy density. Look at graphs of log f(x), log f(2x), log g(x), and log g(2x). Note that the first two are identical except for an additive constant, hence minimizing the sum of several such things will give the same weights to each term regardless of scale. But that is not true for the Cauchy ones.
Comment: In the normal case, we have $\log f(x) = -\log\sqrt{2\pi} - \log\sigma -\frac{(x-\mu)^2}{2\sigma^2}$ and $\log f(2x) = -\log\sqrt{2\pi} - \log\sigma -\frac{(2x-\mu)^2}{2\sigma^2}$. Unlike what you suggested above, the difference between the two are not an additive constant. Sorry, but I still do not see your point.
Comment: Just take mu equal to zero and it'll be clearer. Or if you want nonzero mean, you need to put in 2mu also, since you're asking about EQUIvariance not INvariance.
Comment: Take $\mu=0$ we have $\log f(x) = -\log\sqrt{2\pi} - \log\sigma - \frac{x^2}{2\sigma^2}$ and $\log f(2x) = -\log\sqrt{2\pi} - \log\sigma - \frac{2x^2}{\sigma^2}$. Still the difference is NOT additive. Moreover, we are trying to estimate $\mu$. How can you take it to be 0, please? Sorry if my stupidity offends you.
Comment: You're right, I was wrong when I said there'd be only an additive difference. But note that minimizing 2 times something is the same as minimizing one times something. I was not trying to give a complete solution. I was trying to point you in the right direction. Look at the Cauchy part too.
Comment: On the mu=0 point -- looking at the log density at mu of zero gives you a picture of the influence of a data value on a location estimate, in terms of how far it deviates from the current estimate. But you're right, it wasn't that obvious the way I stated it originally. Also, keep in mind that, at least for smooth cases, you'll do the minimization by differentiating and setting to zero. So the additive sigma cancels and the multiplicative sigma doesn't make a difference when you're equating to zero.
Comment: @20826, I added *a lot* of material to my answer. Apologies for bungling my quick responses earlier.


WooCommerce - How to assign category to user/user-role (WordPress)

Question: I am trying to build a WooCommerce B2B shop with products which are specific to customers/ user roles. E.g. I have some products in a category named "ABC Shop" and an user role named "ABC Shop". I want that only users with the role "ABC Shop" can see products in this category.
Furthermore, I would like to have "normal" categories (which are not user-specific) like "T-shirts", "Pants" and so on. Lets say I do put "T-shirt 1" into categories "ABC Shop" and "T-shirts". Users with the role "ABC Shop" should see this product in "T-shirts"-category.
In short: I would like to build custom shops which are user-role specific.
I prefer solutions with plugins rather than custom code.
Thanks in advance!
Answer: As I have understood, you need vendors for your website products.
WooCommerce already have such a plugin.


what is a portable way in Ruby to check where STDIN will block if you attempt to read from it?

Question: I would like to find out if there is a portable way to check in a Ruby script whether it will block if it attempts to read from STDIN. The following is an approach that works for Unix (and Cygwin) but not native Win32. (It is based on a Perl approach I learned long ago.)

$ cat read-stdin.rb
<code>#! /usr/bin/ruby
# test of reading from STDIN

require 'fcntl'

# Trace info on input objects
$stdout.sync=TRUE if $DEBUG         # make sure standard output and error synchronized
$stderr.print "ARGV=#{ARGV}\n" if $DEBUG
$stderr.print "ARGF=#{ARGF}\n" if $DEBUG

# See if input available, showing usage statement if not
blocking_stdin = FALSE
if (defined? Fcntl::F_GETFL) then
  $stderr.print "F_GETFL=#{Fcntl::F_GETFL} O_RDWR=#{Fcntl::O_RDWR}\n" if $DEBUG
  flags = STDIN.fcntl(Fcntl::F_GETFL, 0)
  $stderr.print "flags=#{flags}\n" if $DEBUG
  blocking_stdin = TRUE if ((flags & Fcntl::O_RDWR) == Fcntl::O_RDWR)
  $stderr.print "blocking_stdin=#{blocking_stdin}\n" if $DEBUG
end
if (blocking_stdin && (ARGV.length == 0)) then
  $stderr.print "usage: #{$0} [-]\n"
  Process.exit
end

# Read input and output it
$stderr.print "Input:\n" if $DEBUG
input_text = ARGF.read()
$stderr.print "Output:\n" if $DEBUG
print "#{input_text}\n"
</code>
Here is the interaction without debugging:
<code>$ grep -v DEBUG read-stdin.rb >| /tmp/simple-read-stdin.rb

$ echo hey | ruby /tmp/simple-read-stdin.rb
hey

$ ruby /tmp/simple-read-stdin.rb
usage: /tmp/simple-read-stdin.rb [-]
</code>
Here is the interaction with debugging:
<code>$ echo hey | ruby -d read-stdin.rb
ARGV=
ARGF=ARGF
F_GETFL=3 O_RDWR=2
flags=65536
blocking_stdin=false
Input:
Output:
hey

$ ruby -d read-stdin.rb
ARGV=
ARGF=ARGF
F_GETFL=3 O_RDWR=2
flags=98306
blocking_stdin=true
usage: read-stdin.rb [-]
</code>
Comment: A very portable way would be to spawn a thread that reads from STDIN (blocking) and writes the data to some kind of buffer, for another thread to use.
Comment: The portable `select` layer also works on Win32.
Comment: Ruby does have a portable layer over the UNIX `select`.
Answer: I don't know if it is universally portable and I also don't know if it is considered a good idea (blocking isn't such a bad concept) but there is a non-blocking read method in IO. You can use it like this:
<code>chunk = nil

begin
  chunk = STDIN.read_nonblock(4096)
rescue Errno::EAGAIN
  # Handle the case if it would block
  chunk = 'nothing there...'
end
</code>
Though, I think it's quite disappointing it doesn't work without specifying a buffer size like IO#read does it, but working around this by using a loop should be quite easy.
Comment: ARGF also has a [read_nonblock](https://ruby-doc.org/core-2.5.2/ARGF.html#method-i-read_nonblock) method now


how to trigger a custom event in jQuery, pass the original event and data

Question: I want to trigger a custom event from within a handler of a native event, under these conditions:

I want the handler to be in the form: function(event, arg) {}, where
event is the original, native event (so I can call event.preventDefault())
arg is some data passed by the triggerer (in the handler of the original event)
both events (native & custom) are fired on the same element (so the custom event's name must differ from the original one)

I was able to get this far, and it works:
// the native event handler
function(event) {
    var e = jQuery.Event("some_custom_event");
    e.foo = 'bar';
    $(this).trigger(e);
    if (e.isDefaultPrevented())
        event.preventDefault();
}

// an example handler for the custom event:
function (event) {
    alert(event.foo);
    event.preventDefault();
}

I guess I could also pass both the original event and foo as arguments to the custom event handler, but I wanted a standard-looking handler (no function(event1,event2).
Can what I want be done (easily) in jQuery?
Answer: The trigger function has 2 flavors and I think you should look at the first one which is of syntax .trigger( eventType [, extraParameters] ).
If I understood correctly then what you need to do is you pass foo as additional param like below,
$(this).trigger(e, [foo]);

An example from jQuery documentation is below,
$("p").click( function (event, a, b) {
// when a normal click fires, a and b are undefined
// for a trigger like below a refers to "foo" and b refers to "bar"

} ).trigger("click", ["foo", "bar"]);

From documentation.. read more..

.trigger( eventType [, extraParameters] )
eventTypeA string containing a JavaScript event type, such as click or
  submit.
extraParametersAdditional parameters to pass along to the event
  handler.
Comment: @ori You can try passing the arg inside another array like `$(this).trigger(custom, [[event, foo]]);` and the custom event will be like `$('a').bind('some_custom_event', function (e, args)` and then you can access original event in `args[0]` and `foo` in args[1]. [See DEMO](http://jsfiddle.net/skram/FUnDB/)
Comment: Your solution doen't allow me to `preventDefault` on the original event in the custom event handler.
Comment: @ori Can you elaborate what is supposed to happen on `preventDefault`? Was it working in first place? I am sure adding addition param is not going to impact anything which was working before.
Comment: First, I made a mistake in the code above, have a look. I'm building a jQuery plugin which I want to be able to interact with. Maybe some kind of callback is a better fit. Basically I wanted a custom event handler that can refer to the original event (can call `preventDefault` on it), but if I do `.trigger('custom', [event, foo])` then the handler becomes `function(c_event, n_event, foo) {}` which looked ugly.
Comment: It's new code I wrote. There was no code before. The code I presented in my question works, but I wanted to see if some jQuery genius can improve it. I'm also using the first `.trigger` flavor elsewhere, but using it here means that (in order to meet my requirements) the handler would need to get 2 events as arguments, which I don't like (and therefore never implemented). Maybe I'm asking for too much... the first handler argument is always the fired event, so maybe my original solution can't be improved...
Answer: You can create a JQuery Event ($.Event), which will allow for event.preventDefault() and will be honored or picked up after the event was triggered.
So, first create the event in your plugin/function, and trigger it:
var event = $.Event('myCustomEvent');
$element.triggerHandler(event, [extraData1, extraData2]);

Then in your handler, you can get access to your extraData and preventDefault;
$('#someElement').on('myCustomEvent', function (event, extraData1, extraData2) {
   event.preventDefault();
};

Now you can check after the original trigger to see of default was prevented;
var event = $.Event('myCustomEvent');
$element.triggerHandler(event, [extraData1, extraData2]);

if (event.isDefaultPrevented()) {
    /* Do something as user prevented default */
} else {
    /* Proceed as normal */
};


Clinet doesn't stop sending messages even server is stopped

Question: I am working on a Java client which has to send a heartbeat message periodically. I am using java.net.Socket for the work. My issue is even the server is stopped client goes on sending the messages without giving any exception.
I read various stackoverflow questions on this, but couldn't find the answer for my implementation. I read that only way to find the server is up is to try sending a message. But here I don't see a way to do this.
Here's how my class looks like.
public void process() {
    sendMessage(HEART_BEAT);
}

public void start(String serverAddress, int port) throws IOException {
    this.serverAddress = serverAddress;
    this.port = port;
    if (isConnected) {
        LOGGER.info("Already connected");
        return;
    }

    socket = new Socket(serverAddress, port);

    out = new PrintWriter(socket.getOutputStream(), true);
}

private void sendMessage(String message) {
     out.print(message + END_OF_MESSAGE);
     out.println();
}

Here's how my main method looks like,
public static void main(String args[]) {
     Client client=new Client();
     client.start("127.0.0.1", 900);
     while(true) {
          client.process();
          Thread.sleep(2000);
     }
}

How to implement a way to identify when the server has stopped?
Comment: @ScaryWombat You mean `BufferedWriter`, surely? A `ByteArrayOutputStream` doesn't have anything to do with a socket.
Comment: @EJP, Yes you are right, I did not mean `ByteArrayOutputStream` - actually I was meaning to suggest that the OP writing directly to the `OutputStream`
Answer: The PrintWriter may not throw any exception when wirting but that doesn't mean that it hasn't run on an error. The underlying OutputStream still throws an exception but it's internally caught and processed.
The original exception and information may be lost but the PrintWriter#checkError() method does still provide a way to check if something went wrong.
My suggestion is, to add some lines of code to your sendMessage method:
private void sendMessage(String message) {
     out.print(message + END_OF_MESSAGE);
     out.println();
     if ( pw.checkError() )
     {
       LOG.log( Level.SEVERE, "An error occured while writing.");
       shutdown(); // <-- method with the shutdown logic (e.g. closing the socket).
     }
}

Note that the PrintWriter#checkError() method also flushes the stream before checking if an error occured.
It may not be the most elegant solution but it get's the job done.
Answer: Lose the PrintWriter. It swallows exceptions. Use a BufferedWriter, which doesn't. When you get an exception when sending, close the socket.


When default virtualhost is not available, no virtual hosts are available

Question: I have an apache2 instance proxying requests to several tomcat instances.  Sometimes the default virtualhost is unresponsive (tomcat is running but app not responding).  When this happens none of the other virtual hosts are reachable through apache but they work going direct to tomcat.  Should the default virtualhost be a static page to avoid this issue?  Any other settings to tell apache to skip the default if it doesn't respond?
In the httpd.conf there are proxypass/proxypassreverse statements outside of the virutalhost blocks.  I don't know if that could be confusing things.
ProxyPass /test/ ajp://localhost:9009/test/
ProxyPassReverse /test/ ajp://localhost:9009/test/
ProxyPass /test2/ ajp:/localhost/9010/test2/
ProxyPassRevers /test2/ ajp://localhost:9010/test2/

NameVirtualHost 192.168.0.1:80

<VirtualHost 192.168.0.1:80>
    ServerName test.domain.com
    ProxyPreserveHost On
    ProxyRequests Off

...

ProxyPass / ajp://localhost:9009/test/
ProxyPassReverse / ajp://localhost:9009/test/

...
</VirtualHost>

<VirtualHost 192.168.0.1:80>
ServerName test2.domain.com
ServerAlias test2
PorxyPreseveHost On
ProxyRequests Off
...

ProxyPass / ajp://localhost:9010/test2/
ProxyPassReverse / ajp://localhost:9010/test2/

...

</VirtualHost>

OS: Solaris 10
Apache version: Apache 2.2.6
Modules: mod_proxy_ajp
Comment: possible duplicate of [How to setup a tomcat virtualhost without webapps?](http://stackoverflow.com/questions/9342767/how-to-setup-a-tomcat-virtualhost-without-webapps)
Answer: It would be suggested to put ProxyPass parameters in VirtualHost section only.


Custom items in ListView control

Question: I need to add a custom item to ListView control (like on the picture), is it possible ? And if it is, what's the best way to do it ?
Comment: Every one get your Magic 8-Ball to know what he means by "custom item"; Could you explain what do you want?
Answer: I don't know that this is possible with Winforms.  The list items in a System.Windows.Forms.ListView are System.Windows.Forms.ListViewItem objects contained in a strongly typed collection.
You could try to create a subclass of ListViewItem, but since that class inherits directly from System.Object and is not an actual windows form control, you might be borrowing trouble, as you would need to replicate all the functionality of the inheritance chain of an actual control.
Now, if you are not too far in to the project, you might consider looking at switching to WPF.  The ListView in WPF uses controls as items, so you could easily create a usercontrol that you would use as your list items.
You might be able to find a control library with a control that gives you the functionality you want, but for the most part, the good libraries are commercial and can be prohibitively expensive for small shops and individual .
I did a quick google search for any library that offers this capability, but I could not find one that displayed custom controls.
Answer: Not sure if this is what you're after, but ListViewItems have a Tag property that can store custom data about each item.
http://msdn.microsoft.com/en-us/library/system.windows.forms.listviewitem.tag.aspx
Answer: How about creating a user control.  In that case you can set your user controls at the top and listview below.  Or are you trying to add these controls as items in the listview itself?

Custom items in ListView control


VoltDB does not support transaction?

Question: I am connecting to a VoltDB enterprise instance through JDBC driver. However, it does not support transaction control. When I call conn.commit or rollback, it throwsjava.sql.SQLFeatureNotSupportedException.
Any thoughts?
Answer: VoltDB executes ACID transactions but does not support external transaction control.  Multi-statement transactions can be implemented as java stored procedures.  Each call/request to the database is essentially one ACID transaction.
There are a number of reasons for this, which have to do with the architecture of VoltDB, and what makes it so fast and scalable.  It is perhaps best explained in this video by John Hugg, one of the founding engineers at VoltDB.
http://voltdb.com/resources/video/h-storevoltdb-architecture-vs-cep-systems-and-newer-streaming-architectures
Comment: Thank you very much Ben. I have much more clear understanding now. I am watching those videos now. They are great also.
Comment: Because my existing program is a generic implementation for all JDBC kind of drivers, any way to by pass the exception thrown from the VoltDB Driver?
Comment: Hi David, you can set autocommit on, which is the default and only supported setting for the driver.  I don't know if that would do anything in your application to avoid calling commit or rollback, but essentially those commands should not be called.

VoltDB does not support transaction?


C++ Variable unexpectedly overwritten

Question: I just don't get it. I have two "main variables" address and parameters.
address is a const char and parameters is a string (char array) which is getting filled with information.
Now the very weird thing (at least for me): After adding the timestamp to parameters, address is cleared!
Does someone have an idea why?
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

void setOnlineStatus(char deviceName[30], bool deviceStatus, const char outputContent[50], int value, const char functionName[30], bool onlineRequestImmediately=true) {

    const char address[128]= "https://api.arduino.myAddress.com";
    char parameters[512] = "?secret=w2q3oifin23c&deviceName=";

cout << "Address: ";
cout << address << endl;

cout << "Parameters: ";
cout << parameters << endl;

    strcat(parameters, deviceName);// +"&status="+deviceStatus+"&value="+value+"&time="+getTimestamp()+"&functionName="+functionName;
    strcat(parameters, "&status=");

    char tempDeviceStatus[1];
    sprintf(tempDeviceStatus, "%d", deviceStatus);
    strcat(parameters, tempDeviceStatus);

cout << "Address01: ";
cout << address << endl;

    strcat(parameters, "&value=");
    char tempValue[3];
    sprintf(tempValue, "%d", (int) value);
    strcat(parameters, tempValue);

cout << "Address02: ";
cout << address << endl;

    strcat(parameters, "&functionName=");
    strcat(parameters, functionName);

cout << "Address03: ";
cout << address << endl;

    strcat(parameters, "&time=");
    char tempTimestamp[10];
    sprintf(tempTimestamp, "%d", 1234567890);
    strcat(parameters, tempTimestamp); // this doesn't work neither: strcat(parameters, "1234567890");

cout << "Address04: ";
cout << address << endl; // output is empty!

    strcat(parameters, "&message=");
    strcat(parameters, outputContent);

cout << "Address05: ";
cout << address << endl; // output is empty!

cout << "Parameters: ";
cout << parameters << endl;

  }

int main()
{
    setOnlineStatus("testControl1", 1, "controlTurnedOff", 201, "toggleFans()");
}

That's the output:
Address: https://api.arduino.myAddress.com
Parameters: ?secret=w2q3oifin23c&deviceName=
Address01: https://api.arduino.myAddress.com
Address02: https://api.arduino.myAddress.com
Address03: https://api.arduino.myAddress.com
Address04:
Address05:
Parameters: ?secret=w2q3oifin23c&deviceName=testControl1&status=1&value=201&functionName=toggleFans()&time=1234567890&message=controlTurnedOff

Comment: This is what happens when you write C code and only pretend that it's C++. Specifically, this is what happens when you use fixed-size `char` arrays, instead of `std::string`s, and use legacy C library functions like `strcat`, `sprintf`. It's very easy to make one of them too small, and one of your `char` buffers is too small, and you scribble over innocent memory, while writing to it. It will take you some time to find which one it is, unless someone takes pity and points it out to you. And after that, perhaps you will want to consider actually writing C++, instead of C?
Comment: Where do you check if you write out of bounds? Why do you use C strings instead of `std::string`. This would fix your problem.
Comment: @Eljay the arduino comes with this programming language (which is some kind of subset of C++) , there is not much option
Comment: @ThomasSablik The `std::string` will use memory from the free store whereas a char array uses automatic storage,  so if the system has a small (or nonexistent) free store but has enough automatic storage for the task, then the automatic storage may be preferable
Comment: @Thomas Sablik -- The reasons for not using Strings on an Arduino are well documented.  It's a pretty big deal int he Arduino world.  It has to do with there not being any garbage collection to clean up the heap and continued use of strings that change sizes eventually leads to a crash once the heap is so full of holes that it can't find room to store the new string.  Arduino doesn't deal well with dynamic allocation if you aren't extremely careful.  If you aren't familiar with the limitations of the Arduino platform then perhaps you should pull back a little on Arduino questions.
Comment: There is also the issue of the additional code overhead involved on such tiny microcontrollers.
Comment: OK.  But they also lead to unstable code and that's well documented.  So when a user wants to try to learn to avoid them that's a great next step.  And we shouldn't discourage it.  And we certainly shouldn't claim that it is somehow better.  Sure the user reserves enough memory for the largest string possible, but Arduino is a one program machine.  So what exactly are they going to use the saved memory for if there is a possibility later that they might need it for a larger string?  In that case what's the harm in having it reserved already?
Comment: you could save yourself a lot of grief by using std::string instead of C-style char arrays
Comment: You overflow buffer of `tempDeviceStatus` .  Also even using sprintf instead of all those strcats would be a whole lot better
Comment: Because I'm actually on an Arduino and because of the limited memory I don't want to use `string` class there. I learned not to use it there.
Comment: Following that same line of (mis)reasoning, then why are you using C++ at all?
Comment: Then you should use `strncat` and `snprintf`. Why do you create a char array of length 1: `char tempDeviceStatus[1];`? That's one char. That should probably be `char tempDeviceStatus[2];`
Comment: @M.M: I don't see the error? Why is there an overflow, how big should `tempDeviceStatus` be if 2 is not enough?
Comment: @alve89 it has size 1, not 2
Comment: @M.M: Still, even with `char tempDeviceStatus[2];` I have an empty output later at address04.
Comment: Why do you think that `std::string` uses more memory than a C string? With C strings you have to remember the current size. You have to add error handling for cases where the C strings get to long. You are allocating memory for 512 chars to avoid std::string and to save memory?
Comment: From my point of view that's the point: An Arduino has very limited memory so I need to control that and cannot allow everything. That's what I learned... It's interesting that you say the opposite? I am a newbie so I don't know it better. :-(
Comment: @M.M. IMHO this is optimization and should only be done if there is a bottleneck. OP is a beginner and doesn't write time or memory critical applications. Arduino provides the string class and it is okay to use it to start learning.
Comment: @Delta_G I started learning C++ with a simple Hello World program and I learned step by step. Usage of C strings can be complex. Starting with a String class makes it easier.
Answer: This pile of buffers and strcats and stuff is waaaaaay more complicated than it needs to be
Instead you can do this:
char parameters[512];
int result = snprintf(parameters, sizeof parameters,
     "?secret=w2q3oifin23c"
     "&deviceName=%s"
     "&status=%d"
     "&value=%d"
     "&functionName=%s"
     "&time=%d"
     "&message=%s",
    deviceName, (int)deviceStatus, value, functionName, 1234567890, outputContent);

if ( result < 0 || result >= sizeof parameters )
     // ...handle error or overflow

In the original code you overflow tempDeviceStatus which has size 1 but you write two characters to it (0 or 1 plus a null terminator) and you also overflow tempValue
Comment: I will try your code but please have a look at this: https://www.onlinegdb.com/Hkg5ohB2YL -> I already increased `tempDeviceStatus` to 2 and the problem is still the same.
Comment: @alve89 you also overflow `tempValue`
Comment: Alright, I always forgot the null terminator. Damn. Many thanks to you! And thanks for your code example, I will study that, I never used that function before.
Comment: Also, don't forget to URL-encode those querystring parameters

C++ Variable unexpectedly overwritten


Is there a "pure R" implementation for loess? (with no C code?)

Question: I would like to manipulate the work done by the loess function in R.  However, the main workhorse of this function is written in C.  Is there some pure R implementation of the code?
Thanks.
Comment: I doubt it... LOESS is built-in in most statistical programs, thus there is no practical need to do that. what precisely do you want to alter inside?
Comment: For one thing, I would like to be able to get the parameters of the function for a particular point.
Answer: The loess.demo function in the TeachingDemos package replicates some of the internals in plain R (it also uses the built in C code version).  You could use that function as a starting place depending on what you want to do.
Comment: Greg, if I understand my poking, it seems that you are using the base loess to fit the original line, but then create an estimate per point according to the user's action (clicking) on the plot.  It will need some tweaking, but it is a good starting point - thank you very much.
Comment: Yes, that is exactly right, so you could take the per point part and build on that.  You will also want to ask yourself if you want the pure loess (the more wiggly line) or the spline smoothed version.


What is a software prefetcher (processor setting)?

Question: Configuring the CPU(s) of a DELL Server (via iDrac) I have run accross the term software prefetcher.

I have an idea that prefetching is to "fetch" in advance data from the memory in to the CPUs' cache(s). I understand that this can be realized via Hardware. I do not understand the "software" here. What would switching enabled/disabled for the "Software Prefetcher" change?
Answer: I would suggest reading the manual for your server model. For example, according to this article:
https://www.dell.com/support/kbdoc/en-uk/000176921/bios-characterization-for-hpc-with-intel-cascade-lake-processors?lwp=rt

Software Prefetcher: [...] when set enabled, the processor will prefetch extra cache line for every memory request.


Firing Android Activity methods on separate process at set interval

Question: I am writing my first Android app(allot of fun so far!) and have run into a roadblock. I am using SDK tools 21.1 targeting Android 4.2.
I am trying to set up a system that allows activities to register to invoke a method at set time intervals. I wanted to do this in such a way that the processing of the request would be handled on it's own process to avoid making the UI unresponsive.
I have been doing some reading and have explored a few avenues. First I thought that  a service would be a good way to go about this but found a fair bit of information suggesting that was not a good course of action due to the OS being able to kill services indiscriminately.
I'm now looking at using a ScheduledThreadPoolExecutor. I've created this simple class with an overridable method to pass methods around:
public abstract class BaseEvent implements EventListener {

    public abstract void onFire(Object... params);
}

I've created this runnable task invoke the method:
public class HeartBeatTask implements Runnable {

    private BaseEvent mCallback;

    private Object mParams;

    public HeartBeatTask(BaseAioEvent callback,Object... params){
        mParams = params;

        mCallback = callback;
    }

    @Override
    public void run() {
        Log.d(LOG_TAG,"Run called");
        if(mCallback != null)
        {
            mCallback.onEvent(mParams);
        }

    }
}

I'm going to use it like this (inside an Activity)
      ScheduledThreadPoolExecutor threadPool = new ScheduledThreadPoolExecutor(1);
      BaseEvent callback = new BaseEvent() {
            public void onFire(Object... params){
                if(params[0] !=null)
                {
                    Context context = (Context)params[0];
                    Toast toast = Toast.makeText(context, "Task ran", Toast.LENGTH_SHORT);
                    toast.show();
                }
            }

      };

      threadPool.scheduleAtFixedRate(new HeartBeatTask(callback,(this)),0, 5, TimeUnit.SECONDS);

This will execute the task every 5 seconds, although the callback method is only being run once.
I'm thinking that this may not be a good way to do things. My feeling is that I'm overcomplicating things. What I really need is the ability to have something that will execute a method, on a process other than the main thread, and at a set interval, that activities can bind multiple actions to. So, for instance, I may want to have a UI component update after a call is made to a database, and would want that to happen every minute.
Could any tell me if I am on the right track here? Is what I am trying to do a viable way to accomplish my goal? Is there a better approach I could be taking? Any advice or suggestions would be very much appreciated. Thanks!
Answer: a few suggestions for an Android beginner.

Don't call it a separate process. Process is a different thing (Google 'Linux process'), you want to call them on a separate thread inside the same process.
ScheduledThreadPoolExecutor IS better than anything else people will suggest you here such as Timers or PostDelayed.

But I think you do have a philosophical error here as to UI updates shouldn't be running on a timed manner but on an event base instead. Once your Db, Disk or Network operation finishes from a background thread you callback to the UI thread to update it immediately.
There`re several tools for that and I'll list a few, point the one I like the best, but let you do some research on each one

Handler: That's basic java way
AsyncTask: Nice framework but doesn't handle screen rotation
Loader: That's my preferred way
Answer: Android is a mobile platform and in all honesty each time you want something run, you better consider it separately. You may be killing the battery or using resources for no reason. I begrudge Zedge app everyday for running for no reason in the background at all times. Especially since on CyanogenMod kernel puts WiFi to sleep, while apparently it is currently on.
I am guessing this is more of an exercise and for running tasks at a specified intervals. One-offs, most universal can be done via AlarmManager class. But this may not be the best solution.
For some system wide events there is the BroadcastReceiver class.
While addressing

So, for instance, I may want to have a UI component update after a call is made to a database, and would want that to happen every minute.

Easier done via an Interface really.
Answer: I think your approach is a bit complicated. Consider you example
So, for instance, I may want to have a UI component update after a call is made
to a database, and would want that to happen every minute.

I think I will do it this way.

Create a AsyncTask which will update the UI component.
Create a thread which will execute a new AsyncTask and sleep one minute in a while loop.
Start the thread in step 3. at some point.
Interrupt the thread if you don't want the component to be updated.

Example of step 2
while (true) {
    try {
        new updateTask.execute();
        Thread.sleep(60000);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
}


Changed Exchange URLs, still a reference to the old one somewhere

Question: So at one of my clients their SSL certificate was up for renewal, and when I renewed it, I did not include their primary External/Internal URL in it (remote.domain.com) instead opting to change everything to mail.domain.com. As far as I can tell, I've changed every instance of the URL, but am still getting a certificate warning in Outlook 2013 that the certificate doesn't match the server, and that it's looking for remote.domain.com
I'm hoping someone can point me in the right direction as to where this would be coming from. Screenshots of the warning and certificate:

<code>[PS] C:\Windows\system32>Get-OWAVirtualDirectory | select name,internalurl,externalurl

Name                         InternalUrl                      ExternalUrl
----                         -----------                      -----------
owa (Default Web Site)       https://mail.domain.com/owa/     https://mail.domain.com/owa/

[PS] C:\Windows\system32>Get-OABVirtualDirectory | select name,internalurl,externalurl

Name                         InternalUrl                      ExternalUrl
----                         -----------                      -----------
OAB (Default Web Site)       https://mail.domain.com/OAB      https://mail.domain.com/OAB

[PS] C:\Windows\system32>Get-ECPVirtualDirectory | select name,internalurl,externalurl

Name                         InternalUrl                      ExternalUrl
----                         -----------                      -----------
ecp (Default Web Site)       https://mail.domain.com/ecp      https://mail.domain.com/ecp

[PS] C:\Windows\system32>Get-ActiveSyncVirtualDirectory | select name,internalurl,externalurl

Name                                    InternalUrl                      ExternalUrl
----                                    -----------                      -----------
Microsoft-Server-ActiveSync (Default... https://mail.domain.com/Micro... https://mail.domain.com/Micro...

[PS] C:\Windows\system32>Get-WebServicesVirtualDirectory | select name,internalurl,externalurl

Name                         InternalUrl                      ExternalUrl
----                         -----------                      -----------
EWS (Default Web Site)       https://mail.domain.com/EWS...   https://mail.domain.com/EWS...
</code>
Comment: I'm assuming this is SBS, based on your mention of remote.domain.com. If you were buying a UCC cert, you should have just left that domain reference in the cert, I've found it best to not work against SBS whenever possible.
Comment: SBS 2011, yes. Unfortunately wasn't my call or I would've left it in. Trying to standardize all our clients a bit though.
Comment: SBS is already standardized, using remote.domain.com. What are you using for a cert, a wildcard?
Comment: Have you tested here: https://testconnectivity.microsoft.com/
Comment: By standardize, I mean by our own processes in that all of our clients use mail.domain.com. The cert is not a wildcard, just a normal cert with up to 5 domains I believe.
Comment: I have ran the microsoft connectivity wizard, and only get two insignificant warnings regarding windows mobile devices and a windows update for Root Certificates.
Answer: I'll bet it's either Autodiscover, or Outlook Anywhere. Or both.
Comment: I did a Test Email AutoConfiguration in Outlook and found the line `Attempting URL https://remote.domain.com/Autodiscover/Autodiscover.xml found through SCP` in the log. Where do I change this?
Comment: Nevermind. I was googling for the wrong thing. I set the URL through the following PowerShell command: `Set-ClientAccessServer -Identity SBSSERVER –AutoDiscoverServiceInternalUri https://mail.domain.com/Autodiscover/Autodiscover.xml`


Finding the minimum cost

Question: I am currently trying to formulate a formula for this problem using Multivariable Calculus.
There is a job of shipping $V$ cubic feet of old steel shavings across the river by barge.
To do this, you'll make a box to put the shavings in for the barge shipment.
Define:
$x = \textrm{length of the box in feet},$
$y = \textrm{width of the box in feet}, \textrm{and}$
$z = \textrm{height of the box in feet}.$
The material for the box consists of five rectangular pieces.
The bottom has area $x \cdot y$; the material for it costs $a$ per square foot. So in dollars,
bottom cost $= a \cdot y.$
Another two pieces (the sides) have area $x \cdot z$ each; these cost $b$ per square foot. So in dollars
side cost $= 2 b \cdot z.$
And the other two pieces (the ends) have area $y \cdot z$; these cost $c$ per square foot. So in dollars,
end cost $= 2 c \cdot y \cdot z.$
The tugboat captain tells us that she will charge $d$ dollars for each trip. You have $V$ cubic feet of shavings;
so you'll need $\frac{V}{x y z}$ trips.
So in dollars,
transportation cost = $\frac{d \cdot V}{x y z}.$
The bottom line is
overall cost = bottom cost + side cost + end cost + transportation cost.
What should the ratios
bottom cost : side cost : end cost : transportation cost
be if the overall cost is at a minimum?
I was trying to use Lagrange multipliers but no constraint equation is given. How can i go about obtaining the ratios for obtaining the formula for minimum cost?
Answer: Suppose there are $n$ trips. For minimum cost, the box must be full on every trip (otherwise you could have made the same number of trips with a smaller box). So volume of box = $xyz = V/n$. By the inequality for arithmetic and geometric means,
$$(axy + 2bxz + 2cyz)/3 \ge (axy\cdot2bxz\cdot2cyz)^{1/3} = (4abcV^2/n^2)^{1/3}.$$
The cost of the box is minimized if AM = GM, which happens if and only if $axy = 2bxz = 2cyz.$ From this you can work out the ratios of the sides of the box. The total cost of the $n$ trips is
$$dn + 3(4abcV^2)^{1/3}n^{-2/3}.$$
If $n$ were a continuous variable, elementary calculus shows that the cost would be a minimum when $n = 2(abcV^2/d^3)^{1/5}.$ Since in practice $n$ has to be an integer, I think you would have to take the integers on each side of the theoretical minimum and see which gives the smaller cost.

Finding the minimum cost


Is there any CDK or CloudFormation support for Container Recipes in AWS Image Builder?

Question: I don't see any resource for Container Recipes for AWS Image Builder in the CDK or CloudFormation documentation.
The closest thing I can find is support for Image Recipes, with is not used for creating container images, only AMIs. I am really needing to add this resource to my CF stack to create a image(as in a container image) pipeline that pushes the resulting image into ECR... which I CAN do manually, only.. so far.
I'm using the TS CDK lib, not that it matters atm.
Answer: In cloudformation there is AWS::ImageBuilder::ContainerRecipe:

Creates a new container recipe. Container recipes define how images are configured, tested, and assessed.

The docs also provide example how to create a container recipe. Thus its not clear from you question, if you expect different type of container recopies to be created in CloudFormation?
Example from docs:
<code>Resources:
   ContainerRecipeAllParameters:
      Type: 'AWS::ImageBuilder::ContainerRecipe'
      Properties:
         Name: 'container-recipe-name'
         Version: '1.0.0'
         ParentImage: !Ref ParentImage
         Description: 'description'
         ContainerType: 'DOCKER'
         Components:
            - ComponentArn: !Ref ComponentArn
            - ComponentArn: !Ref AnotherComponentArn
         TargetRepository:
            Service: 'ECR'
            RepositoryName: !Ref RepositoryName
         DockerfileTemplateData: |
           FROM {{{ imagebuilder:parentImage }}}
           {{{ imagebuilder:environments }}}
           {{{ imagebuilder:components }}}
         WorkingDirectory: "dummy-working-directory"
         KmsKeyId: !Ref KmsKeyId
         Tags:
            Usage: 'Documentation'
</code>
Comment: idk if the AWS::ImageBuilder::ContainerRecipe resource is new or I just didn't see it, but i swear it wasn't an option at the time I posted this... or maybe it was lol... idk
Comment: @aphexlog Not sure if its new or not. Maybe CFN resources have similar names, so it could be just a case as simple as overlooking it.


Storing api keys

Question: I am using the Goodreads api to get book data for my react native app. I have to use a key for using the api. Is it OK for me to store the api key on the app itself or should I put the key on a server that redirects all the data to the app?
Comment: How was your project created?
Did you use expo or react-native init?
Comment: @SHG21 react-native init
Comment: @SHG21 so will it be secure in the production environment? Since the key will still be on the client side application.
Comment: API keys that go to clients can always be extracted from a client application no matter how they get there, and therefore the security of your API cannot depend upon the secrecy of an API key (which is counterintuitive, but this is the truth).  Therefore do not treat them as sensitive values, and do not make your API dependent on the secrecy of such a key.  Bottom line: store the API key on the client.
Answer:
Is it OK for me to store the api key on the app itself

No, because as I demonstrate in the article How to Extract an API Key from a Mobile App by Static binary analysis it can be extracted with the help of several open source tools, like by using the Mobile Security Framework, but you can also grab the API key with a MitM attack, as I show in the article Steal that API Key with a Man in the Middle Attack, that uses the open source tool MiTM Proxy.
If you leave a third party API key in the mobile app, then they are up for grabs from attackers, and when they start using it without your knowledge your bill in the third party provider may go through the roof before you acknowledge that something is wrong, and on that time the only solution is to revoke the API key, thus shutting down the use of the mobile app, and if you make a new release of your mobile app with a new API key it will be just a matter of hours until the attacker come back and steal the API key again.

or should I put the key on a server that redirects all the data to the app?

Yes, and this is a good approach, because now you have only one place to store and protect all third part API keys. This have the benefit to let you control and throttle the use of them as you see fit.
With this solution you still need an API key in your mobile app to allow access to your API server, but while you continue vulnerable for attackers to steal it, you are now in direct control of throttling the access to your API server and if you identify in each access the WHO and the WHAT is accessing the API server, then you have a more fine grade control, but attacker will continue to be able to slip between all our defenses, because is very hard to know WHAT is accessing the API server.
You may be thinking by now... do you mind to explain the WHO vs the WHAT?
The Difference Between WHO and WHAT is Accessing the API Server
To better understand the differences between the WHO and the WHAT are accessing an API server, let's use this picture:

The Intended Communication Channel represents the mobile app being used as you expected, by a legit user without any malicious intentions, using an untampered version of the mobile app, and communicating directly with the API server without being man in the middle attacked.
The actual channel may represent several different scenarios, like a legit user with malicious intentions that may be using a repackaged version of the mobile app, a hacker using the genuine version of the mobile app, while man in the middle attacking it, to understand how the communication between the mobile app and the API server is being done in order to be able to automate attacks against your API. Many other scenarios are possible, but we will not enumerate each one here.
I hope that by now you may already have a clue why the WHO and the WHAT are not the same, but if not it will become clear in a moment.
The WHO is the user of the mobile app that we can authenticate, authorize and identify in several ways, like using OpenID Connect or OAUTH2 flows.

OAUTH
Generally, OAuth provides to clients a "secure delegated access" to server resources on behalf of a resource owner. It specifies a process for resource owners to authorize third-party access to their server resources without sharing their credentials. Designed specifically to work with Hypertext Transfer Protocol (HTTP), OAuth essentially allows access tokens to be issued to third-party clients by an authorization server, with the approval of the resource owner. The third party then uses the access token to access the protected resources hosted by the resource server.
OpenID Connect
OpenID Connect 1.0 is a simple identity layer on top of the OAuth 2.0 protocol. It allows Clients to verify the identity of the End-User based on the authentication performed by an Authorization Server, as well as to obtain basic profile information about the End-User in an interoperable and REST-like manner.

While user authentication may let the API server know WHO is using the API, it cannot guarantee that the requests have originated from WHAT you expect, the original version of the mobile app.
Now we need a way to identify WHAT is calling the API server, and here things become more tricky than most developers may think. The WHAT is the thing making the request to the API server. Is it really a genuine instance of the mobile app, or is a bot, an automated script or an attacker manually poking around with the API server, using a tool like Postman?
For your surprise you may end up discovering that It can be one of the legit users using a repackaged version of the mobile app or an automated script that is trying to gamify and take advantage of the service provided by the application.
Well, to identify the WHAT, developers tend to resort to an API key that usually they hard-code in the code of their mobile app. Some developers go the extra mile and compute the key at run-time in the mobile app, thus it becomes a runtime secret as opposed to the former approach when a static secret is embedded in the code.
The above write-up was extracted from an article I wrote, entitled WHY DOES YOUR MOBILE APP NEED AN API KEY?, and that you can read in full here, that is the first article in a series of articles about API keys.
Your problem is not solved yet
Now that you know the difference between WHO and WHAT is accessing your API server you must have realized that your API server is still vulnerable to be abused by attackers.
You can resort now to employ several layers of defense, starting with reCaptcha V3, followed by Web Application Firewall(WAF) and finally if you can afford it a User Behavior Analytics(UBA) solution.
Google reCAPTCHA V3:

reCAPTCHA is a free service that protects your website from spam and abuse. reCAPTCHA uses an advanced risk analysis engine and adaptive challenges to keep automated software from engaging in abusive activities on your site. It does this while letting your valid users pass through with ease.
...helps you detect abusive traffic on your website without any user friction. It returns a score based on the interactions with your website and provides you more flexibility to take appropriate actions.

WAF - Web Application Firewall:

A web application firewall (or WAF) filters, monitors, and blocks HTTP traffic to and from a web application. A WAF is differentiated from a regular firewall in that a WAF is able to filter the content of specific web applications while regular firewalls serve as a safety gate between servers. By inspecting HTTP traffic, it can prevent attacks stemming from web application security flaws, such as SQL injection, cross-site scripting (XSS), file inclusion, and security misconfigurations.

UBA - User Behavior Analytics:

User behavior analytics (UBA) as defined by Gartner is a cybersecurity process about detection of insider threats, targeted attacks, and financial fraud. UBA solutions look at patterns of human behavior, and then apply algorithms and statistical analysis to detect meaningful anomalies from those patterns—anomalies that indicate potential threats. Instead of tracking devices or security events, UBA tracks a system's users. Big data platforms like Apache Hadoop are increasing UBA functionality by allowing them to analyze petabytes worth of data to detect insider threats and advanced persistent threats.

All this solutions work based on a negative identification model, by other words they try their best to differentiate the bad from the good by identifying what is bad, not what is good, thus they are prone to false positives, despite of the advanced technology used by some of them, like machine learning and artificial intelligence.
So you may find yourself more often than not in having to relax how you block the access to the API server in order to not affect the good users. This also means that this solutions require constant monitoring to validate that the false positives are not blocking your legit users and that at same time they are properly keeping at bay the unauthorized ones.
Regarding APIs serving mobile apps a positive identification model can be used by using a Mobile App Attestation solution that guarantees to the API server that the requests can be trusted without the possibility of false positives.
Mobile App Attestation
Use a Mobile App Attestation solution to enable the API server to know is receiving only requests from a genuine mobile app.
The role of a Mobile App Attestation service is to guarantee at run-time that your mobile app was not tampered or is not running in a rooted device by running a SDK in the background that will communicate with a service running in the cloud to attest the integrity of the mobile app and device is running on.
On successful attestation of the mobile app integrity a short time lived JWT token is issued and signed with a secret that only the API server and the Mobile App Attestation service in the cloud are aware. In the case of failure on the mobile app attestation the JWT token is signed with a secret that the API server does not know.
Now the App must sent with every API call the JWT token in the headers of the request. This will allow the API server to only serve requests when it can verify the signature and expiration time in the JWT token and refuse them when it fails the verification.
Once the secret used by the Mobile App Attestation service is not known by the mobile app, is not possible to reverse engineer it at run-time even when the App is tampered, running in a rooted device or communicating over a connection that is being the target of a Man in the Middle Attack.
The Mobile App Attestation service already exists as a SAAS solution at Approov(I work here) that provides SDKs for several platforms, including iOS, Android, React Native and others. The integration will also need a small check in the API server code to verify the JWT token issued by the cloud service. This check is necessary for the API server to be able to decide what requests to serve and what ones to deny.
Summary
Anything that runs on the client side and needs some secret to access an API can be abused in different ways and you must delegate the access to all third part APIs to a backend under your control, so that you reduce the attack surface, and at the same time protect their secrets from public pry eyes.
In the end, the solution to use in order to protect your API server must be chosen in accordance with the value of what you are trying to protect and the legal requirements for that type of data, like the GDPR regulations in Europe.
Comment: Thanks for the thorough response, and providing alternative methods.  Will definitely check out your service.
Answer: For react native use react-native-config library. While using this library you can secure your api keys as well as you can save more secret keys which use in the native code. Like we can save onesignal, codepush etc keys.
https://github.com/luggit/react-native-config
Comment: This does not work as it takes all the data inside string so it can be found...
Comment: This is not a good idea taking from react-native-config readme

"Keep in mind this module doesn't obfuscate or encrypt secrets for packaging, so do not store sensitive keys in .env. It's basically impossible to prevent users from reverse engineering mobile app secrets, so design your app (and APIs) with that in mind." so this is not suitable for production code the only to store them is in a backend server.
Answer: Store them in a .env file like this API_KEY=yourKey.
Install npm package react-native-dotenv.
Then import to to files as needed with react-native-dotenv package;
import { API_KEY } from 'react-native-dotenv'
The .env file should never be committed to Github.

Storing api keys


Live Loop Setup with Keyboard, Guitar and Mic with Boss RC-300

Question: I'm in the process of setting up a live looping setup, I want to use my vocals, guitar, midi/controller keyboard (controller sounds triggered with a DAW such as FL Studio or Logic). I was hoping anyone could give me some pointers or guidance with the setup at all, I'm currently thinking this is the best way to approach it:
Keyboard -> Laptop/PC (DAW) -> Boss RC-300 Loop Pedal (via 3.5mm Aux)
With:
Guitar -> FX Pedals (Delay and Reverb mainly) -> Boss RC-300 Loop Pedal (via 1/4" jack)
And:
Mic -> FX Pedals (TC-Helicon VoiceLive Play) -> Boss RC-300 Loop Pedal (via XLR)
Then output the Boss RC-300 to a PA/Speakers/Headphones, or if I want to record it, send it through a digital recorder or another Laptop/PC.
As far as I can see that should all work as I expect. So then on the Boss RC 300, I'll be able to have piano/strings loop and layered/dubbed on the 1st channel/pedal, my guitar on the 2nd channel and vocals on the 3rd channel for example!
So yeh, my question, can anyone see any flaws in this, or have any improvements that they could throw my way, if I'm missing anything or if you'd do anything differently in this setup?
Comment: Looks good to me!
Answer: You may have issues running the Keyboard off the 3.5mm Aux in terms of volume and trim and possibly noise.
What I generally do is if I have more than two channels, I run all the signals through a mini-mixer  (like the Behringer 4 channel Euroracks) and then into one input on the RC-300.
It depends on the instrument mix, but I generally set my channels like this:

First channel is a two-bar loop I use for percussion.
Second channel is a 32-bar loop I use for laying down chords
Third channel is another 32-bar loop for laying down melodies.
Use the undo redo pedal as a "fourth channel" where I can switch back and forth between two parts.
Answer: I don't see a guitar amp in that chain. Are you sure you'll like the direct sound of guitar -> pedals -> PA? I'd probably want at least a preamp if not a speaker sim as well. Maybe an amp sim in pedal form like a Sansamp?


Tony Romo broadcast times?

Question: How can I check if Tony Romo is commentating a particular CBS game? I know he usually commentates the afternoon games, but which ones? The CBS site doesn't seem to list the commentators for each game, or am I missing something?
Comment: he's a gem.  such a delight to watch one of his games,
Answer: 506sports.com is generally the big name for NFL broadcast maps. They're most known for finding out what your US local TV station is airing, but their maps also shows who they think the scheduled announcers are.

Tony Romo broadcast times?


Is there a law for summing powers of logarithms?

Question: I have the expression
$$(\ln a)^c+(\ln b)^c$$
Is there a way to combine these (i.e., remove the $+$ sign) to create a single expression in terms of $a,b,c$?
Comment: Have you tried something?
Comment: I can't work out what to try. Seems like I could somehow take a $log$ of both expressions and then change the base, but I can't see how.
Comment: If we would have terms like $\ c\cdot \ln(a)\ $ , we could convert the expression, but this expression cannot be converted.
Comment: Logarithms are (or at least want to be) exponents. There is no simple rule to deal with (and transform) something like $e^{x^y}$ the way you can transform $e^{xy}$ or $e^{x+y}$, and thus I wouldn't expect there to be any corresponding logarithm rule (for instance, $e^{xy} = (e^x)^y$ corresponds to $y\ln x = \ln x^y$).
Answer: No :
You could simplify the different $\ln (a^c)+\ln (b^c)=c\ln (ab)$ or something similar
but unless you know something special about the relationship between $a, b, c$ then $(\ln a)^c+(\ln b)^c$ is essentially as difficult to simplify as $2^c+3^c$
Comment: So, a general rule clearly doesn't exist. But in its absence, the specific function I'm looking at right now is $\biggl(\ln \bigl(\cos (\ln x)+i\sin (\ln x)\bigr)\biggr)^c+\biggl(\ln \bigl(\cos (\ln x)-i\sin (\ln x)\bigr)\biggr)^c$. Any thoughts?
Comment: @RichardBurke-Ward - That expression introduces the traps of the complex logarithm and powers but there may be possible simplifications  using $\cos(\theta) + i \sin(\theta) = e^{i \theta}$ so it becomes $\left(\log_e \left(\exp (i \log_ex)\right)\right)^c+\left(\log_e\left(\exp(-i\log_ex)\right)\right)^c$ and that *might* be something like $\left(\log_e(x)\right)^c\left(i^c+(-i)^c\right)$


solve $\frac{\partial u^2}{\partial x\partial y}=0$

Question: I need to solve $$\frac{\partial u^2}{\partial x\partial y}=0$$ with the boundary conditions: $u(x,y=x^3)=\sin(x^6)$ and $\frac{\partial u}{\partial x}(x,y=x^3)=0$.
I got a particular solution, I thing which is $u_p=A\sin(y^2)$, where $A\in\mathbb{R}$, that satisfies the two boundary conditions, but it is rather a guess.
Answer: Integrate wrt to $x$ to get that
$$\frac{\partial u}{\partial y} = z(y)$$
Integrate wrt to $x$
$$u = \int z(y)dy + g(x) = f(y) + g(x)$$
for two functions $f,g$.
Now add the boundary conditions to find
$$u(x, x^3) = f(x^3 ) + g(x) = \sin x^6$$
$\frac{\partial u}{\partial x} (x,x^3) = g'(x) = 0 \implies g(x) = B \in \mathbb R$
hence $f(x^3) = \sin x^6 - B \implies f(y) = \sin y^2 - B$
which in turn makes $u(x,y) = \sin y^2$
Notice that the coefficient in front of $\sin y^2$ must be $1$ because of the boundary conditions,so your solution is valid only for $A=1$


Gráfico em css ou jquery

Question: É possível, via css, html ou jquery criar um gráfico assim?

Na verdade, o problema é fazer as setas.
Se alguém já trabalhou em algo do tipo para dar uma luz, eu agradeço.
Comment: Sim, é possivel.
Comment: https://www.google.com.br/search?q=como+fazer+setas+em+html
Comment: Basta criar um CSS Grid com 3 linhas e 3 colunas. 2 linhas inteiras com 3 quadrados, e a ultima linha com 2 quadrados com 1 circulo. Depois só ligar as setas do ponto central para as margens. Vai dar bastante trabalho.
Comment: Usa o JsPlumb é um ótima solução para FlowCharts, além de ser possivel arrastar elementos e adicionar, remover, e alterar as setas, https://jsplumbtoolkit.com
Answer: Criei alguns estilos com 5 setas em posições diferentes em CSS usando ::before e ::after, que você pode usar como base e criar as outras baseando-se nos modelos e ajustar da forma que achar melhor para o seu projeto.
Segue o código de exemplo:

#div1{
   width: 100px;
   background: #fff;
   padding: 30px;
   border: 1px solid #ddd;
   border-radius: 10px;
   position: relative;

   /*apenas para posicionar*/
   margin: 100px;
}

.seta{
   position: absolute;
}

.meioH{
   left: 50%;
}

.meioV{
   top: 50%;
}

.setaup, .setaup-right{
   top: 0;
}

.setadown{
   bottom: 0;
}

.setaright, .setaup-right{
   right: 0;
}

.setaleft{
   left: 0;
}

/*estilos comuns before*/
.setaup::before,
.setadown::before,
.setaright::before,
.setaleft::before,
.setaup-right::before{
   content: '';
   position: absolute;
   width: 0;
   height: 0;
   border-style: solid;
}

/*estilos comuns after*/
.setaup::after,
.setadown::after,
.setaright::after,
.setaleft::after,
.setaup-right::after{
   content: '';
   position: absolute;
   background: #000;
}

/*before: setas; after: barra*/

.setaup::before{
   border-width: 0 5px 10px 5px;
   border-color: transparent transparent #000000 transparent;
   bottom: 60px;
   margin-left: -5px;
}

.setaup::after{
   width: 2px;
   height: 60px;
   top: -60px;
   margin-left: -1px;
}

.setadown::before{
   border-width: 10px 5px 0 5px;
   border-color: #000000 transparent transparent transparent;
   top: 60px;
   margin-left: -5px;
}

.setadown::after{
   width: 2px;
   height: 60px;
   bottom: -60px;
   margin-left: -1px;
}

.setaright::before{
   border-width: 5px 0 5px 10px;
   border-color: transparent transparent transparent #000000;
   left: 60px;
   margin-top: -5px;
}

.setaright::after{
   width: 60px;
   height: 2px;
   right: -60px;
   margin-top: -1px;
}

.setaleft::before{
   border-width: 5px 10px 5px 0;
   border-color: transparent #000000 transparent transparent;
   right: 60px;
   margin-top: -5px;
}

.setaleft::after{
   width: 60px;
   height: 2px;
   left: -60px;
   margin-top: -1px;
}

.setaup-right::before{
   border-width: 0 10px 10px 0;
   border-color: transparent #000000 transparent transparent;
   left: 35px;
   top: -39px;
   margin-top: -5px;
}

.setaup-right::after{
   width: 60px;
   height: 2px;
   right: -60px;
   top: 2px;
   left: -2px;
   transform: rotate(-45deg);
   transform-origin: left;
}

   texto

Comment: poderia explicar como fez as pontas das setas? olhei o código mas não entendi muito bem
Comment: Tem um padrão, mas eu peguei neste site http://apps.eky.hk/css-triangle-generator/. Daí é só ajustar o tamanho e a cor.

Gráfico em css ou jquery


run JavaScript if database value equals x

Question: I have a piece of JavaScript that I want on a custom-built intranet to run only for a specific set of users.
When the page is loaded, there is a script in the header that queries the MS SQL database for an ID that would be specific for a group of users (eg. a office location).
The new JavaScript I want to run needs to be added just before the , but I want it to only show for the users of a specific office ID.
The script in the header I refer to uses:
<telerik:RadScriptBlock ID="script1" runat="server" EnableViewState="false">

How do I achieve this?
Answer: Handle checking the ID server side:
<% If "487136f1-4e60-4d1c-a98a-4c01e5b14ed6".Equals(GetMyFirmID()) Then %>

<% End If %>

The script will only be written if the firm ID is the one in question.
If GetMyFirmID() is expensive and you need to use the result more than once (I see you also use it on the myfirmid line), you probably want to stick the result in a variable instead of repeatedly calling the method.
Hope that helps.
Comment: This is a good solution because it avoids exposing that firm ID to the client, and it won't even let the client see the code that it's not supposed to be executing. Well done.
Comment: Sorry, I'm not overly familiar with ASP.
Where in the page would I insert that?  Do I need anything before or after it?  Eg. `
Comment: I'm sorry, I misinterpreted as JSP. The answer, conceptually, is still the same, although the syntax may not be. You would put it in your ASP page source, around where the script currently is. You should probably check out a basic ASP primer first to familiarize yourself with ASP tags.
Comment: I'm glancing around at ASP references, it looks like the syntax is precisely the same as above, except with a capital E for Equals. Can somebody familiar with ASP confirm this before I edit?
Comment: I've tried just inserting that into the  just before the close (using my JavaScript where you specify), and have refreshed the page.  I get a compilation error _BC30081: 'If' must end with a matching 'End If'._ with reference to that first line.
Comment: That's my fault, for using JSP. I have updated the answer above. Sorry for the confusion. Please let me know how it goes. See http://www.freewebmasterhelp.com/tutorials/asp/3 for more syntax examples.


Clustered data WITHOUT multilevel / GEE model?

Question: I have a data-set with around 700 observations from 12 centres. Although the clustering effect as tested in a random intercept model didn't seem significant, it seems more appropriate to use a multilevel / GEE model. But it may not be easy to do so as I want to test for multiple potential mediators. (to be specific, the model in my mind is a SEM without latent variables).
So my question is: is it theoretically advisable to fit the data in a single-level path model using ML (Huber sandwich estimator, or bootstrap SE)?
UPDATE:
Thanks for those helping out. Just a update for what I did yesterday. I tried the following models:

Single-level SEM in R (lavaan package)
Single-level SEM in MPlus
Two-level SEM in Mplus

(1) and (2) were fitted with ML (Huber / Bootstrap SE). (3) was fitted with Bayesian estimator as MLR did not converge. Basically all of the above gave me similar results but (1) and (2) gave me the chance to do multi-group analysis on top of the SEM and (3) seems to be more theoretically correct. I am not quite sure if the results from (1) and (2) can be trusted.
A quick literature search told me that Huber SE will give a correct SE even if the model is specified, but the parameters estimated may be biased. Under this situation, will (1) and (2) provide biased parameter estimates?
Answer: In my experience, the sensitivity of SEM to detect mediating effects is far outweighed by the cluster level variability of multicentre studies, such as samples within schools, hospitals, or counties. Furthermore, any mediator is usually measurable as a covariate and can be discussed as such. And when that fails, there are way too many assumptions involved with SEM to make me feel confident that it can be used.
With 700 observations, it may be possible to adjust for fixed effects accounting for cluster level variability (11 indicator variables). This depends, of course, upon the number of remaining adjustment variables in the model. Too many will lead to collinearity issues. This is a fully parametric approach
Comment: Thanks Adam. Actually I tried the following models:
1. Single-level SEM in R (lavaan package)
2. Single-level SEM in MPlus
3. Two-level SEM in Mplus

(1) and (2) were fitted with
Comment: @fred Why no mean modeling?
Answer: GEEs generally require a fairly large number of clusters (e.g. a minimum of around 50), so you would be better off fitting the data in a mixed model with centre as a random effect.  Any potential mediators (assuming by this you mean covariates?) can then be included simply as fixed effects.
If you wish to fit this model in R, you could use the package nlme with the following syntax:
Mod <- lme( Y ~ mediator1 + mediator2, ~1|centre)

and obtain estimates of the effects of the mediators using summary(Mod).
Comment: Nick, mediators != covariates. The OP has a multivariate system, not just a single equation. Mediation is a term from causal effect modeling literature, and that the stuff that social scientists had to come up with on their own because statisticians did not bother helping them out.
Comment: Can you provide a citation suggesting that mixed models are not sensitive to small numbers of clusters in the same way GEE is? Remember, GEEs and mixed models estimate different parameters for non-linear links, so the model choice should be based upon the question one's trying to answer. Mixed models can be "marginalized" though, to obtain marginal effects.
Comment: @Nick, thanks but, as suggested by StrasK, I think the model you proposed may not answer to my hypothesis. I wish to know the direct and indirect effect of an exposure to the outcome through a few pathways.