ANFIS with cross-validation?

Question: Can I use cross-validation k-fold for making FIS and optimization it with ANFIS or ANFIS itself has cross-validation?
First my data was divided into two group randomly. One for making model and other for testing best model. Then first group was used for k-fold cross-validation. FIS and ANFIS were performed and best model was selected. Then second group was used for test best model.
Is that method true?
Answer: It actually depends on how you do want to measure the final performances.

if you want to measure the FIS/ANFIS performances on the test set
Your method is fine. However the split should be pretty much 70% for training & cross-validation and 30% for testing. You perform then cross-validation on 70% of the data, select the "best results", train again the model with such "best results" and then use such model on the validation set to gather the final performances.
if you want to measure the FIS/ANFIS performances directly through cross-validation
There's no need to split the dataset. You might as well use all of it and gather the best, final performances as the "best results" from the cross-validation itself.

I most certainly will recommend the first approach. The cross-validation is often used to tune a parameter, not to gather the final performances.
Comment: Recall that the correlation coefficient is strictly related to the number of samples, so if the two sets have different sizes is absolutely normal to have different correlation coefficients. Also recall that the `cvpartition()` selects indices in a random fashion, so when testing your code is good practice to *avoid* taking a single run as granted and you might want to perform several runs
Comment: The repeated runs have nothing to do with the correlation coefficient and/or any other performance parameters. It was just something that I wanted to add for the sake of completeness given the fact that `cvpartition()` acts in a random fashion. It was just an observation to raise some awareness on the matter: in the "lucky case" `cvpartition()` will select the "lucky" combination of indices that give good performances but in the "unlucky case" `cvpartition()` might select combination of indices that give bad performances. That's why is unusual to take a single run as granted.
Comment: To my knowledge, there are no size-independent parameters. Even if the number of samples *N* is not explicit in the formula, all of these parameters depend on the mean and/or standard variation and/or variance and all of these parameters are intrinsically related to the number of samples. Some examples of common parameters are: RMSE, MAE, Pearson's-R, coefficient of determination (in case of estimated/imputed values) and *all* of them depend at least on one of the above (number of samples, mean, variance, standard deviation). I reckon this also partially answers the question on SO
Comment: Thanks, but a question! , After that I'm select the best result, why am I train the best model again with ANFIS? Best result is model that has best correlation coefficient or minimum cross-validation RMSE. With this model I can evaluate a test data for correlation coeff. and RMSE for prediction. is that?
Comment: Indeed. You can evaluate models measuring performances on the test set. So you train and cross-validate the model and then measure the performances on the test set.
Comment: Another question is that, The myfis=genfis1(params); must be in k-fold 'for' loop? in my code both genfis1(params) and anfis(params) are in k-fold 'for' loop. is it true? or fis structure maker must be out of loop and only anfis must be in it?
Comment: Can the model making is in cross validation or it must be out of there?
Comment: What you do mean by k-fold for-loop? In Matlab there is the `crossval()` function that works brilliantly
Comment: cv = cvpartition(80, 'kfold', k);for ii = 1:k training = find(cv.training(ii));test = find(cv.test(ii));P = Inputs(training,:);T = Targets(training);Pt = Inputs(test,:);Tt = Targets(test);end
Comment: @OmidOmidi Sure, sure. Because you've used `cvpartition()`. In this case yes, you have to manually evaluate the FIS/ANFIS performances inside the for-loop. So (1) run the loop, (2) extract the test/training thanks to the indices given by `cvpartition()`, (3) evaluate FIS/ANFIS on the given test/training sets. At the end of the loop, finally, select the best result.
Comment: Thanks Alessiox, I do this. In result in many case for example correlation coefficient of validation data is 30~40%, but corr. coef. of test data is 70~80% or 90. Is it problem in my data or it's regular?
Comment: Instead correlation coefficient, which parameter can I use? I'm testing all type of fuzzy parameters for making prediction model and it take 5 hours in single run, so for each iteration it will take many time.
Comment: OK, my recent comment has 2 separate section, Instead correlation coefficient, which parameter can I use that not related to the number of sample? my code is here: http://stackoverflow.com/questions/36382998/compute-overall-performance-in-k-fold-cross-validation

ANFIS with cross-validation?


Multinomial logistic regression with "constant variable"

Question: I am encountering some problems with setting up a multinomial logistics regression (stepwise).
My data/model is the following:

Dependent variable: strategy1/strategy2/no preference
"Indenpendent" variable: risk perception value (prob*impact in
percentage)
Moderator: risk profile (likert scale)
+some other control variables (demogrpahics etc.)

Since the risk perception value is given, there is no variance in the values. I basically have two scenarios, each with different risk peception values (high_prob*high_impact and low_prob*high_impact) and these values are the same for all respondents. This is because the choice/preference for a certain strategy is supposed to be dependent on the (given) risk perception). However, when I run the regression for ONE of the scenarios with only this variable it gives me the following output:
No significance level for the varaible but only for the intercept. For the significance level of the variable it only shows a "." (dot).
So, SPSS does not give me a significance level for the variable (probably because of no variance), only for the intercept. Do I just have to accept it or is there any way to tackle this problem? Is this output even useful? Would it be better to generate a random perceived value in the range of 0.9 to 0.99 for e.g. high probability to get some variance? And then cluster them in two categories, thereby obtaining two perceived values for each scenario?
Thank you very much in advance.
Answer: First of all, stepwise is problematic. Search variable selection or model selection for lots of discussions here and elsewhere.
Second, if you have a variable that doesn't vary then it cannot have an effect.
What to do next? Well, sorry, but I'd say "start over". Either you need different data or a different model. You cannot investigate this model with these data.
To see this most blatantly, suppose you are trying to see if 8th grade boys are taller than 8th grade girls, but you only collect data on 8th grade boys.
Comment: Thanks for your reply, even though it sounds frustrating. However, collecting new data wouldnt help me since I would still have the same scenarios determining the risk perception. I just dont know how to quantify them and  incorporate them into the regression. For example scenario 1 has a risk perception value of 0,09 (prob*impact), whereas scenario 2 has a risk perception value of 0,01. The problem is that this value is the same for all respondents since they had to select a certain strategy according to the scenario they read. I want to find out, what strategy is chosen given the scenario.
Answer: Ok, I got valuable input from by supervisor:
Since I have have two scenarios (perceptions) per respondent, I can use binary coding for the scenarios to distinguish between them. By doing that, I obtain varying independent variables (perceptions) and can run one regression for both scenarios at once.


Why does the plot of $y=x+y$ follow the y-axis?

Question: On online graphing calculators (e.g. Desmos, Mathway), $ y = x + y $ is a vertical line which follows the y-axis. Why does it look like this?
What term is $ x $ or $ y $ counted first?
Comment: $y=x+y$ is the same as $x=0$
Comment: $=$ means equality, not assignment. This is **math**stackexchange.
Comment: The two answers and the comment that were already given are correct, but you might find it useful to do some experimenting: Try some points on the $y$-axis, say $(0,1)$ and $(0,-7)$  and some point really far away like $(0,100000000)$ and check that they all satisfy your equation $y=x+y$, so they're on the graph. Then try some points off the $y$-axis and check that they don't satisfy the equation, so they're not on the graph. Keep experimenting until it becomes obvious that the graph is exactly the $y$-axis.
Answer: if you subtract $y$ from both sides, you get
$x = 0$
which is a vertical line with $x$-coordinate $0$,
basically the $y$ axis.
Answer: $y=x+y$ is equivalent to
$$x=0.$$
This gives you a line parallel to the y-axis at $x=0$.
Comment: That **is** the y-axis
Answer: If you simplify the equation by subtracting $y$ from both sides, you get the equation $x=0$. Thus, $y=x+y$ represents all of the points in the $(x,y)$ plane such that their $x$-coordinate is $0$. Thus, you get a straight, vertical line, going through the origin, because this equation's solutions are all points $(0,y)$ for $y$ a real number.


Criteria to select the number of neighbors in the k-th-nearest-neighbor density estimation

Question: I am doing a cluster analysis based on k-th-nearest-neighbor (KNN) method in SAS. The CLUSTER procedure requires to specify the $k$ (=number of neighbors to use for KNN density estimation) and I was wondering if there is a way or criteria to set the number of neighbors.
Based on the SAS manual,

k th-nearest-neighbor density linkage is strongly set consistent for high-density (density-contour) clusters if $k$ is chosen that
  $\frac{k}{n} \rightarrow 0$ and $\frac{k}{\ln(n)} \rightarrow \infty$
  as $n \rightarrow \infty$.

Is this answer to my question?
Any good papers on the application of this method will be helpful as well.
Answer: You can -- and, generally, should always -- do some cross-validation and try out a set of different values for k and see what looks best and produces the best results.
Comment: Thanks for your suggestion but cross-validation is what I've been trying and it can be tedious when there are so many different values to try... I want to know if there is a legitimate way to pick the number of $k$.
Comment: I don't know what to tell you. The optimal *k* parameter can vary greatly based on your data. Cross-validation or visual examination of the data is the "legitimate" way.


View does not pick up webform submission is from url

Question: I have created a block that is to print out the currently displayed webform submission. The problem is that it does not pickup the submission id from the url. It is always displays submission id 11 regardless of what submission you are looking at. Can anyone tell what the problem is how to fix it?
The Details: this is for Drupal 7.19 and using views. My block is set up the following way:
<code>Contextual Filter: Content Nid
Filter: Content NID = 33
Fields: Content NID
        Web submissions: Sid
        Global Custom Text
</code>
In global custom text I try and read the nid and sid fields using tokens. The block is only displayed when you view a submission of a specific webform (in this case: webform with node id 33).
Please help as I am desperate here.
Thanks
Comment: Not sure what context the block is being displayed in. Current user? Current page (which won't work unless current page is the webform)? Need much more detail.
Comment: I add more detail to the description to hopefully answer your question.
Comment: Your display is in a block. Where do you display the block? On what/what type of pages? That is the context, and governs how you would use a contextual filter. Content:nid is the nid of the empty form, not the nid included in the submissions table. Do you have a relationship established between the two tables? If you look in the webform_submissions table, how many submissions to you have, and is sid=11 one of the submissions for the webform at nid=33? How are you using the Global:Custom Text field? What exactly are you putting in that field, and how do you expect it to behave? More detail!
Comment: I answered that question about where the block is being displayed - on the page you view the submission. Therefore the nid and sid are viewable in the url. So if on node/33/submission/14 you would see the block. The block should be picking up the two values (it is picking up the nid). I do have a submission with sid=11 and that is the one it is always selecting even though I am viewing sid 14 the global context field contains: Create a PDF As you can see I am using tokens for substitution.
Answer: OK. I see the problem. The submission will be for only one node, so all you need to get the information is the sid. The nid you are looking at is not the nid in the webform submission record. (You will understand in a moment.)
First, delete both the contextual filter on Content:nid and the filter on Content:nid.
Then create a contextual filter using Webform Submission:sid. Under 'When the filter value is NOT available', select Provide Default Value > Raw value from URL, and set the Path Component to 4.
Now to get the correct nid value, you need to create a Relationship on Webform submissions: Node, and check 'Require this relationship'. Now go back to your fields, and edit the Content:nid field changing 'Relationship' to 'Webform Node', and save. Your field should now display as '(Webform Node) Content: Nid (Nid)'. This is the nid found in the Webform Submission record.
Save your view, and you will get the correct values in the block display. I don't know about the token replacements, but it looks like it should work. If not, the Views PHP module will be able to do it without the tokens, using the $row values directly from your view.
Good Luck.
Comment: Thanks-you for the clear well written explanation. I will give this a try and let you know how it works out. It is weird that what I had before did work, but now doesn't. I don't understand why but your explanation on how to set it up now makes sense.


Guitar Notation E-Gt

Question: What does E-Gt at the beginning of a guitar piece mean? It's just left of the Clef.
I saw something online suggesting that it had something to do with overdrive vs clean guitar, but I couldn't confirm that anywhere.
Comment: Electric Guitar.
Comment: Exactly as @Bob said. To the left of a staff is where the staff's instrument is specified, and the instrument names are usually abbreviated.
Comment: @Patrx2, why not add your comment as an answer...?
Comment: Fair enough, @Bob. I was wondering why you hadn't. :D
Comment: Also sometimes abbreviated El-G
Answer: As mentioned in Bob Bradley's comment, it means Electric Guitar. To the left of a staff is where the staff's instrument is specified, and the instrument names are usually abbreviated.

Guitar Notation E-Gt


pegar dados de variavel na input

Question: Fala Povo!
Tenho um sistema que cadastrada produtos e cria o que preciso no campo de dados, porem to em uma parte crucial, depois que crio os campos que preciso ele gera input com esses campos! o problema é:
Quando envio via post o input ta como abaixo
<input type="text" name="campo[]" class="campo_input" placeholder="<?php echo $campo; ?>" />

preciso enviar o valor desse campo e separar pra inserir no bd, pois esta num while!!!
<?php
include "conecta.php";
   mysql_set_charset('utf8');

$lista =$_POST['lista'];

$sku = implode(",", array_map(function ($item) {
 return sprintf('"%s"', $item);
}, $_POST['campo']));

$sqli = mysql_query ("INSERT INTO $lista VALUES ('', '".$sku."')")  or die(mysql_error());

if($sqli){
    echo $sku;
}
else {
    echo 'deu erro';
}

?>

ele pega legal, porem nao consigo separar pra incluir no banco, ele da a resposta
Column count doesn't match value count at row 1

justamente porque ele pega a resposta e nao separa, joga tudo em uma unica coluna
Answer:
Column count doesn't match value count at row 1
=
A contagem de colunas não corresponde à contagem de valores na linha 1

Verifique se o numero de colunas na query corresponde ao numero de valores passados no value

Tudo vai depender do valor da  variável $lista

A query também tem erro '".$sku."' o correto é simplesmente $sku conforme soluções abaixo

Supondo que o HTML é composto de 4 inputs conforme exemplo abaixo
<input type="text" name="campo[]" class.....

Assumindo que a variável $lista é composta com o nome da tabela seguida pelos nomes das colunas entre parêntesis
$lista = "nomeTabela(nomeColuna,nomeColuna1,nomeColuna2,nomeColuna3,nomeColuna4)";

Sua query deve ser
$sqli = mysql_query ("INSERT INTO $lista VALUES ('', $sku)")  or die(mysql_error());

Caso o nome da tabela não esteja na variável $lista
$sqli = mysql_query ("INSERT INTO nomeTabela $lista VALUES ('', $sku)")  or die(mysql_error());

Caso a primeira coluna seja AUTO_INCREMENT
$lista = "nomeTabela(nomeColuna1,nomeColuna2,nomeColuna3,nomeColuna4)";

$sqli = mysql_query ("INSERT INTO $lista VALUES ($sku)")  or die(mysql_error());

Existe a possibilidade de estar passando somente o nome da tabela através da variável
$lista = "nomeTabela";

$sqli = mysql_query ("INSERT INTO $lista VALUES ($sku)")  or die(mysql_error());

Comment: perfeito! obrigado!

pegar dados de variavel na input


How can I find commits in which a specified change happened?

Question: How can i find commits in which a specified change happened? In this case, neither the exact filename nor the exact linenumber are known. Its basically a raw search.  I.E. i search for the commits in which a line contains a change from "before" to "after".
ie. commit history as follows:

Commit A (Base)
  The line without change
  The text *before* the change
  Another line without relevant change, before

Commit B:
  The line without change
- The text *before* the change
+ The text *after* the change
  Another line without relevant change, before

commit C:
  The line without change
  The line of code *after* the change
- Another line without relevant change, before
+ Another line without a relevant change

As a result i just want to have the hash for commit B. I basically need all changes in all files.
Comment: Just for my own understanding, should the -/+ not be swapped in your `Commit B`?
Comment: oops, yes, should be swapped
Comment: Good, I was really concerned I was being thick =D
Answer: Use git log -G<search pattern>. For more info, see the second part of this article.
Comment: -G for a regex, or -S for a fixed string
Comment: This works, but it produces a lot of false positives. I have not yet found a way to surpress those commits that do not contain the "after" change.


Matlab "Not enough input arguments" error

Question: I'm new to Matlab and am working on implementing Newton's method on a nonlinear system. I'm using a very simple example but when I call the function I create, I get the error: "Not enough input arguments.
Error in function1 (line 2)
f=x.^3-x.^2-1;"
Here are my function definitions:
Newton's method part:
function [new, iter] = Netn(function1,dfunction1,x)
tol=0.0001;
old=x;
u=function1(x);
v=dfunction1(x);
new=old-u/v;
iter=1;
while (abs(new-old)>=tol)
old=new;
u=function1(old);
v=dfunction1(old);
new=old-u/v;
iter=iter+1;
end
end
Simple function definition
function f=function1(x)
f=x.^3-x.^2-1;
end
Simple function derivative definition (for Newton's)
function f=dfunction1(x)
f=3*x.^2-2*x;
end
Can someone please help me with where I'm going wrong? Typing things like "Netn(function1, dfunction1, 3)" in the command window gets the error. I'd appreciate anything, thanks!
Answer: You need to use function handles.
Change your call to
Netn(@function1, @dfunction1, 3)

Change you main function to (you need to use feval when evaluating the functions passed by function handles)
function [new, iter] = Netn(func1,dfunc1,x)
tol=0.0001;
old=x;
u=feval(func1,x);
v=feval(dfunc1,x);
new=old-u/v;
iter=1;
while (abs(new-old)>=tol)
  old=new;
  u=feval(func1,old);
  v=feval(dfunc1,old);
  new=old-u/v;
  iter=iter+1;
end
end

Now you can also create other objective and derivative functions, with other names. And you pass those names as function handles in your call to Netn, without changing the code to Netn.
Comment: perfect, thanks so much!


A non-Boolean ring without unity with this property

Question: I'm exploring rings that have the following property:
$\rule{10cm}{0.4pt}$
For all $x \in R$, and for all $n \in \mathbb{N}$, there exists $y \in R$ such that $\sum_{i=1}^{n} x = y \cdot x$.
$\rule{10cm}{0.4pt}$
Any ring with unity satisfies this, $y = \sum_{i=1}^{n} 1$. Any Boolean ring also satisfies this, $y = \sum_{i=1}^{n} x$.
My general questions are:
(i) Are there non-Boolean rings without unity that have this property? Or better: are there any rings with elements of infinite order without unity that have this property? (ii) Is this property interesting? (iii) What if we require that $y$ be unique?
Thank you
Answer: 1) yes, any ring with local right identities has this property. That is, if for every $x$, there exists $e$ such that $xe=x$.  Such an example is $\prod _{i=1}^\infty \mathbb Z$.
Actually if you think about it, this is equivalent to your condition, since $\sum_{i=1}^n=y$ does the trick.
2) rings with local identities are interesting.
3) Let me share a bit about what I puzzled out about what happens if you additionally required right identities to be unique.
The first thing you notice is that all the right identities are idempotent:
$x=xe=xe^2\implies e=e^2$
So each idempotent is its own unique right identity.
But $0\cdot e=0$ for every idempotent, so $e=0$, if $0$ has a unique right identity. Unfortunately this means that $R$ is the ring $\{0\}$. So with the uniqueness assumption on all right identities, everything collapses.
Perhaps it can be saved by supposing that just nonzero elements have a unique right identity?
Supposing that, let $e$ be a nonzero idempotent and $f$ be an idempotent such that $ef=e$, it must be that $f=e$, because $e$ is its own unique right identity.
For any element $r$ and idempotent $e$, $e+re-ere$ is an idempotent, and in particular if $r=x$ and $xe=x$, you have $f=e+x-ex$ is an idempotent. Furthermore, $ef=e$. If $e$ is nonzero, it must be that $e=e+x-ex$ and then we see that $x=ex$ also, so the right identity is actually a two-sided identity.
Another thing, suppose that $e$ and $f$ are two idempotents that commute. Then $ef$ is an idempotent such that $(ef)e=ef$ and $(ef)f=ef$, therefore either $ef=0$ or else $e=ef=f$. The upshot is that if two different idempotents commute, they have to be orthogonal. In a commutative ring with identity, it would mean there are no nontrivial idempotents.
Comment: Very clear, thank you.
Comment: @JonathanHebert Added a few of the thoughts that I had which I couldn't enter with my phone.


Animate background color with jQuery

Question: I'm trying to animate a div element when a button is clicked using a function. But, it doesn't work. What may be the problem?
JS:
function caller() {
    $("#hfont1").animate({
        "background-color": "#efbe5c"
    }, 1000);
}

HTML:

Test Stuff

<button id="bid" onclick="caller()">Hit me!</button>

CSS:
#hfont1 {
    color:#e8a010, font-size:48pt;
}

Example is linked here : http://jsfiddle.net/GELTP/1117/
Update : I know the way of using selectors but the question is why it is not working when function _______ is used?
Comment: The function caller is not in scope. Try this fiddle http://jsfiddle.net/edXfg/
Answer: It doesn't work because you're defining the function inside of another function, not the global scope. If you switched from onload to no wrap or no wrap it works.
http://jsfiddle.net/GELTP/1121/
It would be even better if you switched from onclick to a javascript event binding.
Answer: I took onclick="caller()" out and instead put in $('#bid').click(caller); into the jQuery bit. This does the same thing and achieves the result you like. See it here: http://jsfiddle.net/GELTP/1118/
Answer: You are getting this error --> Uncaught ReferenceError: caller is not defined
You need to define your function before using it in onClick
select No Wrap -in on your fiddle
Demo ---> http://jsfiddle.net/GELTP/1120/
Answer: The function caller is not in scope because the jsfiddle wraps it inside window.load
//<![CDATA[
$(window).load(function(){
  function caller() {
    $("#hfont1").animate({"background-color":"#efbe5c"}, 1000);
   }
});//]]>

Try changing the option to No wrap in Head in jsfiddle >> http://jsfiddle.net/GELTP/1124/ <<
Answer: Your function is never getting called.  In JSFiddle, the scope of the html is not the same as the JavaScript, so calling 'caller()' will not call the variable you set up.  Here's a working updated fiddle where you attach the action from JavaScript instead (which is normally considered best practice anyway).
function caller() {
    $("#hfont1").animate({"background-color":"#efbe5c"}, 1000);
}

$('#bid').click(caller);

Answer: Your jsfiddle is to blame, the script is set to "onload" in the settings on the left, set it to "No wrap in " and it works

Animate background color with jQuery


Synology NAS: hdd hibernation and disk life

Question: I've a Synology NAS with 2 WD Red hard drives. I've enabled the hibernation for the drives, so they stop after 20 minutes inactivity.
However, we use the NAS a lot and the disks get powered up very frequently. I was wondering if hibernation could cause earlier failure of the drives in the situation, and if I should disable it. I understand hibernation saves energy, but I care about the integrity of my data more.Thank you!
Comment: What does the documentation say? Generally drives have a datasheet and the startup planning is part of that, you know.
Comment: @TomTom I'm not a real hardware expert, I'm not really sure! The NAS is for home use only, so price was a big factor (together with online reviews for quality and reliability)
Comment: Ever considered READING? "No hardware expert" is not the same as "incapable of reading". And as professional admin (requirement to ask here) I would assume you are capable of reading. Normally restart limits are part of the datasheet. And quite explicitly so.
Comment: @TomTom I didn't know there was a documentation, and that a documentation said that. And there's no need to be this rude.
Comment: Go into a restaurant. Walk into the kitchen. Ask basic questions. See how the chef treats you. This site is for "professional administration ONLY".
Comment: So, I've read the documentation but I don't know where it says that: http://www.wdc.com/wdproducts/library/?id=368&type=8 And I'm asking here because I want to learn how to become more "professional".
Comment: No, you ahve not. You have put random words into google. Datasheet. Datasheet. "WD RED DATASHEET" and you get something. Maybe you should pay someone to do that?
Answer: I think hibernation would be more risky than just leaving the disk spinning simply because you are exercising the motor more. Steady state should be more reliable. However I can't find any studies supporting the theory.
Comment: Documentation gives 600k cycles. Over 5 years of 365 days that resutls into 547 restarts per day under warranty.
Comment: Personally I don't care about "under warranty" warranty won't get your data back, and the warranty is 3 years.  The fact that they call out that the number of cycles is different than the MTBF hours tells me it an additional risk.  So the question is is the power saving worth the risk?  If the data is redundant then I'm inclined to say the risk of data loss is probably outweighed by the savings, particularly as you add disks.  If however as posted the disks are used heavily anyway so that there is little savings I'm inclined to turn it off.
Comment: Noone ever suggested that disks are somehow equivalent of backups but you certainly don't need the added expenses of recovery from failed disks because you knowingly took on additional risks, additionally unless you've invested in CDP your backup is not going to contain data between backups.
Comment: That is as ignorant as it gets - for a forum for professional admins. Warrants is cost. Data protection - here is a hint: No raid is a backup. Never. So, you want not to loose your data, keep MULTIPLE backups on OFFLINE media. Which a professional would do anyway. Which puts the risk into "there is inconvenience and cost". But if the data is at risk becasue of that - then someone is gross incompetent and ignoring the backup side.
Answer: The documentation says they are good for 600k load cycles with a 3 year warranty.
<code>600k/(365*3) ~= 548
</code>
That is ~540 stop/start cycles per day. That sounds ok for me for normal power cycling as described.
Comment: That's the same file I linked you. And as I told you, even after I read the docs, I did not know what it meant. That's why I asked here. There's no need to be rude.
Comment: @Qualcuno And I am not as rude. Realism please. If you dont know computer hardawre, do not go to a place where end users are not supposed to be.


Low(est) level digital piano vs keyboard comparison

Question: first of all, I have not touched a keyboard/piano in my life before.
I am planning to pick it up as soon as next week. Normally if I knew that I'd stick with this new hobby, I would not limit myself on budget this much. I just don't want to but something really expensive and try to ebay it one week after.
I did some reading, and I am down to two, a Yamaha Digital Piano or a Yamaha keyboard.
These are the cheapest models that I can get with "touch sensitivity" although both lack any hammer action or weighted keys.
I guess my question is: Is there any advantage of buying a digital piano when the keyboard offers a bunch more functions (and buttons!) From my point of view, please do correct me if I am wrong

They are both 61 keys
They are both touch sensitive (speed sensitive?)
They lack weighted keys
They use the same recorded concert piano to produce a grand piano sound

Can anyone point at what I'm missing here? I mean is there any reason to buy a "digital piano" over keyboard?
Comment: This will likely be closed as being opinion based, and asking for reccommended as equipment.  I suggest rewording to ask what features to look for, as opposed to which product is better.
Comment: Edited to remove as much of the product recommendation piece as possible.
Comment: I was not really asking for a product recommendation. Since my knowledge is very limited about the equipment, I am just trying to justify buying a digital piano over a keyboard with very similar features.
Answer: Personal opinion:  companies shouldn't be allowed to market an instrument as a "digital piano" if it lacks weighted keys.  That's really the biggest difference in my opinion between a keyboard and a digital piano.  It changes the entire feel of the instrument, and adds a layer of intimacy that a keyboard lacks.
In your case it will come down to what sound you are looking for.  If you are looking for an organ sound, chances are pretty good that the keyboard will be able to emulate that better.  For the piano sound, the "digital piano" will probably sound more accurate.  Ultimately you'll have to listen to them in person to decide.
Answer: In my opinion this depends on the music style you intend to play. Typically the e-piano will have more than one piano sound (and thats because different pieces benefit from different sounds), while the keyboard has tons of instruments, effects, drum rythms and the like. I don't see facts that specify, that these instruments use the "same grand piano sound"; even if the same real instrument was sampled, they could sound somewhat different. I would recommend to get "hear and feel" impressions in a real shop or look for a used instrument in the same price range to get better keys.
Comment: Thanks for your answer guidot. I did just that, visited a store put the mentioned equipment next to each other and kept hitting the same notes on both. As far as my untrained ears are concerned I could not hear a tone difference. I tried asking the shopkeepers opinion but it is very hard to get an unbiased answer from them. He was a big advocate of another very popular brand and their digital pianos, so he left me in more confused state.
Comment: And for my style, I really don't have one at the moment, but I'm mostly concerned about learning and playing classical pieces on a piano
Comment: @kali: I also know such shops; I would take the e-piano then: less additional keys, less distraction. This is stuff not necessary for piano playing and not present on a real piano either.
Answer: A different angle : why should you make the loss selling something which you later decide isn't for you ? Let someone else take the drop. Buy a pre-owned example, and you'll get a) a better deal and b)most of your money back when you sell it. Most of these are pretty bomb-proof now, until dropped, but you'll soon tell. So when you've decided on two or three 'boards, start looking  - but not necessarily in shops, although some may sell second-hand.


Error: Variable uses an Automation Type not supported in VBScript

Question: I am executing a VBScript function in C++ using  Microsoft script control (IScriptControlPtr interface).
VBScript Function:
Function Add1(a,b)
   Add1 = a+b
End Function

I need to pass unsigned long values to the parameters a and b.
So I assigned parameters datatype as VT_UI4. But When executing the function I am getting the error "Variable uses an Automation Type not supported in VBScript".
To my requirement , how to handle unsigned long values?
Comment: As long as you don't try to do maths with them they are as you set them. It is how the integer is interpreted that matters. So `A = &hffffffff`. It is -1 if you try and use it in VBScript. But it holds it's value if you don't try to use it as a number. If passing to a function just pass it. VBScript will think you are passing -1 and the function will gets the bits that an unsigned integer of 2^30 has.
Answer: VBScript only supports signed 2- and 4-byte Integers. So you have to change your arguments to VT_I4, or use type conversion functions like UnsignedToLong and LongToUnsigned. See below for the additional information.

How To Convert Between Signed and Unsigned Numbers
VBScript Data Types
Comment: True, but could you at least refer to it in relation to VBScript not Visual Basic for Applications, they are not mutually exclusive. In this case the information is correct but here is the [VBScript documentation](https://msdn.microsoft.com/en-us/library/9e7a57cf(v=vs.84).aspx) for reference.


Como criar uma view com crosstab no SQLITE via Android?

Question: Tenho as seguintes tabelas:
┌──────────────┐  ┌──────────────┐   ┌─────────────────────────────┐
│ TABLE pessoa |  | TABLE lente  |   | TABLE pessoa_lente          |
├──────────────┤  ├──────────────┤   ├─────────────────────────────┤
│ _id, NAME    │  │ _id, COLOR   │   │ _id, idpessoa, idlente, qty │
├──────────────┤  ├──────────────┤   ├─────────────────────────────┤
│ 1, "mary"    │  │ 1, "BLACK"   │   │ 1, 1, 1, 50                 │
├──────────────┤  ├──────────────┤   ├─────────────────────────────┤
│ 2, "juan"    │  │ 2, "BLUE"    │   │ 2, 1, 3, 30                 │
├──────────────┤  ├──────────────┤   ├─────────────────────────────┤
│ 3, "jose"    │  │ 3, "GRAY"    │   │ 3, 1, 4, 25                 │
├──────────────┤  ├──────────────┤   ├─────────────────────────────┤
│ ...          │  │ 4, "YELLOW"  │   │ ...                         │
└──────────────┘  ├──────────────┤   └─────────────────────────────┘
                  │ 4, "YELLOW"  │
                  └──────────────┘

E preciso obter o seguinte:
[crosstab]
NAME   | BLACK | GRAY | YELLOW
"mary" | 50    | 30   | 25
...

Nota Importante: Não se trata de join. Gostaria de uma crosstab, recurso conhecido em outros bancos, como PostgreSQL.
Answer: Olha, pelo que vi até agora do SQLite não existe uma forma de trabalhar com o crosstab, ou algo parecido, pois uma característica marcante do gerenciador de banco é sua simplicidade, o que o tornou um banco de dados leve, fazendo com que o Android dê suporte nativo a ele.
O único lado negativo do SQLite é que foi retirado e/ou não implementado um monte de recursos legais que tem em outros gerenciadores de banco de dados. Já no caso de crosstab são muito poucos gerenciadores de banco de dados que possuem essa ferramenta.
Ao meu ver você vai ter que tratar esse tipo de dados pelo app e não pelo banco.
Comment: Para ser bem sincero, como trabalho com gerenciadores de banco de dados robustos como o PostgreSQL, com o SQLite me sinto um confeiteiro com apenas açucar como matéria-prima para produzir toda gama de possíbilidades de sabores. O SQLite **NÂO** tem um monte de coisas que me faz sentir seguro ao usar banco de dados, invés de criar um texto plano como _bando de dados_ . Recentemente, por exemplo, descobri o caso do tipo Boolean(não nativo?!) e a cada dia tenho mais surpresas.Não acho que coisas essenciais a um banco deva ser descartada em preferência a leveza.


React Native Text Input Android Cursor/Caret and Text Cursor Color

Question: I'm struggling with the React Native Text Input component and can't seem to find a way to change the color of the cursor and the text selection indicators on android.
The official documentation only lists a prop for the selection color (the highlighted background of the text).
Is there any way to do this without changing it globally?
And if changing it globally is the correct way to do it, what is the best way to do it? Change it in the styles.xml? React-Native crashes when i try to change that.
Answer: We Can change it for android in styles.xml file located at android/app/src/main/res/values/styles.xml
But, before that it's better to stop the NodeJS server, then make the below changes
<resources>

</resources>

You can also add custom hex color codes by adding a colors.xml file in the same directory with the following code.
<resources>
    <color name="primaryRed">#EB1E27</color>
</resources>

Then in your styles.xml file you can reference that color with @color/primaryRed. Which would look something like this:
<resources>

</resources>

Once this is done, It is recommended to Re-build the project with Android studio, if the build is successful, then you can run react-native run-android.
Comment: Thanks, that works great! BTW, no need to declare AppTheme and such. You can just simply add the 'colorControlActivated' line on its own.
Answer: Godwin's answer helped me but I had to run this afterwards
cd android
./gradlew clean
cd ..
react-native run-android

because the build would throw this error Failed to capture snapshot of output files for task ':app:processDebugResources' property 'sourceOutputDir' during up-to-date check
Source


MLE of $f(x\mid\theta) = \theta x^{\theta−1}e^{−x^{\theta}}I_{(0,\infty)}(x)$

Question:
$X_1$, $X_2$, and $X_3$ are i.i.d random variables having pdf
$$f(x\mid\theta) = \theta
 x^{\theta−1}e^{−x^{\theta}}I_{(0,\infty)}(x)$$  where $\theta \gt0$.
  Using the observed values of these random variables given below, give
  the mle of $\theta$.
$x_1= 0.60$, $x_2= 5.17$, $x_3= 0.23$

My Attempt:
We have
$$\begin{align*}
L(\theta\mid x)=\prod_{i=1}^3 \theta
 x_i^{\theta−1}e^{−x_i^{\theta}}
&=\theta^3 \left(\prod_{i=1}^3 x_i\right)^{\theta-1} \mathsf{exp}\left(-\left(\sum x_i^{\theta}\right)\right)
\end{align*}$$
Upon taking the taking the natural log of both sides, we get
$$\mathcal{L}(\theta\mid x)=3\cdot\text{log}(\theta)+(\theta-1)\left(\sum \text{log } x_i\right)-\sum\left(x_i^{\theta}\right)$$
Then we have
$$0=\frac{d}{d\theta}\left(\mathcal{L}(\theta\mid x)\right)=\frac{3}{\theta}+\sum\text{log }x_i-\sum\left(\text{log }x_i\cdot x_i^{\theta}\right)$$
Is this valid thus far? If so, how can I proceed from here to find which value of $\theta$ makes this function $0$? It's not immediately clear to me how to do so.
Edit:
Using Newton's Method to obtain a mle we have
$$\hat{\theta}_{j+1} = \hat{\theta}_j - \frac{g(\hat{\theta}_j)}{g'(\hat{\theta}_j)}$$
where $g(\theta)=\frac{d}{d\theta}\left(\mathcal{L}(\theta\mid x)\right)$.
With an initial value of $\theta=1$, I get that this converges to approximately $0.67715$ after only a few iterations.
Check:
$$\frac{3}{0.67715}+\sum\text{log }x_i-\sum\left(\text{log }x_i\cdot x_i^{0.67715}\right)\approx 0$$
Is it necessary to show that we have a global maximum still?
Comment: I did not find any problem until last equation. For the solution, maybe you need the Newton-Raphson Method.
Comment: @a_statistician I think you're right. I recall learning about that.
Comment: I think you need more iterations, because 0.0024 is far from 0.
Comment: I did a lot of rounding on my calculator. In any case, I think I have the correct mle now. Thank you for your suggestion!
Comment: I don't see why that term is wrong. I checked it on wolfram. Also, I'm not sure how I would go about showing that this is a global max. It's not even clear to me that the second derivative of $\frac{3}{\theta^2}-\sum(x_i^{\theta})log(x_i)^2$ is less than $0$
Comment: I made a mistake, thinking about $\frac{d}{dx_i}$ instead of $\frac{d}{d\theta}$
Comment: No problem. Any ideas on how I could show what I obtained is indeed a global maxima? In previous exercises, it was more straightforward.
Comment: The second derivative has the first term negative as well $-3/\theta^2 - \sum x_i^\theta \log(x_i)^2$
Comment: Is it enough to say that for all $\theta\in(0,\infty)$ and for any set of observations, the second derivative is negative except when $\theta$ is our maximum likelihood estimate?
Answer: If you're not sure whether or not your answer is correct, a useful check is to plot a graph of the log-likelihood function and see if your purported MLE looks visually to give the maximising value.  I will do this below, but I include the mathematics for deriving the MLE in the general case.

MLE in the general case: For IID data from this distribution, you have log-likelihood:
$$\ell_\mathbf{x}(\theta) = n \ln \theta + (\theta-1) \sum_{i=1}^n \ln x_i - \sum_{i=1}^n x_i^\theta \quad \quad \text{for } \theta>0.$$
The corresponding score function is:
$$s_\mathbf{x}(\theta) = \frac{d\ell_\mathbf{x}}{d\theta}(\theta) = \frac{n}{\theta} + \sum_{i=1}^n (1-x_i^\theta) \ln x_i,$$
and the observed information is:
$$I_\mathbf{x}(\theta) = - \frac{d^2\ell_\mathbf{x}}{d\theta^2}(\theta) = \frac{n}{\theta^2} + \sum_{i=1}^n x_i^\theta (\ln x_i)^2 > 0.$$
We can see from the positive observed information that the log-likelihood is strictly concave, which means that the MLE will be at the unique critical point (unless the score is monotone, in which case the maximum is approached at the boundary of the parameter range, and there is no MLE).  The critical point is given implicitly by solving the score equation:
$$0 = s_\mathbf{x}(\hat{\theta}) = \frac{n}{\hat{\theta}} + \sum_{i=1}^n (1 - x_i^\hat{\theta}) \ln x_i.$$
There is no closed-form expression for the MLE in this case, so we need to find it numerically.

Iterative algorithm for MLE: Applying Newton's method, with your chosen starting-point, gives:
$$\hat{\theta}_0 = 1 \quad \quad \quad \hat{\theta}_{k+1} = \hat{\theta}_{k} + \frac{s_\mathbf{x}(\hat{\theta})}{I_\mathbf{x}(\hat{\theta})} = \hat{\theta}_{k} + \frac{n \hat{\theta}_k + \hat{\theta}_k^2 \sum_{i=1}^n (1 - x_i^{\hat{\theta}_k}) \ln x_i}{n + \hat{\theta}_k^2 \sum_{i=1}^n x_i^{\hat{\theta}_k} (\ln x_i)^2}.$$
(Note: The starting point you have chosen is a reasonable one.  With some calculus, it is possible to show that $\mathbb{E}(X) = \Gamma(1 + 1/\theta)$, so we could approximate $\bar{x} \approx \Gamma(1 + 1/\theta)$ as a starting point for the iteration.  However, the problem is that this already requires numerical solution, so it is not a great starting point.  The value you have chosen is reasonable, and the iteration should converge quite rapidly in any case.)  We can implement this iteration algorithm in the following R code:
#Create function to find the MLE via iteration
#The input m is the number of iterations to perform (default is five iterations)
MLE_ITERATION <- function(x, m = 5) {
    n  <- length(x);
    T  <- 1;
    theta <- rep(T, m+1);
    for (k in 1:m) {
        NUMERATOR   <- n*theta[k] + theta[k]^2 * sum((1-x^theta[k])*log(x));
        DENOMINATOR <- n + theta[k]^2 * sum(x^theta[k]*(log(x))^2);
        theta[k+1]  <- theta[k] + NUMERATOR/DENOMINATOR; }
    theta; }

Application to your data set: You have the data vector $\mathbf{x} = (0.60, 5.17, 0.23)$.  With $m=10$ iterations (which is more than you need) you get the MLE $\hat{\theta} = 0.6771516$.  The log-likelihood and MLE are shown here:

Here is the R code used to generate the MLE and the plot:
#Enter your data
x <- c(0.60, 5.17, 0.23);

#Choose number of iterations
m <- 10;

#Generate the iterations, and display the last value
THETA_ITER <- MLE_ITERATION(x, m);
THETA_ITER[m+1];

[1] 0.6771516

#Generate vectorised log-likelihood function
LOG_LIKE <- function(theta) {
    LL <- rep(0, length(theta));
    for (i in 1:length(theta)) {
        LL[i] <- length(x)*log(theta[i]) + (theta[i]-1)*sum(log(x)) - sum(x^theta[i]); }
    LL }

DATA <- data.frame(Theta    = 1:200/100,
                   Log_Like = LOG_LIKE(1:200/100));

#Plot the log-likelihood function with MLE
library(ggplot2);
ggplot(data = DATA, aes(x = Theta, y = Log_Like)) +
    geom_line(size = 1.2) +
    geom_vline(xintercept = THETA_ITER[m+1],
               size = 1.2, linetype = 'dashed', colour = 'red') +
    theme(plot.title    = element_text(hjust = 0.5, face = 'bold'),
          plot.subtitle = element_text(hjust = 0.5)) +
    ggtitle('Plot of Log-Likelihood Function') +
    labs(subtitle = '(Red line shows MLE - obtained via iteration)') +
    xlab(expression(theta)) + ylab('Log-Likelihood');


How to implement custom theme functions (theme_textfield, theme_form_element) for some forms only?

Question: How to implement custom theme functions like theme_textfield or theme_form_element?
I know we can do it using hook_theme_registry_alter:
/**
 * Implements hook_theme_registry_alter().
 */
function krown_admin_theme_registry_alter(&$theme_registry) {

  //Override some Drupal defaul theme functions

  $theme_registry['textfield']['theme path'] = drupal_get_path('module', 'krown_admin');
  $theme_registry['textfield']['function'] = 'krown_admin_theme_textfield';
  $theme_registry['textarea']['theme path'] = drupal_get_path('module', 'krown_admin');
  $theme_registry['textarea']['function'] = 'krown_admin_theme_textarea';
  $theme_registry['form_element']['theme path'] = drupal_get_path('module', 'krown_admin');
  $theme_registry['form_element']['function'] = 'krown_admin_theme_form_element';
  $theme_registry['form_element_label']['theme path'] = drupal_get_path('module', 'krown_admin');
  $theme_registry['form_element_label']['function'] = 'krown_admin_theme_form_element_label';

}

The problem with this approach, is that the custom functions are applied on all website. I just want to apply them for some forms or for a specific theme.
Answer: Those theme functions don't get a reference to the form where the element is used. Therefore, they cannot be used to alter an element for a single form.
Every form element accepts #theme, so you can alter the #theme function used for those form fields in a single form. This is what Drupal does in node_filter_form().
  $form['filters'] = array(
    '#type' => 'fieldset',
    '#title' => t('Show only items where'),
    '#theme' => 'exposed_filters__node',
  );

theme_exposed_filters() then does its job.
  $form = $variables['form'];
  $output = '';

  if (isset($form['current'])) {
    $items = array();
    foreach (element_children($form['current']) as $key) {
      $items[] = drupal_render($form['current'][$key]);
    }
    $output .= theme('item_list', array('items' => $items, 'attributes' => array('class' => array('clearfix', 'current-filters'))));
  }

  $output .= drupal_render_children($form);

  return '
' . $output . '
';

Unfortunately, this doesn't work with theme_form_element() or theme_form_element_label().


Como determinar se dois valores aleatórios estão próximos?

Question: Tenho uma dúvida que gostaria de obter respostas.
Possuo uma função RAND que gera valores de 1 a 20.
O que pretendo fazer é:
Se o valor $rand1 tiver perto do valor $rand2
echo ok

Como faço para dizer "Perto de" em PHP?
Comment: Depende, o que você considera "próximo"?
Comment: Definir um valor para 'perto' e subtrair os dois números? seria isso?
Comment: Por mais que falta um exemplo do que tentou, não concordo dessa pergunta está negativada!
Comment: Sim, Se o rand1 for 12 e o rand2 for 13, echo está perto.
Mas queria fazer isso para os 2 rands de forma facil sbaendo que ambos podem ir de 1-20.
Answer: É, o caminho é o que o @rray comentou:
function perto($a, $b)
{
    $limite = 2;
    return abs($a - $b) <= $limite;
}

if(perto($rand1, $rand2)) {
    echo 'perto';
}

Comment: Alias + 1, entendi o que acontece no seu método.
Comment: Gostei desta, foi a mais acertada e resolve o meu problema. Obrigado
Comment: Poderia explicar o funcionamento do método @bfvaretto? Não sei nada de PHP, achei interessante a dúvida.
Answer: Tente essa lógica, não testei pôs estou pelo celular.

if ($rand1 > $rand2) {
    if ($rand1 - $rand2 == 1) {
        echo "Esta perto";
    }
    else {
        echo "Está longe";
    }
}
else if ($rand2 > $rand1) {
    if ($rand2 - $rand1 == 1) {
        echo "Esta perto";
    }
    else {
        echo "Está longe";
    }
}


Derivation of closed form lasso solution

Question: For the lasso problem
$\min_\beta (Y-X\beta)^T(Y-X\beta)$ such that $\|\beta\|_1 \leq t$. I often see the soft-thresholding result
$$ \beta_j^{\text{lasso}}= \mathrm{sgn}(\beta^{\text{LS}}_j)(|\beta_j^{\text{LS}}|-\gamma)^+ $$
for the orthonormal $X$ case. It is claimed that the solution can be "easily shown" to be such, but I've never seen a worked solution. Has anyone seen one or perhaps has done the derivation?
Comment: Thanks for a great discussion!
I came across this video on coursera - [Deriving the lasso coordinate descent update](https://www.coursera.org/learn/ml-regression/lecture/6OLyn/deriving-the-lasso-coordinate-descent-update), that is very relevant to this discussion, and walks through the solution very elegantly. Might be helpful for future visitors :-)
Comment: Partially responding to @cardinal, finding the $\beta$ that minimizes $(Y-X\beta )'(Y-X\beta )$ subject to $\|\beta\|_1 \leq t$ is equivalent to finding the $\beta$ that minimizes $(Y-X\beta )'(Y-X\beta )+\gamma\sum_j |\beta_j |$.  There is a 1-1 relationship between $t$ and $\gamma$.  To 'easily' see why the soft-thresholding result is so, I'd recommend solving the second expression (in my comment).
Comment: Another note, when finding the $\beta$ that minimizes $(Y-X\beta )'(Y-X\beta )+\gamma\sum_j |\beta_j |$, break the problem up into the cases $\beta_j >0$, $\beta_j<0$, and $\beta=0$.
Comment: @cardinal Ah yes, 1-1 is incorrect.  Correction:  for every $t\geq0$, you can find a $\gamma\geq 0$.
Comment: This seems slightly confused. At the beginning you assume a constraint $t$ and in the solution you introduce a parameter $\gamma$. I'm guessing you intend these two to be related via the dual problem, but maybe you can make clear what you are looking for.
Comment: @Mike: Regarding your first comment: That was my point. I was hoping to get the OP to clarify this for himself (and others).
Comment: @Mike: Also, the relationship is not quite one-to-one, since for large enough $t$, the solution will always be the least-squares solution, i.e., the one corresponding to $\gamma = 0$. :)
Comment: @cardinal Your method of egging the OP to further elaborate on the problem is a good one :)  I attempted to use your question as a lead-in to my hint.  I made the assumption that the OP was unfamiliar with the result -- if I am incorrect, sorry Gary!
Comment: @Mike: I like doing that. Sometimes it "degenerates" into long comment streams; but, it's always nice to see when it clicks in someone's head because he/she thought about it rather than being shown. That said, I've gone ahead and resisted that temptation a bit in this instance. Cheers. :)
Comment: @Mike: By the way, your hint in your first comment is a good one!
Comment: @MikeWierzbicki indeed. It would seem that this relationship between $t$ and $\gamma$ is $\sum_{i}^p (|\hat{\beta}_i^{\text{LS}}| - \gamma)_+ = t$
Comment: See [here](https://stats.stackexchange.com/questions/123672/coordinate-descent-soft-thresholding-update-operator-for-lasso/351134#351134)  for a full derivation of the coordinate descent case
Comment: worth watching this video on proximal gradient descent: https://www.youtube.com/watch?v=sy4pRJ3g530 and the following one on lasso: https://www.youtube.com/watch?v=CBMDwMESuWs
Comment: what happens if we don't have $X$ being orthonormal?
Answer: This can be attacked in a number of ways, including fairly economical approaches via the Karush–Kuhn–Tucker conditions.
Below is a quite elementary alternative argument.
The least squares solution for an orthogonal design
Suppose $X$ is composed of orthogonal columns. Then, the least-squares solution is
$$
\newcommand{\bls}{\hat{\beta}^{{\small \text{LS}}}}\newcommand{\blasso}{\hat{\beta}^{{\text{lasso}}}} \bls = (X^T X)^{-1} X^T y = X^T y \>.
$$
Some equivalent problems
Via the Lagrangian form, it is straightforward to see that an equivalent problem to that considered in the question is
$$
\min_\beta \frac{1}{2} \|y - X \beta\|_2^2 + \gamma \|\beta\|_1 \>.
$$
Expanding out the first term we get $\frac{1}{2} y^T y - y^T X \beta + \frac{1}{2}\beta^T \beta$ and since $y^T y$ does not contain any of the variables of interest, we can discard it and consider yet another equivalent problem,
$$
\min_\beta (- y^T X \beta + \frac{1}{2} \|\beta\|^2) + \gamma \|\beta\|_1 \>.
$$
Noting that $\bls = X^T y$, the previous problem can be rewritten as
$$
\min_\beta \sum_{i=1}^p - \bls_i \beta_i + \frac{1}{2} \beta_i^2 + \gamma |\beta_i| \> .
$$
Our objective function is now a sum of objectives, each corresponding to a separate variable $\beta_i$, so they may each be solved individually.
The whole is equal to the sum of its parts
Fix a certain $i$. Then, we want to minimize
$$
\mathcal L_i = -\bls_i \beta_i + \frac{1}{2}\beta_i^2 + \gamma |\beta_i| \> .
$$
If $\bls_i > 0$, then we must have $\beta_i \geq 0$ since otherwise we could flip its sign and get a lower value for the objective function. Likewise if $\bls_i < 0$, then we must choose $\beta_i \leq 0$.
Case 1: $\bls_i > 0$. Since $\beta_i \geq 0$,
$$
\mathcal L_i = -\bls_i \beta_i + \frac{1}{2}\beta_i^2 + \gamma \beta_i \> ,
$$
and differentiating this with respect to $\beta_i$ and setting equal to zero, we get $\beta_i = \bls_i - \gamma$ and this is only feasible if the right-hand side is nonnegative, so in this case the actual solution is
$$
\blasso_i = (\bls_i - \gamma)^+ = \mathrm{sgn}(\bls_i)(|\bls_i| - \gamma)^+ \>.
$$
Case 2: $\bls_i \leq 0$. This implies we must have $\beta_i \leq 0$ and so
$$
\mathcal L_i = -\bls_i \beta_i + \frac{1}{2}\beta_i^2 - \gamma \beta_i \> .
$$
Differentiating with respect to $\beta_i$ and setting equal to zero, we get $\beta_i = \bls_i + \gamma = \mathrm{sgn}(\bls_i)(|\bls_i| - \gamma)$. But, again, to ensure this is feasible, we need $\beta_i \leq 0$, which is achieved by taking
$$
\blasso_i = \mathrm{sgn}(\bls_i)(|\bls_i| - \gamma)^+ \>.
$$
In both cases, we get the desired form, and so we are done.
Final remarks
Note that as $\gamma$ increases, then each of the $|\blasso_i|$ necessarily decreases, hence so does $\|\blasso\|_1$. When $\gamma = 0$, we recover the OLS solutions, and, for $\gamma > \max_i |\bls_i|$, we obtain $\blasso_i = 0$ for all $i$.
Comment: +1 The entire second half can be replaced by the simple observation that the objective function $\beta\to\frac{1}{2}\beta^2+(\pm\gamma-\hat{\beta})\beta$ is a union of parts of two convex parabolas with vertices at $\pm\gamma-\hat{\beta}$, where the negative sign is taken for $\beta\lt 0$ and the positive otherwise.  The formula is just a fancy way of choosing the lower vertex.
Comment: @Cardinal: thanks for a nice derivation. One observation. If I recall, matrix with orthogonal columns is not the same as an orthogonal (aka orthonormal) matrix. Then $X'X=D$ for some diagonal matrix $D$ (not necessarily identity matrix). With orthogonal matrix assumption (as is in the original question), we do have $X'X=I$ and all looks great :)
Comment: @whuber that's not what I meant. I cant see why this soft thresholding gives us the vertex of the **lower** parabola. Can you explain it please?
Comment: If possible, I would like to see the derivations using the KKT-optimality conditions. What other ways are there to derive this result?
Comment: @cardinal I don't get why you say "since otherwise we could flip its sign and get a lower value for the objective function". We are taking the derivative of the objective function. So what if the objective function is higher or lower, who cares. All we care about is the derivative is set to zero, we care about the extrema. Whether it's higher or lower by a constant does not affect the argmin.
Comment: I didn't understand exactly your solution. I asked a similar question here: https://stats.stackexchange.com/questions/323234/how-is-the-lasso-orthogonal-design-case-solution-derived/323242?noredirect=1#comment613347_323242
Comment: why $ \beta_i > 0 $ implies LS $ \beta_i > 0$ ? @cardinal
Comment: why $ \beta_i > 0 $ implies LS $ \beta_i > 0$ ? or maybe I should say why LS $ \beta_i > 0$ implies $ \beta_i > 0 $ @cardinal
Comment: @whuber can you please elaborate your suggested trick? what do you mean by lower vertex, and why we prefer it? I don't get why the soft-thresholding is true.
Comment: @Dennis Parabolas have vertices: see https://www.google.com/search?q=vertex+parabola
Comment: what happens if we don't have $X$ being orthonormal?
Answer: Assume that the covariates $x_j$, the columns of $X \in \mathbb{R}^{n \times p}$, are also standardized so that $X^T X = I$. This is just for convenience later: without it, the notation just gets heavier since $X^T X$ is only diagonal. Further assume that $n \geq p$.  This is a necessary assumption for the result to hold. Define the least squares estimator $\hat\beta_{OLS} = \arg\min_\beta \|y - X \beta\|_2^2$. Then, the (Lagrangian form of the) lasso estimator
\begin{align*}
 \hat\beta_\lambda
 & = \arg\min_{\beta} \frac{1}{2n} \|y - X \beta\|_2^2 + \lambda \|\beta\|_1 \tag{defn.} \\
 & = \arg\min_\beta \frac{1}{2n} \|X \hat\beta_{OLS} - X \beta\|_2^2 + \lambda \|\beta\|_1 \tag{OLS is projection} \\
 & = \arg\min_\beta \frac{1}{2n} \|\hat\beta_{OLS} - \beta\|_2^2 + \lambda \|\beta\|_1 \tag{$X^TX=I$} \\
 & = \arg\min_\beta \frac{1}{2} \|\hat\beta_{OLS} - \beta\|_2^2 + n \lambda \|\beta\|_1 \tag{algebra} \\
 & = \mathrm{prox}_{n \lambda \|\cdot\|_1} \left( \hat\beta_{OLS} \right) \tag{defn.} \\
 & = S_{n \lambda} \left( \hat\beta_{OLS} \right) \tag{takes some work},
\end{align*}
where $\mathrm{prox}_f$ is the proximal operator of a function $f$ and $S_{\alpha}$ soft thresholds by the amount $\alpha$.
This is a derivation that skips the detailed derivation of the proximal operator that Cardinal works out, but, I hope, clarifies the main steps that make possible a closed form.
Comment: what happens if we don't have $X$ being orthonormal?
Comment: @CharlieParker In practice, I usually the QR transform. So $X = QR$ then $X \beta$ becomes $Q \hat{\beta}$ with $\hat{\beta} = R \beta$. Then, at the end, transform back and check that the original $\beta$ has the sparsity you want. If not, increase $\lambda$ and repeat.

Usually, I just find a $\lambda$ that tends to work for my dataset, set it, and deploy. I like the coordinate descent implementation because it can be done in a distributed manner, and oftentimes can be interrupted while still giving me a workable answer.


how to get specific property name via rest call in strongloop loopback

Question: I am having students model in loopback which is accessible via rest using this url. /api/students rest call gives all the student data but I want to get only the registration numbers (regno) of students. Like in sql (SELECT regno FROM students). how I should filter the above rest call to get only the registration numbers of students.
Answer: Use the fields filter.
?filter[fields][regno]=true

https://docs.strongloop.com/display/public/LB/Fields+filter
Answer: ?filter={"fields":"regno"}

If you want to get multiple columns
?filter={"fields":["regno","column2","column3",....]}


Rails - using :include to find objects based on their child's attributes

Question: I have a sentence and correction model
class Sentence < ActiveRecord::Base
    has_one :correction

class Correction < ActiveRecord::Base
   belongs_to :sentence

and I'm trying find all sentences which don't have a correction. To do this I'm simply looking for corrections which don't exist i.e. whose id = nil. But it is failing and i can't figure out why
 Sentence.find :all, :include => :correction, :conditions => {:correction => {:id => nil}}

from (irb):4>> Sentence.find :all, :include => :correction, :conditions => {:correction => {:id => nil}}

ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'correction.sentence_id' in 'where clause': SELECT * FROM sentences WHERE (correction.sentence_id IS NULL)
Perhaps its the syntax or maybe just the overall approach. Can anyone help?
Answer: You can use this:
Sentence.all(:include => :correction,
  :conditions => "corrections.sentence_id IS NULL")

Comment: the problem is im getting this error

Sentence.all(:include => :correction,
?>   :conditions => "corrections.sentence_id IS NULL")
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'correction.sentence_id' in 'where clause': SELECT * FROM `sentences` WHERE (`correction`.`sentence_id` IS NULL)
Comment: It looks like you are referring `correction` instead of `corrections` in `conditons` string.


Ubuntu 10.04 LTS Virtual Machine on VirtualBox not able to use network

Question: Hey all, no matter what Network Adapter I use on VirtualBox for my newly setup Ubuntu VM, I cannot get a valid IP when I do ipconfig. All I get is a loopback address. Does anyone know what I would do to fix this and which Network Adapter in VirtualBox I would use to properly connect to my actual PC (host) and the outside world?
Thanks,
Darren
Answer: for anyone with the same problem this is how you would do this:
change eth0 to eth1 in /etc/network/interfaces, and choose the bridged network.
Comment: The proper way would be to first check your available interfaces with `ifconfig -a` and then configure the appropriate interface. FYI: In your case, the kernel renames the interface to eth1 during bootup because of the persistent-net-rules in udev.


Consulta relação muito para muitos SQL server

Question: Ola, estou com uma duvida para fazer um select no SQL SERVER. Possuo 3 tabelas. Elas são, LIVROS, AUTOR e LIVRO_AUTOR. A LIVRO_AUTOR possui chave estrangeira do id da LIVROS E AUTOR. Preciso fazer um select que retorne todos os livros com autores nascidos no Brasil (esse campo tem na tabela autor). Fiquei pensando algum tempo nesse problema, me parece simples. mas ate agora não achei como fazer funcionar.
Answer: Jhow tem mais de uma maneira de resolver seu problema , uma delas seria :
      select titulo from livros inner join livro_autor on livros.id = livro_autor.fk_livro
      inner join autor on autor.id = livro_autor.fk_autor
      where autor.pais = 'Brasil'group by livros.id , livros.titulo

Answer: Olá, não conheço a estrutura de suas tabelas, então vou tentar te ajudar...
select livros.titulo,
       autor.pais
From livros inner join livro_autor
On livros.objectid = livro_autor.objectidlivro
Inner join autor
On autor.objectid = livro_autor.objectidautor
where autor.pais = 'Brasil'

livros.titulo deverá ser substituído pelo nome da coluna em que está a informação do título na sua tabela livros. (Livros.nome_da_coluna)
autor.pais deverá ser substituído pelo nome da coluna onde está a informação do país na tabela autor. (Autor.nome_da_coluna)
Nos inner join, onde está objectid você deve substituir pelo nome da coluna onde estão as informações que se relacionam nas suas tabelas, inicialmente na tabela livros, depois na livro_autor que se relaciona com livros. Depois a coluna da tabela autor que se relaciona com a tabela livro_autor.
Espero ter te ajudado, caso tenha ficado meio confuso, informe os nomes dessas colunas que monto novamente o script correto.
Avise se deu certo.
Comment: Olá, Guilherme. Seja bem-vindo! Quando a pergunta que você pretende responder não possui detalhes suficientes para dar uma resposta satisfatória, é preferível esperar esclarecimentos do autor. Você poderá comentar para pedir isso você mesmo assim que ganhar um pouco mais de reputação. Além disso, use as ferramentas de formatação para separar código de texto, para que fique mais fácil distinguir. Dá pra ter uma noção de como está ficando a resposta olhando a prévia logo abaixo da caixa de edição.


Range-based for loop with abstract class as declaration C++

Question: I want to iterate through a list of an abstract class list<abstract> using a for loop.
Here's my code:
class Component {
    protected:
        string name;
        int price;

    public:
        Component(string name, int price): name(name), price(price){};

        virtual int getPrice() =0;
};

class Composite : public Component {
    list<Component> components;

    public:
        int getPrice() {
            for(Component comp : components) {
                return comp.getPrice();
            }
        }
};

class Leaf : public Component {
    public:
        int getPrice() {
            return price;
        }
};

But I get an error; cannot allocate an object of abstract type 'Component'. This error makes sense, as abstract types can't be instantiated.
Comment: Ok, you understand that abstract types can't be instantiated. So how do you expect to instantiate any member of your `std::list`, which is abstract? The list contains abstract objects. So they have to exist, in the list. But they can't. They're abstract. I have a hunch that you have a Java background, because you expect it to work like that. It won't, not in C++.
Comment: Your question is not clear. Do you want to know how to do range-based loop or how to create list of abstract object? Also, you haven't not tell on which line you got the error! Thus because of the title, I was thinking that you want to understand why the ranged-based loop was not compiling.
Comment: Haha haven't touched Java, but was following an example in it. An answer suggests to use pointers of type abstract class which makes sense (same method was used on cplusplus iirc.
Comment: @RsFps: Java treats objects and pointers to objects as almost the same, C++ doesn't. This is one of those cases where you will notice that.
Comment: What is the type of ```comp```? Isn't it some iterator?
Answer: Your code has some problems:
You declare virtual int getPrice() = 0; and this makes your Component pure virtual -> you CANNOT instantiate an object of it -> list<Component> components; does NOT make sense to appear. Please consider to use list<Component*> (or smart-pointer) instead!
Range-based for loop:
for(Component comp : components) {
    return comp.getPrice();
}

Suppose you have updated your list to list<Component*> then the for loop could be:
for(auto& comp : components) {
    return comp->getPrice();
}

Comment: @RsFps As you have caught it, `auto &` means we want the `comp` reference to each element of the list -- which has the advantage of NOT being copied. If we, however, use `auto` then each element of the list will be first copied to `comp` -- which is usually more costly compared with just making reference to. However, as `Component*` is already pointer type so the advantage won't be significant.
Comment: Why are you doing ```auto&``` in the for loop? Shouldn't it be ```Component*```?
Comment: I've been trying to figure it out with no luck; but I don't understand why we're using ```auto&``` instead of ```auto```. Can you please elaborate as to why we are using the reference? Thanks!
Answer: list<Component> means the list can only contain objects whose type is exactly Component. It cannot contain objects of some other type (even if derived from Component).
Since it is not possible to create (complete) objects of type Component, this list is not usable.
To create a list which can store any object having Component as a base class, the list items must be stored by reference. But then you have to think about lifetime management of the objects in the list.

One way to do this would be:
std::list< std::shared_ptr<Component> > components;

and the loop might be:
for(auto& comp : components) {
     return comp->getPrice();
}

(although why you're using a loop when you always exit on the first item is another question).
There are other options available too which represent different ownership semantics.  I would recommend reading a guide on smart pointers in C++ for further understanding of this topic.
Comment: I've read up a bit about smart pointers. Don't understand their use in this case. Won't the pointers be deleted when going out of scope anyway? I will continue to investigate smart pointers, but so far all I'm  understanding is that they're a way of automatically deleting pointers that reference memory created with the new keyword.
Comment: @RsFps In this case the benefit is that you can have a list of them, where the pointers point to objects of different types (with a common base class).  The lifetime management is an added bonus. BTW you can use `make_shared` rather than `new` to create the objects.
Comment: Are you talking about pointers in general? I'm that case yes, I know why they need to be used here. I was talking about smart pointers. What's the point of having a list of Component smart pointers as opposed to regular Component pointers?
Comment: @RsFps Then the objects will be deleted when they are no longer being referenced
Comment: Isn't that what we want? The for loop isn't being "called" (sorry if that's the wrong term). I changed it and it's skipping it.
Comment: Oh do you mean that after referencing in the for loop, they will be destroyed?
Comment: Yes that's what we want (the objects to be deleted when they are no longer referenced), which is why I suggested using the smart pointer.
Comment: Why are we using a reference in the for loop? Why not just say "auto comp"?
Comment: @RsFps to avoid making a copy of the object in the container
Comment: Can we use "Component* comp : components"?
Comment: @RsFps No , since the elements are not of type `Component*` nor implicitly convertible to
Comment: Sorry I don't understand. Isn't the list ```list```?
Comment: @RsFps my suggestion was to use `std::list< std::shared_ptr >`
Answer: You have to indicate that you want a reference:
for(Component & comp : components)

Or simply
for(auto & comp : components)

If you want (or need), you can also add const modifier.
for(const Component & comp : components)

Note
While my anser might be incomplete, it does explain the error reported in the question. As other have not reported, it make no sense to create a list of abstract object by value.
However the question was specific about the problem related to the loop too. As such, I think that my answer was adequate given the question.
Comment: This is not the problem. The code attempts to create a `list`.
Comment: I tried that, and get these errors: 8:7: note:   because the following virtual functions are pure within 'Component':
16:21: note:  virtual int Component::getPrice()
Comment: That's nice, but this is too little, too late. The shown code will be unable to add any actual values to the list, because of object slicing, and there will never be anything to iterate over.
Comment: How do I get around that?
Comment: @RsFps My response is about the error you reported. I haven't analysis you whole code.
Comment: @PeteBecker The question started with **Range-based for loop** so my answer was specifically about why the range based loop was not working. Also, OP didn't tell on which line he got the error.
Comment: @SamVarshavchik In it question title, OP ask about range-based for loop. So I think it was perfectly right to only answer about that specific point.
Comment: Why are we using a reference in the for loop? Can't we use a pointer?
Comment: @RsFps You can use pointer only when the for loop returns pointers (think array of pointer vs array of object).
Comment: @Phil1979 but doesn't the list consist of Component*?
Comment: The declaration *(in the question)* is `list components;` so it is a list of objects, not pointers. A list of pointers would be `list components;`.
Comment: Sorry I assumed we have updated the list to list
Comment: Well, the problem is that the question was misleading as the error has nothing to do with range based loop... but you start the description with **I want to iterate...**. Thus I have focused only on the problem of the loop that cannot make copy but not about problem with the list itself.
Comment: Sorry for the misunderstanding; in the past couple of comments I was talking about why the loop is using ```auto&```.


WPF ListView still reserving space when collapsed

Question: I have a WPF ListView within a ScrollViewer.
I need to collapse the ListView and I am trying withthis code (pretty striaghtoforward):
this.myListView.Visibility = Visibility.Collapsed;

Problem is the ListView seems to be reserving the space even when collapsed - it disappears but the ScrollViewer doesn't accordingly resize.
Anything I am missing?
Any Help appreciated
Answer: Possible reason could be that ScrollViewer by default shows its vertical ScrollBar.
Try to set ScrollViewer.VerticalScrollBarVisibility to auto.
You can write something like this:
<ListBox ScrollViewer.VerticalScrollBarVisibility="Auto" />

I hope it helps.
Answer: Thanks to everyone for their answers - I found out that a globally applied style was causing the problem! Solved now.
Comment: can you share that style?
Comment: Can you share your solution?


Yii 2: Load a module's view as a partial view on another application view

Question: I need to load a module's view as a partial view inside another view of the application. I find no clue about how to do this in the manual.
The view is completely independent of the module:
<?php
    // This is the module's class. Do I need it here?
    use vendor\xxx\cropk\CropK;

/* @var $this yii\web\View */
    $this->title = 'Cropping Test';
?>

Cropping Test

    <?php
        // ...
    ?>

How can I accomplish this?
Answer: Looking at render's documentation you have a few options:

The view to be rendered can be specified in one of the following formats:

path alias (e.g. "@app/views/site/index");
absolute path within application (e.g. "//site/index"): the view name starts with double slashes. The actual view file will be looked for under the view path of the application.
absolute path within module (e.g. "/site/index"): the view name starts with a >single slash. The actual view file will be looked for under the view path of $module.
relative path (e.g. "index"): the actual view file will be looked for under $viewPath.

Based on those choices, it looks like you will specify the absolute path within application, or create a path alias and use that syntax (app? main site view? wherever it's at.)
So, if you wanted to render the {basePath}/views/site/my_partial.php you'd do something like $this->renderPartial('//site/my_partial.php'); in your view.
Comment: I used `$cropk = Yii::$app->runAction("cropk");`, passed it to the view, and it works fine too. Should this be considered unconventional?


Rate of convergence of EM algorithm?

Question: What can be said in general about the rate of convergence of EM algorithm?
For example, if I let the parameters to be $\theta$, starting point to be $\theta^0$, and the optimal solution is $\theta^*$, so that $||\theta^t-\theta^*||<\alpha||\theta^0-\theta^*||$, what can be said about $\alpha$ in general?
Also, how can we prove that it is slower than the other methods (Like Newton-Rapson, Expected Conjugate Gradient)?
Comment: I think [this paper](https://arxiv.org/pdf/1609.00368.pdf) is relevant to your question.
Answer: In the general case you need to verify that your problem setup satisfies certain properties for the EM algorithm to converge to a stationary point that is a local maximum. Further requirements are needed for global maximum. Assuming these criteria are met you can then quantify the rate of the convergence toward the global optimum.
In the general case (and assuming you have global optimum) the most you can usually say is that the EM algorithm is a first order algorithm.
First order algorithms are algorithms such that:
$$|\theta^{k+1} - \theta^*| \leq \gamma |\theta^{k}-\theta^*|.$$
If $\gamma=1$ then convergence is linear and if $1<\gamma<2$ then the algorithm is said to have super-linear convergence and if $\gamma=2$ is quadratic convergence. The convergence rate really depends on the specifics of the problem. Many examples and an a pedagogic introduction to convergence rates of the EM algorithm are given in the book by McLachlin and Krishnan if you want more details.
Xu and Jordan provide an in-depth study for the mixture of Gaussians case.
Comment: It took a year but I am glad to find the answer :D
Comment: @user3086871, yeah I only saw the question a couple days ago. Sometimes discoverability is challenging.

Rate of convergence of EM algorithm?


Can I pick any N in the epsilon N - definition?

Question: If I were asked to pick a $N$ that would satisfy the definition such that for any $n > N$, the distance between $f(n)$ and the limit would be smaller than a specific epsilon....
...can I just pick $N$'s at random that would satisfy this, and then say that for any $n > N$ it would be true as well?
I think no; just because it works for a given $N$, doesn't mean it'll work for any $n > N$... but then somebody told me that it would work, because, and he said it like this, "once it's close to the limit, it stays close, and only gets closer", so if the distance between $f(N)$ and the limit is smaller than a given epsilon, then that would remain true for any $n>N$.
Is this true? Is he making some assumptions that aren't always given? Or is it complete bollocks?
Comment: For functions that approach the limit "nicely" it is often true. In general it is not. So if you have found an $N$ such that $f(N)$ is "close enough" you must always verify that the same is true for all larger $n$.
Comment: Yeah. If it increases monotonically then this is justifiable
Comment: I think you misunderstood that the definition of the limit (which is not that easy to understand when you see it first). Could you give us an specific example?
Comment: If $|f(N)-L|<\epsilon$ for some $N$, it's absolutely not guaranteed in general that it's still true for all $n>N$. If the sequence $f(n)$ is converging, it means this will happen eventually (you will find some $N$ such that all $n>N$ satisfies the inequality), but actually it's the definition of convergence.
Comment: Hi guys. Sorry, I forgot to mention something that might be of importance; f(n) is MONOTONOUSLY INCREASING.... but if this is true, and it is bounded by the limit L, then it HAS to be true that if N works, then it WILL work for all n>N, right? Because for bigger and bigger n's, it approaches its limit (i.e. gets closer), as it's bounded by its limit but always increasing for bigger n's? Am I correct in saying this?
Comment: $N$ is likely to be related to $\epsilon$ in some way, and you want the statement to be true for all positive $\epsilon$, so picking $N$s at random are unlikely to satisfy this.
Comment: @Dain If this question is related to [your comment here](http://math.stackexchange.com/questions/928826/range-of-function-with-limit/928831#comment1917428_928831), then see my answer below that comment. ;-)
Answer: [In the general case]
Suppose you somehow found some $N$ such that for any $n > N$, we have $|f(n)-L| < \epsilon$.
Then any $N' \ge N$ will also "work," that is, for any $n > N'$, we have $|f(n)-L| < \epsilon$.
However, as Jean-Claude pointed out it is not true that $|f(n)-L|<\epsilon$ implies that for all $n' \ge n$ we have $|f(n')-L| < \epsilon$. But, as I am writing this, you have mentioned that $f$ is monotonic, so in that case, this does hold.


Hamiltonian cycle adjacency sum Proof

Question: Let $C$ be a Hamiltonian cycle on a graph with vertices labeled {$1,...,9$}. Prove that there are $3$ vertices adjacent in $C$ whose labels sum to at least $12$.
I understand why this fact is true by drawing a graph and different and calculating the adjacent vertices sum to be at least $12$.
How would I go about proving it? Would I use the definition of a Hamiltonian cycle and how would I show that the vertices have a minimum value of $12$?
Comment: By "$3$ vertices adjacent in $C$" do you mean three vertices $x,y,z$ with both edges $(x,y),(y,z)$ in $C$?
Comment: Yes I believe thats what the question is referring to.
Answer: Consider the $9$ vertex. The minimum sum it can be a part of is $1 + 2 + 9  = 12$.  Hence the sum of the vertices around $9$ (and including it) is always at least $12$.

Hamiltonian cycle adjacency sum Proof


Does sampling error include measurement error?

Question: Gross sampling error (MSE) appears to be a composite of two errors sampling and measurement error. How do we assess measurement error ? can we find out net sampling error ?
Comment: Where are you getting the idea about "gross sampling error"? What is "MSE"?
Answer: Yes, assuming by "gross sampling error" you mean mean-squared error or the $\epsilon$ term in a model like $Y=AX + \epsilon$
The error component of a model includes all sources of variability that are not explicitly included in the model. This includes sampling errors (uncertainty due to measuring only a subset of the population), measurement errors (uncertainty due to imprecisions in each measurement), and other things, like error attributable to a misspecified model (e.g., missing predictors/interactions).
Keep in mind that these are actually types of errors. For example, there may be measurement error associated with each variable in the model, and that error might be a combination of systematic error (essentially, a bias; e.g., someone forgot that the scale reports the weight of the container + its contents) and random error.  Given that, there isn't an automatic, all-purpose way of identifying the various error contributions.
One way to examine measurement errors is through calibration. For example, you could put a weight on the scale and compare the scale's reading to the known mass of the weight. In many cases, the phenomena causing measurement error are reasonably well understood and have a specific structure (e.g., shot noise), which allows them to be incorporated into the model. Some large-scale physics experiments take this to incredible extremes to compare an apparatus's expected performance to the real data. Surveys are sometimes benchmarked by comparing data collected during the survey to larger data sets. For example, you might ask participants for demographic information (e.g., age, gender, income). These values are then compared to known population values (e.g., from a census or tax records), which might tell you how representative your respondents are of the general population.
Sampling error is much harder to measure directly. You might expect sampling error to shrink as the number of samples approaches the size of the population, whereas a systematic measurement error would remain approximately the same, regardless of sample size.
Comment: I'm not sure I understand your question. In reality, measurement error can take many different forms, but it's often **modelled** as a random effect coming from an $N(0, \epsilon)$ distribution. If that assumption is true, then the measurement error won't affect the mean (it's zero-mean), but will increase the variance. Since correlations are scaled by the (product of the) variances, it will bias them downwards. Its affect on regression depends on where it is in the model. Maybe this will help: http://www3.nd.edu/~rwilliam/stats2/l21.pdf
Comment: thanks for your answer. However, the literature suggests that measurement error does not influence variance though constant bias (construct bias) here may have a bearing on the estimate of mean effect-size. These ideas are very explicit to me, Please elaborate for me in the context of meta-analysis of sample correlations
Comment: A re-look suggests that the answer is good and the comment suggests correctly the effect on mean. However, in the case of variance also, being a random effect, it should not affect the variance. In general, the answer is accepted.
Comment: Thanks for the checkmark! However, I disagree that measurement error does not affect the variance.

Suppose the "true" data are distributed normally with some mean and variance: $N(\mu, \sigma^2)$ and contaminated with (additive) normally-distributed measurement error $N(0, \epsilon)$. Then, your observations will be from another normal distribution $N(\mu, \sigma^2 + \epsilon)$. Both $\sigma^2$ and $\epsilon$ have to be positive, so the variance is therefore biased upwards.
Comment: Variance is computed as sum of squared deviations from the mean. If we have a random variable, the deviations part of values indicates "gross sampling error" which includes measurement error. The latter part once again is distributed as a randomly distributed variable. The measurement error thus affects individual variances of values randomly implying that (overall) there is a zero effect on average variance for the dataset. If we have a constant bias (scale error), it will influence mean estimate. Once again, it will not affect the overall (average)variance.


How do I monitor the transfer speed of an interface from a Linux terminal?

Question: I am connecting to a server via SSH, and I'd like to know how fast it's uploading and downloading. Preferably I'd also like to see what is downloading/uploading and the speed for that process. Any suggestions, or hints on what I should be searching for?
Answer: To get a more specific breakdown of traffic, I use tcpdump and pass the dump to wireshark.  Then use the statistics menu.  But for ssh, it will be hard to see what is going on since it is already encrypted.  Do you just want to the speed of your transfers? rsync --progress -av srouce/ dest/ will do that.
Or maybe you want: IP/Process based bandwidth usage stats tool for linux?

If you don't want to install anything, you can cat /proc/net/dev over an interval (Maybe in a loop with sleep), and then just take the difference of the send and receive bytes between the two polling.
For a program, I liked apt-get install bmon.  bmon is a nice little terminal curses program.
Comment: Oops, read to quickly, didn't see you wanted per process :-/
Comment: Woah, bmon is neat! To be honest, per process was just a nice-to-have.
Answer: iftop
Comment: Nice, not sure which I like better, iftop or bmon.
Answer: Or if you don't have root access, the data is stored in /proc/net/dev.  Little hacked up python script I wrote a bunch of years ago to do it w/o pcap/root.
Answer: Just to summarize from Kyle Brandt and Flow's answers. I just compared bmon and iftop (both installable using apt-get on Debian). Both are ncurses applications, and are really quite neat!

bmon gives only a general usage, but has a pretty graph :)
iftop shows detailed usage between each connection (and shows source/dest ips)

Thanks for your answers!
Answer: I didn't like any of these answers. Either it required some 3rd party program or was too much info.
Really you can find all the net info you need in the following path. Replace $int_name with your interface name
/sys/class/net/$int_name/statistics
Examples:

rx bytes per second = $(cat /sys/class/net/$int_name/statistics/rx_bytes)
tx bits per second = $(expr $(cat /sys/class/net/$int_name/statistics/tx_bytes) \*8)
tx pps = $(cat /sys/class/net/$1/statistics/tx_packets)

You can use this script I created below to show all tx, rx, and totals for a given interface. I call it ninfo
#!/bin/bash
output(){
    echo "==== $1 ===="
    echo "rx_bits       $(expr $(cat /sys/class/net/$1/statistics/rx_bytes) \* 8)"
    echo "rx_bytes      $(cat /sys/class/net/$1/statistics/rx_bytes)"
    echo "tx_bits       $(expr $(cat /sys/class/net/$1/statistics/tx_bytes) \* 8)"
    echo "tx_bytes      $(cat /sys/class/net/$1/statistics/tx_bytes)"
    echo "rx_packets    $(cat /sys/class/net/$1/statistics/rx_packets)"
    echo "tx_packets    $(cat /sys/class/net/$1/statistics/tx_packets)"
    echo "total_bits    $(expr $(expr $(cat /sys/class/net/$1/statistics/tx_bytes) \* 8) + $(expr $(cat /sys/class/net/$1/statistics/rx_bytes) \* 8))"
    echo "total_bytes   $(expr $(cat /sys/class/net/$1/statistics/tx_bytes) + $(cat /sys/class/net/$1/statistics/rx_bytes))"
    echo "total_packets $(expr $(cat /sys/class/net/$1/statistics/tx_packets) + $(cat /sys/class/net/$1/statistics/rx_packets))"
}

if [ -z $1 ]; then
    echo "[!] ERROR: please provide an interfaces name"
    exit 1
fi

cints=$(ip -br a | awk '{print$1}' | sed 's/@.*//g')
found=0

for i in $cints; do
    if [ "$i" == "$1" ]; then
        found=1
    else
        :
    fi
done

if [ $found -eq 1 ]; then
    output $1
else
    echo "[!] ERROR: '$1' is not an interface on this system. Must use one of the following:"
    echo "$cints"
    exit 1
fi

To use run ninfo $int_name.
Tthe script will list interfaces on your system if you dont know the names.
Now I said that I dont like 3rd part programs for this but check out mmwatch its a utility that works like watch but converts diff integers for rates:
https://github.com/cloudflare/cloudflare-blog/blob/master/2017-06-29-ssdp/mmwatch
Download it and save in your path. Then you can run mmwatch + my script
mmwatch "./ninfo $int_name"
Now you can view rates.