How is the cost function from Logistic Regression differentiated

Question: I am doing the Machine Learning Stanford course on Coursera.
In the chapter on Logistic Regression, the cost function is this:

Then, it is differentiated here:

I tried getting the derivative of the cost function, but I got something completely different.
How is the derivative obtained?
Which are the intermediary steps?
Comment: when you say "derivated" do you mean "differentiated" or "derived"?
Comment: +1, check @AdamO's answer in my question here. https://stats.stackexchange.com/questions/229014/matrix-notation-for-logistic-regression
Comment: "Completely different" is not really sufficient to answer your question, besides telling you what you already know (the correct gradient).  It'd be much more useful if you gave us what your calculations resulted in, then we can help you shore up where you made the mistake.
Comment: @MatthewDrury Sorry, Matt, I had arranged the answer right before your comment came in. Octavian, did you follow all the steps? I will edit to give it some added value later...
Comment: [Here](https://towardsdatascience.com/animations-of-logistic-regression-with-python-31f8c9cb420) is another, in my opinion easy to follow, explanation of how the partial derivatives of the logistic regression cost function can be obtained.
Answer: Adapted from the notes in the course, which I don't see available (including this derivation) outside the notes contributed by students within the page of Andrew Ng's Coursera Machine Learning course.

In what follows, the superscript $(i)$ denotes individual measurements or training "examples."
$\small
\frac{\partial J(\theta)}{\partial \theta_j}  =
\frac{\partial}{\partial \theta_j} \,\frac{-1}{m}\sum_{i=1}^m
\left[ y^{(i)}\log\left(h_\theta \left(x^{(i)}\right)\right) +
(1 -y^{(i)})\log\left(1-h_\theta \left(x^{(i)}\right)\right)\right]
\\[2ex]\small\underset{\text{linearity}}= \,\frac{-1}{m}\,\sum_{i=1}^m
\left[
y^{(i)}\frac{\partial}{\partial \theta_j}\log\left(h_\theta \left(x^{(i)}\right)\right) +
(1 -y^{(i)})\frac{\partial}{\partial \theta_j}\log\left(1-h_\theta \left(x^{(i)}\right)\right)
\right]
\\[2ex]\Tiny\underset{\text{chain rule}}= \,\frac{-1}{m}\,\sum_{i=1}^m
\left[
y^{(i)}\frac{\frac{\partial}{\partial \theta_j}h_\theta \left(x^{(i)}\right)}{h_\theta\left(x^{(i)}\right)} +
(1 -y^{(i)})\frac{\frac{\partial}{\partial \theta_j}\left(1-h_\theta \left(x^{(i)}\right)\right)}{1-h_\theta\left(x^{(i)}\right)}
\right]
\\[2ex]\small\underset{h_\theta(x)=\sigma\left(\theta^\top x\right)}=\,\frac{-1}{m}\,\sum_{i=1}^m
\left[
y^{(i)}\frac{\frac{\partial}{\partial \theta_j}\sigma\left(\theta^\top x^{(i)}\right)}{h_\theta\left(x^{(i)}\right)} +
(1 -y^{(i)})\frac{\frac{\partial}{\partial \theta_j}\left(1-\sigma\left(\theta^\top x^{(i)}\right)\right)}{1-h_\theta\left(x^{(i)}\right)}
\right]
\\[2ex]\Tiny\underset{\sigma'}=\frac{-1}{m}\,\sum_{i=1}^m
\left[ y^{(i)}\,
\frac{\sigma\left(\theta^\top x^{(i)}\right)\left(1-\sigma\left(\theta^\top x^{(i)}\right)\right)\frac{\partial}{\partial \theta_j}\left(\theta^\top x^{(i)}\right)}{h_\theta\left(x^{(i)}\right)} -
(1 -y^{(i)})\,\frac{\sigma\left(\theta^\top x^{(i)}\right)\left(1-\sigma\left(\theta^\top x^{(i)}\right)\right)\frac{\partial}{\partial \theta_j}\left(\theta^\top x^{(i)}\right)}{1-h_\theta\left(x^{(i)}\right)}
\right]
\\[2ex]\small\underset{\sigma\left(\theta^\top x\right)=h_\theta(x)}= \,\frac{-1}{m}\,\sum_{i=1}^m
\left[
y^{(i)}\frac{h_\theta\left( x^{(i)}\right)\left(1-h_\theta\left( x^{(i)}\right)\right)\frac{\partial}{\partial \theta_j}\left(\theta^\top x^{(i)}\right)}{h_\theta\left(x^{(i)}\right)} -
(1 -y^{(i)})\frac{h_\theta\left( x^{(i)}\right)\left(1-h_\theta\left(x^{(i)}\right)\right)\frac{\partial}{\partial \theta_j}\left( \theta^\top x^{(i)}\right)}{1-h_\theta\left(x^{(i)}\right)}
\right]
\\[2ex]\small\underset{\frac{\partial}{\partial \theta_j}\left(\theta^\top x^{(i)}\right)=x_j^{(i)}}=\,\frac{-1}{m}\,\sum_{i=1}^m \left[y^{(i)}\left(1-h_\theta\left(x^{(i)}\right)\right)x_j^{(i)}-
\left(1-y^{i}\right)\,h_\theta\left(x^{(i)}\right)x_j^{(i)}
\right]
\\[2ex]\small\underset{\text{distribute}}=\,\frac{-1}{m}\,\sum_{i=1}^m \left[y^{i}-y^{i}h_\theta\left(x^{(i)}\right)-
h_\theta\left(x^{(i)}\right)+y^{(i)}h_\theta\left(x^{(i)}\right)
\right]\,x_j^{(i)}
\\[2ex]\small\underset{\text{cancel}}=\,\frac{-1}{m}\,\sum_{i=1}^m \left[y^{(i)}-h_\theta\left(x^{(i)}\right)\right]\,x_j^{(i)} \\[2ex]\small=\frac{1}{m}\sum_{i=1}^m\left[h_\theta\left(x^{(i)}\right)-y^{(i)}\right]\,x_j^{(i)}
$

The derivative of the sigmoid function is
$\Tiny\begin{align}\frac{d}{dx}\sigma(x)&=\frac{d}{dx}\left(\frac{1}{1+e^{-x}}\right)\\[2ex]
&=\frac{-(1+e^{-x})'}{(1+e^{-x})^2}\\[2ex]
&=\frac{e^{-x}}{(1+e^{-x})^2}\\[2ex]
&=\left(\frac{1}{1+e^{-x}}\right)\left(\frac{e^{-x}}{1+e^{-x}}\right)\\[2ex]
&=\left(\frac{1}{1+e^{-x}}\right)\,\left(\frac{1+e^{-x}}{1+e^{-x}}-\frac{1}{1+e^{-x}}\right)\\[2ex]
&=\sigma(x)\,\left(\frac{1+e^{-x}}{1+e^{-x}}-\sigma(x)\right)\\[2ex]
&=\sigma(x)\,(1-\sigma(x))
\end{align}$
Comment: that is why I appreciate your effort. you spend time to us OP's language!!
Comment: My understanding is that there are convexity issues that make the squared error minimization undesirable for non-linear activation functions. In matrix notation, it would be $\frac{\partial J(\theta)}{\partial \theta}=\frac{1}{m}X^\top\left( \sigma(X\theta)-\mathbf y\right)$.
Comment: +1 for all the efforts!, may be using matrix notation could be easier?
Comment: @hxd1011 Ty! Yes, no more $\sum$'s, but wanted to be consistent with OP.
Comment: @MohammedNoureldin I just took the partial derivative in the numerators on the prior line, applying the chain rule.
Comment: can I say in linear regression, objective is $\|Ax-b\|^2$ and derivative is $2A^Te$, where $e=Ax-b$, in logistic regression, it is similar, the derivative is $A^Te$ where $e=p-b$, and $p=\text{sigmoid}~(Ax)$ ?
Comment: Thanks! could you help me with this question here? https://stats.stackexchange.com/questions/278866/derive-logistic-loss-gradient-in-matrix-form
Comment: also what do you mean by "make the squared error minimization undesirable" ? do you mean in logistic regression setting, we are not minimize squared loss on predicted $p$ and $b$, but use logistic loss?
Comment: Right, the loss function from linear is not applicable to logistic regression.
Comment: ye, that's another view. I personally think the reason people use logistic loss over squared loss is because the probabilistic interpretation (maximize likelihood on binomial distribution) in addition to convexity  issue.
Comment: It seems to me, that the "minus" sign is lost in sigmoid function derivative. So, must be:
$\begin{align}
\sigma(x)\,\left(\frac{1+e^{-x}}{1+e^{-x}} - \frac{1}{1+e^{-x}}\right)\\[2ex]
\end{align}$
Comment: Thank you for this! May I ask to explain a bit more how did you derive the `σ` function in the 5th line?
Comment: @MohammedNoureldin I have included tags for every step so that they can be referenced with clarity, but it sounds as though you are just making reference to the sigmoid function?
Comment: @AntoniParellada, Thank you for your reply. You are right, I could not understand how you calculated the derivative of the Sigmoid `σ` function (so I could not get how you get the line number 5). Have you used some specific rule for it?
Comment: @MohammedNoureldin This is just saying that the [hypothesis function for logistic regression is the sigmoid function](https://www.internalpointers.com/post/cost-function-logistic-regression).
Comment: @AntoniParellada, ok you will probably laugh now, I have just seen that you already wrote the derivation of Sigmoid function at the end of your answer. Apparently I was too sleepy yesterday at night when I read the deriviation of the Cost function :). I will check it when I back to home. Thanks a lot!
Comment: @AntoniParellada, so everything is almost clear, but could you please tell at step 5 why did you multiply by the derivative of ´θ⊤x(i)´ (which represents x of Sigmoid function) after derivating sigmoid function? It is probably some rule I miss (sorry the last time I used calculus was 10 years ago).
Answer: To avoid impression of excessive complexity of the matter, let us just see the structure of solution.
With simplification and some abuse of notation, let $G(\theta)$ be a term in sum of $J(\theta)$, and $h = 1/(1+e^{-z})$ is a function of $z(\theta)= x \theta $:
$$ G = y \cdot \log(h)+(1-y)\cdot \log(1-h) $$
We may use chain rule:
$\frac{d G}{d \theta}=\frac{d G}{d h}\frac{d h}{d z}\frac{d z}{d \theta}$ and solve it one by one ($x$ and $y$ are constants).
$$\frac{d G}{\partial h} = \frac{y} {h} - \frac{1-y}{1-h}  = \frac{y - h}{h(1-h)}  $$
For sigmoid $\frac{d h}{d z} = h (1-h) $ holds,
which is just a denominator of the previous statement.
Finally, $\frac{d z}{d \theta} = x $.
Combining results all together gives sought-for expression:
$$\frac{d G}{d \theta} = (y-h)x  $$
Hope that helps.
Comment: This is just fine. I was having a hard time converting this into a matrix notation.
Answer: For those of us who are not so strong at calculus, but would like to play around with adjusting the cost function and need to find a way to calculate derivatives... a short cut to re-learning calculus is this online tool to automatically provide the derivation, with step by step explanations of the rule.
https://www.derivative-calculator.net
Answer: The credit for this answer goes to Antoni Parellada from the comments, which I think deserves a more prominent place on this page (as it helped me out when many other answers did not). Also, this is not a full derivation but more of a clear statement of $\frac{\partial J(\theta)}{\partial \theta}$. (For full derivation, see the other answers).
$$\frac{\partial J(\theta)}{\partial \theta} = \frac{1}{m} \cdot X^T\big(\sigma(X\theta)-y\big)$$
where
\begin{equation}
\begin{aligned}
X \in \mathbb{R}^{m\times n} &= \text{Training example matrix} \\
\sigma(z) &= \frac{1}{1+e^{-z}} = \text{sigmoid function} = \text{logistic function} \\
\theta \in \mathbb{R}^{n} &= \text{weight row vector} \\
y &= \text{class/category/label corresponding to rows in X}
\end{aligned}
\end{equation}
Also, a Python implementation for those wanting to calculate the gradient of $J$ with respect to $\theta$.
<code>import numpy
def sig(z):
return 1/(1+np.e**-(z))

def compute_grad(X, y, w):
    """
    Compute gradient of cross entropy function with sigmoidal probabilities

    Args:
        X (numpy.ndarray): examples. Individuals in rows, features in columns
        y (numpy.ndarray): labels. Vector corresponding to rows in X
        w (numpy.ndarray): weight vector

    Returns:
        numpy.ndarray

    """
    m = X.shape[0]
    Z = w.dot(X.T)
    A = sig(Z)
    return  (-1/ m) * (X.T * (A - y)).sum(axis=1)
</code>
Answer: Another presentation, with matrix notation.
Preparation: $\sigma(t)=\frac{1}{1+e^{-t}}$ has $\frac{d \ln \sigma(t)}{dt}=\sigma(-t)=1-\sigma(t)$ hence $\frac{d \sigma}{dt}=\sigma(1-\sigma)$
and hence $\frac{d \ln (1- \sigma)}{dt}=\sigma$.
We use the convention in which all vectors are column vectors. Let $X$ be the data matrix whose rows are the data points $x_i^T$. Using the convention that a scalar function applying to a vector is applied entry-wise, we have
$$mJ(\theta)=\sum_i -y_i \ln \sigma(x_i^T\theta)-(1-y_i) \ln (1-\sigma(x_i^T\theta))=-y^T  \ln \sigma (X\theta)-(1^T-y^T)\ln(1-\sigma)(X\theta).$$
Now the derivative (Jacobian, row vector) of $J$ with respect to $ \theta$ is obtained by using chain rule and noting that for matrix $M$, column vector $v$ and $f$ acting entry-wise we have $D_v f(Mv)=\text{diag}(f'(Mv))M$. The computation is as follows:
$$m D_\theta J= -y^T [\text{diag}((1-\sigma)(X\theta))] X-(1^T-y^T) [\text{diag}(-\sigma(X\theta))]X=$$
$$=-y^TX+1^T[\text{diag}(\sigma(X\theta))]X=-y^TX+(\sigma(X\theta))^TX.$$
Finally, the gradient is
$$\nabla_\theta J=(D_\theta J)^T=\frac{1}{m}X^T(\sigma(X\theta)-y)$$
Comment: This is an example of a generalized linear model with canonical activation function See also Bishop, "Pattern Recognition and Machine Learning", Section 4.3.6, p.212.


Collapsible fieldsets in form using button

Question: How can I make a button collapse a fieldset in my custom form? I tried the following which did not seem to work:
<code>$form['stepone']['next'] = array(
    '#type' => 'button',
    '#value' => t('Next'),
    '#name' => 'nextone',
    '#weight' => 1,
    '#submit' => array('collapse_next'),
    );
</code>
The function is:
<code>function collapse_next($form, &$form_state){
    if(isset($form_state['values']['nextone'])){
    $form['stepone']['#collapsed'] = TRUE;
    $form['steptwo']['image']['#collapsed'] = FALSE;
    return $form;
    }
}
</code>
Comment: "did not seem to work" is very vague. How did you expected it to work? What actually happened? Why built-in functionality wasn't sufficient for you? Why you seem not to use neither AJAX not JS, when the changes you want to happen are client-side ones? And so on...
Answer: you may use jquery for this. give some id to your button and on button click trigger the default fieldset collase or use toggle to hide show fieldset. Hope this help!
Answer: I figured this one out! If anyone needs, I am attaching my method. I added the following into my form:
<code>'#prefix' => '<class ="panel-collapse collapse fade" data-toggle="collapse" data-target="#name">
</code>


4th moment greater or equal to 2nd moment

Question: I'm trying to proof something, I've taken some steps and it eventually comes down to the fact that $\mathbb{E}(X^4) \geq \mathbb{E}(X^2)$ where X is any random variable. I'm now wondering if this is even true, and if so how i could proof it.
I've tried writing the expectation of $X^4$ as an integral and using integration by part twice to get something with of the form $A-B+12\mathbb{E}(X^2)$ but that didn't get me anywhere.
Help would be appreciated!
Comment: This is clearly false. take for example the pdf of x is the uniform distribution from 0 to 1
Answer: This clearly not true as the inequality is not dimensionless.
Let me give you an informal reasoning. Formal proof will require some kind of $\epsilon-\delta$ argument.
The left hand side has units U^4 and the right hand side has units U^2 where $U$ is the unit of $X$. To see what this means, let $Y_1=X/10$, $Y_2=X/100$, $Y_3=X/1000$ and so on, i.e. $Y_n =X/{10}^n$. Each $Y$ is just $X$ except unit of $Y$ is ${10}^n$ times the unit of $X$. i.e. If $X$ is some length measured in millimeters, $Y_1$ is measured in centimeters, $Y_2$ in decimeters etc. If the unit is chosen so high that $|Y|<1$ with very high probability. Note that for $|Y|<1$, $|Y|^4 < |Y|^2$ so $E(Y^4) < E(Y^2)$.
Answer: It can be shown that if X < Y then E(X) < E(Y). As long as your X values don't belong to (-1,0) or (0,1), then your statement is true.


docker chmod no such file or directory

Question: hi i need to chmod this file /root/Desktop/folderdocker/index.php
using chmod 774 command
here my dockerfile:
FROM php:7.4-cli

copy . index.php

RUN chmod -R 774 /Dekstop/folderdocker/index.php

RUN chown -R root /var/www

output:
Sending build context to Docker daemon  342.9MB
Step 1/4 : FROM php:7.4-cli
 ---> f4f453029716
Step 2/4 : copy . index.php
 ---> Using cache
 ---> bc9a68fff22f
Step 3/4 : RUN chmod -R 774 /Dekstop/folderdocker/index.php
 ---> Running in 4ddc85713576
chmod: cannot access '/Dekstop/folderdocker/index.php': No such file or directory```

Answer: You are copying the directory of the Dockerfile into a dir called index.php in the root of your image. Then you are referencing a file in a path that does not exist.
Make sure index.php is next to your Dockerfile, for the next command to work, or you would need to modify the source path.
You should COPY index.php /some/path/or/just/root/index.php make sure you RUN mkdir if the path does not exist. And then you can chown the file.
In order to be able to see the image you are inheriting from you can run:
docker run -it php:7.4-cli sh to get a shell in it and see the available dirs.

docker chmod no such file or directory


Lock association inside callback with ActiveRecord?

Question: Is it permissible to lock associated object inside ActiveRecord's callback?
class Product < ApplicationRecord
  # stock (integer)

  has_many :sales
end

class Sale < ApplicationRecord
  # quantity (integer)

  belongs_to :product

  before_save do
    self.product.lock!
    throw :abort if self.product.stock < self.quantity
  end
end

The idea is prevent creating Sale records with insufficient stock.
Answer: "Permitted" it may be but IMO it's not a good practice. Here is why I consider that:

Sale should not know how to lock the product, should not know the product attributes. It should be as dumb as possible.
Sale shouldn't dictate if it can be saved or not. IMO it should be more of a confirmation that a product was sold.
That lock is Pessimistic and it is mandatory to be wrapped in a transaction. I'm not seeing the entire flow, but if if it's not then then rollbacks will be incomplete (it happened to me)

When dealing with sales, payments (delicate Business flows) I usually extract the code in a Service Object. It can be tested in isolation, you don't pollute models with logic out of their scope, code quality principles are followed, etc.
Here is an empirical example:
class PurchaseProduct
  def initialize(product, quantity)
   @product = product
   @quantity = quantity
  end

  def purchase
    Product.transaction do
      @product.lock!
      if @product.stock >= @quantity
        product.update_attribute(:stock, @product.stock - @quantity)
        product.sales.create(quantity: @quantity)
        # other stuff to do
        # send confirmation email, etc
      else
        throw "Something"
      end
    end
  end
end

When writing software one very important aspect is to have loosely coupled code. You can check Principle of least knowlede for more info if you are not familiar. SRP is also a good one.
Answer: You may want custom validate:
class Sale < ApplicationRecord
  belongs_to :product

  validate :valid_quantity

  private

  def valid_quantity
    if quantity > product.stock
      errors.add(:quantity, "can't be greater than product stock")
    end
  end
end

With this validation, Sale won't be created with insufficient stock, and have a descriptive error message.
Comment: It is very likely that the site is an e-commerce one, so race conditions are very likely to happen, hence that `lock`. This validation works for simple scenarios but will fail when multiple users purchase the same thing on Black Fridays :D


How to compare a leapsecond with java.time and Junit?

Question: So I have a LocalDateTime parser which accepts a date as a String.
I have wrote several tests for it e.g. checking for leap year and some more.
Now I want a JUnit test to check for a leap second.
I did some research and found that probably it is not possible with java.time(?).
So far I found a suitable date on which a leap second occured.
Here is what I've tried so far:
@Test
@DisplayName("Check for leap second")
void shouldLeapSecondOccurReturnExactlyTwoMinuteSpan() {
    String leapSecond = "2015-06-30T23:59:00+0000";
    String leapSecond2 = "2015-07-01T00:01:00";
    Assertions.assertTrue(DateConvertUtils.parseIso8601ToUTC(leapSecond)
   .isEqual(LocalDateTime.parse(leapSecond2).minusSeconds(120)));
}

While DateConvertUtils contains my above mentioned parser and a custom DateTimeFormatter.
I appreciate any help to the right direction.
Comment: As far as I know, `java.time` will ignore leapseconds, see [How does the (Oracle) Java JVM know a leap second is occurring?](https://stackoverflow.com/questions/30984599/how-does-the-oracle-java-jvm-know-a-leap-second-is-occurring) (see the answer by Basil Bourque).
Comment: What is `DateConvertUtils`...?
Comment: As far as I know, the only leap second support in java.time is that for an `Instant` a leap seoncd can be parsed. For example `Instant.parse("2015-06-30T23:59:60Z")` works (but yeilds `2015-06-30T23:59:59Z` because `Instant` cannot *hold* a leap second).
Answer: Well, leap seconds are ignored by most libraries including java.time-package. My lib Time4J however, supports it. Example using your input:
    String s1 = "2015-06-30T23:59:00+0000";
    String s2 = "2015-07-01T00:01:00"; // no offset information

    ChronoFormatter<Moment> f =
        ChronoFormatter
            .ofMomentPattern(
                "uuuu-MM-dd'T'HH:mm:ss[X]", // optional offset
                PatternType.CLDR,
                Locale.ROOT,
                ZonalOffset.UTC); // default offset

    Moment m1 = f.parse(s1);
    Moment m2 = f.parse(s2); // no offset => UTC

    long delta = SI.SECONDS.between(m1, m2);
    System.out.println(delta); // 121

Objects of type Moment can be easily converted to/from java.time.Instant if you are willing to loose the possible leap second information (in case you have 2015-06-30T23:60Z).
Good to know: You really need at least a class connected to UTC timeline. Classes like LocalDateTime have no concept of time zones so they have no connection to UTC and are unusable for determining leap seconds by definition.
More background information can be found in my DZone paper.


Find domain and range of relation

Question: Find domain and range of relation : $\;R=\{ (a , b)\in\Bbb N\times\Bbb R^+\; :\; ⌊b⌋ −2 \le a\le⌊b⌋+2 \}\;$. $\Bbb R^+\;$ means real positive numbers and $\;\Bbb N\;$  natural numbers
Can someone explain it ? My teacher said something like you put a=b in this and there comes domain as Natural numbers? I dont understand this... I know for a fact that if we have Relation like <1,5> <4,6> then domain is 1,4 and range is 5,6 but I dont understand how to calculate this in this case...
Comment: Do you know *the definition* of domain and range of relation? Write it down in your question, at least.
Comment: I mean I know like domain is N in this case and range is R+... I think...
Comment: Yep, that is all. More interesting would have been to ask about the relation's **image** ...
Comment: I see but what if I didnt know that what i'm looking for is just in front of my eyes? My teacher doesn't accept short anwsers....
Answer: In your question: $R=\{(a,b)∈\Bbb N×\Bbb R^+:⌊b⌋−2≤a≤⌊b⌋+2\} $
The first part: $(a,b)∈\Bbb N×\Bbb R^+$ means that a belongs to natural numbers and b belongs to positive real numbers.
So we can first limit the domain (values of a) to Natural numbers and range (values of b) to Real positive numbers.
But that is not enough to find the actual domain as a and b must also satisfy the condition given.
However in your case... $b∈\Bbb R^+$ so $b∈(0,∞)$ so for a also $a∈[0,∞)$ but only natural numbers.
So your domain is natural numbers and range is positive real numbers.

Find domain and range of relation


why some css codes doesnt work on android 4 or less?

Question: I am using phonegap to mobile application
this simple code was used to give the background an effect
background:radial-gradient(#f2f2f2,#8d8d8d);
it works fine in the browser and on android 5
but when i tested it on a 4.0.3 device, it didnt works at all along with some other css codes.
why is this happening and how to solve it ?
Comment: Try prefixing with -webkit, `-webkit-radial-gradient`
Answer: Properties in CSS are sometimes not universally supported on all devices or browsers at certain periods in time.
Vendor prefixes are used when software versions are shipped with proactive support, before the property had reached the approved specification.
You need to check if your target platform has support.
http://caniuse.com/#search=gradients
You can see Android 4.1 uses the -webkit- prefix. You can try this for Android 4.0.3, it may very well work.
See also: https://developer.mozilla.org/en-US/docs/Web/CSS/radial-gradient#Browser_compatibility

Example of how to use vendor prefixes.
Template: -#{PREFIX}-#{PROPERTY}
Gradients are a little special in nature, the prefix is on a function of sorts, inside the value of the property.
.example {
  background-image: -webkit-radial-gradient(#222, #333);
  background-image: radial-gradient(#222, #333);
}

Most vendor prefixes look like like:
.example2 {
  -webkit-animation: 1s anim;
  -moz-animation: 1s anim;
  animation: 1s anim;
}

Comment: could you give an example on how to apply it ? your ref was very good but   I couldn't apply the syntax
Comment: @IbrahimHero Take a look.


Front page link token in full HTML?

Question: When Im using the full html input format. Is there a way I can use some code to print the url of the front page of my site, so If I were to migrate the site it would still work?
I could do this with pathlogic but im wondering if there is a php type value that full html would use?
Thanks
Answer: Front page

But this doesn't respect the base folder. E.g. if your Drupal installation path looks like http://example.com/drupal_site/, this won't help you, because link will lead to http://example.com/.

Front page link token in full HTML?


expression must have a constant value and did not evaluate to a constant

Question: #include <iostream>
#include <deque>
using namespace std;

void printKMax(int arr[], int n, int k) {
    deque<int>dq;
    int i;
    for (i = 0; i < k; i++) {
        while (dq.back() < arr[i]) {
            dq.pop_back();
            dq.push_back(i);
        }
    }
    for (; i < n; i++) {
        cout << dq.front();
        while (dq.front() <= i - k) {
            dq.pop_front();
        }
        while (dq.back() < arr[i]) {
            dq.pop_back();
            dq.push_back(i);
        }
    }
    cout << dq.front();
}

int main() {

    int t;
    cin >> t;
    while (t > 0) {
        int n, k;
        cin >> n >> k;
        int i;
        int arr[n];
        for (i = 0; i < n; i++)
            cin >> arr[i];
        printKMax(arr, n, k);
        t--;
    }
    return 0;
}

I don't know where I did wrong in program that caused the following errors appear:

This error appears when program run to this code in arr[n]

expression must have a constant value
the value of variable cannot be used as a constant
expression did not evaluate to a constant
If someone knows how to solve above errors, please help me.
Thanks you so much.
Comment: `int arr[n];` - `n` is not a constant expression. Use `std::vector arr(n);` and be happy.
Comment: `int arr[n];` requires that n be a compile time constant.
Comment: @TedLyngmo is right. Use a vector instead of an array.
Comment: @fireshadow52 `-Weverything -pedantic-errors` usually irons out further attempts. With newer `gcc` I've found the `-fanalyzer` to be brilliant too.
Comment: @TedLyngmo Ah, thanks. I'd forgotten about those flags. Those warnings do appear now.
Comment: @fireshadow52 Warnings you say? Darn. Add: `-Werror` :-)
Comment: @TedLyngmo That definitely stopped compilation.
Comment: Interestingly, this compiles under clang 14.0.3...but the program generates a segmentation violation due to `dq.back()` being called without first checking the size...
Answer:
I don't know where I did wrong in program that caused the following errors appear:
This error appears when program run to this code in arr[n]

The C++ standard doesn't require that a conforming compiler supports Variable Length Arrays.
Dynamic stack allocations may be supported by the implementation anyway. Many compilers support alloca/_alloca to do a stack allocation that will be automatically freed at the end of the function.
The general advice is to not use these. The stack should not be the target of a user-selectable amount of data.
In C++, there's a simple stand-in solution. Allocate the memory dynamically (on the heap, if there is one):
#include <vector>

//...

std::vector<int> arr(n);

From a user's perspective, this is an almost perfect stand-in for int arr[n];


Show that the two random variables with F-distribution are independent

Question: The following is an exercise problem from Hogg, McKean and Craig's book titled "Introduction to Mathematical Statistics" that I am working on.
3.6.16. Let $X_1$, $X_2$, and $X_3$ be three independent chi-square variables with $r_1$, $r_2$, and $r_3$ degrees of freedom, respectively.
(a) Show that $Y_1 = X_1/X_2$ and $Y_2 = X_1 + X_2$ are independent and that $Y_2$ is
$\chi^2(r_1 + r_2)$.
(b) Deduce that
$$\cfrac{X_1 /r_1}{X_2/r_2} \ \ \textrm{ and } \ \ \cfrac{X_3 /r_3}{(X_1 + X_2)/(r_1 + r_2)}$$ are independent F-variables.
My attempt:
(a) The inverse of the transformations is given by $X_1 = Y_1Y_2/(1+Y_1)$ and $X_2=Y_2/(1+Y_1)$ and the Jacobian is the determinant of
$$ \begin{bmatrix} \frac{\partial x_1}{\partial y_1} & \frac{\partial x_1}{\partial y_2} \\ \frac{\partial x_2}{\partial y_1} & \frac{\partial x_2}{\partial y_2} \end{bmatrix} = \begin{bmatrix} y_2/(1+y_1)^2 & y_1/(1+y_1) \\ -y_2/(1+y_1)^2 & 1/(1+y_1) \end{bmatrix} $$ which ends up being $y_2/(1+y_1)^2$. The joint pdf of $Y_1$ and $Y_2$ is then given by
\begin{align}f_{Y_1,Y_2}(y_1,y_2) &= \cfrac{y_2}{(1+y_1)^2} \left( \cfrac{y_1y_2}{1+y_1} \right)^{\left(\frac{r_1}{2}-1\right)}\cfrac{e^{-\left(\frac{y_1y_2}{2(1+y_1)}\right)}}{2^{\frac{r_1}{2}}\Gamma(\frac{r_1}{2})}\left( \cfrac{y_2}{1+y_1} \right)^{\left(\frac{r_2}{2}-1\right)}\cfrac{e^{-\left(\frac{y_2}{2(1+y_1)}\right)}}{2^{\frac{r_2}{2}}\Gamma(\frac{r_2}{2})} \\ &= \cfrac{1}{2^{\left(\frac{r_1+r_2}{2}\right)}\Gamma(\frac{r_1}{2})\Gamma(\frac{r_2}{2})} \times
\left[ \frac{y_1^{\left( \frac{r_1}{2}-1\right)}}{(1+y_1)^{\left(\frac{r_1+r_2}{2} \right)}}\right]
\times\left[y_2^{\left(\frac{r_1+r_2}{2}-1\right)}e^{-\left(\frac{y_2}{2}\right)}\right]\end{align}.
With that, the pdf nicely splits into a product of a function in $y_1$ alone, and that of $y_2$ alone thereby letting us to conclude that $Y_1$ and $Y_2$ are independent. We need to be careful about the support before we conclude that but as the support is that of positive real numbers for both of them, we should not have a problem on that front.
(b) Let $Y_3 = X_3/(X_1+X_2)$. We need to show that $Y_1$ and $Y_3$ are independent. I will spare the details but taking an approach similar to the one above, we get the Jacobian to be $y_2^2/(1+y_1)^2$ and the joint pdf to be
\begin{align}f_{Y_1,Y_2,Y_3}(y_1,y_2,y_3) &= \cfrac{1}{2^{\left(\frac{r_1+r_2+r_3}{2}\right)}\Gamma(\frac{r_1}{2})\Gamma(\frac{r_2}{2})\Gamma(\frac{r_3}{2})} \times
\left[ \frac{y_1^{\left( \frac{r1}{2}-1\right)}y_3^{\left(\frac{r_3}{2}-1\right)}}{(1+y_1)^{\left(\frac{r_1+r_2}{2} \right)}}\right]\times
\left[y_2^{\left(\frac{r_1+r_2+r_3}{2}-1\right)}e^{-\left(\frac{y_2(1+y_3)}{2}\right)}\right].\end{align}
Clearly, $Y_1$, $Y_2$ and $Y_3$ are not jointly independent. However, when we integrate out $y_2$ over its support to obtain the marginal pdf $f_{Y_1,Y_3}(y_1,y_3)$, the expression in the second square braces integrates to some kind of Gamma function in $y_3$ along with possibly another factor involving $2$ rasied to a power of $y_3$, which will let us conclude that $Y_1$ and $Y_3$ are independent.
This still lacks details but I think I have explained my main thought process above. Here are my questions to you.

Do you find any error in this thought process?

More importantly, is there a more elegant/sleek solution to part (b) that uses part (a) and some other result? Please note that $Y_1$, $Y_2$, and $Y_3$ are not mutually independent. If they were, part (b) would have been quite trivial.

I am asking this question mainly because in part (b) he starts with "Deduce that ..." by which I feel like there might be a one liner to deduce that $Y_1$ and $Y_3$ are independent. Is there a simpler solution? Please let me know.
Answer: Your answer to (a) is correct.
For part (b), the two random variables are $F$-distributed by construction. We could prove that they're independent by establishing joint independence of $Y_1, Y_2, X_3$.
I believe you can do this via the joint moment-generating function. We have
$$
 M_{Y_1,Y_2,X_3}(s,t,u) = \mathbb{E}[\exp(sY_1+tY_2+uX_3)]=\mathbb{E}[\exp(uX_3) \mathbb{E}[\exp(sY_1+tY_2|X_3)] ]
$$
We can drop the conditioning on $X_3$ in the inner expectation, as
$ (Y_1, Y_2) $ are constructed from $(X_1,X_2)$ only. This gives
$$
 \mathbb{E}[\exp(sY_1+tY_2|X_3)]=\mathbb{E}[\exp(sY_1+tY_2)]=M_{Y_1,Y_2}(s,t)=M_{Y_1}(s)M_{Y_2}(t),
$$
by independence of $Y_1,Y_2$.
Putting it all together, we have
$$
M_{Y_1,Y_2,X_3}(s,t,u)=\mathbb{E}[\exp(uX_3) M_{Y_1}(s)M_{Y_2}(t)] =M_{Y_1}(s)M_{Y_2}(t)M_{X_3}(u)
$$
and we have joint independence.
Comment: So even if we grant that $Y_1$, $Y_2$, and $X_3$ are mutually independent, we eventually need to deduce that $Y_1$ and $Y_3 \triangleq X_3/Y_2$ are independent. So does it follow that because $Y_1$, $Y_2$, and $X_3$ are mutually independent, $Y_1$ and $Y_3 \triangleq X_3/Y_2$ are independent? I think so because then $Y_1$ and $f(Y_2,X_3)$ will be independent so we have our result. Is that right?
Comment: Yes, you can deduce that if you have mutual independence, though not pairwise independence (as in my initial, wrong, answer).
Comment: Thanks. So basically I had to look at $Y_1$, $Y_2$ and $X_3$ whereas I was looking at $Y_1$, $Y_2$ and $Y_3$. That's pretty cool!
Comment: It's a nice question, though I wonder if the authors realised that the obvious answer is wrong! It definitely fooled me.
Comment: @TryingHardToBecomeAGoodPrSlvr, I didn't study Hogg&Craig but if I remember, I came across a solution manual or so.
Comment: That's the reason why I feel like these texts should have a solution manual so that we know what they had in their minds. Thanks a lot once again for the cool solution and for your time Sir!
Comment: @User1865345 That is true but it has solutions to selected exercises ... usually the ones that are "easier" :).
Answer: To show the independence of $\frac{X_1/r_1}{X_2/r_2}$ and $\frac{X_3/r_3}{(X_1 + X_2)/(r_1 + r_2)}$ in part (b), denote $\frac{X_1/r_1}{X_2/r_2}$ by $Z_1$,
$\frac{X_1 + X_2}{r_1 + r_2}$ by $Z_2$, and $\frac{X_3}{r_3}$ by $Z_3$.  By the assumption and the result of part (a), we have $Z_1 \perp Z_2$, $Z_3 \perp (Z_1, Z_2)$, whence by Theorem 20.3 in Probability and Measure by Patrick Billingsley, for any $x, y \in \mathbb{R}$:
\begin{align}
 & P\left[\frac{X_1/r_1}{X_2/r_2} \leq x, \frac{X_3/r_3}{(X_1 + X_2)/(r_1 + r_2)} \leq y\right] \\
=& P\left[Z_1 \leq x, \frac{Z_3}{Z_2} \leq y\right] \\ \tag{by $Z_3 \perp (Z_1, Z_2)$}
=& \int_0^\infty P[Z_1 \leq x, z Z_2^{-1} \leq y]f_{Z_3}(z)dz \\
=& \int_0^\infty P[Z_1 \leq x]P[z Z_2^{-1} \leq y]f_{Z_3}(z)dz \\ \tag{by $Z_1 \perp Z_2$}
=& P[Z_1 \leq x]\int_0^\infty P[z Z_2^{-1} \leq y]f_{Z_3}(z)dz \\ \tag{by $Z_3 \perp Z_2$}
=& P[Z_1 \leq x]P\left[\frac{Z_3}{Z_2} \leq y\right] \\
=& P\left[\frac{X_1/r_1}{X_2/r_2} \leq x\right]P\left[\frac{X_3/r_3}{(X_1 + X_2)/(r_1 + r_2)} \leq y\right].
\end{align}
This shows $\frac{X_1/r_1}{X_2/r_2}$ and $\frac{X_3/r_3}{(X_1 + X_2)/(r_1 + r_2)}$ are independent.

Maybe an overkill (but it sheds some geometric insight of this fact), another way to prove part $(a)$ is using spherical coordinates.  Since $X_1 \sim \chi_{r_1}^2, X_2 \sim \chi_{r_2}^2$ are independent, we have
\begin{align}
& X_1 \overset{d}{=} Z_1^2 + \cdots + Z_{r_1}^2, \\
& X_2 \overset{d}{=} Z_{r_1 + 1}^2 + \cdots + Z_{r_1 + r_2}^2,
\end{align}
where $(Z_1, \ldots, Z_{r_1 + r_2}) \sim N_{r_1 + r_2}(0, I_{(r_1 + r_2)})$.
Express $Z_1, \ldots, Z_{r_1 + r_2}$ in spherical coordinates:
\begin{align}
& Z_1 = R\cos(\Theta_1) \\
& Z_2 = R\sin(\Theta_1)\cos(\Theta_2) \\
& \cdots\cdots \\
& Z_{r_1 + r_2 - 1} = R\sin(\Theta_1)\cdots\sin(\Theta_{r_1 + r_2 - 2})\cos(\Theta_{r_1 + r_2 - 1}) \\
& Z_{r_1 + r_2} = R\sin(\Theta_1)\cdots\sin(\Theta_{r_1 + r_2 - 2})\sin(\Theta_{r_1 + r_2 - 1}),
\end{align}
then
\begin{align}
& \frac{X_1}{X_2} \overset{d}{=}
\frac{Z_1^2 + \cdots + Z_{r_1}^2}{Z_{r_1 + 1}^2 + \cdots + Z_{r_1 + r_2}^2} = f(\Theta_1, \ldots, \Theta_{r_1 + r_2 - 1}), \\
& X_1 + X_2 \overset{d}{=} Z_1^2 + \cdots + Z_{r_1 + r_2}^2 = R^2. \tag{1}
\end{align}
Since $R$ and $(\Theta_1, \ldots, \Theta_{r_1 + r_2 - 1})$ are independent (because the joint density of them factorizes), it follows by $(1)$ that $\frac{X_1}{X_2}$ and $X_1 + X_2$ are independent.
Comment: Thank you. Also, the thing is that if we know $Z_1=X_1/X_2$, $Z_2=X_1+X_2$, and $X_3$ are mutually independent, then $Z_1$ and $f(Z_2,X_3)$ are independent for any measurable function $f$. I was looking at an inconvenient triplet but as it turns out, the right triplet to consider was $Y_1$, $Y_2$ and $X_3$. That's where I went wrong. :(
Comment: With regards to the addition (proof of part (a)), it's a cool thing to express it in terms of spherical co-ordinates. Most of mathematics/problem solving is about seeing "how things move" or "recognizing certain structure" in a given mathematical setup and in this instance, these RVs looked at as co-ordinates helps a lot. Thanks for sharing it with us!


Deserialize json to subtypes with complex constructor Kotlin + Jackson

Question: I am using kotlin and jackson and I am looking for a way to deserialize json with the following structure, that can vary between two cases:
Case 1:
{
    "parent1": "parentvalue1",
    "parent2": "parentvalue2",
    "child1": "childvalue1",
    "child2": "childvalue2"
}

Case 2:
{
    "parent1": "parentvalue1",
    "parent2": "parentvalue2",
    "child3": "childvalue3",
    "child4": "childvalue4"
}

My Model classes look as follows:
Parent:
open class Parent(
    val parent1: String,
    val parent2: String
) {
    constructor (parent: Parent) : this(
        parent1 = parent.parent1,
        parent2 = parent.parent2
    )
}

Children:
class Child1(
    parent: Parent,
    val child1: String,
    val child2: String
) : Parent(parent)

class Child2(
    parent: Parent,
    val child3: String,
    val child4: String
) : Parent(parent)

Is it doable to deserialize such structure using Jackson?
val parent = ObjectMapper().readValue(myJson, Parent::class.java)

Comment: Try [this guide](https://stackabuse.com/reading-and-writing-json-in-kotlin-with-jackson/). Let me know if it helps.
Comment: Hello @AliasCartellano, thank you for your answer.

Mentioned guide does not cover inheritance - my case is not bidirectional relationship.
Answer: You have a couple of things missing. First you will need to declare the constructor to use when deserializing the JSON, the ones that you have do not match JSON format:
class Child1(
    parent: Parent,
    val child1: String,
    val child2: String
) : Parent(parent) {

  @JsonCreator constructor (parent1: String, parent2: String, child1: String, child2: String) {
      super(parent1, parent2)
      child1 = child1
      child2 = child2
  )
}

Then, you also need to tell Jackson how to differentiate between Child1 and Child2. You can do it as follows:
@JsonTypeInfo(
  use = JsonTypeInfo.Id.NAME,
  include = JsonTypeInfo.As.PROPERTY,
  property = "type")
@JsonSubTypes(
  JsonSubTypes.Type(value = Child1::class, name = "child1"),
  JsonSubTypes.Type(value = Child2::class, name = "child2")
)
open class Parent(
    val parent1: String,
    val parent2: String
) {
    constructor (parent: Parent) : this(
        parent1 = parent.parent1,
        parent2 = parent.parent2
    )
}

In your case, you can even use the following given that the fields in the subtypes are different between them and thus Jackson can infer which subtype it is:
@JsonTypeInfo(use=Id.DEDUCTION)
@JsonSubTypes(
  JsonSubTypes.Type(value = Child1::class, name = "child1"),
  JsonSubTypes.Type(value = Child2::class, name = "child2")
)
open class Parent(
    val parent1: String,
    val parent2: String
) {
    constructor (parent: Parent) : this(
        parent1 = parent.parent1,
        parent2 = parent.parent2
    )
}

Comment: Thank you for your answer. Your solution introduces problem that I am trying to avoid - having 20 children, I need to copy 20 times parameters of Parent (parent1, parent2) into children's constructor, and if there is a need to introduce a new parameter in Parent, I will need to add it in 20 places as well.
Comment: That is true, but I am not sure if you can avoid it. Maybe you can try a custom deserializer, but I would say that it is not prettier than this.
Comment: If there was a way to access original value by some annotation I think it would be possible:

`@JsonCreator
constructor(@JsonProperty child1: String, @JsonProperty child2: String, @JsonParentTypeSomehow(like original value)
parent: Parent)`
Comment: Additionally, it seems there is a feature in Jackson that allows you to avoid having the `type` property to handle subtypes: `@JsonTypeInfo(use=Id.DEDUCTION)`. Never used it, but it seems it works if the fields in the subtypes are different between them and thus Jackson can infer which subtype it is.


Solution to Locomotive Problem (Mosteller, Fifty Challenging Problems in Probability)

Question: My question concerns the solution Professor Mosteller gives for the Locomotive Problem in his book, Fifty Challenging Problems in Probability. The problem is as follows:
A railroad numbers its locomotives in order 1, 2, ..., N. One day you see a locomotive and its number is 60. Guess how many locomotives the company has.
Mosteller's solution uses the "symmetry principle". That is, if you select a point at random on a line, on average the point you select will be halfway between the two ends. Based on this, Mosteller argues that the best guess for the number of locomotives is 119 (locomotive #60, plus an equal number on either "side" of 60 gives 59 + 59 + 1 = 119.
While I feel a bit nervous about challenging the judgment of a mathematician of Mosteller's stature, his answer doesn't seem right to me. I've picked a locomotive at random and it happens to be number 60. Given this datum, what number of locomotives has the maximum likelihood?
It seems to me that the best answer (if you have to choose a single value) is that there are 60 locomotives. If there are 60 locomotives, then the probability of my selecting the 60th locomotive at random is 1/60. Every other total number of locomotives gives a lower probability for selecting #60. For example, if there are 70 locomotives, I have only a 1/70 probability of selecting #60 (and similarly, the probability is 1/n for any n >= 60). Thus, while it's not particularly likely that there are exactly 60 locomotives, this conclusion is more likely than any other.
Have I missed something, or is my analysis correct?
Comment: This is a specific subcase of the [German tank problem](http://en.wikipedia.org/wiki/German_tank_problem).
Comment: In a sense, it depends a little bit on how you define "best", but in any event, there are generally better estimators than just choosing the number you've seen. Indeed, consider that your estimate is *a fortiori* less than or equal to the true number. In statistical parlance, your estimate is (negatively) *[biased](http://en.wikipedia.org/wiki/Bias_of_an_estimator)*. (Bias is not wholely a bad thing, but in this case, you can remove the bias *and* reduce the variance simultaneously.)
Comment: Here's my first thoughts: You seem to be analyzing the question from the perspective of "If there are N locomotives, what is the chance of seeing Locomotive 60?", while the correct way to analyze it is "If I see Locomotive 60, what is the chance that there are N locomotives."
Comment: I tagged this as a recreational problem, but the German Tank Problem, of which this is a special case, couldn't be further from recreational! Thanks to the commenters and answerer for setting me straight on the bias issue.
Answer: Choosing $2\times 60 - 1$ gives an unbiased estimate of $N$.
Choosing $60$ gives a maximum likelihood estimate of $N$.
But these two types of estimator are often different, and indeed this example is the one used by Wikipedia to show that the bias of maximum-likelihood estimators can be substantial.
Comment: (+1) Indeed, it is not hard to show that $2 X - 1$ is the *only* unbiased estimator of $N$ (hence it is also, trivially, the unique uniformly minimum variance unbiased estimator).
Comment: @Shreyans $2X-1$ (not $2N-1$) is unbiased because $\mathbb E[X\mid N]=\frac{N+1}{2}$ so  $\mathbb E[2X-1 \mid N]=N$
Comment: How can we show that 2N-1 is an unbiased estimator (and the only one)?


squid still caching

Question: I have set this
maximum_object_size 0 KB
minimum_object_size 0 KB
acl all src 0.0.0.0/0.0.0.0
no_cache deny all

but when I do test for the caching
curl -x ip1:3128 http://blabla.com/iptest.php
sleep 5
curl -x ip2:55555 http://blabla.com/iptest.php

both will return only the fist ip (ip1)
If the sleep is bigger that 30, it will show the ip2 too
LATER EDIT:
I see that the problem is if you hit the squid with 2 connsecutive requests it will always exit on the internet with the ip used on the first request
Comment: Which is your curl version ?
Comment: is also happening in firefox... so I'm pretty sure that isn't from curl
Answer: Shouldn't it be cache deny all instead of no_cache deny all?
Comment: Just to make sure I understand this correctly: This is a single instance of squid answering on two different IP addresses and two different ports? Have you checked the access log on blabla.com to see if there is indeed only one request?
Comment: So the problem is not squid but your test. You are starting with wrong assumptions. If you access squid on one ip address, there is no guarantee that squid itself will fetch the requested data using that same ip address. This is not a caching issue.
Comment: tried that too and doesn't work
Comment: Maybe I didn't explained very clear. I made 2 requests, one after the other on a ip verification page on. The second request is served from the cache instead of  sending to the verification page
Comment: Sorry but I still don't get it. What do you test exactly? What is ip1:3128 and what is ip2:55555? What happens and what do you expect to happen?
Comment: made a later edit "I see that the problem is if you hit the squid with 2 connsecutive requests it will always exit on the internet with the ip used on the first request". Thanks for the interest
Answer: In addition to Oliver's answer, you can also put this to your squid.conf
refresh_pattern . 0 100% 0

Comment: tried that too and doesn't work

squid still caching


Using Rules to Prune Inactive Users - will this work?

Question: I am relatively new to Drupal and learning as I develop a new website for our network of chapters. Needless to say, that has been quite the challenge :)
One of the functionalities I needed was the ability to prune inactive users. We have this on our current system and like having it to keep our membership numbers accurate, as well as help our members feel more "secure" by knowing only active members have access to the chapter.
There were a couple of modules out there to do this, but they were all for Drupal 6 or only had development modules for Drupal 7.
I found this link:
https://drupal.org/node/1582710
and have followed it to set up my own "prune users" using rules. But I have NO idea how to test this in the development stage and certainly don't want it to screw up and accidentally mess up memberships when we move to the live versions.
What I have done is as follows:
Set up the following components:

Send email stating user has been inactive for 30 days and schedule next letter in 30 days
Send email stating user has been inactive for 60 days and schedule next letter in 25 days
Send email stating user is in danger of being pruned and must login to keep account and schedule blocking user in 5 days.
Block user

Set up the following rules:

Inactive User: When user logs in, remove all currently scheduled rules and set component evaluation for inactive for 30 days letter.
Never Logged In: When user account is first approved, set component evaluation for inactive for 30 days letter.

I think this means that when a user first signs up, they will get scheduled for the inactive user letter. Every time they log in, it will reset to the 30 day letter in 30 days. If they never log in, in 30 days, they will get the first letter and the 2nd letter will schedule... until they are pruned. If they log in, everything resets.
I know this means that every user will have a scheduled inactive user letter task -- not sure if this will bog down my system too much?
Also, if this does sound right -- how do I go about testing it to make sure it works the way it is supposed to and doesn't accidentally block users its not supposed to?
One last thing -- what can I do to automatically delete the users that are "blocked" and have been members for a certain amount of time (to ensure that new members who are just not approved yet don't accidentally get "pruned"?)
If I have made a complete mess of this, please feel free to tell me so! Like I said, I am really new at this and still figuring it out as I go along... I appreciate any help you can give me!
Comment: Apologies. Since this was posted such a long time ago, I have just been using the above solution without too much issue. I am currently working on another project, but will try this option when I go back to these sites for updating. Thanks again for your answer!
Answer: Your solution "may" work, but another approach is to process the results of a VBO enabled view via Rules, whereas your rule performs 2 Rules Actions:

load the VBO list of users that are candidate for deletion.
add a loop to perform a Rules Action "delete user", for each user contained in your Views results.

For more details, have a look at the tutorial "How to process the results of a VBO enabled view via Rules?".
The advantage is that, if you first run it manually, you can see which users will get deleted (simply by browsing the Views results). When you feel comfortable enough, just automate this by rescheduling it every day (at the end of the rule itself, after you first triggered the rule manually).


RoR Impressionist capture external click

Question: I'm using Impressionist gem to record page views.
Let's say I have a Question model and an Answer model:
question.rb:
class Question< ActiveRecord::Base
  has_many :answers
  is_impressionable

answer.rb:
class Answer< ActiveRecord::Base
  belongs_to :question
  is_impressionable

In the questions\show.html.erb, I have a list of answers.
When a user hovers over an answer, they see 2 links appear:  details and go to.
The details link just opens the answer show page (which provides more details) and records a show impression and works correctly:
answers_controller.rb:
 impressionist(@answer)

The go to page is tricker.  When the user clicks on the link:
 <%= link_to "Go to", :class => 'answer_link', :remote=>:true,
       :data=>{outval: answer.type_url, cuid: answer.id.to_s, refval: user_question_path(@question.user, @question), txtval: answer.img_url} do %>

               <%= image_tag(answer.img_url, :alt => 'go to answer', :class =>'img_answer')  %>

  <% end %>

, I run the following jQuery snippet:
 $('.answer_link').click (->
    url_val=''
    url_val = "http://api.somewebservice.com/api/click?key=abc123"
    url_val += "&out=" + $(this).data('outval')
    url_val += "&loc=" + $(this).data('refval')
    url_val += "&cuid=" + $(this).data('cuid')
    url_val += "&ref=" +  $(this).data('refval')
    url_val += "&txt=" +  $(this).data('txtval')
    window.open url_val
  )

This works correctly and runs a web service and then opens the returned external link in a new window.
My problem is trying to capture this external click as an impression.

What is the best way to do this?
Currently, when I click the external link, the Questions controller, show action runs.

Specific question #1:  This "seems" correct in that I don't want to go anywhere (another window has already opened with the external link).  Is it correct to use the questions controller, show action and if params[:id]=="Go to" then just stay put.

Specific question #2:  If this is correct, then is there a way to send Impressionist an :action_name (I'd send "go to" instead of the default that it's sending now of "show":

Specific question #3:  If it's not correct, then what should I be doing?  Perhaps going to the Answers controller, to a custom-defined "go_to"?
TIA
Answer: OK - I have a solution (not sure if it's the best one but it works)

question.rb and answer.rb from above stay the same
To count the impressions of the answer show.html.erb page, I still use in the answer_controller, :
 impressionist(@answer)

To count the impressions when I redirect from answer, I created a new def in the answer_controller:
def visit_external_site
  @answer= Answer.find(params[:answer_id])
  impressionist(@answer)
  respond_to do |format|
    format.html {render :nothing => true, :status => 200}
  end
end

(The part I'm not sure about is the format.html (I don't want to go anywhere but still....)

My routes.rb has this:
match '/:answer_id' => 'answers#visit_external_site', :as => 'visit_external_site'

The questions\show.html.erb page has this now:

 <% @question.answers.each do |answer| %>
  <%= link_to url_for(:controller => :answers, :action => :visit_external_site, :answer_id => answer),
                          :class => 'answer_link', :remote => true,
                          :data => {outval:
                                            answer.type_url, cuid: answer.id.to_s, refval:
                                            user_question_path(@question.user, @question), txtval:
                                            answer.img_url} do %>

                    <%= image_tag(answer.img_url, :alt => 'go to answer', :class => 'img_answer') %>

              <% end %>
<% end %>

My jQuery is still the same as above

If anyone has a better way to doing this, do tell; I'm not sure if this way is "kosher"


Why is there a unique ordinal $\alpha$ for every infinite cardinal $\kappa$ such that $\kappa = \aleph_\alpha$?

Question: For finding the $\alpha$ I literally can't get any further than writing out the definitions.
For the second part:
Suppose $\aleph_\alpha = \aleph_\beta$ and $\beta \neq \alpha$. Then either $\alpha \in \beta$ or $\beta \in \alpha$. Assume w.l.o.g. that $\alpha \in \beta$. If $\beta$ is a limit ordinal, then $\aleph_\beta = \bigcup \{\aleph_\gamma \ | \ \gamma < \beta\}$, so maybe that is strictly greater than $\aleph_\alpha$, but I can't prove that, and I don't know what to do if $\beta$ is not a limit ordinal.
Comment: I'd think $\alpha$ would the ordinality of the set of cardinals $\kappa'$ such that $\aleph_0 \le \kappa' < \kappa$.
Comment: Do note this requires choice, and maybe heavily depend on what you already proved.
Answer: First, use transfinite induction to prove that $\alpha\le \aleph_{\alpha}$ for all ordinal alpha.
From that we get $\kappa\le\aleph_{\kappa}$, so the following is well defined: $$\alpha=\min\{\beta\in On\mid \kappa\le \aleph_{\beta}\}$$
Now try proving that this $\alpha$ is indeed the alpha you are searching for.
For the other part, define
$$\alpha=\min\{\beta\in On\mid \exists\gamma\in On(\gamma\ne \beta\land \aleph_\beta=\aleph_\gamma)\}$$
And
$$\beta=\min\{\gamma\in On\mid \gamma\ne\alpha\land \aleph_\alpha=\aleph_\gamma\}$$
Now we get $\aleph_\alpha<\aleph_{\alpha+1}\le\aleph_\beta$


Pemutations and combinations problem

Question: I have this one question in statistics:
There are five women and six men in a group. From this group a committee of 4 is to be chosen. In how many ways can the committee be formed if the committee is to have at least 3 women in it?
I am not exactly sure on how to approach this question since I cannot find a way to determine how many object there will and how many times they will be taken so that I can use the combination formula. My worries is with the words at least 3 women, does this mean that there could be 4 women then?
Comment: Is this a homework excise? Please add `[self-study]` tag and check http://stats.stackexchange.com/tags/self-study/info
Comment: It's not evident what information you might be lacking: there are $5+6=11$ people and the committee is to have $4$ people.  What "object" are you referring to, then, where you state you don't know how many there will [be]? And you are correct that in English "at least $x$" means "$x$ or more."
Comment: I am given all the information. i am having trouble with using the formula.
Comment: I calculated the answer:315. Is it correct.
Comment: You might be having trouble because this question requires you to think about the situation and decompose the solution into a few parts.  You don't just dump the numbers into a formula.  Incidentally, since there are only $\binom{11}{4}=330$ possible committees, regardless of gender, $315$ looks quite a bit too large to be correct.
Answer: Consider two cases:
1. There are exactly three women on the committee:
c(5,3)*c(6,1)=60

There are exactly four women on the committee:
c(5,4)=5

Hence:
Solution = 60+5 = 65

Comment: Thanks for leaving exactly nothing left for the OP to do. Self-study guidelines apply to answers as well as questions http://stats.stackexchange.com/tags/self-study/info .
Answer: I did this:
Okay. I said:
How many ways committee can be formed without specification i.e No 3 women restriction e.t.c:
c(11, 4) = 330
After that I say:
Number of combinations that 5 women that can be taken 3 at a time = c(5, 3) = 10
Then I say, number of combinations that 5 women can be taken 4 at a time
= c(5, 4) = 5
Final answer = 330 - 10 - 5 = 315
Do people agree with this?
Comment: Do you really think your calculation makes sense?  Think about it again. You might want to approach it the other way and determine how many committees there are with exactly 3 women, and how many there are with exactly 4 women.
Comment: No and no, but you only have 327 possible answers left (well, maybe 328 if you think 0 could be the answer). Think it through carefully, and show your logic. Think not only about the number of women on the committee, but also what the corresponding number of men would be. That is my last hint and correctness verdict rendering.
Comment: There can only be 1 or 0 men on the committee

Pemutations and combinations problem


How do I build a relation of users having a specific role, automatically?

Question: I have installed the Relation module. I need that users with the "test" role are related with all users having the "student" role, automatically.
How can I build this relation?
Comment: It's worth pointing out that _roles_, can be neither target nor source in a relation. _Users_ of the "test" role can however. Please re-phrase and I will up-vote.
Comment: Is the question better, now?
Answer: With the Rules module, you could take action on a "user created with role student" event.
Comment: This is precisely the reason we added Rules support.


Mix of query and form params in REST service?

Question: We have a REST API which, for certain operations, we provide the option of asynchronous requests. For async requests, the service will return immediately with a token which can be used to query for completion, while for synchronous requests, the response will not be returned until the operation completes.
The current design for this looks something like this:
URL: PUT /api/orders/1234?async=true
Request Body: customerName=My Company&city=Dallas
Intuitively, it seems wrong to mix query and form params like this, however the query param (async) is providing options to the service call and not an attribute of the resource. That was the primary reason we did not include it in the request body.
Does this approach seem like a good design, or is there a better more "REST-y" way to accomplish the same?
Answer: An URL like this
POST /api/orders/1234?async=true

is not RESTful. Why POST? This looks like RPC over HTTP.
What are your resources? If there is some computation involved, model the computation as resources. POST to a collection of compuations to create a new one. Receive a Location header for the result of the computation.
POST /api/orders

custoerName=My Company
city=Dallas

This could return a response like:
201 Created
Location: /api/orders/1234

Then the client could GET the result:
GET /api/orders/1234

And the server would respond:
200 OK

Some result body.

How to make this asynchronouse?
You write:

however the query param (async) is providing options to the service call and not an attribute of the resource

This is plain wrong. Everything that is in the URI is used to identify the resource. REST is not about calling methods with parameters.
I recommend to always use the two-step-approach described above when modeling compuations/transactions/shopping carts as resources. Create it, retrieve it.
Comment: The client waits or he doesn't wait. That's totally up to him. In both cases the server should always respond to a `GET` request with a response that describes the current server state of the resource.
Comment: You're correct, this should have been a "PUT" (update) in the scenario I was trying to describe. I'll edit the original question to clarify. However, the gist of the question was how to let the client specify whether they want to wait for the operation to complete or not.
Answer: The Prefer header is designed to do exactly what you are trying to do.  See the spec.
Comment: From the draft: "Expires: July 11, 2013". So this header is not part of HTTP.
Comment: @Tichodroma  The spec has been approved and is working its way through IETF due process. http://datatracker.ietf.org/doc/draft-snell-http-prefer/history/  The web works due to a wide range of interoperating specs.  Media types are not "part of HTTP".  Many of the HTTP methods are not defined by the HTTP spec (e.g. PATCH). Same goes for response codes. That's how it is supposed to work.


Qual seria a função desse array?

Question: Vendo um pouco sobre a linguagem JavaScript, observei um código em que uma sintaxe que não conhecia.
No código em questão tenho duas variáveis com linguagens contendo os valores passados para a função e a função que retorna umobjeto:

let languages = 'Javascript';
let notExist = 'Go';
const program = (languages) => ({
  'Javascript': 'Belongs to the js universe!',
  'Typescript': 'Belongs to the js universe!',
  'Ecmascript': 'Belongs to the js universe!',
  'Default': 'Does not belong to the js universe!!'
})[languages];

console.log(program(languages));

console.log(program(notExist) ? program(notExist) : program('Default'));

Minha dúvida se trata da declaração do array no final da função contendo a variável languages:

Para que serve o array e qual sua utilidade?
No final de funções assim, posso declarar outros tipos de valores como {} por exemplo?
Answer: Primeiro eu sugiro mudar a estratégia de aprendizado. Primeiro aprende os conceitos depois vai para códigos. Estamos vendo muitas pessoas perguntando aqui usando a mesma estratégia que você e nossa observação é que não está dando certo, as pessoas vão patinando no aprendizado.
Array
Começo pela pergunta explicitamente colocada na pergunta.

Para que serve o array, qual sua utilidade?

O que é na verdade o array?
Especificamente esse array funciona mais como um array associativo, também chamado em algumas situações de dicionário, um hash table ou mapa. Pode ver mais em Qual a diferença entre um mapa, um dicionário, um array associativo e uma tabela hash?.
E é assim que JavaScript monta seus objetos. Veja também Objetos são semelhantes a arrays?.
Então nesse caso você tem uma lista de elementos com chave e valor. A chave servirá para acessar um dos elementos e obterá o valor correspondendo.
A outra pergunta é confusa não sei responder e a outra resposta não responde também, se era importante deveria reescrever essa parte para ficar mais claro.
Código adequado
Em um exemplo assim faz pouco sentido porque todas informações estão disponíveis e esse código em específico é uma enorme complicação. Ele serve apenas para apresentar o mecanismo. É bom deixar isso bem claro.
Então através da variável languages será determinado qual é o elemento que está interessado. Note que se a variável vier um dado que não bate com qualquer chave existente dará erro. Esse código não é um bom exemplo por isso também.
E na verdade demonstra que esse mecanismo está bem errado, um simples switch resolveria bem o caso.
Aí entramos em outro uso errado do mecanismo, que é criar uma função anônima para algo que não precisa ser anônimo.
O que estou querendo dizer é que está usando um exemplo complexo e provavelmente vai pegar vícios de programação fazendo assim.
Entender um conceito é bom, aprender como um mecanismo funciona é bem interessante, mas muitos exemplos que colocam por aí não ensinam onde usar, como usar, não dão um contexto, e por isso ensinam errado.
Vou te passar como esse código pode ser melhor escrito, mesmo que seja diferente do que está originalmente na pergunta porque é assim que deveria fazer. O código da pergunta desensina.

function program(languages) {
    switch (languages) {
    case 'Javascript':
    case 'Typescript':
    case 'Ecmascript': return 'Belongs to the js universe!';
    default : return 'Does not belong to the js universe!!'
    }
}

console.log(program('Javascript'));
console.log(program('Go'));

Coloquei no GitHub para referência futura.
Talvez um if funcionasse até melhor, mas sei lá se depois isso não seria mudado. Exatamente assim acho que funcionaria bem até com um operador condicional:

function program(languages) {
    return (languages === 'Javascript' || languages === 'Typescript' || languages === 'Ecmascript') ? 'Belongs to the js universe!' : 'Does not belong to the js universe!!';
}

console.log(program('Javascript'));
console.log(program('Go'));

Coloquei no GitHub para referência futura.
Além de mais simples tende a ser mais rápido de execução, e um pouco mais robusto.
No contexto usado não tem porque usar algo mais complicado que isso. Pode existir contextos que algo parecido seja útil? Pode, mas ele precisaria ser demonstrado por completo, colocado desta forma leva as pessoas programarem de forma confusa.
Claro, todo mundo pode escolher a forma que acha melhor fazer, eu só dei a simples. Por exemplo, alguém deve ter achado que essa forma é errada por ter negativado a resposta, é típico de quem prefere coisas confusas.
Comment: No primeiro parágrafo já ganhou meu voto de utilidade, tem que o deixar em negrito.
Comment: Valeu pela explicação Maniero, conheço sobre arrays, só não havia entendido o seu uso ali naquele contexto declarado logo depois da função. Obrigado!
Comment: Você explicitamente perguntou sobre isso: `Para que serve o array e qual sua utilidade?`. Eu introduzi isso e te mostrei que nesse contexto essa não é a forma correta de fazer esse código e justifiquei na resposta.
Answer: Fora a notação similar, isso nada tem a ver com array. Nesse caso, os colchetes denotam um assessor de propriedades.
Note, no seu exemplo, que você está criando um objeto através de sua forma literal:
{
  'Javascript': 'Belongs to the js universe!',
  'Typescript': 'Belongs to the js universe!',
  'Ecmascript': 'Belongs to the js universe!',
  'Default': 'Does not belong to the js universe!!'
}

Existem duas principais formas de acessar as propriedades de objetos:

Notação ponto;
Notação colchete.

Veja a diferença entre elas:

const obj = {
  'Javascript': 'Belongs to the js universe!',
  'Typescript': 'Belongs to the js universe!',
  'Ecmascript': 'Belongs to the js universe!',
  'Default': 'Does not belong to the js universe!!'
};

// Notação ponto:
console.log(obj.Javascript);
console.log(obj.Typescript);

// Notação colchete:
console.log(obj['Ecmascript']);
console.log(obj['Default']);

Mas note que, na sintaxe de assessor via colchetes, você pode passar uma string, o que te permite um certo dinamismo nesse acesso, diferentemente da outra notação de ponto, que é "hard-coded".
Esse dinamismo te permite fazer coisas como:

const obj = {
  'Javascript': 'Belongs to the js universe!',
  'Typescript': 'Belongs to the js universe!',
  'Ecmascript': 'Belongs to the js universe!',
  'Default': 'Does not belong to the js universe!!'
};

const prop = 'Ecmascript';

console.log(obj[prop]);

A partir daí você pode mapear um valor a outro. No caso da pergunta, você está mapeando o nome de uma linguagem de programação a uma determinada string.
Note que, conforme visto anteriormente, a notação de colchete nos permite acessar essas propriedades dinamicamente, veja:

let exist = 'Javascript';
let notExist = 'Go';

const obj = {
  'Javascript': 'Belongs to the js universe!',
  'Typescript': 'Belongs to the js universe!',
  'Ecmascript': 'Belongs to the js universe!',
  'Default': 'Does not belong to the js universe!!'
};

console.log(obj[exist]);
console.log(obj[notExist]); // undefined

Note, no exemplo acima, que declaramos duas variáveis par atingir isso. Contudo, isso não é algo necessário. Objetos te permitem fazer isso diretamente, assim:

console.log(
  {
    name: 'Foo',
    age: 100
  }.name
); // Foo

console.log(
  {
    name: 'Foo',
    age: 100
  }['age']
); // 100

Assim você pode criar funções (como foi feito na pergunta) que isolam esse comportamento. Você pode ainda criar uma função que retorna um valor padrão caso a propriedade não exista (e retorne undefined):

function getLanguageFeature(language) {
  return {
    'Go': 'Simple',
    'JS': 'Bizarre',
    'TS': 'JavaScript, but type safe'
  }[language] || 'Unknown language';
}

console.log(getLanguageFeature('Go')); // Simple
console.log(getLanguageFeature('JS')); // Bizarre
console.log(getLanguageFeature('TS')); // JavaScript, but type safe
console.log(getLanguageFeature('WOOT')); // Unknown language

Note que utilizamos o operador OR (||) (em sua função de curto circuito) para garantir que retornemos a string Unknown language no caso de buscarmos por uma linguagem que não foi declarada no mapa. Isso porque os assessores de propriedade retornam undefined caso a propriedade acesada não exista.
Isso pode ser visto como uma alternativa para o switch statement. Em Python, por exemplo, essa construção não existe e dicionários são frequentemente utilizados como substitutos.
Comment: Show Luiz, muito bem explicado! Valeu :)

Qual seria a função desse array?


I can't redirect to Route in Symfony

Question: everyone.
I am building symphony 5 project, but I have an issue.
The URL is not redirecting after I create or edit an element.
But in other functions, the redirectToRoute() function works.
Strange problem...
These are my controller code.
Please check it and let me know what I missed.
Thanks.
<code>/**
 * @Route("/list", name="animals")
 */
public function animals(): Response
{
    if (!$this->getUser()) {
        $this->redirectToRoute('app_login');
    }

    $animals = $this->getUser()->getAnimals();

    return $this->render('animals/overview.html.twig', [
        'animals' => $animals,
    ]);
}
</code>
And
<code>/**
 * @Route("/create", name="animal_create")
 */
public function create(Request $request, EntityManagerInterface $entityManager): Response
{
    $animal = new Animal();
    $customer = $this->em()->getRepository(Customer::class)->findOneBy(array('id' => $request->request->get('customer')));
    $animal->setCustomer($customer);

    $form = $this->createForm(AnimalType::class, $animal, [
        'csrf_protection' => false,
    ]);
    $form->handleRequest($request);

    if (!$form->isSubmitted() || !$form->isValid()) {
        return new Response('failed');
    }

    $customer->addAnimal($animal);

    $entityManager->persist($animal);
    $entityManager->flush();

    return $this->redirectToRoute('animals');
}

/**
 * @Route("/{id}/update", name="animal_update")
 */
public function update(Request $request, Animal $animal, EntityManagerInterface $entityManager): Response
{
    $form = $this->createForm(AnimalType::class, $animal, [
        'csrf_protection' => false,
    ]);
    $form->handleRequest($request);

    if (!$form->isSubmitted() || !$form->isValid()) {
        return new Response('failed');
    }

    $entityManager->persist($animal);
    $entityManager->flush();

    return $this->redirectToRoute('animals');
}
</code>
Comment: You're processing the form incorrectly. [Here](https://symfony.com/doc/current/forms.html#processing-forms) is an example of how to handle and process a form when submitted and redirect etc..
Comment: What happens when you submit the form? Also `RePSonse` is incorrect class name.
Comment: In your first code example within the if you aren't returning the redirect response
Comment: When I submit the form, the data is changed and saved in the db correctly. But the page isn't redirected to a list page.
Answer: You must redirect to the index route only if the form is submitted and validated, otherwise the form must be displayed.
The symfony documentation explains this perfectly:
<code>public function create(Request $request, EntityManagerInterface $entityManager): Response
{
    $animal = new Animal();
    $customer = $this->em()->getRepository(Customer::class)->findOneBy(array('id' => $request->request->get('customer')));
    $animal->setCustomer($customer);

    $form = $this->createForm(AnimalType::class, $animal);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $animal = $form->getData();

        $customer->addAnimal($animal);

        $entityManager->persist($animal);
        $entityManager->flush();

        return $this->redirectToRoute('animals');
    }

    // Here it's the name of your twig template for animal creation
    return $this->renderForm('animal/create.html.twig', [
        'form' => $form,
    ]);
}
</code>
Do the same thing for the update action.

I can't redirect to Route in Symfony


How to go about naming a chord

Question: I'm transcribing something in C. The chord notes in a particular bar are, in rising order, C#, G, B, E. As I see it, there are three choices - Em6, A9 (no A), and C#m7b5. None of them seems to be a good name for the chord. It comes between a C bar and a D bar, so maybe C#m7b5 is best, as a sort of V/V/V. What criteria are there for naming it?
Comment: Subjective, and Western theory is still archaically wedded to conventions in a way which doesn't provide a unique description for every situation.  I'd intuit the root (subjective!), strongly considering the bassline.  I've seen plenty of "criteria" which struck me as people making rationalizations for falling upon prejudice.  I believe that jdjazz makes a good point about trying to match a familiar progression -- though it relies on familiarity, it can make things easier conceptually for performers in several ways.
Answer: My take is that function wins out as the most important criterion. The two best candidates are: C#m7b5 (or C half diminished, C#ø7) and A7. When we look at the function, I think A7 is the stronger choice.
A half diminished chord has the primary function of leading into a V7alt chord. For example, if the progression were: | unknown chord | F#7alt | Bmin | then C#ø7 would be a great candidate for the unknown chord because it would complete the minor ii-V-i. (Note: if the song were modulating from C to D, then this might actually be a C#ø7, but that's not the case as you've pointed out.)
But given that the progression is |  C    | unknown chord |  D    |, I think the best candidate for the harmony is A7, because the A7 chord serves as a V to the D chord, as you've pointed out. If C is the tonic, then this is the start of a I-VI-ii-V progression (or maybe I-VI-II-V, depending on the quality of the D chord).
There are many ways to voice an A7 chord without changing the underlying dominant seventh/mixolydian harmony. It can be voiced with the 9, it can be voiced without the root, it can be voiced with the 13 instead of the 5, it can be voiced with the 4 alongside the 3, etc. I view these changes as stylistic--voicing the chord in these different ways doesn't change the underlying dominant seventh harmony, and in all cases, the A7 chord is still playing the exact same functional role.
So the question, "what's the underlying harmony?" requires an eye on the function, in my opinion. In any given chorus, an underlying chord can have many different instantiations which would add extensions, remove notes, etc. So the approach I take is to write the underlying harmony--I write what a chord sheet/lead sheet would show. To that end, I'll listen to how the same measure is played in other choruses, other versions, etc. In the next chorus, maybe there's an A in the root. In the following chorus, maybe the 13th is included. But if I notice that the C# is always in the root, then I might notate this as A7/C#.
Functionally, the other strong candidate for this chord would be C# diminished (which would serve the same role as the VI7 chord). But as you yourself have already noticed, the presence of the B note in the chord rules out C# diminished as a possibility.
Comment: Thanks for the insights. Funnily, as I wrote it out on first hearing, C#o got put in that place. It was only in consultation with the writer - who plays but doesn't know names - that the particular voicing came out.
Comment: @KyleStrand, I think you've misread. I didn't say there's a rule that half-diminished chords only lead to V7alt chords. This isn't a process-of-elimination argument. I have characterized the "primary function" and have used that to make a best selection between two candidate chords.
Comment: @KyleStrand, half-dim chords occur naturally on the 7th degree of a major scale and on the 2nd degree of a natural minor scale. They can serve three functions (diminished, dominant, and predominant). In a diminished function, they typically are a #ivø7 chord. In a cadence, they might serve a dominant function, typically as a viiø7 chord. Or they can serve a predominant function, as in a iiø-V7. You're proposing C#ø7 here, a #iø7 chord. This seems like a weird choice: it deviates from the normal uses of half-dim, & one could instead just characterize it as a standard I-VI7-II7-V7 progression.
Comment: There's no reason a half-diminished chord can't also lead into a tonic chord, though; there's no "rule" that half diminished chords only lead into V7alt chords.
Comment: I said "lead into a tonic chord", meaning that I'm considering D the I chord in this context, which would make it a viiø7, not a #iø7 (!). Without more context clues, I'm suggesting this is a modulation. Of course, if the next few chords indicate that G is the tonic, then this could also be a #ivø7 as you mention.
Comment: In the absence of context clues a lot stronger than what's given above, I just don't see any reason to *assume* a root that's not present in the chord.
Comment: @KyleStrand, Tim said the tonic is C. If there were modulation involved he would have stated that. Are you suggesting it's more likely that D is a *new tonic* than this is a rootless chord? [Rootless chords](https://en.wikipedia.org/wiki/Root_(chord)#Assumed_root) are extremely common.
Comment: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/68593/discussion-between-kyle-strand-and-jdjazz).
Comment: @jdjazz - the song itself does not modulate anywhere. What *is|* happening here is a set of chords, each going up a fourth. I call it 'going round the houses'. No idea why! So, F > ? > C > A > D > G > C (tonic).
Comment: Perhaps "modulation" is too strong a word; but looks like it is at least tonicization.
Comment: I'm quite new to such analysis. But what about interpreting it as Em/C#? That would make a I-iii-ii progression with chromatic base line C-C#-D.
Answer: If you subscribe to the theory that wants every chord to have a dominant relationship to the one that follows, we're looking to analyse the C# as a leading note to D.  So this could be thought of as a rootless A9.  THINK of it as that, but WRITE it for performance as C#m7b5, else the player will be tempted to add an A bass note.  Same harmonic function but different sound, hence NOT freely interchangable!
Comment: Is there such a theory? Surely that can't be the case. Although an awful lot of chords are followed by their I, as in II V I it won't be every chord - we'd end up doing the cycle of fourths every time!
Comment: I said 'wants'. Functional Harmony  doesn't always get what it wants!  It can get very upset by, say, a bVII chord

How to go about naming a chord


Calculate the average distance over all pairs of nodes in a graph

Question: How can one calculate the average distance on a graph with 5 nodes?
Is there a simple way to find the average?
Can some one share some examples?
Thanks
Comment: What's the configuration of the graph?
Answer: Your question is very vague.
If you have a graph on 5 nodes then you can probably calculate the distances between all the pairs of vertices manually and then divide the result by 10
If you happen to have a large graph you can construct the all pair distance matrix (using for example this algorithm) and then sum all the entries of the matrix and divide by the number of all pairs of vertices.
Additionally, if you happen to know the Wiener index of your graph you just divide the index by $n(n-1)$ where $n$ is the number of vertices of your graph.


Issue using OpenCV Kurento module in Java

Question: I have created an opencv module on java for kurento based on a tested program i did, it's recognized and loaded in kms correctly and now i'm trying to test it. My idea was using the Magic Mirror example and substitute the call of the faceoverlayfilter with my plugin call so i included it in the pom.xml file dependencies and made this changes in the MagicMirrorHandler.java:
import org.kurento.module.fotoacceso3.*;
//import org.kurento.client.FaceOverlayFilter;

...
//      // Media logic
//      FaceOverlayFilter faceOverlayFilter = new FaceOverlayFilter.Builder(pipeline).build();
//
//      String appServerUrl = System.getProperty("app.server.url",
//          MagicMirrorApp.DEFAULT_APP_SERVER_URL);
//      faceOverlayFilter.setOverlayedImage(appServerUrl + "/img/mario-wings.png", -0.35F, -1.2F,
//          1.6F, 1.6F);
//
//      webRtcEndpoint.connect(faceOverlayFilter);
//      faceOverlayFilter.connect(webRtcEndpoint);

      fotoacceso3 fotoacceso3 = new fotoacceso3.Builder(pipeline).build();

  String appServerUrl = System.getProperty("app.server.url",
      MagicMirrorApp.DEFAULT_APP_SERVER_URL);
//      faceOverlayFilter.setOverlayedImage(appServerUrl + "/img/mario-wings.png", -0.35F, -1.2F,
//          1.6F, 1.6F);

webRtcEndpoint.connect(fotoacceso3);
fotoacceso3.connect(webRtcEndpoint);

When i run the code it doesn't gives error and shows the two videos but the module doesn't do anything. I have very little experience with Java, what can i do to check where is the problem?
Answer: The problem was that, although i didn't used capital letters when i created my plugin, when the Java code code was generated the functions were created with capital letters, so to call the plugin i have to make this change in my code from this:
fotoacceso3 fotoacceso3 = new fotoacceso3.Builder(pipeline).build();

To this:
Fotoacceso3 fotoacceso3 = new Fotoacceso3.Builder(pipeline).build();


iptables - use service name or port number, IP-address or hostname?

Question: iptables (and/or the successor tool nftables) is the user-space utility program that allows a system administrator to configure the IP packet filter rules of the Linux kernel firewall, which is implemented as different Netfilter  modules. (summary from Wikipedia)
Since iptables and nftables are a user-space utility programs intended to be used by people, by system administrators, and people are not always completely comfortable with the underlying numbers, they accept human readable descriptions for IP-protocols (tcp, icmp, udp etc), IP-addresses (hostnames), and port numbers (service-names).
From a UI aspect using human readable names appears to be a good thing:

as an administrator I'm probably not the only one that has a much better intuitive grasp of the meaning of:
iptables --protocol udp
compared to: iptables --protocol 17 and similar.
a hostname gateway.example.net often makes more sense than an IP-address such as 192.0.2.1
and on a web server allowing HTTP and HTTPS is more obvious than allowing TCP/80 resp. TCP/443.

Is that safe to do?
Or should you stay away from that?
What are the caveats?
Answer: The effect may not always be what the administrator expects.
A little background:

The netfilter modules in the linux kernel operate on IP packets. They mainly evaluate, process and/or modify the headers from those IP packets  and generally do not touch upon the the data section (that would be more the realm of deep packet inspection (DPI)).
The headers of IP packets do not contain human readable names, they consist of things like the IP protocol number , a source and destination IP-address,   TCP source and destination port numbers etc.

To be the most efficient the rules for the  packet filters  in the Linux kernel are also stored with the values for the IP protocol, port numbers and IP-address in the same format as that will be found in packets they're evaluating.

When a rule is loaded into the kernel iptables and nftables translate the human readable input into the numeric values needed by and stored in the netfilter kernel modules.

The system resolver is used to translate protocols, services, network names and hostnames. That makes the actual resolving dependent on your configuration ( nsswitch.conf)

iptables and nftables translate the human readable input into the numeric values when the commands are run.

the resulting numeric rules remain effective in the kernel for as longs as they are not explicitly flushed/removed/updated.

That also has a number of effects and/or consequences that are not immediately obvious.

Using protocol names with iptables:
That is generally speaking safe and does not lead to any unnecessary confusion.
The file /etc/protocols is used by default to translate protocol names to protocol numbers in iptables --protocol [human readable protocol name]
It is unlikely that IP protocol names (such as UPD and TCP) cause confusion.

Using hostnames with iptables or nftables:
The default file /etc/networks could be used to translate network names  used in the destinations and/or sources. At least according to the manual, I have never seen anybody use that in the real world yet. Since as far as I know getent networks network-name also doesn't provide a method to return a netmask either, that makes using network names impractical IMHO.
The default file /etc/hosts is used to translate hostnames to IP-addresses in destinations and/or sources, with typically DNS as the fallback.
Don't use hostnames.
The packet filter works on IP-addresses. Use IP-addresses and IP-address ranges in your firewall configuration too.
When a hostname is used in a firewall rule with iptables it will effected with the  (single) IP-address to hostname resolves to at the moment the iptables command was executed.
When a hostname can't be resolved, the rule won't be added at all.
That seems obvious and not very problematic but that results in many corner cases, that in practice turn out to really common.

a firewall configuration is typically applied at boot before fully activating the network and then DNS resolving won't work yet. A rule to for example allow remote administration from your bastion host iptables -I INPUT --source bastion.example.com -j ALLOW won't apply (at system boot) when there is no entry bastion.example.com in the system hosts file. (And who wants to maintain hosts files when they have internal DNS?)

When the forward DNS record is updated: the Linux firewall will continue to use old IP-address until the rule is explicitly flushed/removed/updated (by running the iptables command again).

When the forward DNS record is a round-robin record or uses geo-location: often only a single IP-address will be used.
In other words: iptables -I OUTPUT --destination www.google.com -j REJECT will for example only partially block access to Google for no more than 300 seconds. Then the DNS TTL expires and queries for www.google.com return a different IP-address, usually a new one that is not blocked in your effective firewall rules.

Normally using a hostname to reject undesired access is already a bad idea.
Systems normally only see the IP-address from an incoming connection and they'll need to do a reverse lookup on the IP-address to determine the associated hostname. Blocking by hostname is then problematic for two reasons:

reverse DNS lookups can be slow

the resulting hostname can't be trusted because the owner of an IP-address can set any hostname they want on the reverse DNS record. They can change their untrusted.example.net to trusted.example.com at will (Some systems address that by doing a second forward lookup of the hostname from the PTR record and will only accept systems where the two match.)
iptables -A INPUT --source untrusted.example.net -j REJECT

For iptables the problem is slightly different, as only a forward DNS lookup will be done. But nevertheless, when you use untrusted.example.net rather than an IP-address you no longer control what is blocked.
If the owner of that DNS record changes untrusted.example.net to let's say the IP-address of your gateway, you will lock yourself out.
If the owner completely removes the record, you're no longer blocking their IP either.

Another potentially confusing thing: when displaying the firewall rules the mapping from IP-address to hostname if done via reverse DNS lookup. Frequently that PTR record won't exists  and when it does exist, as often as not the resulting hostname won't correspond to the hostname that was used when the rule was created. For example:
www.google.com.                 300 IN  A   188.8.131.52
188.8.131.52.in-addr.arpa. 18721 IN  PTR ams15s40-in-f4.1e100.net.

And probably some other reasons I forgot.

Using service names  with iptables:
Although safe this also seems to generate some confusion.
The file /etc/services is used to translate the service name to port number when instead of a port number a human readable "service name"  is used  with iptables --protocol tcp --source-port (or the alias --sport and the similar --destination-port / --dport ect.).
The service name is effectively a human readable port number and (as long as nobody modified /etc/services) when an administrator uses for example  ssh that  will be the same as using port 22.
iptables -I INPUT -p tcp --dport ssh -j ACCEPT

The source of confusion seems that people confuse the service name (ssh) with the application protocol (ssh).
The rule above does not allow all incoming ssh traffic.
When for example your ssh server is configured to listen on port 22222  that port is not opened by this rule.
That rule only allows all traffic to port 22.
It also does not block traffic that is not ssh from connecting to port 22 either. The typical ssh daemon won't be able to respond properly but requests such as http://hostname:22/ are not blocked the packet filter either.  (That would require DPI.)


Existe alguma forma de "desmembrar" um elemento filho de um elemento pai?

Question: Eu tenho a seguinte situação na versão destkop de um template:

    ...

Quando o template fosse visualizado no mobile gostaria que a estrutura se desdobrasse como:

   ...

   ...

Existe alguma classe de Bootstrap que faça isso?
Comment: Se precisa fazer isso, talvez seja mais interessante rever a estrutura do seu HTML. Idealmente a semântica do HTML não deveria mudar entre desktop e mobile, a outline se mantém a mesma. Se for só uma questão estética, você não precisa "desfiliar", apenas estilizar com o CSS, mas para isso será necessário que você descreva como gostaria de exibir tais elementos e preferencialmente elabore um [mcve].
Comment: Se vc realmente quer fazer isso, talvez essa quastão possa te interessar https://pt.stackoverflow.com/questions/352318/para-que-serve-e-como-usar-o-display-contents-do-css mas o mais provável é que vc na realidade não precise fazer isso que vc quer, o problema é que vc não está sabendo usar o grid do Bootstrap. Inclusive esse pedaço de código nada tem a ver com o Bootstrap e o problema em si vc praticamente não falou
Comment: "desmembrar" não sei se é possível. Mas creio que via *CSS* você pode deixar seu layout responsivo e assim conseguir esconder algo no desktop ou esconder algo no mobile e vice versa.
Comment: O problema surgiu fruto de um erro no projeto, assim a melhor resposta para esse problema foi refazer o projeto.
Answer: Para isso pode usar insertBefore(), insertAdjacentElement() ou after()
Aqui um exemplo usando after, onde seleciono o elemento "pai" e adiciono o "fillho" após o mesmo. Lembrando que, o elemento é movido uma vez que já existe no DOM:

document.getElementById("mover").addEventListener("click", function(){
   var filho = document.querySelector(".bloco-filho");
   var pai = document.querySelector(".bloco-pai");

   pai.after(filho);
});
.bloco-pai {
  padding: 10px;
  width: 100px;
  height: 100px;
  background-color: cyan
}

.bloco-filho {
  width: 70px;
  height: 70px;
  background-color: yellow
}

<button id="mover">
Mover elemento
</button>


Pandas Dataframe Changing values in a column

Question: I have a large Dataframe with the following columns:
The data used as the example here can be found here
import pandas

x = pd.read_csv('example1_csv.)
x.head()

ID  Year    Y
22445   1991    40.0
29925   1991    43.333332
76165   1991    403.0
223725  1991    65.0
280165  1991    690.5312

I want to change the numbers in the column Y to the categories low, mid, high, where each category is specific to a range of numbers in Y:

Low replaces any number within the range of -3000 to 600 in Y.

Mid replaces any number within the range of 601 to 1500 in Y.

High replaces any number within the range of 1501 to 17000 in Y.

For example, if an ID has a Y value between -3000 and 600 then that ID will have the numeric value in Y replaced as Low.
How does one make these replacements? I have tried several ways but have run into str and int type errors every time. The data file used in this question is in the Github link above. Many thanks in advance for the help.
Answer: use numpy.select
import numpy as np
x.Y = np.select([x.Y.lt(601), x.Y.lt(1501), x.Y.lt(17000)], ['Low', 'Mid', 'High'])

Comment: Glad I've been of help
Answer: This should work too.
x['Y'] = x['Y'].apply(lambda i : 'Low' if i > -3000 and i < 600 else ('Mid' if i >601 and i < 1500 else 'High'))


Combinatorics identity algebraic proof

Question: Prove that:
$$\sum _{k=1}^n \:k\binom{n}{k}^2=n\binom {2n-1}{n-1}$$
I tried to prove it using induction:
For n+1:
$$ \begin{align*} \sum_{k=1}^{n+1} \:k\binom{n+1}{k}^2 &=  \sum \:k\left(\binom{n}{k}+\binom{n}{k-1}\right)^2 \:\left[Pascal's\:rule\right] \\
\\
&= \sum \:k\binom{n}{k}^2+\sum \:2k\binom{n}{k}\binom{n}{k-1}+\sum \:k\binom{n}{k-1}^2 \\
\\
&= n\binom{2n-1}{n-1} +\sum \:2k\binom{n}{k}\binom{n}{k-1} + n\binom{2n-1}{n-2} [*]
\end{align*}$$
[*] - Induction hypothesis
Now I'm stuck with the middle expression.
Any advice on how to proceed would be appreciated.
Note: I'm looking for an algebraic proof not combinatorial interpretation.
Answer: $\sum k {n \choose k}^2 = \sum k {n \choose k} {n \choose n -k} $
combinatorial interpretation is that you have a group of $n$ men and $n$ women, you want to choose a team leader who has to be a man and $n-1$ team members - no matter what their gender is. your left hand side corresponds to first saying "ok - my group will have $k \geq 1$ men in it so I choose those men in ${n \choose k}$ ways, choose the leader from them in $k$ ways and then choose $n-k$ women to complete the team in ${n \choose n -k}$ ways. The right hand side corresponds to the method - first choose the leader from $n$ men in $n$ ways then choose the rest of the team $(n-1)$ people from a group of $2n - 1$ in ${2n -1 \choose n-1}$ ways
Comment: sorry, didn't notice and I really like combinatorial proofs
Comment: I didn't look for the combinatorial interpretation (In the title I said algebraic proof) but thanks anyway.
Answer: You can prove it directly by using the identities
$$k\binom{n}k=n\binom{n-1}{k-1}$$
and
$$\sum_k\binom{m}k\binom{n}{\ell-k}=\binom{m+n}\ell\;;$$
the latter is Vandermonde's identity. Both have nice combinatorial proofs that have appeared on this site.
$$\begin{align*}
\sum_{k=1}^nk\binom{n}k^2&=\sum_{k=1}^nn\binom{n-1}{k-1}\binom{n}k\\\\
&=n\sum_{k=1}^n\binom{n-1}{k-1}\binom{n}{n-k}\\\\
&=n\sum_{k=0}^{n-1}\binom{n-1}k\binom{n}{n-1-k}\\\\
&=n\binom{2n-1}{n-1}
\end{align*}$$
Comment: @GinKin: You can always include their proofs. :-) (I'm just pointing out that the option is available, not saying that you necessarily should use it.) The first one is easily proved computationally just by manipulating factorials; Vandermonde's is most easily proved combinatorially, as is done [here](https://en.wikipedia.org/wiki/Vandermonde%27s_identity#Combinatorial_proof), for instance.
Comment: @GinKin: $\binom{n}k=\binom{n}{n-k}$, so I replaced the second factor of $\binom{n}k$ by $\binom{n}{n-k}$. Then I shifted the index of summation by $1$. Finally, I used Vandermonde's identity to get rid of the summation. (I will not say what I think of instructors who impose arbitrary restrictions on the kinds of argument allowed.)
Comment: This is really nice but we haven't covered these identities so I doubt we can use them.
Comment: I don't get what's inside the red line: http://i.imgur.com/I4IVfOP.jpg do you multiply the binoms to simplify them ? In the second line, how did $\binom {n}{n-k}$ get there ? How did you get rid of the sum ? Also, we have to strictly prove it algebraically, so using other identities and proving them combinatorially is not allowed.

Combinatorics identity algebraic proof


GUI do Tkinter trava quando executo o código

Question: Sou novato em programação e estou fazendo um macro para realizar algumas ações. Esse macro fica em loop:
    while True:
    window = win32gui.GetWindowText(win32gui.GetForegroundWindow())
    #print(window)
    now = time.time()
    if "Tibia" in window:
        #Procura IMG
        localAnel = pyautogui.locateCenterOnScreen(anel, grayscale=True, confidence=0.9)
        localColar = pyautogui.locateCenterOnScreen(colar, grayscale=True, confidence=0.9)

        #Se localizar a img do Anel
        if localAnel is not None:
            pyautogui.press("NUM1")
            #print("Anel")

        #Se localizar a img do Colar
        if localColar is not None:
            pyautogui.press("NUM3")
            #print("Colar")

        #Usar berserk potion
        if now - last_berserk_potions > usar_berserk_potions * 60:
            last_berserk_potions = now
            pyautogui.press("NUM2")
            #print("Berserk")

Preciso criar um botão onde ao clicar ele roda esse código que fica em looping infinito e ao clicar nesse botão novamente ele pare a execução do código.
Estou fazendo a chamada da seguinte maneira:
botao = Button(janela, text="Gerar", command=loop, bg="CYAN", fg = "BLACK", font=padrao)

Quando clico no botão ele até executa o looping porém a GUI trava.
Answer: Tens que definir uma condição no while para terminar o loop.
while quit == False:
    ...