How to apply coefficient term for factors and interactive terms in a linear equation?

Question: Using R, I have fitted a linear model for a single response variable from a mix of continuous and discrete predictors. This is uber-basic, but I'm having trouble grasping how a coefficient for a discrete factor works.
Concept: Obviously, the coefficient of the continuous variable 'x' is applied in the form y = coefx(varx) + intercept but how does that work for a factor z if the factor is non-numeric? y = coefx(varx) + coefz(factorz???) + intercept
Specific: I have fitted a model in R as lm(log(c) ~ log(d) + h + a + f + h:a) where h and f are discrete, non-numeric factors. The coefficients are:
Coefficients:
              Estimate
(Intercept)  -0.679695
log(d)        1.791294
h1            0.870735
h2           -0.447570
h3            0.542033
a             0.037362
f1           -0.588362
f2            0.816825
f3            0.534440
h1:a         -0.085658
h2:a         -0.034970
h3:a         -0.040637

How do I use these to create the predictive equation:
log(c) =  1.791294(log(d)) + 0.037362(a) + h??? + f???? + h:a???? + -0.679695

Or am I doing it wrong?
I THINK that that concept is if the subject falls in category h1 and f2, the equation becomes:
log(c) =  1.791294(log(d)) + 0.037362(a) +  0.870735  + 0.816825  + h:a???? + -0.679695

But I'm really not clear on how the h:a interactive term gets parsed. Thanks for going easy on me.
Answer: This is not a problem specific to R.  R uses a conventional display of coefficients.
When you read such regression output (in a paper, textbook, or from statistical software), you need to know which variables are "continuous" and which are "categorical":

The "continuous" ones are explicitly numeric and their numeric values were used as-is in the regression fitting.
The "categorical" variables can be of any type, including those that are numeric!  What makes them categorical is that the software treated them as "factors": that is, each distinct value that is found is considered an indicator of something distinct.
Most software will treat non-numerical values (such as strings) as factors.  Most software can be persuaded to treat numerical values as factors, too.  For example, a postal service code (ZIP code in the US) looks like a number but really is just a code for a set of mailboxes; it would make no sense to add, subtract, and multiply ZIP codes by other numbers!  (This flexibility is the source of a common mistake: if you are not careful, or unwitting, your software may treat a variable you consider to be categorical as continuous, or vice-versa.  Be careful!)
Nevertheless, categorical variables have to be represented in some way as numbers in order to apply the fitting algorithms.  There are many ways to encode them.  The codes are created using "dummy variables."  Find out more about dummy variable encoding by searching on this site; the details don't matter here.

In the question we are told that h and f are categorical ("discrete") values.  By default, log(d) and a are continuous.  That's all we need to know.  The model is
$$\eqalign{
y &= \color{red}{-0.679695} & \\
  &+ \color{RoyalBlue}{1.791294}\ \log(d)  \\
  &+ 0.870735 &\text{ if }h=h_1 \\
  & -0.447570 &\text{ if }h=h_2 \\
  &+ \color{green}{0.542033} &\text{ if }h=h_3  \\
  &+ \color{orange}{0.037362}\ a \\
  & -0.588362 &\text{ if }f=f_1  \\
  &+ \color{purple}{0.816825} &\text{ if }f=f_2  \\
  &+ 0.534440 &\text{ if }f=f_3  \\
  & -0.085658\ a &\text{ if }h=h_1  \\
  & -0.034970\ a &\text{ if }h=h_2   \\
  & -\color{brown}{0.040637}\ a &\text{ if }h=h_3   \\
}$$
The rules applied here are:

The "intercept" term, if it appears, is an additive constant (first line).
Continuous variables are multiplied by their coefficients, even in "interactions" like the h1:a, h2:a, and h3:a terms.  (This answers the original question.)
Any categorical variable (or factor) is included only for cases where the value of that factor appears.

For example, suppose that $\log(d)=2$, $h=h_3$, $a=-1$, and $f=f_2$.  The fitted value in this model is
$$\hat{y} = \color{red}{-0.6797} + \color{RoyalBlue}{1.7913}\times (2) + \color{green}{0.5420} + \color{orange}{0.0374}\times (-1) + \color{purple}{0.8168} -\color{brown}{0.0406}\times (-1).$$
Notice how most of the model coefficients simply do not appear in the calculation, because h can take on exactly one of the three values $h_1$, $h_2$, $h_3$ and therefore only one of the three coefficients $(0.870735, -0.447570, 0.542033)$ applies to h and only one of the three coefficients $(-0.085658, -0.034970, -0.040637)$ will multiply a in the h:a interaction; similarly, only one coefficient applies to f in any particular case.
Answer: This is just a comment but it won't fit as such in the limited edit boxes we have at our disposal.
I like seeing a regression equation clearly written in plain text, as @whuber did in his reply. Here is a quick way to this in R, with the Hmisc package. (I'll be using rms too, but that does not really matter.) Basically, it only assumes that a $\LaTeX$ typesetting system is available on your machine.
Let's simulate some data first,
n <- 200
x1 <- runif(n)
x2 <- runif(n)
x3 <- runif(n)
g1 <- gl(2, 100, n, labels=letters[1:2])
g2 <- cut2(runif(n), g=4)
y <- x1 + x2 + rnorm(200)

then fit a regression model,
f <- ols(y ~ x1 + x2 + x3 + g1 + g2 + x1:g1)

which yields the following results:
Linear Regression Model

ols(formula = y ~ x1 + x2 + x3 + g1 + g2 + x1:g1)

                Model Likelihood     Discrimination
                   Ratio Test           Indexes
Obs      200    LR chi2     35.22    R2       0.161
sigma 0.9887    d.f.            8    R2 adj   0.126
d.f.     191    Pr(> chi2) 0.0000    g        0.487

Residuals

    Min      1Q  Median      3Q     Max
-3.1642 -0.7109  0.1015  0.7363  2.7342

                   Coef    S.E.   t     Pr(>|t|)
Intercept           0.0540 0.2932  0.18 0.8541
x1                  1.1414 0.3642  3.13 0.0020
x2                  0.8546 0.2331  3.67 0.0003
x3                 -0.0048 0.2472 -0.02 0.9844
g1=b                0.2099 0.2895  0.73 0.4692
g2=[0.23278,0.553)  0.0609 0.1988  0.31 0.7598
g2=[0.55315,0.777) -0.2615 0.1987 -1.32 0.1896
g2=[0.77742,0.985] -0.2107 0.1986 -1.06 0.2901
x1 * g1=b          -0.2354 0.5020 -0.47 0.6396

Then, to print the corresponding regression equation, just use the generic latex function, like this:
latex(f)

Upon conversion of the dvi to png, you should get something like that

IMO, this has the merit of showing how to compute predicted values depending on actual or chosen values for numerical and categorical predictors. For the latter, factor levels are indicated in bracket near the corresponding coefficient.
Comment: +1 That's a nice capability.  The syntax of terms like $+0.2099013\{b\}$, though, is potentially confusing: there is no evident relationship between this expression and the categorical variable `g1`, nor is it entirely evident that $\{b\}$ really stands for an *indicator* that $g_1=b$ rather than for the numerical value of $b$!  (Here, $b$ really means "b"--the letter--which may be sufficient warning, but when the categories are coded by numbers, such as $0$ and $1$, watch out...)
Comment: @whuber The above image has been cropped but there's sort of a footnote recalling that "{c} = 1 if subject is in group c, 0 otherwise" (the choice of `c` might be confusing in this particular case, because I choose two letters to represent g1 levels, but usually it's quite intuitive--and that's pure tex so we can still edit the source file afterwards). [Attached](http://i.imgur.com/cheOL.png) is another summary where I altered `g1` so that it is now a four-level factor. Yet, with 0/1 labels that might be [more confusing](http://i.imgur.com/uWOlP.png).
Answer: You can check your "contrasts" are the default  by options() and looking for:
$contrasts
        unordered           ordered
"contr.treatment"      "contr.poly"

If your unordered contrasts are set as contr.treatment (as they should be unless you've changed them), then the first level of each of your factors will be set as a baseline.  You will only be given estimates for the coefficients in front of the dummy variables created for other levels of the factor.  In effect, those coefficients will be "how different on average is the response variable at this level of the factor, compared to the baseline level of the factor, having controlled for everything else in the model".
I am guessing from your output there is a an h0 and f0 which are the baseline levels for h and f (unless you have a non-default option for contrasts, in which case there are several possibilities; try ?contr.treatment for some help).
It's similar with the interaction.  If my previous paragraph is correct, the estimate given for a will really be the slope for a when h=h0.  The estimates given in the summary that apply to the interactions are how much that slope changes for different levels of h.
So in your example where h=h1 and f=f2, try:
log(c) =  1.791294(log(d)) + (0.037362 - 0.085658) (a) +  0.870735  + 0.816825  -0.679695

Oh, and you can use predict() to do a lot of useful things too... if you actually want to predict something (rather than write out the equation for a report).  Try ?predict.lm to see what predict() does to an object created by lm.
Comment: +1 (actually, I upvoted this a month ago & just happen to be back rereading it now) at any rate, it occurs to me that you recommend checking the contrast type by `options()`. You will have to scroll through a lot of junk to find what you need.  You might try `options()$contrasts`, which will only output what you want.
Comment: good suggestion, my brain must have been asleep.
Comment: You know, I often answer CV questions right before I go to bed...
Answer: Rather than thinking of some of the coefficients being included and some not, resulting in a number of different equations depending on the values of the variables, another way to think about it is that all coefficients are included in a single equation. But, they are multiplied by either 1 or 0 depending on whether that condition is true.
That is, each possible value of each factor variable (i.e. discrete variable) appears in the final equation as either a 0 or a 1 depending on whether the variable has that value, and its coefficient is applied to it.
So, as already mentioned, each factor variable is split into dummy variables, one for each level in the factor (e.g. h becomes h1, h2, h3, ... hn), representing all possible conditions of the variable. Then, each dummy variable gets its own unique coefficient.
So lm(log(c) ~ log(d) + h + a + f + h:a) becomes
Coefficients:
              Estimate
(Intercept)  -0.679695
log(d)        1.791294
h1            0.870735
h2           -0.447570
h3            0.542033
a             0.037362
f1           -0.588362
f2            0.816825
f3            0.534440
h1:a         -0.085658
h2:a         -0.034970
h3:a         -0.040637

which becomes
log(c) == -0.679695 + 1.791294*log(d) + 0.870735*h1 - 0.447570*h2 + 0.542033*h3 + 0.037362*a - 0.588362*f1 + 0.816825*f2 + 0.534440*f3 - 0.085658*h1*a - 0.034970*h2*a - 0.040637*h3*a

Now plug in ones and zeroes:
If h equals h1, then h1 equals 1 (or TRUE), else h1 equals 0 ( or FALSE).
If h equals h2, then h2 equals 1, else h2 equals 0.
... etc.


Como calcular os 4 primeiros bytes de um Address ex: 00F28758

Question: Queria poder entender como calcular os 4 Bytes de um address para depois usar um ReadProcessMemory.
Queria apenas entender como calcular isso por que sempre que tento dá errado.
Vamos dizer que tenho um Address 181BA700 e preciso pegar os 4 bytes para calcular com um offset. Mas não entendo como fazer isso.
Answer: O que você quer é algo assim?
using System;

public class Test
{
    public static void Main()
    {
        int Address = 0x181BA700;
        int byte1 = Address >> 24;
        int byte2 = (Address >> 16) & 0xFF;
        int byte3 = (Address >> 8) & 0xFF;
        int byte4 = Address & 0xFF;
        Console.WriteLine("A: " + byte1.ToString("X")
            + ", B: " + byte2.ToString("X")
            + ", C: " + byte3.ToString("X")
            + ", D: " + byte4.ToString("X"));
    }
}

Eis a saída:
A: 18, B: 1B, C: A7, D: 0

Veja funcionando no ideone.
Comment: Vlw.. Era isso mesmo que faltava..


How do I migrate block content from a dev to a production site?

Question: I've finally started looking at Drupal 8 seriously and I'm particularly interested in the configuration management. I've come across something that could be a little bit problematic and that's regarding custom block content.
I can see that the configuration management system is able to export block configuration - region, theme, weight, visibility etc. however the actual block content does not come across in the config export, which is reasonable and understandable.
On importing that block config to a production site, what appears to happen is the block config is created and a holding message is put in place, reporting the block is broken or missing. Obviously the block content doesn't exist on the production server.
How can custom blocks be migrated from a dev/staging server to a production server? I realise that blocks in Drupal 8 are fieldable entities like nodes and so will need to be migrated in the same way and I understand there is a Migrate API in Drupal 8 but this appears to be built for migrating content from Drupal 6 and 7 sites to Drupal 8 as opposed to Drupal 8 to Drupal 8 sites.
This issue is specifically regarding custom blocks as blocks generated by other modules such as Views will obviously migrate across as configuration.
Comment: Similar: [Block error on CMI](http://drupal.stackexchange.com/q/146037/1908)
Comment: There are several content staging solutions in the works including deploy module and entitypilot.com (disclaimer, that's my product)
Answer: Another answer I haven't seen mentioned here is to use the Simple Block module, which is pretty much identical to core's 'Custom Block' setup, but instead of having a weird hybrid of content + config, you have all the Block settings and content stored in configuration, which can be cleanly exported and imported.
See, for further discussion in Drupal 8 core: Custom blocks cannot be properly exported and imported.
Answer: I have just publish a contributed module that solves this. Essentially, the module provides a type of block based on configuration (the fixed block) that wraps a custom block (the content block). If the content block does not exist, it is created with a default content or empty if no default content was set. Everything is done through UI, no special files or custom module are needed.
I named it Fixed block content and it is published at:
https://www.drupal.org/project/fixed_block_content
Answer: Please have hands on the Structure Sync module.

Structure sync provides Drush commands and admin interface screens for
  synchronizing content that could also be considered configuration.
  Including menu items, custom blocks and taxonomy terms.

Steps:

Go to structure sync.
Go to Blocks tab.
Export.
Your configurations and content will be exported in configuration folder.
Take the configurations to other sites & Import.
Go to structure sync & click on import.
Done
Answer: Another approach for keeping content that is added as part of development also pushed to live is to use the Default Content module to export the content. It is built for the content to be exported to an installation profile's 'content' folder, and then the module, if enabled, automatically brings the content in when the site is installed, but it's also possible to import the content one item at a time, such as in an update hook, with the below code in your example.install or example.profile:
<?php
/**
* Import a piece of content exported by default content module.
*/
function example_import_default_content($path_to_content_json) {
  list($entity_type_id, $filename) = explode('/', $path_to_content_json);
  $p = drupal_get_path('profile', 'guts');
  $encoded_content = file_get_contents($p . '/content/' . $path_to_content_json);
  $serializer = \Drupal::service('serializer');
  $content = $serializer->decode($encoded_content, 'hal_json');
  global $base_url;
  $url = $base_url . base_path();
  $content['_links']['type']['href'] = str_replace('http://drupal.org/', $url, $content['_links']['type']['href']);
  $contents = $serializer->encode($content, 'hal_json');
  $class = 'Drupal\\' . $entity_type_id . '\Entity\\' . str_replace(' ', '', ucwords(str_replace('_', ' ', $entity_type_id)));
  $entity = $serializer->deserialize($contents, $class, 'hal_json', array('request_method' => 'POST'));
  $entity->enforceIsNew(TRUE);
  $entity->save();
}

Export a custom block with an ID of 8:
drush dcer block_content 8

(If you don't set your profile path in Drush settings you'll have to specify it above.)
And use the resultant export in your example.install file like this:
<?php
/**
* Add the footer block content.
*
* Implements hook_update_N().
*/
function example_update_8001() {
  example_import_default_content('block_content/136efd63-021e-42ea-8202-8b97305cc07f.json');
}

http://data.agaric.com/easily-add-content-update-hooks-use-default-content-module-exports-create-content-needs-be-sync-conf
Answer: Having the same issue and not really a solution, only additions:
In collaborative development we're using a staging server which pulls from the repository and resets all configuration. This means that block config is being reset automaticalls, you simply cannot place blocks you consider as "content" directly on that server.
It's easy to use drush config-export sync while knowing exactly what you've done and being sure that any configurations changes are meant for deploying. But Drupal decides for us that blocks are configuration (while obviously block content is handled as content). So this seems to be broken by design.
For the time given I reckon the most practical solution would be to add the block-related yml files to .gitignore.
Comment: Config Ignore is likely better than .gitignore:
https://www.drupal.org/project/config_ignore
Answer: I'm not sure I see any strong advantages of synching block configurations amongst multiple environments as blocks are so intertwined with content.
The reason for this is that there's a new block being created from the yml files that has no title/body (content) and is therefore giving the 'broken/missing' message.
You could try to make the UUID (if you wanted to make the block in both places - make sure the machine name matches...) in your development block_content table match what uuid you have in production (the other relationships seem to use the entity id).  Then, when you do a config sync you could see the 'View differences' in the yml files and possibly see what else you need to change on dev to get it to match production uuids, etc.  I got this to work, but still figured it's easiest to ignore all your block configurations in code unless you go through this process or create some kind of database block sync for yourself using block_content, block_content__body, and block_content_field_data.
It's not very elegant but could allow you to keep your block configs in code.  Otherwise, if you keep deploying blocks with config, they will always be 'broken or missing'.
Another blog post suggests creating a custom block in live environment but not placing it. After syncing the database to dev, custom block could be configured, config exported and since it already exists in live import of placement is possible.
Answer: I am not sure too, however, if you didn't find any solution you may look this module https://www.drupal.org/project/deploy. Frankly speaking, I don't remember can deploy push blocks from DEV to PROD or not.
Answer: I think the best way to handle this would be to:

set up a drush alias for the various versions of your site
use drush commands like:

drush rsync
drush sql-sync

This is what I usually see people using and I personally use. But it syncs the entire database as compared to only the block content.
Comment: That can work if there is no issue with a database overwrite. Now, if the only desire is to move a new custom block into an existing database, this method would be difficult to implement.
Comment: This answer has its place, in theory. But in practice, this isn't a good solution, especially if the project is using configuration split, or has any different configuration between environments (which is very likely).


ERROR The requested URL could not be retrieved after deploying in external tomcat server

Question: I am using Spring boot to create a REST API. The API is perfectly working when I am running the project. It works for all the endpoints that I have created. However, When I deploy the application in an external tomcat I am shown an error just like the following image

But The same endpoint was working before getting deployed into tomcat.

I am unable to figure out where the problem lies. I am using
Java 8, Oracle 12C, tomcat 8.5 .
Same Problem when I used Tomcat 10
Answer: I found the issue. I accidentally forgot to mention the war file/application name which must be added.


XAML Change Frame Background Based on Binding

Question: I have an ObservableCollection which is based on the following class:
public class Data
{
    public string Text { get; set; }
    public DateTime Date { get; set; }
    public bool IsActive { get; set; }
}

This observablecollection is used and binded as ItemSource for ListView. The following is DataTemplate for displaying data -
<DataTemplate>
    <ViewCell>
        <Frame OutlineColor="White" HasShadow="False">

        </Frame>
    </ViewCell>
</DataTemplate>

Since the ObservableCollection is a collection of Data class which has a boolean property, I want to use it in order to change the frame's background color:

If property IsActive is true - BackgroundColor is Red
If property IsActive is false - BackgroundColor is Blue

I've looked into implementation of Triggers, however I can't seem to get them working correctly, and I'm not sure what I'm missing.
According to Xamarin Documentation I should be able to do :
<Frame>
    <Frame.Trigger>

    </Frame.Trigger>
</Frame>

However that doesn't seem to be possible. Neither is this -
<Frame>
<Frame.Style>

    </Style>
</Frame.Style>
</Frame>

The code above gives the following error message:
Xamarin.Forms.Xaml.XamlParseException: Position 28:26. The Property TargetType is required to create a Xamarin.Forms.DataTrigger object.

Comment: Are you sure the line numbers match where your frame style is defined?  I'm not sure about xamarin, but possibly try using the {x:Type } extension instead of relying on string -> type conversion in the TargetType property?
Comment: Are you setting your `BackgroundColor` from directly in the `` tag? If so, that property will override any triggered or styled values. Another thing to try is maybe use `TargetType={x:Type Frame}`, occasionally I see the text string fail to resolve into the actual type, so this prevents that.
Answer: Not sure about your trigger problem but I think you should be able to accomplish the color changing by first implementing INotifyPropertyChanged on your Data class like so:
public class Data : INotifyPropertyChanged
{
    public string Text { get; set; }
    public DateTime Date { get; set; }

    private bool _isActive;
    public bool IsActive
    {
        get { return _isActive; }
        set
        {
            if (value == _isActive)
            {
                return;
            }

            _isActive = value;

            NotifyPropertyChanged("IsActive");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Then, in your xaml, you should be able to do something like this:
<DataTemplate>
    <ViewCell>
        <Frame Background="{Binding IsActive, Converter={StaticResource IsActiveToColorConverter}}" OutlineColor="White" HasShadow="False">

        </Frame>
    </ViewCell>
</DataTemplate>

Where IsActiveToColorConverter looks something like:
public class IsActiveToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var isActive = (bool) value;

        return isActive ? "Red" : "Blue";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Comment: OP Did not mention that boolean property will be changed real time. so NotifyProperty is not required. But Converter is a good option for this scenario. So +1 for that.


Using CSS to change single page background

Question: Is it possible to add a line to my CSS like this:
.custom_bg {
    background-image:url(http://domain.tld/img.ext);
    background-repeat:no-repeat;
    background-color:#000;
}

And then on that certain page, call this some how so that the page 'knows' to listen to this line... is that possible?
Any help is appreciated. I'm fairly new to CSS.
Thanks
Comment: Live website link to the website or jsFiddle link will be highly appreciated.
Comment: You could add this manualy on page like ``, and leave your common `.css` file clean
Comment: When should the line be loaded?
Answer: What you can do is make a class with a background-image(I guess you are already having that), and than use that class on the body element of that page, so for example
.custom_bg {
   background: url('URL_HERE');
}

And say you want to change the background of the contact page, you can make your HTML like this

Note: Call this class on the element you want to over ride the
  background image, here I am assuming that you want to over ride the
  background image for the body tag.

This will over ride the default styling, I guess you must be using a general element selector in your stylesheet like
body {
   background: url('URL_HERE');
}

So when you define the class, CSS will pick the image from the class thus by over riding the default background image
Comment: That's great thanks. - Just to add to this, is it possible to call it without the body tag? Thanks
Comment: You can assign a class to any element, be it a div, span, h3 or otherwise. Just keep in mind that to work well with an anchor you're going to likely want to apply display:block; to the style and a set width and height.
Answer: You could add a class to the body of that single page: . The rest of the pages will be unaffected by that style.
Answer: as well as I understand all you should do is add this class to the  tag or any other container like this:

...

Answer: Hi If you want to specify  background for a particular page only in your application then do

...

while the css will be
.custom_bg
{
  background: url('URL_HERE');
}

and if you want the background for all pages then try the following CSS
body {
   background: url('URL_HERE');
}


Logarithms and regression

Question: I have a regression where I am trying to predict Y, using several variables. My variables are A, B, C.

I'm using R for my analysis. My equation is as follows:
mod <-lm(  log(Y/B) ~ log(A/B) + log(A/C), data = dataframe)

and a summary of the model is as follows:
Coefficients:
                      Estimate Std. Error t value Pr(>|t|)
(Intercept)          -1.326698   0.080683  -16.44   <2e-16 ***
log(A/B)              0.756779   0.007507  100.81   <2e-16 ***
log(A/C)             -0.292373   0.012762  -22.91   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.6925 on 17063 degrees of freedom
Multiple R-squared: 0.4043, Adjusted R-squared: 0.4042
F-statistic:  5790 on 2 and 17063 DF,  p-value: < 2.2e-16

I understand that log(j/k) = log(j) - log(k); so log(Y/B) can be written similarly.
Is there a way that I can include the B variable in the dependent part of my regression equation?
Also, it is acceptable to use a variable for normalizing, on both dependent and independent variables? I have seen other examples where the same variable is used in this way, but I was not certain if this makes sense.
Comment: As it is, $−\log(B)$ appears on both sides of the regression equation and so is redundant - whatever coefficient appeared in front of $\log(B)$ would actually be 1 unit too small. Also $\log(A)$ appears twice on the right hand side. Is this what you intended?
Comment: If I write the LHS as `log(Y) - log(B)`, the R-sq remains the same. If I write the RHS as (log(A) - log(B)) + log(A/C) the R-sq changes (though I don't understand why). I intended to include `log(A)` twice.
Answer: By virtue of properties of logarithms, the original model,
$$\log(Y/B) = \beta_0 + \beta_1\log(A/B) + \beta_2\log(A/C) + \varepsilon$$
($\varepsilon$ is a zero - mean random variable) can be written
$$\log(Y) - \log(B) = \beta_0 + \beta_1 (\log(A)-\log(B)) + \beta_2(\log(A)-\log(C)) + \varepsilon,$$
which algebraically is identical to
$$\log(Y) = \beta_0 + (\beta_1+\beta_2)\log(A) + (1-\beta_1)\log(B) - \beta_2\log(C) + \varepsilon.$$
Note particularly that nothing funny has happened to the random term $\varepsilon$, so that fitting one (via least squares or maximum likelihood) will give the same results as fitting the other.
Conversely, if you start with the general model
$$\log(Y) = \gamma_0 + \gamma_1\log(A) + \gamma_2\log(B) + \gamma_3\log(C) + \delta,$$
comparing coefficients shows that $\gamma_0 = \beta_0$, $\delta = \varepsilon$, and
$$\gamma_1 + \gamma_2 + \gamma_3 = (\beta_1+\beta_2) + (1-\beta_1) + (-\beta_2) = 1.$$
These are the only relations among the coefficients, as evidenced by the fact you can recover the betas from the gammas in many ways such as
$$\beta_1 = 1 - \gamma_2, \quad \beta_2 = -\gamma_3 \quad \text{or}$$
$$\beta_1 = \gamma_1 + \gamma_3, \quad \beta_2 = \gamma_1 + \gamma_2 -1,$$
for example.
Thus the original model is the general model with a single linear constraint.  Either method will result in equivalent fits and the same parameter estimates.  Therefore,

Yes, there is a way to include $B$ as an independent variable, as shown; and
The original model makes sense.
Comment: I am considering the $R^2$ in both models, and in the first case the $R^2$ is much higher (0.4 compared to 0.07). I understand your clear explanation, but I'm struggling to understand why the $R^2$ is so different, if the model structures are similar. Is it a sign that my variables are not heteroskedastic?
Comment: While you are correct, Macro's example with independent variables is important to consider. While the constrained model is predictive, it is difficult to tell whether it is only due to the trivial/artificial effect of having $-log(B)$ on both sides, or there is a deeper relationship.
Comment: @Aniko I don't understand your remark, because both Celenius' model and the linearly constrained model provide exactly the same inferences; they are related merely by a (linear) change of parameterization.  Perhaps you could expand on your point?
Comment: @celenius In this context (where one model is nested in the other with a common set of data), $R^2$ is directly (although negatively) related to the mean squared residual.  The algebra shows the residuals will be the same, whence $R^2$ must be the same in either formulation (that is, your original one or the linearly constrained one).  Am I misunderstanding what you mean by "both models" and "the first case"?
Comment: @whuber, the $R^2$ is different for each model. When I use the `original model` it is $0.4042$; when I use the `general model` it is 0.07. (to clarify, by first case I meant `original model`)
Comment: @celenius There's an error there: $R^2$ *cannot* decrease for the general model, because it's more flexible than the original one.  I think you haven't fit the general model, but have fit something else.
Comment: Could it be due to a linear relationship not existing between variables?
Comment: @celenius No, I think it's likely a mistake using the software :-(.  Have you plotted the usual diagnostic graphs (such as a scatterplot of residuals vs. predicted values) for both fits?
Comment: Hmm, it's definitely possible I'm making a mistake using the software, but it's quite straightforward to re-organize so I think I'm doing everything correctly. I have been examining the residuals - but as there are so many data points it's a bit hard to spot anything incorrect.
Comment: @celenius Make a scatterplot of residuals for one model versus residuals for another.  If the models are the same, you'll get the usual diagonal lineup of points.  Otherwise the plot will show you where the fit is improving and where it's getting worse.
Comment: @whuber But celenius is not fitting the general model with a constraint: he is talking about the four-parameter version. These will not provide the same fit. Also, if in the correct 4-parameter model $\gamma_2=0$, then from your formulas $\beta_1=1$, so any test of it being equal to 0 should be rejected. The lack of relationship in the "usual sense" leads to a significant effect in the smaller model.
Comment: @Aniko I beg to differ: please reread the original question.  That's a three parameter model, with one for the constant, one for $\log(A/B)$, and the third for $\log(A/C)$, as listed under "Coefficients" in the output.  In this model, $\gamma_1+\gamma_2+\gamma_3=1$ is the constraint, not $\gamma_2=0$.
Comment: @whuber Sorry for being confusing. Yes, celenius is fitting the smaller model in the post. But I suppose he wants to conclude something from its results, for example that $Y$ is predicted by $A, B, C$ somehow (per his first sentence). The point I am trying to make is that $\log(Y/B)$ is _always_ predicted by $\log(A/B)$, even if all the variables are independent.
Comment: @Aniko Perhaps I'm the one who's confused.  It seems to me that when $\log(Y)$ is predicted as $\widehat{\log(Y)}$ in terms of independent variables $A$, $B$, and $C$ (either the "full" or constrained model), then one immediately obtains the prediction for $\log(Y/B)$ as $\widehat{\log(Y)} - \log(B)$.  I just don't see what point you're making.  We can continue in chat if you would like to clear up my puzzlement (but please expect delays--I can't do it in real time today).
Answer: Just doing
$$ \log(Y) \sim \log(A) + \log(B) + \log(C) $$
would be equivalent to the model you're fitting, under the constraint that the coefficients sum to 1 (thanks whuber for making this clear). The coefficients would be changed to compensate for the redundancies in your current formulation, but the fit would be the same.
Edit: If you ignore the constraint and fit the model above then note that the $R^2$ value will change (and typically be much lower) when compared to your original formulation. This is because $B$ appears on both sides of the regression function, so there may appear to be an impressive $R^2$ regardless of whether $Y$ relates to $A,B,C$. See
A=runif(100)
B=runif(100)
C=runif(100)
Y=runif(100)

g1 = lm( log(Y/B) ~ log(A/B) + log(A/C))
g2 = lm( log(Y) ~ log(A)+log(B)+log(C))

as an example. Clearly none of the variables are related to each other, but model g1 shows a pretty impressive $R^2$ (around $45\%$ in my simulation).
Comment: I think this is part of my confusion - when I used the model you suggested, the fit is not the same; the `R-sq` value is now 0.07. I'm going to update my question showing a plot of all of the variables.
Comment: What you have now is $\log(Y/B) \sim \log(A/B) + \log(A/C)$. This is the same as $\log(Y)-\log(B) \sim \log(A)-\log(B)+\log(A)-\log(C)$. So, $\log(A),\log(B),\log(C)$ are all still in the model. The coefficient of $\log(B)$ should just be shifted by 1, that for $\log(A)$ should be doubled, and that for $\log(C)$ should be negative what it was before.
Comment: The $R^2$ will change. This is because $B$ appears on both the left and right side of the regression equation originally. I was only saying the relationship between $Y$ and the rest of the variables will not change. This is important enough to add as an edit to the main post.
Comment: I think I understand - so is it incorrect to use $B$ on both sides of the equation?
Comment: It's incorrect in the sense that it may lead one to believe something is going on when there truly isn't if $R^2$ is used as a guide.
Comment: Thanks @Macro, I had fallen into this trap of believing something was going on.
Comment: Your claim that $\log(Y) \sim \log(A) + \log(B) + \log(C)$ is equivalent to $\log(Y/B) \sim \log(A/B) + \log(A/C)$ is obviously false: the former requires three parameters and the latter only two (in addition to the implied constant).
Comment: @whuber - I believe the **effective number of parameters** are the same due to redundancies inherent to replicating the same variable multiple times on one or both sides of the regression function. I will post an edit later displaying (or not) the equivalent fit between the two models.
Comment: $\log(Y) - \log(B)$ = $\beta_0 + \beta_1 (\log(A)-\log(B)) + \beta_2(\log(A)-\log(C)) + \varepsilon$ implies $\log(Y) = \beta_0 + (\beta_1+\beta_2)\log(A) + (1-\beta_1)\log(B) - \beta_2\log(C) + \varepsilon$, forcing a nontrivial linear relation among the coefficients of $\log(A)$, $\log(B)$, and $\log(C)$ (they must sum to unity).  Naturally, $R^2$ will increase when you relax that relation.  @celenius: this answers your first question.
Comment: @whuber, as you point out in your answer, "Note particularly that nothing funny has happened to the random term ε, so that fitting one (via least squares or maximum likelihood) will give the same results as fitting the other." - that's all I meant in my post when I said "The coefficients would be changed to compensate for the redundancies in your current formulation, but the fit would be the same." - so I can see that we don't actually disagree.
Comment: But if we agree, you must conclude the opposite of what you have written!  $\log(Y) \sim \log(A) + \log(B) + \log(C)$ is *not* equivalent to @Celenius' model.  In fact, your more general model is nested within Celenius' model; it effectively adds one more parameter; it will not decrease, and usually increase, $R^2$; and you cannot estimate Celenius' parameters from its parameter estimates.
Comment: But you just said that the two provide the same fit. Is that not equivalence in some sense? I understand that one is over-identified version of the other and that they are not equivalent in that way, but they are equivalent in terms of the fit which is all I meant (as it was all that seemed relevant).
Comment: The two that provide the same fit are (1) @Celenius' original model and (2) the general model *with the linear constraint.*  If you don't apply the constraint--which is not mentioned in your reply--you will get a different (better) fit and a higher $R^2$.

Logarithms and regression


Faulty memory or controller?

Question: I have a issue on a Dell PowerEdge R420 on CentOS 6, with 6 * 8GB RAM.
I started seeing :
<code>Nov  9 16:43:45 hostname kernel: [20343924.149151] sbridge: HANDLING MCE MEMORY ERROR
Nov  9 16:43:45 hostname kernel: [20343924.149156] CPU 0: Machine Check Exception: 0 Bank 9: cc00008c000800c1
Nov  9 16:43:45 hostname kernel: [20343924.149160] TSC 0 ADDR 421c11000 MISC 90024f4dae6988c PROCESSOR 0:206d7 TIME 1478727825 SOCKET 0 APIC 0
[...]

Nov  9 16:43:46 hostname kernel: [20343925.090225] EDAC sbridge: Lost 6 memory errors
Nov  9 16:43:46 hostname kernel: [20343925.090369] EDAC MC0: CE row 2, channel 0, label "CPU_SrcID#0_Channel#3_DIMM#0": 2 Unknown error(s): memory scrubbing on FATAL area OVERFLOW: cpu=0 Err=0008:00c1 (ch=1), addr = 0x421c0c000 => socket=0, Channel=3(mask=8), rank=0
Nov  9 16:43:46 hostname kernel: [20343925.090373]
[...] It repeats multiple time for row 0 and 1 as well.
</code>
This makes me think memory DIMM are not faulty, but rather the controller. The chance that all 3 DIMMs on the same controller are faulty but none on the other one is unlikely.
<code>$ edac-util -v
mc0: 0 Uncorrected Errors with no DIMM info
mc0: 0 Corrected Errors with no DIMM info
mc0: csrow0: 0 Uncorrected Errors
mc0: csrow0: CPU_SrcID#0_Channel#1_DIMM#0: 754182 Corrected Errors
mc0: csrow1: 0 Uncorrected Errors
mc0: csrow1: CPU_SrcID#0_Channel#2_DIMM#0: 7181 Corrected Errors
mc0: csrow2: 0 Uncorrected Errors
mc0: csrow2: CPU_SrcID#0_Channel#3_DIMM#0: 16891 Corrected Errors
mc1: 0 Uncorrected Errors with no DIMM info
mc1: 0 Corrected Errors with no DIMM info
mc1: csrow0: 0 Uncorrected Errors
mc1: csrow0: CPU_SrcID#1_Channel#1_DIMM#0: 0 Corrected Errors
mc1: csrow1: 0 Uncorrected Errors
mc1: csrow1: CPU_SrcID#1_Channel#2_DIMM#0: 0 Corrected Errors
mc1: csrow2: 0 Uncorrected Errors
mc1: csrow2: CPU_SrcID#1_Channel#3_DIMM#0: 0 Corrected Errors
</code>
Can anybody confirm the issue ? What can I do ?
If this is really the DIMM that are dying, how can I know which it is ?
<code>*-memory
      description: System Memory
      physical id: 1000
      slot: System board or motherboard
      size: 48GiB
    *-bank:0
         description: DIMM DDR3 Synchronous 1333 MHz (0.8 ns)
         product: 36KSF1G72PZ-1G4M1
         vendor: 002C04B3002C
         physical id: 0
         serial: 3067A18C
         slot: DIMM_A1
         size: 8GiB
         width: 64 bits
         clock: 1333MHz (0.8ns)
    *-bank:1
         description: DIMM DDR3 Synchronous 1333 MHz (0.8 ns)
         product: 36KSF1G72PZ-1G4M1
         vendor: 002C04B3002C
         physical id: 1
         serial: 30679E65
         slot: DIMM_A2
         size: 8GiB
         width: 64 bits
         clock: 1333MHz (0.8ns)
    *-bank:2
         description: DIMM DDR3 Synchronous 1333 MHz (0.8 ns)
         product: 36KSF1G72PZ-1G4M1
         vendor: 002C04B3002C
         physical id: 2
         serial: 30679E66
         slot: DIMM_A3
         size: 8GiB
         width: 64 bits
         clock: 1333MHz (0.8ns)

   *-bank:6
         description: DIMM DDR3 Synchronous 1333 MHz (0.8 ns)
         product: 36KSF1G72PZ-1G4M1
         vendor: 002C04B3002C
         physical id: 6
         serial: 30679E63
         slot: DIMM_B1
         size: 8GiB
         width: 64 bits
         clock: 1333MHz (0.8ns)
    *-bank:7
         description: DIMM DDR3 Synchronous 1333 MHz (0.8 ns)
         product: 36KSF1G72PZ-1G4M1
         vendor: 002C04B3002C
         physical id: 7
         serial: 30679CF1
         slot: DIMM_B2
         size: 8GiB
         width: 64 bits
         clock: 1333MHz (0.8ns)
    *-bank:8
         description: DIMM DDR3 Synchronous 1333 MHz (0.8 ns)
         product: 36KSF1G72PZ-1G4M1
         vendor: 002C04B3002C
         physical id: 8
         serial: 30679CEF
         slot: DIMM_B3
         size: 8GiB
         width: 64 bits
         clock: 1333MHz (0.8ns)
</code>
Comment: Switch all of the DIMMs to opposite slots, and then see where the errors occur.
Comment: I would like to be able to pin-point the DIMM on the motherboard without having to shutdown the server since I have lots of clients relying on it. I'm wondering if mc0 are the first 1 DIMM, and mc1 are the other 3.
Comment: You will have to shut down the server anyway, to replace the faulty component! Migrate everyone to the other server.
Comment: But I would need 2 maintenance window, 1 to switch all DIMMs, leave the server running to find the issue, shut-it down again to replace the DIMMs.
Comment: Or the motherboard. Either way, that's why you have two servers.
Answer: You can blacklist edac then the idrac/bmc will record the slot at the hardware level. The edac module blocks the hardware from recording the issue.
https://www.dell.com/support/article/en-us/sln283389/edac-errors-in-messages-log-in-redhat-enterprise-linux-rhel-and-poweredge?lang=en
Comment: Link only answers are not very useful due to link-rot (I've had several answers of mine die that way). Please include enough details in this answer for it to be viable when the link rots.

Faulty memory or controller?


Problemas pra exibir meu site no chrome

Question: Ola,
estou tendo problemas pra exibir meu site no Google Chrome. Eu edito os códigos principalmente no css, no mozila exibe na hora, mas no chrome ou demora muito (depois de atualizar umas 20 vezes) que funciona, ou nem atualiza de jeito nenhum.
O problema é só com o css.
Eu estou fazendo um tema para o wordpress, então eu uso o style.css
Eu chamo o style assim:
<link href="<?php bloginfo('template_url'); ?>/vendor/bootstrap/css/bootstrap.css" rel="stylesheet">

Eu estou usando mais 3 páginas css:
<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo('stylesheet_url'); ?>" >
<link href="<?php bloginfo('template_url'); ?>/css/grayscale.css" rel="stylesheet">
<link href="<?php bloginfo('template_url'); ?>/css/agency.css" rel="stylesheet">

Foi necessário usa-las pra obter certos resultados que eu queria. Não sei se isso ta atrapalhando. Mas tive o mesmo problema num outro tema que eu estava desenvolvendo e só usei o style e o bootstrap.
Tudo isso pra fazer um site responsivo também.
Comment: Provavelmente seja porque o Chrome está armazenando cache de sua aplicação sem verificar alterações no arquivo.  Você pode tentar limpar o cache do navegador e atualizar a página ou utilizar as tags meta do HTML para desabilitar o cache permanentemente (aconselhável apenas enquanto desenvolvimento, para produção é melhor manter o cache).
Comment: Para limpar o cache do navegador: https://support.google.com/accounts/answer/32050?hl=pt-BR. Sobre as tags meta, pode ler mais sobre aqui: http://stackoverflow.com/questions/1341089/using-meta-tags-to-turn-off-caching-in-all-browsers
Comment: Como eu faço isso?? Algumas coisas ainda estou aprendendo hehe!
Comment: Deu certo Muito obrigado. Usei a tag. Você disse então que quando eu for colocar o site no ar eu preciso tirar as tags??
Comment: Exato, pois o cache beneficia no tempo de carregamento do seu site, deixando-o mais rápido.
Comment: como responder a pergunta nos comentários não é uma boa prática (o fiz apenas porque não tinha certeza se era esse mesmo o problema), transcrevi a solução como resposta. Se possível, marque-a como útil e sinalize o post como respondido.
Comment: Ok, muito obrigado.
Comment: Possível duplicata de [Como forçar carregamento de arquivos JS e CSS a cada nova versão publicada?](http://pt.stackoverflow.com/questions/84211/como-for%c3%a7ar-carregamento-de-arquivos-js-e-css-a-cada-nova-vers%c3%a3o-publicada)
Answer: Apenas transcrevendo a solução que foi apresentada nos comentários.
O fato de não atualizar as alterações no navegador Chrome é o cache. Se não me engano, localmente, pelo menos, o Firefox mantém também versões dos arquivos em cache, mas os atualiza caso haja alguma alteração. Diferente do Chrome, que demora a atualizar os arquivos em cache naturalmente. Ou seja, uma vez que você carregou o arquivo CSS no Chrome, novas alterações não funcionarão enquanto o cache não atualizar.
São duas possíveis soluções:

Apagar o cache armazenado no navegador manualmente, seguindo os passos apresentados no site oficial, Limpeza de cache e cookies; ou
Utilizar as meta tags do HTML para controlar o cache da página, como pode ser visto aqui.

Resumidamente, basta adicionar a tag abaixo para que os navegadores não façam mais cache da página.
<meta http-equiv="Cache-Control" content="no-store" />

Nota: É aconselhável manter esta configuração apenas em ambiente de desenvolvimento. Uma vez que seu site está concluído e vá para o ambiente de produção, manter cache é algo extremamente saudável e recomendado, pois diminui o tempo de carregamento de sua página e reduz, também, tráfego do seu servidor.
Answer: A única coisa que funcionou para mim foi inserir uma "hash" informando que existe uma nova versão do css para o chrome:

<link rel="stylesheet" type="text/css" href="css/style.css?version=2">

Problemas pra exibir meu site no chrome


Writing a custom loss function without y_true in Keras

Question: I'm implementing a triplet loss function in Keras. In general, loss functions take predicted values with ground truth as arguments. But triplet loss doesn't use labels, just the output. I tried to write the function with just one parameter:
def triplet_loss(y_pred):
    margin = 1
    return K.mean(K.square(y_pred[0]) - K.square(y_pred[1]) + margin)

It failed saying triplet_loss() takes 1 argument but two arguments were given (in score_array = fn(y_true, y_pred). When I write the function with two arguments y_true, y_pred, the program runs without error. Why is that? Should I just implement this function with these two arguments although y_true won't be used? Is this correct or is there another way of doing it?
Answer: Well.... simply don't use the ground truth:
def triplet_loss(y_true,y_pred):
    #all your code as it is.

It's not very usual to have networks trained without ground truth. When we expect it to learn something, there is very very often a ground truth. If you don't, simply ignore it.
Also, if y_true is ignored, what are you passing to the fit method? Just a dummy array?
Comment: I did't solved it yet, I'm just trying to deal with it now.
Comment: @sachinruk, It's possible to use `model.add_loss(some_calculated_tensor)` and probably compile with `loss=None`.
Comment: I wonder if this is still the best solution with tf 2.3 out now?
Answer: implement it via K.function method.
output_tensor = your_model(input_tensor)
total_loss = K.mean( K.abs (input_tensor - output_tensor) )

nn_train = K.function ([input_tensor],[total_loss],
                Adam(lr=5e-5, beta_1=0.5, beta_2=0.999).get_updates(total_loss, your_model.trainable_weights)

loss, = nn_train ([input])


Static Route Setup on Windows 10 for Split Tunnel VPN

Question: I have an L2TP VPN service setup on a Ubiquity USG 3 Router.
I trying to set up a split tunnel service on Window 10 machine so that:

Internet Traffic is sent outside VPN (for speed)
Certain Local IP is sent through the VPN tunnel

I've unchecked the "Use default gateway on remote network" and get the expected external IP address when on VPN however, my local IP traffic also gets sent to out to the internet.
I've tried setting some static routes to force the traffic through the VPN tunnel but its not working as expected.
IP config:
    Ethernet adapter Ethernet 3:

   Connection-specific DNS Suffix  . : routerlogin.net
   Link-local IPv6 Address . . . . . : fe80::ce9:xxxx:xxxx:xxxx%20
   IPv4 Address. . . . . . . . . . . : 192.168.1.4
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.1.1

   PPP adapter AoE_VPN:

   Connection-specific DNS Suffix  . :
   IPv4 Address. . . . . . . . . . . : 192.168.14.1
   Subnet Mask . . . . . . . . . . . : 255.255.255.255
   Default Gateway . . . . . . . . . :

I am bit puzzelled whey the Default Gateway is blank for the VPN connection.
Routes I try adding but have no effect:
route add 192.168.19.0 MASK 255.255.255.0 192.168.14.1
route add 192.168.14.0 MASK 255.255.255.0 192.168.14.1

IPv4 Route Table
===========================================================================
Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
          0.0.0.0          0.0.0.0      192.168.1.1      192.168.1.4     75
    24.145.xxx.xx  255.255.255.255      192.168.1.1      192.168.1.4     76
        127.0.0.0        255.0.0.0         On-link         127.0.0.1    331
        127.0.0.1  255.255.255.255         On-link         127.0.0.1    331
  127.255.255.255  255.255.255.255         On-link         127.0.0.1    331
      192.168.1.0    255.255.255.0         On-link       192.168.1.4    331
      192.168.1.4  255.255.255.255         On-link       192.168.1.4    331
    192.168.1.255  255.255.255.255         On-link       192.168.1.4    331
     192.168.14.0    255.255.255.0     192.168.14.1      192.168.1.4     76
     192.168.19.0    255.255.255.0     192.168.14.1      192.168.1.4     76
        184.108.40.206        240.0.0.0         On-link         127.0.0.1    331
        188.8.131.52        240.0.0.0         On-link       192.168.1.4    331
        188.8.131.52        240.0.0.0         On-link      192.168.14.1    331
  255.255.255.255  255.255.255.255         On-link         127.0.0.1    331
  255.255.255.255  255.255.255.255         On-link       192.168.1.4    331
  255.255.255.255  255.255.255.255         On-link      192.168.14.1    331
===========================================================================

TraceRoute:
Tracing route to 192.168.19.10 over a maximum of 30 hops

  1     3 ms     3 ms     1 ms  attwifimanager.routerlogin.net [192.168.1.1]
  2    53 ms    27 ms    28 ms  172.26.96.169
  3     *        *        *     Request timed out.

I not confident I have the correct destination IP for the add route command but I am not sure what IP address I should be using.
Answer: Do you have "Use Default Gateway on Remote" ticked.
I don't think the Rout table looks correct.
I would think the result should have been something like this.
 192.168.14.0    255.255.255.0     On-link      192.168.14.1     76
 192.168.19.0    255.255.255.0     On-link      192.168.14.1     76


Unable to get mp4 tkhd info using go-mp4

Question: Using package github.com/abema/go-mp4 when i try to run the example in the docs i get following error:
invalid operation: box (variable of type *mp4.BoxInfo) is not an interface

Here is the example that i am trying:
// extract specific boxes
boxes, err := mp4.ExtractBox(file, nil, mp4.BoxPath{mp4.BoxTypeMoov(), mp4.BoxTypeTrak(), mp4.BoxTypeTkhd()})
if err != nil {
   :
}
for _, box := range boxes {
  tkhd := box.(*mp4.Tkhd)
  fmt.Println("track ID:", tkhd.TrackID)
}

https://pkg.go.dev/github.com/abema/go-mp4#section-readme
Comment: You should ask for support on github! I think there's something wrong here, maybe it hasn't been updated after some modifications.
Comment: The code does a type assertion on a variable `box` that is not an Interface, I can't know what your intention is, but if you declare `box` as an interface before you do the range, it might do the trick for you. [ref](https://go.dev/tour/methods/15)
Answer: Sorry, it is an error of my sample code.
I created Pull Request to fix it.
https://github.com/abema/go-mp4/pull/113/files
When you find any other problems about go-mp4, I would be grateful if you could create new Issue on GitHub.
https://github.com/abema/go-mp4/issues


Ruby and RVM cannot install ruby-1.9.3-p392

Question: I check what versions of ruby I have with the right command.
Vincents-MacBook-Pro:~ vincentwarmerdam$ rvm list known
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-p371]
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p320]
[ruby-]1.9.3-p125
[ruby-]1.9.3-p194
[ruby-]1.9.3-p286
[ruby-]1.9.3-p327
[ruby-]1.9.3-p362
[ruby-]1.9.3-p374
[ruby-]1.9.3-p385
[ruby-]1.9.3-[p392]
[ruby-]1.9.3-head
[ruby-]2.0.0-rc1
[ruby-]2.0.0-rc2
[ruby-]2.0.0[-p0]
ruby-head

The list goes on and on but I can see that there are many versions of ruby 1.9.3. I try to switch to the newer version of 1.9.3, but I get an error.
Vincents-MacBook-Pro:~ vincentwarmerdam$ rvm use 1.9.3
ruby-1.9.3-p392 is not installed.
To install do: 'rvm install ruby-1.9.3-p392'

This seems odd to me because I can see [ruby-]1.9.3-[p392] appear in the list. When I try to install I suddenly get this:
    Vincents-MacBook-Pro:~ vincentwarmerdam$ rvm install ruby-1.9.3
Searching for binary rubies, this might take some time.
No binary rubies available for: osx/10.7/x86_64/ruby-1.9.3-p392.
Continuing with compilation. Please read 'rvm mount' to get more information on binary rubies.
Installing requirements for osx, might require sudo password.
error: Your local changes to the following files would be overwritten by merge:
    suite-sparse.rb
Please, commit your changes or stash them before you can merge.
Aborting
Error: Failed to update tap: homebrew/science
Already up-to-date.
Certificates in '/usr/local/etc/openssl/cert.pem' already are up to date.

apple-gcc42 is not available in PATH, try:

    brew unlink apple-gcc42 && brew link apple-gcc42

and make sure that apple-gcc42 is available in your PATH:

    which gcc-4.2

Could anybody explain to me what is happening? I've reinstalled RVM and I still get this message. I just want to be able to use ruby 1.9.3 so I can follow my first ruby tutorials.
Comment: Did you try to follow the instructions regarding the gcc? To install ruby, you need a compiler (gcc)
Comment: `list known` just lists all the versions RVM knows about. It does not mean you have all of them installed. You must install one from that list before you can use it.
Answer: It looks like suite-sparse.rb is part of a git repository and it has been corrupted.  Try to find that file, cd to its directory, and run the git checkout suite-sparse.rb command on it.
Answer: turned out that all I needed to do was install the command line tools for xcode. This removed all errors because now the gcc compiler works.
Comment: Can you add your resolution as an answer and accept it?  Also, these days you can install the command-line tools from XCode with `xcode-select --install` (assuming you have XCode installed)


Scroll direction in an OnPageChangeListener

Question: Is it possible to know the direction of the scroll in the method onPageScrolled of the OnPageChangeListener applied to a ViewPager?
Comment: you can easily get the position of the page.. with the position increasing you can determine it as  swiping towards right end pages.. with the position decreasing it is swiping towards left end pages
Answer: I've used something like this:
mPager.setOnPageChangeListener(new OnPageChangeListener() {

    int oldPos = mPager.getCurrentItem();

    @Override
    public void onPageScrolled(int position, float arg1, int arg2) {

            if(position > oldPos) {
                 //Moving to the right

            } else if(position < oldPos) {
                 //Moving to the Left
            }

        .
        .
        .

--Edit--
Sorry, changed to onPageScrolled, not onPageSelected..
Comment: @CACuzcatlan late response sorry, but maybe it will be useful to someone else. I found that **`onPageScrolled`** is called every time the Pager view is dragged one px in either direction (possibly dp?), so in one drag it will be called many times until the user lets go of the Pager view. **`onPageSelected`** is called right before page change when the page starts settling in its new position. The two events seem to be mutually exclusive in time, meaning that there is no temporal overlap between the time windows when each event is triggered.
Comment: Can you explain the change? It works for me in onPageSelected but onPageScrolled
Comment: Using your code above, it works for me in onPageSelected, but not in onPageScrolled. onPageScrolled always hits the else case
Comment: Without seeing your particular implementation, there's nothing to say. Maybe post a question, with your code and explaining the issue, and I'll take a look.
Comment: Your edit says you changed it from onPageScrolled from onPageSelected. I was just wondering if you could explain why you did that.
Comment: Because OP asked about onPageScrolled, but initially I thought he/she was asking about onPageSelected..
Comment: This worked for me, _HOWEVER_ I needed to keep the **`int oldPos = mPager.getCurrentItem();`** in the **`onPageScrolled`** and, as suggested, add the **`if`** clause to the **`onPageScrolled`** portion.


Is there a better (more modern) tool than lex/flex for generating a tokenizer for C++?

Question: I recent added source file parsing to an existing tool that generated output files from complex command line arguments.
The command line arguments got to be so complex that we started allowing them to be supplied as a file that was parsed as if it was a very large command line, but the syntax was still awkward.  So I added the ability to parse a source file using a more reasonable syntax.
I used flex 2.5.4 for windows to generate the tokenizer for this custom source file format, and it worked.  But I hated the code.  global variables, wierd naming convention, and the c++ code it generated was awful.  The existing code generation backend was glued to the output of flex - I don't use yacc or bison.
I'm about to dive back into that code, and I'd like to use a better/more modern tool. Does anyone know of something that.

Runs in Windows command prompt (Visual studio integration is ok, but I use make files to build)
Generates a proper encapsulated C++ tokenizer. (No global variables)
Uses regular expressions for describing the tokenizing rules (compatible with lex syntax a plus)
Does not force me to use the c-runtime (or fake it) for file reading.  (parse from memory)
Warns me when my rules force the tokenizer to backtrack (or fixes it automatically)
Gives me full control over variable and method names (so I can conform to my existing naming convention)
Allows me to link multiple parsers into a single .exe without name collisions
Can generate a UNICODE (16bit UCS-2) parser if I want it to
Is NOT an integrated tokenizer + parser-generator (I want a lex replacement, not a lex+yacc replacement)

I could probably live with a tool that just generated the tokenizing tables if that was the only thing available.
Comment: Boost.Spirit, Boost.Proto and Boost.Xpressive are not options?
Comment: @Konrad: They might be, I'm not familiar with them. something that generated templates rather than a C++ class would be acceptable.
Answer: Ragel: http://www.complang.org/ragel/ It fits most of your requirements.

It runs on Windows
It doesn't declare the variables, so you can put them inside a class or inside a function as you like.
It has nice tools for analyzing regular expressions to see when they would backtrack.  (I don't know about this very much, since I never use syntax in Ragel that would create a backtracking parser.)
Variable names can't be changed.
Table names are prefixed with the machine name, and they're declared "const static", so you can put more than one in the same file and have more than one with the same name in a single program (as long as they're in different files).
You can declare the variables as any integer type, including UChar (or whatever UTF-16 type you prefer).  It doesn't automatically handle surrogate pairs, though.  It doesn't have special character classes for Unicode either (I think).
It only does regular expressions... has no bison/yacc features.

The code it generates interferes very little with a program.  The code is also incredibly fast, and the Ragel syntax is more flexible and readable than anything I've ever seen.  It's a rock solid piece of software.  It can generate a table-driven parser or a goto-driven parser.
Comment: I use this myself and strongly agree. There's also Kelbt, by the same author, for parsing - it's not a release version and it has its quirks (in particular, it doesn't do associativity or precedence disambiguation) but I've been using it anyway and the only crashes I've had were build-time and resulted from code errors (referencing a non-existent token in a parse action).
Comment: I also used treecc for AST node definition with multiple-dispatch operations, in combination with Kelbt and Ragel. These days, I have my own replacement with some additional tricks (e.g. AST-traversing iterator classes) - I may even release it, one day...
Comment: @Steve314: Please let us know if you do.
Answer: Flex also has a C++ output option.
The result is a set of classes that do that parsing.
Just add the following to the head of you lex file:
<code>%option C++
%option yyclass="Lexer"
</code>
Then in you source it is:
<code>std::fstream  file("config");
Lexer         lexer(&file)
while(int token = lexer.yylex())
{
}
</code>
Comment: As you don't maintain the produced code its asthetic niceties are irrelavant. Your source file is the lex file. As long as the code works does not pollute the global namespace the implementation details are irrelavant. Note1: Don't build the source files only build the object files and headers (ie delete the source immediately). Make the dependency between the object and lex file not the source file. Note: They are messey because to make it effecient.
Comment: Yeah, I tried this, it's even worse than the C code IMO, but it does solve the golbal variables and multiple instance problem.
Answer: Boost.Spirit.Qi (parser-tokenizer) or Boost.Spirit.Lex (tokenizer only). I absolutely love Qi, and Lex is not bad either, but I just tend to take Qi for my parsing needs...
The only real drawback with Qi tends to be an increase in compile time, and it is also runs slightly slower than hand-written parsing code. It is generally much faster than parsing with regex, though.
http://www.boost.org/doc/libs/1_41_0/libs/spirit/doc/html/index.html
Comment: Thanks, I'll have a look at Boost.Spirit.Lex
Answer: There's two tools that comes to mind, although you would need to find out for yourself which would be suitable, Antlr and GoldParser. There are language bindings available in both tools in which it can be plugged into the C++ runtime environment.
Comment: Speaking of which when I went for a spot of googling, there is apparently a object oriented version of flex/bison using the boost library - check it out http://dudka.cz/vyp08
Comment: I already knew about antler and found it unsuitable.  I's a parser generator and it requires the installation of the java runtime, which in my experience is unstable and annoying on Windows.  GoldParser looks promising though - thanks.
Comment: @tommeib75: dudka.cz good find. maybe he has a better way of wranging the flex output into clean code.
Comment: +1 (wish it could be more) for ANTLR. ANTLRworks is great for visualizing your work and for debug stepping through it,  http://www.antlr.org/works/screenshots/editor.jpg
Comment: Goldparser is nice but it is extremely slow. Even in speed optimized C++ code it takes 10 seconds to parse a 15000 lines of code. If you compare this with the speed of the PHP parser this is extremely slow.
Answer: boost.spirit and Yard parser come to my mind. Note that the approach of having lexer generators is somewhat substituted by C++ inner DSL (domain-specific language) to specify tokens. Simply because it is part of your code without using an external utility, just by following a series of rules to specify your grammar.


Find a recurrence relation and give initial conditions for the number of words of length $n$ that do not contain two consecutive vowels.

Question: I'm trying to find a recurrence relation for the number of words of length $n$ that do NOT contain two consecutive vowels.
I'm trying to relate the problem to a similar one, of bit strings (Say, a relation for the number of bit strings that do not contain four consecutive 0's), but I'm starting to think I don't understand that either.
My though process:
Assume $a_n $ represents the number of words that do not contain consecutive vowels.
If there are 26 letters in the English alphabet and 5 vowels (not including the letter "y"), then there are $26^n$ total words. When researching online, I find that similar problems lead people to using $a_{n-1}$, but I don't understand where that comes from. Can anyone offer me some insight? Thank you.
Comment: You could look at [this question](https://math.stackexchange.com/questions/3147333/recurrence-relation-for-the-number-of-strings-of-length-n-over-the-alphabet), which is the same except for the number of characters in each group.
Comment: Oops, typo. Thank you.
Answer: Think as follows: A sequence of length $n$ that doesn't end in a vowel can be followed by a non-vowel or a vowel followed by a non-vowel. This gives a recurrence (How many of those are of length 0? 1?). Solve that one, and consider the case where it ends in vowel.


Ubuntu server is sending emails via Gmail. How do I stop this?

Question: I have recently taken ownership of a sever which hosts multiple websites.
For some reason, when the websites send notification emails (i.e - when a contact form is submitted) the email received comes from a personal Gmail account.
I am trying to get to the bottom of this, but am having difficulty. How do I find out which email mosdule is installed on my server? How do I stop the emails being routed through the Gamil account? Which is the best email module I need to install to send php emails?
Interestingly, when I SSH into my server I see the following message:
No mail.
85.xx.93.xx
The program 'mailx' is currently not installed.  You can install it by typing:
sudo apt-get install mailutils

Thanks!
Comment: Are you sure that e-mails really "come from" that gmail account? Check the headers of the e-mail to see where the mail is really coming from.
Answer: If the email is being sent when a form is submitted from a web page, then the FROM address is most likely set in the code of the web page.
Begin your investigation in the websites code.
Answer: How to detect which email/MTA server is installed?
Check your SMTP server greeting message.
telnet 127.0.0.1 25


Is there an issue using an imbalanced covariate (not dependent) in logistic regression?

Question: I am investigating data from a randomised control trial, where treatment allocation is done on a 2:1 ratio (2 patients on the experimental treatment for every 1 patient on placebo). 400 on experimental treatment and 200 on placebo, for example.
I am conducting some exploratory analysis to investigate if there are any significant interactions between the treatment term (binary covariate) and other selected covariates, when using death (binary dependent variable) as the outcome.
As mentioned, the trial has 2:1 randomisation, so there is an imbalance in the number of patients on the experimental drug and placebo.
My plan is to build a logistic regression model with death as the dependent variable. The model will include a treatment term and other relevant covariates (selected using AIC) and also any relevent interactions involving the treatment term.
My question surrounds the imbalance in the treatment allocation (Note: not the imbalance in the dependent variable death): Does the fact that the treatment arms are unequal (400 experimental, 200 placebo) have any impact on the conclusions I can draw from the logistic model? I have been led to believe that the treatment imbalance leads to differing variance in each treatment arm (np(1-p)) - is this really a problem? If so, can it be solved?
I have considered using upsampling to balance the treatment arms. Is there anything such as weighted logistic regression or conditional logistic regression that would be suitable?
I realise there has been much discussion surrounding unbalanced datasets, however the discussion is usually focused on imbalance in the dependent variable: Are unbalanced datasets problematic, and (how) does oversampling (purport to) help?
Answer: There is nothing wrong with this and nothing you can do. Compared to a 400-400 split, you have less precision; compared to a 200-200 split, you have more precision; compared to a 300-300 split, you have less precision. But it doesn't matter; the data you have is the data you have. No weighting or sampling can change that. You can increase precision by including covariates in the estimation of the effect using the techniques recommended by Culantuouni and Rosenblum (2015). It is rarely the case that all binary predictors in an analysis are exactly 50-50 distributed. It's true that among all possible allocations, an even split yields the best precision (all else equal), but that doesn't mean you have to have it that way. If an event is rarer in the experimental group than in the placebo, then you increase your ability to detect effects by allocating more participants to that group, which was probably the motivation here.


CLIPS: retrieve slot value from multislot if INSTANCES

Question: I have an object with a multislot of INSTANCE.
I have problems in getting instance's slot with one call.
Example:
(defclass AUTOMA (is-a USER)
(slot uuid))

(defclass TUTOMA (is-a USER)
(multislot list
(type INSTANCE)))

(make-instance A1 of AUTOMA
(uuid a1))
(make-instance A2 of AUTOMA
(uuid a2))
(make-instance T1 of TUTOMA
(list [a1] [a2]))

I want to retrive the first object uuid of the multislot list.
1) Try with "first$":
CLIPS> (first$ (send [T1] get-list))
([a1])
CLIPS> (send (first$ (send [T1] get-list)) get-uuid)
[MSGFUN1] No applicable primary message-handlers found for get-uuid.
FALSE

2) Try with "implode$":
CLIPS> (implode$ (first$ (send [T1] get-list)))
"[a1]"
CLIPS> (send (implode$ (first$ (send [T1] get-list))) get-uuid)
[MSGFUN1] No applicable primary message-handlers found for get-uuid.
FALSE

It seems that both ([a1]) and "[a1]" are not good for the (send XXX get-uuid) command.
Any suggestions, please?
Thank you
Nic
Answer: First$ returns a multifield value and implode$ returns a string. You need to use an instance name. Use nth$ to retrieve a field from within a multifield. You also need to keep the case used for your instance names consistent:
CLIPS>
(defclass AUTOMA
   (is-a USER)
   (slot uuid))
CLIPS>
(defclass TUTOMA
   (is-a USER)
   (multislot list (type INSTANCE)))
CLIPS> (make-instance A1 of AUTOMA (uuid a1))
[A1]
CLIPS> (make-instance A2 of AUTOMA (uuid a2))
[A2]
CLIPS> (make-instance T1 of TUTOMA (list [A1] [A2]))
[T1]
CLIPS> (send (nth$ 1 (send [T1] get-list)) get-uuid)
a1
CLIPS>


What is the difference between Reference type and ref in C#?

Question: In both ref type and Reference type I am able to change the value of the object so what is the difference between them?
Someone has provided an answer but it's still unclear for me.
static void Main(string[] args)
{
    myclass o1 = new myclass(4,5);
    Console.WriteLine("value of i={0} and j={1}", o1.i, o1.j); //o/p=4,5

    o1.exaple(ref o1.i, ref o1.j);    //Ref type calling
    Console.WriteLine("value of i={0} and j={1}", o1.i, o1.j);// o/p=2,3
    myclass o2 = o1;
    Console.WriteLine("value of i={0} and j={1}", o2.i, o2.j);  // o/p 2,3
    o1.i = 100;
    o1.j = 200;
    Console.WriteLine("value of i={0} and j={1}", o1.i, o1.j);  //o/p=100,200
    Console.WriteLine("value of i={0} and j={1}", o2.i, o2.j); //o/p=100,200
    Console.ReadKey();
}

public class myclass
{
    public int i;
    public int j;

    public myclass(int x,int y)
    {
        i = x;
        j = y;
    }
    public void exaple(ref int a,ref int b) //ref type
    {
        a = 2;
        b = 3;
    }
}

Comment: It's simple: It's the difference between a pointer and a pointer to a pointer in C.
Answer: Parameter with ref keyword provide reference to reference of the object where you can change where this reference will point after changes
public void TestObject(Person person)
{
    person = new Person { Name = "Two" };
}

public void TestObjectByRef(ref Person person)
{
    person = new Person { Name = "Two" };
}

Then when you use those methods
var person = new Person { name = "One" };

TestObject(person);
Console.WriteLine(person.Name); // will print One

TestObjectByRef(ref person);
Console.WriteLine(person.Name); // will print Two

Below a quatation from MSDN: https://msdn.microsoft.com/en-us/library/14akc2c7.aspx

The ref keyword causes an argument to be passed by reference, not by
  value. The effect of passing by reference is that any change to the
  parameter in the called method is reflected in the calling method. For
  example, if the caller passes a local variable expression or an array
  element access expression, and the called method replaces the object
  to which the ref parameter refers, then the caller's local variable or
  the array element now refer to the new object.

When you passing reference type as parameter to the method without ref keyword the reference to the object passed as the copy. You can change values(properties) of the object, but if you set it to reference another object it will not affect original reference.
Comment: could u please Elaborate more i have started learning C# last week only
Comment: "if you set it to reference another object it will not affect original reference." could u pls write a simple example(code) to explain it  thanks in advance :)
Comment: The example is in the answer, see using of `TestObject` method


Keda ScaledObject with MSSQL trigger

Question: I'm using keda in my k8s cluster for autoscaling. After deploying my scaled object with MSSQL trigger , I'm getting ready and active status as unknown.. In logs I'm only seeing that it's trying to create a HPA and then nothing happens. I guess the keda-operator is getting stuck here as I'm not getting any updates on logs.

My ScaledObject:

On query execution, I'm getting value as 300 which is enough to initate scaling.
Also I'm attaching operator logs :
2021-06-04T08:06:20.130Z INFO controller Starting EventSource {"reconcilerGroup": "keda.sh", "reconcilerKind": "ScaledJob", "controller": "scaledjob", "source": "kind source: /, Kind="}
2021-06-04T08:06:20.130Z INFO controller Starting EventSource {"reconcilerGroup": "keda.sh", "reconcilerKind": "ScaledObject", "controller": "scaledobject", "source": "kind source: /, Kind="}
2021-06-04T08:06:20.130Z INFO controller Starting EventSource {"reconcilerGroup": "keda.sh", "reconcilerKind": "TriggerAuthentication", "controller": "triggerauthentication", "source": "kind source: /, Kind="}
2021-06-04T08:06:20.230Z INFO controller Starting Controller {"reconcilerGroup": "keda.sh", "reconcilerKind": "ClusterTriggerAuthentication", "controller": "clustertriggerauthentication"}
2021-06-04T08:06:20.230Z INFO controller Starting workers {"reconcilerGroup": "keda.sh", "reconcilerKind": "ClusterTriggerAuthentication", "controller": "clustertriggerauthentication", "worker count": 1}
2021-06-04T08:06:20.230Z INFO controller Starting Controller {"reconcilerGroup": "keda.sh", "reconcilerKind": "TriggerAuthentication", "controller": "triggerauthentication"}
2021-06-04T08:06:20.230Z INFO controller Starting workers {"reconcilerGroup": "keda.sh", "reconcilerKind": "TriggerAuthentication", "controller": "triggerauthentication", "worker count": 1}
2021-06-04T08:06:20.230Z INFO controller Starting EventSource {"reconcilerGroup": "keda.sh", "reconcilerKind": "ScaledObject", "controller": "scaledobject", "source": "kind source: /, Kind="}
2021-06-04T08:06:20.232Z INFO controller Starting Controller {"reconcilerGroup": "keda.sh", "reconcilerKind": "ScaledJob", "controller": "scaledjob"}
2021-06-04T08:06:20.330Z INFO controller Starting Controller {"reconcilerGroup": "keda.sh", "reconcilerKind": "ScaledObject", "controller": "scaledobject"}
2021-06-04T08:06:20.331Z INFO controller Starting workers {"reconcilerGroup": "keda.sh", "reconcilerKind": "ScaledObject", "controller": "scaledobject", "worker count": 1}
2021-06-04T08:06:20.332Z INFO controller Starting workers {"reconcilerGroup": "keda.sh", "reconcilerKind": "ScaledJob", "controller": "scaledjob", "worker count": 1}
2021-06-04T08:07:19.986Z INFO controllers.ScaledObject Reconciling ScaledObject {"ScaledObject.Namespace": "xyz", "ScaledObject.Name": "mssql-keda-scaledobject"}
2021-06-04T08:07:19.986Z INFO controllers.ScaledObject Adding Finalizer for the ScaledObject {"ScaledObject.Namespace": "xyz", "ScaledObject.Name": "mssql-keda-scaledobject"}
2021-06-04T08:07:20.052Z INFO controllers.ScaledObject Detected resource targeted for scaling {"ScaledObject.Namespace": "xyz", "ScaledObject.Name": "mssql-keda-scaledobject", "resource": "apps/v1.Deployment", "name": "xyzxyzxyzxyzxyzxyzxyz"}
2021-06-04T08:07:20.052Z INFO controllers.ScaledObject Creating a new HPA {"ScaledObject.Namespace": "xyz", "ScaledObject.Name": "mssql-keda-scaledobject", "HPA.Namespace": "xyz", "HPA.Name": "keda-hpa-mssql-keda-scaledobject"}
I'm getting hpa created for other scaled objects. Also if I'm providing sql connection credentials wrong, I'm getting appropriate authentication error. So I guess this is not a connection issue.
Any leads would be greatly appreciated. Thanks in advance
Answer: I also had issues when I started using the MSSQL Trigger for KEDA on an Azure Function app in Azure Kubernetes Service.
I faced a couple issues so you could try checking these out as well:
KEDA VERSION
KEDA version 2.2 was the first version to support the MSSQL trigger, I had to get the operator logs for keda-operator to figure out that the MSSQL trigger could not be found using version 2.1 of KEDA. Your operator logs don't seem to say "cannot find trigger MSSQL" but you could double check. I have had the best luck with KEDA 2.4 which you can install using these instructions: https://keda.sh/docs/2.4/deploy/
Troubleshooting ScaledObject Issues
You can start by finding the name of your ScaledObject with
kubectl get scaledobject -A

Then describe the ScaledObject, and you should see any errors or issues listed towards the bottom such as authentication issues or issues creating an HPA for KEDA:
kubectl describe scaledobject <your-scaled-object-name>

Ultimately my issue required installing the right version of KEDA, and also modifying my MSSQL connection string.

Keda ScaledObject with MSSQL trigger


The area between $f(x)=x^2 - 1$ and $g(x)=sin(\frac{\pi}{2}(1-x))$ for $x \in [-1, 1]$

Question: I Know that $g(x)\geq f(x)$ for all $x \in [-1, 1]$.
I also know that they cut each other at $x=1, -1$.
Is it correct If I calculate the integral of $g(x)-f(x)$ only?
It seems that I need to divide the calculation somehow, right?
Answer: Your calculations are right:

It suffices to calculate the integral of $g(x) - f(x)$ because, in fact, the red area is
$$
\color{red}{\int_{-1}^1g(x)dx}
$$
and the green one is
$$
\color{green}{\int_{-1}^1(-f(x))dx = -\int_{-1}^1f(x)dx}.
$$

The "green" integral can be seen as the area between the function $-f(x)$ and the $x$-axis, which is the same that you want to find.
In general, to find the area between two functions $f,g$ in a certain interval $I=(a,b)$, you must just calculate the integral of $|f(x)-g(x)|$, that is $g(x)-f(x)$ in your case because, as you said, $g(x)>f(x)\;\; \forall x\in(-1,1)$.


How do I group data into a column by a common feature?

Question:
I have a huge list of genes separated into clusters.  As in the image, the cluster IDs and the genes are in separate columns.  What I'd like to do is create a new data frame using the cluster IDs as the column names/headers, then have the genes be the rows below each column name.  For example:
cluster 0
Ccl5
Trbc2
Cd3g
Nkg7
etc

Comment: Images are not the right way to share data/code. Add them in a reproducible format which is easier to copy. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269).
Answer: One option is to use the spread function from the tidyr package. Assuming your data is called df:
library(tidyr)

spread(df, key = cluster, value = gene)

Comment: spread has been [superseded](https://tidyr.tidyverse.org/reference/spread.html).
Answer: you can use the function pivot_wider from the tidyr package to convert your data from long to wide by doing something like:
new_dataframe <- old_dataframe %>%
  pivot_wider(names_from = cluster,
              values_from = gene)


Two Stringed Instruments in Unison?

Question: I have been receiving conflicting information from orchestration bigwigs. Some say that composers should never orchestrate two stringed instruments together because they will be ever so slightly out of tune with each other. But then there are compositions like Vivaldi's Violin Concerto Op 3 No 8 that have two violins playing in unison. What's the scoop?
Okay there seems to be confusion as to what I mean. I have read that for smaller ensembles, one section of stringed instruments should either consist of one or more than three because only two stringed instruments playing together would sound out of tune. Is that true?
Comment: This is supposed to be regarding never arranging *exactly two instruments* in unison. Supposedly more than two makes a nice sound as a section, and one is a solo, but two is a weird out-of-tune thin section, I would assume the logic goes.
Comment: To expand on @Tetsujin 's comment, I am guessing you are referring to something like the first and second violins playing in unison, but you have to take into consideration that in an orchestra, the first violins aren't just one violin, they are numerous and they all have to play in unison with each other, so there is no reason why they can't play in unison with another group of string instruments
Comment: Which is the issue: orchestrating two stringed instruments (pretty general and would seem to rule out string quartets and many other genres) or two stringed instruments in unison?
Comment: I'm wondering how that interpretation would work on a 100-piece orchestra… they all have to be playing something different, all the time…?
Answer: Two string instruments in unison don't blend well.  But in the concerto for two solo violins, you don't want them to blend.  You want them to remain individually recognisable.  Which is tricky in a unison passage, so such passages are short.
There are also tutti passages where the solo violins are joined by a section: of course there the blend is intended.
Answer: This is dangerously close to "feng shui" territory.  But I suspect that, rather like the infamous Bose 900 series speakers, his point is that 3 or more instruments produce enough interferences that  your neuroacoustical  interpretation is that of "smoothed" sound.  When there's only 2 instruments the beat frequency (difference of the two pitches) is much easier to pick out.  When you have 4 or 5 instruments,  the collection of beat frequencies (go ahead and do the math of combining  5  separate complex waveforms :-) ) crushes your ability to notice them.
footnote: Bose 900 "Direct-reflecting" speakers drive 9 identical elements.  The company and some listeners found them delightful;  Consumer Reports slammed them & pointed out the guaranteed phasing mismatches. and that most of the subjective likes were due to the high volume output. Lawsuits ensued.

Two Stringed Instruments in Unison?


Limit listed Kubernetes namespaces by access

Question: I have a set of users(dev-team) who need access only to dev and qa namespaces. I created a service account, cluster role and cluster role binding as shown below.
Service Account
apiVersion: v1
kind: ServiceAccount
metadata:
  name: dev-team

Cluster role
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  name: dev-team-users
rules:
  - apiGroups: ["rbac.authorization.k8s.io",""]
    resources: ["namespaces"]
    resourceNames: ["dev","qa"]
    verbs: ["get","list","create"]

Cluster role binding
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: dev-team-user-bindings
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: dev-team-users
subjects:
- kind: User
  name: dev-team
  namespace: kube-system
  apiGroup: rbac.authorization.k8s.io

When I try to verify the access
kubectl get namespaces --as=dev-team
I get the below error message
Error from server (Forbidden): namespaces is forbidden: User "dev-team" cannot list resource "namespaces" in API group "" at the cluster scope

I am expecting to see only dev and qa namespaces to show up. Am I missing something here?
Answer: The list operation fails because you are using the resourceNames field in the ClusterRole to restrict the namespace objects to grant access too but list would return all namespace objects.
But I guess what you really want is to restrict access to the resources in a namespace, and not the namespace objects themselves (which contain not much more information than the name of the namespace).
To achieve this, you have to create Roles (or a ClusterRole) and RoleBindings in those namespaces that you want to grant access to the users.
Here is how you can grant access to all resources for the dev-team user in the dev and qa namespace but deny access to any resources in any other namespace.
Create a ClusterRole (you could also create a Role in the dev and qa namespaces, but using a ClusterRole allows you to define the permissions only once and then reference it from multiple RoleBindings):
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: dev-team-users
rules:
- apiGroups:
    - '*'
  resources:
  - '*'
  verbs:
  - '*'

Create a RoleBinding in both the dev and qa namespaces:
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
  name: dev-team-user-bindings
  namespace: dev
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: dev-team-users
subjects:
- kind: User
  name: dev-team
  apiGroup: rbac.authorization.k8s.io

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
  name: dev-team-user-bindings
  namespace: qa
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: dev-team-users
subjects:
- kind: User
  name: dev-team
  apiGroup: rbac.authorization.k8s.io

Test access:
kubectl get pods -n qa --as=dev-team           # Succeeds
kubectl get pods -n dev --as=dev-team          # Succeeds
kubectl get pods -n default --as=dev-team      # Fails
kubectl get pods -n kube-system --as=dev-team  # Fails

See Kubernetes RBAC documentation.
EDIT
1. Identify namespaces a user has created
Can't do this with RBAC. You would need some form of auditing.
2. Identify namespaces a user has access to
Also can't do this easily with RBAC. But you could just iterate through all namespaces and test whether a given user has access:
for n in $(kubectl get ns -o jsonpath='{.items[*].metadata.name}'); do
  echo -n "$n: "
  kubectl auth can-i get pods -n "$n" --as=dev-team
done

You can vary the verb/resource part (e.g. get pods) as needed.
Comment: Is there a way to have "kubectl get ns" to only list the namespaces a user has access to? Natively or via extending kubernetes by functionality?
Comment: Thank you for the detailed explanation. I have a requirement to identify all the namespaces a user has created or has access to. Is there a way to list it based on RBAC rules?


Finding dimension of vector spaces

Question: Let $V$ be a vector space and $W$ a subspace of $V$. Let $q : V\to V/W$ be
defined by $q(v) = v + W$ for $v\in V$.
a) Prove that $q : V\to V/W$ is a linear transformation which is onto
and show that $N(q) = W$.
b) Suppose that $V$ is finite dimensional. Use a) and the dimension theorem to derive a formula relating $\dim(V)$, $\dim(W)$, and $\dim(V/W)$.
c) Alternatively, let $\{w_1,\ldots, w_k\}$ be a basis for $W$. and let $\{w_1,\ldots, w_k, v_1,\ldots, v_{n−k}\}$
be a basis for $V$ extended from the basis for $W$. Show that
$\{v_1 +W,\ldots, v_{n−k} +W\}$ is a basis for $V/W$.
For a), I proved it with:
$q : V\to V/W$, $q(v) = v + W$ for $v\in V$. Suppose $u,v\in V$ and $c\in\Bbb R$.
$q(u + v)= (u + v) + W = (u + W ) + (v + W) = q(u) + q(v)$
$q(cv) = cv + W = c(v + W) = cq(v)$
So all elements $v + W$ in $V/W$ we have $q(v)= v + W$. Therefore it's onto.
$q(v) = v + W = 0 + W$ so $v - 0 = v\in W$.
Therefore $N(q) = W$.
For b) I tried to prove it with:
If $q$ is onto, then $R(T) = V/W$ also $N(q) = W$.
So we have $\dim(V) = \dim(V/W) + \dim(W)$ by the dimension theorem.
Does my proof make sense? Also, I'm not sure how to prove c). Any input would be helpful. Thanks!
Comment: You didn't show that $N(q) = W$
Comment: sorry, I forgot to type in that part.
Answer: Not only makes sense: up to the point you did, your proof is correct and nice: at the end, you already proved that $\;\dim V/W = \dim V-\dim W\;$
For (c) you can try the following: let $\;a_1,...,a_{n-k}\in F=$ the definition field, be such that
$$\sum_{i=1}^{n-k}a_i(v_i+W)=\overline0 \;(=\text{the zero element in}\;\;\;V/W)=W\implies$$
$$\left(\sum_{i=1}^{n-k}a_iv_i\right)+W=\overline0\iff\sum_{i=1}^{n-k}a_iv_i\in W\implies$$
$$\sum_{i=1}^{n-k}a_iv_i=\sum_{j=1}^nb_jw_j\;,\;\;\text{for some scalars}\;\;b_j\in F\implies$$
$$\sum_{i=1}^{n-k}a_iv_i-\sum_{j=1}^{n}b_jw_j=0$$
but $\;\{w_i\,,\,v_j\}\;$ is a basis, so...complete the proof.
Answer: Hint for c: Note that every $v \in V$ can be written as
$v = \sum_{i=1}^ka_i w_i + \sum_{i=1}^{n-k}b_i v_i$.
Show that
$$
q\left(\sum_{i=1}^k a_i w_i + \sum_{i=1}^{n-k}b_i v_i\right) =
\sum_{i=1}^{n-k}b_i v_i + W
$$
Because $q$ is onto, the $v_i$ span the set $V/W$.  Also, $\sum_{i=1}^{n-k}b_i v_i + W = 0 + W$ implies that all $b_i$ are $0$, so the set is linearly independent.  Thus, the vectors $v_i + W$ form a basis of $V/W$.

Finding dimension of vector spaces


Behaviour of this function

Question: Consider the function $f(n,a) = n^{-a}-n^{a-1}$
(i)I have to investigate the function for n between 1 to infinity and a between 0 to 1 . I think the function starts from zero at 1 reaches a maxima and again reduces to zero. What is its maxima for variable a and variable n?
(ii)Also if the sum $\sum\sum\limits_{n=1}^\infty g(n)f(n,a)=0$ where the first sum is over variable a .Impose condition on g(n) such that a=0.5 for all natural number n between 1 and infinity
Answer: HINT
You do not need to think that, for n=1, f(n,a) is 0; it is. Next, you say that the function goes through a maximum. If you start from 0, in order to reach a maximum, you need to have a positive derivative in order the function increases.
So, what is the derivative of f(n,a) with respect to n and what is its value if n=0 ? Does the value of "a" of any impact ?
By the way, what does happen if the exponents are identical (-a = a - 1) ?
I am sure you can continue from here.
Comment: @ Claude Thank you Can you answer my second question
Comment: @Surya. For the time being, I don't have any idea for the second question. If I find anything, I shall post it. Cheers.

Behaviour of this function


Choosing a better model and dealing with missing data?

Question: I am trying to create a logistic regression model to predict whether a customer given a loan will be a bad or a good customer: bad meaning missing a certain amount of payments and good meaning frequent enough and in time with payments. For the purpose of the model I have coded Bad as 1 and Good as 0 and tried different combinations with the variables.
One of the models I have built has an AIC of 5383.7 and Gini coefficient of 0.416733. This is the result after I play around with the threshold:
<code>     FALSE TRUE
  0  3327  638
  1   165   95
</code>
So the model guessed that 165 customers would be good, but they are bad, but also put 638 good customers into the bad customers group.
The second model I built has an AIC of 5734.6 (350.9 higher), but its Gini is 0.4190394 and is slightly better at predicting the bad customers:
<code>     FALSE TRUE
  0  3537  673
  1   177  105
</code>
[UPDATE] Okay. After checking a few things - It turns out that one of the variables has missing values and the model excludes the observations that have them by default. Hence the difference in observations in my models. I know about multiple imputation, but I don't really feel alright with it. My question is should I impute the missing data or should I exclude it from the data set so I can compare models with different number of variables?
Comment: Hi, it looks like you are using different data for both models (all the numbers in the second matrix are higher than the corresponding value in the first matrix). Why don't you run both models over the same data?
Comment: 1. Welcome to SO! 2. This is a CrossValidated question, not a SO question. 3. Use your cross-validation classification error rates in conjunction with use-case specific qualitative choice of the appropriate accuracy measure to use from the dozen or so that you can get from the cross-validated confusion matrix.
Comment: Your title seems misleading. You don't seem concerned with how to deal with data that is missing in regression.
Comment: @Roland That is what I am asking at the end of my post... Should I use multiple imputation or should I delete observations with missing data in the additional variables I use in the model.
Comment: The problem is completely misconceptualized.  Arbitrary binary designations were created for "good" and "bad" customers, resulting in arbitrariness and a huge loss of power/precision.  The raw data must be modeled.
Answer: Others have noted that there are differences between the data sets - and you certainly can't compare these metrics across different data sets successfully. The rest of the answer is around which metric should be preferred.
The AIC and Gini Coefficient are measuring quite different things, so you need to pause to consider what they are.
The AIC exists to check whether additional variables or different combinations of variables in some sense offer 'value for money' - they explicitly check the relative fit of the model penalising for extra variables added to achieve it. The intention is to find the optimum tradeoff between number of variables and fit of the model.
The Gini coefficient only checks one side of that balance - how well the model splits your data into the two categories.
One danger you face if you only consider the Gini coefficient is that the model may not generalise as well as you hoped when applied to new data. This is especially the case if you persist with 'statistically insignificant' variables (you didn't state your criterion, but my best guess is that it's something similar to p-value>0.05)
AIC is by no means perfect or the final answer, but some diagnostics around stability etc. are needed to have confidence that the model will perform on the next data set.
Answer: First of all, in the model with 2 more variables you have less observations so check that (probably because of missing values).
I wouldn't use AIC criterion in this case since it's more practical to assess which model did a better performance in the classification. The statistics "sensitivity", "specificity", "false negative" and "false positive" are more useful in this scenario, and you should regard the one that fits your objective.
If that is to identify bad customers accurately, maximize specificity.
Comment: Well. I just noticed that. I am using the same data set for both models. Which the same settings on the model with the same threshold on the prediction. Why does it generate a different contingency table?