Is it right to give equal chances?

Question: In a certain town, the probability that it will rain in the afternoon is known to be $0.6$ Moreover, meteorological data indicates that if the temperature at noon is less than or equal to $ 25°C$, the probability that it will rain in the afternoon is $ 0.4$. The temperature at noon is equally likely to be above$ 25°C$, or at/below $25°C$. What is the probability that it will rain in the afternoon on a day when the temperature at noon is above $25°C$____?

My attempt:
$\implies P(\text{rain in afternoon}) = 0.5 \times P(\times{rain when temp \leq25}) + 0.5 \times P(\text{ rain when temp>25})$,
$\implies 0.6 = 0.5\times 0.4 + 0.5\times P( \text{rain when temp>25})$
So,
$P( \text{rain when temp>25}) = 0.8$
I am using the rule $P(R)=(1/2)\times P(A)+(1/2)\times P(B)$ , I applied it , but I have no clear point , why I gives the priority to equal , why I don't give the chances to not equal $(i.e.  P(R)=(2/3)\times P(A)+(1/3)\times P(B)$ ).

Can you explain it in a formal way, please?
Answer: The full equation reads:
$$P(\text{rain}) = P(T>25)\cdot P(\text{rain if } T>25) +
 P(T\le 25)\cdot P(\text{rain if } T\le 25).\tag 1$$
The problem statement explicitly contains:

The temperature at noon is equally likely to be above 25°C, or at/below 25°C.

That is, $P(T>25) = P(T\le 25)$. Since $P(T>25) + P(T\le 25) = 1$, it immediately follows that $P(T>25) = P(T\le 25) = \frac12$.
Inserting in (1) immediately gives your equation.
Answer: Let $R$,$G_{25}$,$L_{25}$,  denote the events that it rains in the afternoon, the temperature is greater than $25$, the temperature is less than $25$, respectively.
We have the following data: $P(R)=0.6$, $P(G_{25})=P(L_{25})=0.5$, $P(R\mid L_{25})=0.4$.
The question is
$$P(R\mid G_{25})=\frac{P(R\cap G_{25})}{0.5}=2(P(R)-P(R\cap L_{25}))=$$
$$=2(P(R)-P(R\mid L_{25})P(L_{25}))=2(0.6-0.4\times0.5)=0.8.$$

Is it right to give equal chances?


Convert rows into RDD in pyspark

Question: I want to produce a file like this image with the below dataset.
The result is the result of filtering a dataframe with this line:
df1 = df0.rdd.filter(lambda x: 'VS' in x.MeterCategory)
  .map(lambda x: [x.vId,x.Meters]).take(2)

DataSet of rows:
[ABCD1234, Row(0=6.0, 10=None, 100=None, 1000=None, 10000=None, 1000000=None, 100000000=None, 10235=None, 1024=None)]
[WXYZ9999,Row(0=40.0, 10=None, 100=None, 1000=None, 10000=None, 1000000=None, 100000000=None, 10235=None, 1024=None)]

I've been trying with some approaches, that I found in this forum, but I cannot achieve the result.
Thanks
Comment: Thanks, both solutions works.
but now, the dev. team add the field names with .(dot), and pyspark is unable to find the field name.
So far, I do not know if using for .. in ... will reduce the performance in the worker nodes, even if this instruction will handle it by nodes.
the cluster is a microsoft hdinsight with default configuration.
Thanks
Answer: Using your example data:
df = sc.parallelize([('ABCD1234',6.0,'None','None','None','None','None','None','None','None'),
                     ('WXYZ9999',40.0,'None','None','None','None','None','None','None','None')]).toDF(['Id','0','10','100','1000','10000','1000000','100000000','10235','1024'])

You can pivot the data using the following snippet:
from pyspark.sql import functions as F
from pyspark.sql.types import StringType

kvp = F.explode(F.array([F.struct(F.lit(c).cast(StringType()).alias("Key"), F.col(c).cast(StringType()).alias("Value")) for c in df.columns if c!='Id'])).alias("kvp")
df_pivoted = df.select(['Id'] + [kvp]).select(['Id'] + ["kvp.Key", "kvp.Value"])
df_pivoted.show()

And you can output the data to a single CSV by converting the Dataframe to pandas:
df_pivoted.toPandas().to_csv('e:/output.csv',index=False,header = 'true', sep='|')

This gives as output:
Id|Key|Value
ABCD1234|0|6.0
ABCD1234|10|None
ABCD1234|100|None
ABCD1234|1000|None
ABCD1234|10000|None
ABCD1234|1000000|None
ABCD1234|100000000|None
ABCD1234|10235|None
ABCD1234|1024|None
WXYZ9999|0|40.0
WXYZ9999|10|None
WXYZ9999|100|None
WXYZ9999|1000|None
WXYZ9999|10000|None
WXYZ9999|1000000|None
WXYZ9999|100000000|None
WXYZ9999|10235|None
WXYZ9999|1024|None

Answer: Look at this.
Firstly please note, what you are referring to df1 is an RDD not a dataframe
You can create that RDD with the dataset you mentioned as follows to try the things out.
Please note that I have used '_' as prefix for your column names as a pure number cant be used as a column name directly.
>>> from pyspark.sql import Row

>>> row1 = Row(_0=6.0, _10=None, _100=None, _1000=None, _10000=None, _1000000=None,
           _100000000=None, _10235=None, _1024=None)
>>> row2 = Row(_0=40.0, _10=None, _100=None, _1000=None, _10000=None, _1000000=None,
           _100000000=None, _10235=None, _1024=None)

>>> yourStartDataset = sc.parallelize([
                                         ['ABCD1234',row1],
                                         ['WXYZ9999',row2]
                                      ])

Now your dataset looks like this
>>> yourStartDataset.take(2)

[['ABCD1234',
  Row(_0=6.0, _10=None, _100=None, _1000=None, _10000=None, _1000000=None, _100000000=None, _10235=None, _1024=None)],
 ['WXYZ9999',
  Row(_0=40.0, _10=None, _100=None, _1000=None, _10000=None, _1000000=None, _100000000=None, _10235=None, _1024=None)]]

Now the below line will do the magic
>>> yourStartDataset.flatMapValues(lambda v: v.asDict().items()).map(lambda (a, (b, c)): (a, b, c)).collect()

[('ABCD1234', '_1000000', None),
 ('ABCD1234', '_100000000', None),
 ('ABCD1234', '_100', None),
 ('ABCD1234', '_10000', None),
 ('ABCD1234', '_0', 6.0),
 ('ABCD1234', '_1000', None),
 ('ABCD1234', '_10', None),
 ('ABCD1234', '_10235', None),
 ('ABCD1234', '_1024', None),
 ('WXYZ9999', '_1000000', None),
 ('WXYZ9999', '_100000000', None),
 ('WXYZ9999', '_100', None),
 ('WXYZ9999', '_10000', None),
 ('WXYZ9999', '_0', 40.0),
 ('WXYZ9999', '_1000', None),
 ('WXYZ9999', '_10', None),
 ('WXYZ9999', '_10235', None),
 ('WXYZ9999', '_1024', None)]

Or, if you want to get only numeral part of your columns, the below will do
>>> yourStartDataset.flatMapValues(lambda v: v.asDict().items()).map(lambda (a, (b, c)): (a, b[1:], c)).collect()

[('ABCD1234', '1000000', None),
 ('ABCD1234', '100000000', None),
 ('ABCD1234', '100', None),
 ('ABCD1234', '10000', None),
 ('ABCD1234', '0', 6.0),
 ('ABCD1234', '1000', None),
 ('ABCD1234', '10', None),
 ('ABCD1234', '10235', None),
 ('ABCD1234', '1024', None),
 ('WXYZ9999', '1000000', None),
 ('WXYZ9999', '100000000', None),
 ('WXYZ9999', '100', None),
 ('WXYZ9999', '10000', None),
 ('WXYZ9999', '0', 40.0),
 ('WXYZ9999', '1000', None),
 ('WXYZ9999', '10', None),
 ('WXYZ9999', '10235', None),
 ('WXYZ9999', '1024', None)]

Hope this is helpful

Convert rows into RDD in pyspark


File operations: Java (can't find file)

Question: try {
     File makefile = new File("jk.txt");
     FileWriter fwrite = new FileWriter(makefile);
     fwrite.write(temp);
     fwrite.flush();

} catch (IOException e) {
     e.printStackTrace();
}

Where does the file get created in this code snippet? I can't find my file in my PC.
Comment: @home...before clicking the post as unuseful,i guess thepeople need to know that the coder may have tried the most obvious routes.I have searched in my computer for your kind information and cant find it.That is the reason i had posted it so that i wanted to know that whether the code has a bug or not and if not what possible reasons may be that the file is not getting created
Comment: Using Eclipse kind of messes with where the file goes, it goes where the Eclipse build path is. You can get the path where it is within the program as well like @MichaelBesteck 's answer.
Answer: To print out the path where the file is created:
File f = new File(".");
System.out.println(f.getCanonicalPath());

or
System.out.println(makefile.getCanonicalPath());

Comment: thanx man!..Stackoverflow really requires people like you...rather than those who downvote for fun sake without even help!!..thnx again.I had messed up with my root quiet badly that is why couldnt find the file
Comment: @user1485323 StackOverflow requires people to do some research before posting a new question that may be already solved. Or, a least, read the [official documentation](http://docs.oracle.com/javase/6/docs/api/java/io/File.html#File(java.lang.String)) and ask about what didn't you understand.
Answer: If you're running this on Eclipse, the file should be on the project root.
If you're running this standalone, the file should be created on the current directory.
Comment: it is not.Is there an another way of creating files than this?
Answer: It is created in the current path (probably the project root) since you don't set a path for the file when you create it.

File operations: Java (can't find file)


How can a sysadmin trap potential attackers?

Question: After honeypots comes IBM's Billy Goat to lock horns with malicious coders.
So, is there any other approach to detect and collect potential attacks from malicious people?
What is your security plan against some kind of attacks? Why do many attackers find solutions to trap sysadmins?
Please offer your feedback.
Answer: I can't make heads or tails of "Why do many attackers find solutions to trap sysadmins?", because I've never known hackers to "trap sysadmins", other than the all too common social engineering. Honeypots are intended to steer amateurs away from the main targets. They are useless against a skilled hacker.
We will never be able to do better than play catchup with skilled hackers, just as we can never be ahead of virus creators. I believe the most valuable things you can have are reliable backups and a good disaster recovery plan. That way, if you do fall victim, or indeed suffer any other kind of disaster, you have a decent chance of betting back to normal.
For me, one of the most important components in blocking attacks from outside is a good firewall. Not just a bunch of static rules but one which actively responds to threats (e.g. Smoothwall with the Guardian add-on). One common mistake is to install a firewall and think that's all there is to it. Like any other part of the system, a firewall needs to be monitored and adjusted as and when necessary and should never be taken for granted.
As for trying to detect hacking attempts, there are a bunch of things you can do, such as monitoring logs. Not just manually browsing them but also an automated system to warn of anomalies. It takes a little effort to create, as it needs to be customised for your specific setup, but well worth the effort.
Answer: I don't think it is possible to track a smart hacker, even for FBI, with their subpoena powers, links to other governments, etc. I also think it will take a lot of effort and a little satisfaction to nail a dumbass idiot who got too much life, and uses it to nose around other's data. Although it is infuriating to know that someone is there to hack into your network, to harm you, you will very likely not be able to catch the guy who did it. Most probably you will track down some grandma who unknowingly became a host to a botnet.
That said, not all hope is lost ;). There is a lot you could do about security, and the more resources you have, the more you can do. If you are on a tight budget, I would start with simple steps, such as: running "nmap" tool against your network to check open ports, and turn them off if they are not used. There are quite a few network vulnerability scanners, which could test if your network is a target for well known hacks; some are free some cost $$$, check out Nessus for example. You will not get 100% protection, but if you are not running any high-profile data network, the serious hackers will not care about you; however it will be just enough to repel the high-school wannabe hackers, which I would imagine account for a lot of intrusion cases.


editable composite primary key

Question: I have two fields being used as composite primary key which are not auto generated.  Now in Edit view, these two fields are read only, not able to edit them.  So how can editing be done...?
Model:
[Key][Column (Order=0)]
[Required]
public string LOV_COLUMN { get; set; }
[Key][Column(Order = 1)]
[Required]
public string LOV_CODE { get; set; }
[Required]
public string LOV_DESC{ get; set; }
public string COLUMN_TYPE { get; set; }
public string LOV_STATUS { get; set; }

Controller:
public ActionResult Edit(string LOV_COLUMN= "", string LOV_CODE="")
{
    return View(dv.LOV.Find(LOV_COLUMN, LOV_CODE) ?? new ADM_LOV_Master());
}

[HttpPost]
public ActionResult Edit(ADM_LOV_Master entity, FormCollection form)
{
    try
    {
        var model = dv.LOV.Find(entity.LOV_COLUMN, entity.LOV_CODE);
        TryUpdateModel<ADM_LOV_Master>(model, form.ToValueProvider());
        dv.Entry<ADM_LOV_Master>(model).State = System.Data.EntityState.Modified;
        dv.SaveChanges();
        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

Edit View Code as:

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>LOV_Master</legend>

        @Html.LabelFor(model => model.LOV_COLUMN)

        @Html.EditorFor(model => model.LOV_COLUMN)
        @Html.ValidationMessageFor(model => model.LOV_COLUMN)

        @Html.LabelFor(model => model.LOV_CODE)

        @Html.EditorFor(model => model.LOV_CODE)
        @Html.ValidationMessageFor(model => model.LOV_CODE)

        @Html.LabelFor(model => model.LOV_DESC)

        @Html.EditorFor(model => model.LOV_DESC)
        @Html.ValidationMessageFor(model => model.LOV_DESC)

        @Html.LabelFor(model => model.COLUMN_TYPE)

        @*@Html.EditorFor(model => model.COLUMN_TYPE)*@
        @Html.RadioButtonFor(model => model.COLUMN_TYPE, "C", new { id = "COLUMN_TYPE_false"})
        Character
        @Html.RadioButtonFor(model => model.COLUMN_TYPE, "N", new { id = "COLUMN_TYPE_true"})
        Number
        @Html.ValidationMessageFor(model => model.COLUMN_TYPE)

        @Html.LabelFor(model => model.LOV_STATUS)

        @*@Html.EditorFor(model => model.LOV_STATUS)*@
        @Html.RadioButtonFor(model => model.LOV_STATUS, "Y", new { id = "LOV_STATUS_true"})
        Yes
        @Html.RadioButtonFor(model => model.LOV_STATUS, "N", new { id = "LOV_STATUS_false" })
        No
        @Html.ValidationMessageFor(model => model.LOV_STATUS)

</fieldset>
}

Comment: show the code of the view, it's simpler to post the correction that way.
Comment: What do you mean "can't edit"?  I tried this using @Html.EditorForModel, and I'm able to make changes to the key properties in the view.
Comment: yaa can't edit used @Html.EditorFor. can edit rest of the fields other than composite key.
Answer: Primary Keys should be immutable, I would strongly encourage using an ID (int, GUID) as the primary key that is and then add these as a surrogate key with a unique constraint only. I am saying this because if any data refers to this data in any other table you will get constraint violations and this will be a nightmare for you.

editable composite primary key


RedHen Registration: where is the display settings?

Question: I'm using the RedHen module for registration to an event.
I created the registration type and a content type Event that has a field "Registration".
For what I read, when one visualize a instance of a content Event it should appear the fields of the registration type but it isn't. In the description it says that depends on the "display settings" but when I create registration type I didn't see an option like that.
What can be wrong? Thanks in advance.
Comment: This is a pretty specific support question about one specific module. You may have a better chance at getting an answer when you post a support request in the [Redhen issue queue](https://drupal.org/project/issues/redhen?categories=All) on drupal.org.
Comment: While this question is probably pretty on-topic here, I'm afraid @marcvangend is right. Only 591 sites currently report using this module. This is pretty low count, and it means that encountering people who know it here is pretty improbable. On the other hand their issue queue does not seem heavily overloaded. Of course if you will get your answer there, don't hesitate to post it here as a self-answer :)
Comment: Yeah, I resolve my problem with the display but I still need to do more things with redhen, so let see how it goes. Thank you for leaving the link of redhen support.
Answer: Well after a break and with my mind a bit clearer.
The display settings don't depend of the registration type, they can be found in "manage display" of the content type (in my case "Event"). Only that no matter what display one choose it will always appear a tab for "registration", but I think this can be manage with the css.


Clone/Duplicate Records in SQL Server with automatic increment of ids

Question: I want to duplicate all rows with registry_id = 1 and the id's of the cloned rows should be automatically incremented.
When I execute
insert into [ZAC2].[dbo].[rubrics]
  ([reference]
  ,[name_de]
  ,[name_fr]
  ,[name_en]
  ,[name_es]
  ,[registry_id]
  ,[registry_type]
  ,[parent_id]
  ,[created_at]
  ,[updated_at]
  ,[creator_id]
  ,[updater_id]
  ,[level]
  ,[inherited_rating]
  ,[has_children])
select ([reference]
  ,[name_de]
  ,[name_fr]
  ,[name_en]
  ,[name_es]
  ,[registry_id]
  ,[registry_type]
  ,[parent_id]
  ,[created_at]
  ,[updated_at]
  ,[creator_id]
  ,[updater_id]
  ,[level]
  ,[inherited_rating]
  ,[has_children]
FROM [ZAC2].[dbo].[rubrics]
  where registry_id=1

I get the error message:

Cannot insert duplicate key row in object 'dbo.rubrics' with unique index 'idx_rubrics_reference_registry'. The duplicate key value is (0, 1, RegistryPlan).

With Script Keys as > Create To > New Query Editor Window I get:
ALTER TABLE [dbo].[rubrics]
ADD PRIMARY KEY CLUSTERED ([id] ASC)
    WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF,
          IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON,
          ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 80
) ON [PRIMARY]

With Script Table as > Insert To > New Query Editor Window I get:
(<reference, varchar(255),>
           ,<rating, varchar(1),>
           ,<name_de, varchar(255),>
           ,<name_fr, varchar(255),>
           ,<name_en, varchar(255),>
           ,<name_es, varchar(255),>
           ,<registry_id, int,>
           ,<registry_type, varchar(255),>
           ,<parent_id, int,>
           ,<created_at, datetime,>
           ,<updated_at, datetime,>
           ,<creator_id, int,>
           ,<updater_id, int,>
           ,<level, int,>
           ,<inherited_rating, varchar(1),>
           ,<has_children, bit,>)

Comment: What is the definition of your **unique index** called `idx_rubrics_reference_registry` ? That's the one causing an error - not your PK that you're showing .... obviously, with your `INSERT .. SELECT ...` statement, you're producing new rows that violate this unique constraint - you need to fix this!
Comment: What a poorly designed table.
Comment: What are the fields in the composite key?  And what field types are they? int? varchar? auto incremental? not null?
Comment: i pasted some output from SQL Server Management Studio in my original post. How can i get the ID field type?
Comment: @kermit, how would you design that table?
Comment: @StandardNerd Normalize it. Separate table for names & languages, registry, and levels.
Answer: @marc_s you are absolutely right, i didn't read the error messages correctly. idx_rubrics_reference_registry has an unique constraint and therefor it could not duplicate the rows. I changed it (in fact my insert method with another id for the reference registry) and now it works. Thanks a lot!


AR And MA Order

Question: I have a time series which has sequence as follows

Upon eye balling this series , it sounds me a series which has 20 cycles where frequency/counts of event increase from 0 and then decrease after half of its cycle.
I generated ACF and PACF plot of this series without any transformation (log or differencing)

I am not getting any insights from this ACF and PACF plot for AR and MA order. However , based on my intuition I used 20 AR order for building model without any differencing and MA order

as in model summary lag 1, lag 4 and lag 19 are more significant as they have less than 0.05 value. However , I am going with all 20 lags and forecast for the next period from 151 to 175

When I check residual plots autocorrelation 4th plot, it does not tell me there is any more improvement required for the model

This model giving me almost same cycle and overall more or less same number counts estimated for this forecasted period from 151 to 175.
My confusions are
Can I go with this model for live deployment forecasting?
Why model does not give same number of forecasted counts upon reducing the number of lags, it works best with 20 but when I start to decrease from 20 to 15 or 10 then model forecasting is not expected what business wants?
How can I find AR and MA order for the model using PACF and ACF plot?
Answer: From your plots it appears that you might have a series with a periodic signal in it.  The best way to check this is to look at the Fourier intensity of the time-series, which you can do using the intensity function in the ts.extend package.   (In the present case it would be best to look at the Fourier intensity of the log-data.  If you see clear spikes in the Fourier intensity then this shows that you have a periodic signal.  Typically if you have a time-series with a periodic signal, you would begin your modelly using a regression model with sinusoidal terms and you would add enough terms to capture the periodic component adequately.  You would then examine the residual component from this model using diagnostic plots and tests and refine your model accordingly.  You should note that AR models do not give you signals with a fixed period, so they are usually not ideal for periodic time-series data.
Comment: An auto-regressive part in a model is stochastic, so it can create waves in the time-series, but those waves don't occur at regular intervals.  Contrarily, when we model a time-series using periodic regression, the periodic terms are fixed and we merely need to estimate the relevant period, amplitude, etc.  It is possible to construct models for periodic signals with varying periods, but these are a bit more complex.
Comment: this is not fixed period it varies between 17 to 25. Any reason why AR does not give signals with fixed period? I have not come across or read any caveats like that about AR model.

AR And MA Order


When a slope doesn't match a visual trend

Question: I am trying to determine the linear slope between two variables in a dataset. The ordinary least squares (OLS) method returns a slope which does not appear to fit the trend that one's eye sees in the data--the relationship looks steeper. I have tried using also a Theil-Sen estimator, but it gives a similar value to the OLS.
Are there any other ways to estimate the slope? Or is my eye just being fooled by a few points?
Comment: OLS minimizes sum of squared **vertical** distances between the data points and the fitted values. Sometimes the eye "prefers" other distances like the shortest line between the regression line and the data points in an $(x,y)$ plane.
Comment: The problem is, in part, like an election. In this example we, and you, can't even tell easily from the scatter plot how many voters are going which way in the central cluster of the data. The problem is also, in part, one of leverage. Some of those points near the periphery will lever the line towards them in the least squares case; the comparison with Theil-Sen gives a fair sense of that. It may be that you don't even have a linear pattern overall; some kind of kernel or similar regression would be a useful check. Can you post the data?
Comment: I don't know how anyone could provide any recommendations without a clear understanding of what the purpose of your analysis is.  What is it that you are trying to do, and why do you think it is important that the estimated slope match your subjective view of where the best fit line should be?
Comment: This technique is called "regression" precisely because the fitted lines tend to be more level than they eye would suggest (they "regress to the mean"). The amount of leveling depends on how scattered the points are. Perhaps the very best answer you can get is to learn what regression is really doing, so that you can determine whether regression or some other technique is appropriate for your data and your objectives.
Comment: As several people have pointed out, you would probably do best to learn more about what OLS regression does & why the answer you found is the right answer to that question. However, what I suspect is going on here is that there is a horizontal band of data 3/4 of the way up the plot that seems to be behaving differently from the rest. You could ask a new Q regarding how to detect & deal w/ that. (If so, your Q would be about detecting latent groups / clusters, &/or dealing with bivariate outliers.)
Answer: You can use orthogonal regression, which fits a line that minimizes the sum of the lengths of orthogonal projections on the line (instead of the sum of squared errors, as OLS does). In R (borrowing from this answer):
<code>set.seed(1)
xx <- rnorm(20)
yy <- rnorm(20)
r <- prcomp( ~ xx + yy )
slope <- r$rotation[2,1] / r$rotation[1,1]
intercept <- r$center[2] - slope*r$center[1]
plot(xx,yy)
abline(lm(yy~xx))
abline(a=intercept,b=slope,lty=2)
legend("bottomleft",lwd=1,lty=1:2,legend=c("OLS","Orthogonal"))
</code>


AWK new line sorting

Question: I have a script that sorts numbers:
{
    if ($1 <= 9) xd++
    else if ($1 > 9 && $1 <= 19) xd1++
    else if ($1 > 19 && $1 <= 29) xd2++
    else if ($1 > 29 && $1 <= 39) xd3++
    else if ($1 > 39 && $1 <= 49) xd4++
    else if ($1 > 49 && $1 <= 59) xd5++
    else if ($1 > 59 && $1 <= 69) xd6++
    else if ($1 > 69 && $1 <= 79) xd7++
    else if ($1 > 79 && $1 <= 89) xd8++
    else if ($1 > 89 && $1 <= 99) xd9++
    else if ($1 == 100) xd10++
    } END {
    print "0-9 : "xd, "10-19 : " xd1, "20-29 : " xd2, "30-39 : " xd3, "40-49 : " xd4, "50-59 : " xd5, "60-69 : " xd6, "70-79 : " xd7, "80-89 : " xd8,  "90-99 : " xd9,  "100 : " xd10
    }

output:
$ cat xd1 | awk -f script.awk
0-9 : 16 10-19 : 4 20-29 :  30-39 : 2 40-49 : 1 50-59 : 1 60-69 : 1 70-79 : 1 80-89 : 1 90-99 : 1 100 : 2

how to make that every tenth was on a new line?
like this:
0-9 : 16
10-19 : 4
20-29 :
30-39 : 2

print with \n doesn't work
additionally:
in the top ten I have 16 numbers, how can I get this information using the "+" sign
like this:
0-9 : 16 ++++++++++++++++
10-19 : 4 ++++
20-29 :
30-39 : 2 ++

thank you in advance
Comment: Please [edit] your question to provide concise, testable sample input (the contents of that `xd1` file and expected output given that input so we can help you with the question you asked and your original script (you don't need all those if-else's and hard-coded values in the print statement).
Comment: Does this answer your question? [awk histogram in buckets](https://stackoverflow.com/questions/6948586/awk-histogram-in-buckets)
Answer:
how to make that every tenth was on a new line?

Inform GNU AWK that you want OFS (output field separator) to be newline, consider following simple example
awk 'BEGIN{x=1;y=2;z=3}END{print "x is " x, "y is " y, "z is " z}' emptyfile

gives output
x is 1 y is 2 z is 3

whilst
awk 'BEGIN{OFS="\n";x=1;y=2;z=3}END{print "x is " x, "y is " y, "z is " z}' emptyfile

gives output
x is 1
y is 2
z is 3

Explanation: OFS value (default: space) is used for joining arguments of print. If you want to know more about OFS then read 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR
(tested in gawk 4.2.1)
Answer: If we rewrite the current code to use an array to keep track of counts, we can then use a simple for loop to print the results on individual lines, eg:
    {      if ($1 <=  9) xd[0]++
      else if ($1 <= 19) xd[1]++
      else if ($1 <= 29) xd[2]++
      else if ($1 <= 39) xd[3]++
      else if ($1 <= 49) xd[4]++
      else if ($1 <= 59) xd[5]++
      else if ($1 <= 69) xd[6]++
      else if ($1 <= 79) xd[7]++
      else if ($1 <= 89) xd[8]++
      else if ($1 <= 99) xd[9]++
      else               xd[10]++
    }
END { for (i=0;i<=9;i++)
          print (i*10) "-" (i*10)+9, ":", xd[i]
      print "100 :", xd[10]
    }

At this point we could also replace the 1st part of the script with a comparable for loop, eg:
    { for (i=0;i<=9;i++)
          if ($1 <= (i*10)+9) {
             xd[i]++
             next
          }
      xd[10]++
    }
END { for (i=0;i<=9;i++)
          print (i*10) "-" (i*10)+9, ":", xd[i]
      print "100 :", xd[10]
    }

As for the additional requirement to print a variable number of + on the end of each line we can add a function (prt()) to generate the variable number of +:
function prt(n   ,x) {
   x=""
   if (n) {
      x=sprintf("%*s",n," ")
      gsub(/ /,"+",x)
   }
   return x
}
    { for (i=0;i<=9;i++)
          if ($1 <= (i*10)+9) {
             xd[i]++
             next
          }
      xd[10]++
    }
END { for (i=0;i<=9;i++)
          print (i*10) "-" (i*10)+9, ":", xd[i], prt(xd[i])
      print "100 :", xd[10], prt(xd[10])
    }

Answer: you don't need to hard-code in 10-buckets like that :

jot -r 300 1 169 | mawk '

BEGIN { _+=(_+=_^=_<_)*_*_ } { ++___[_<(__=int(($!!_)/_))?_:__] }
END   {
        ____ = sprintf("%*s", NR, _)
                  gsub(".","+",____)

   for(__=_-_;__<=_;__++) {

       printf(" [%3.f %-6s] : %5.f %.*s\n",__*_,+__==+_?"+   "\
               : " , " __*_--+_++, ___[__], ___[__], ____) } }'

 [  0 , 9   ] :    16 ++++++++++++++++
 [ 10 , 19  ] :    17 +++++++++++++++++
 [ 20 , 29  ] :    16 ++++++++++++++++
 [ 30 , 39  ] :    19 +++++++++++++++++++
 [ 40 , 49  ] :    14 ++++++++++++++
 [ 50 , 59  ] :    18 ++++++++++++++++++
 [ 60 , 69  ] :    18 ++++++++++++++++++
 [ 70 , 79  ] :    16 ++++++++++++++++
 [ 80 , 89  ] :    20 ++++++++++++++++++++
 [ 90 , 99  ] :    19 +++++++++++++++++++
 [100 +     ] :   127 ++++++++++++++++++++++++++++++++++++++++++
                      ++++++++++++++++++++++++++++++++++++++++++
                      +++++++++++++++++++++++++++++++++++++++++++

AWK new line sorting


Measure of the boundary

Question: Let us been given a bounded domain $A\subset \mathbb{R}^n$. There is a function $u:A\to[0,1]$ such that
$$
A' = \{x\in A:u(x) = 1\}
$$
is not empty and $u\in Lip(A)$ with rate $\alpha$. Is it right that $\mu(\partial A') = 0$ where $\mu$ is Lebesgue measure? If not, please help me to construct a counterexample, if yes - please, help me to prove it.
Comment: There is a version of Rademacher's theorem initially due to Cheeger valid for quite general metric measure spaces in the sense of Gromov. Googling these key-words should lead you to a wealth of results.
Comment: Thanks. Since you only advised me without giving a feedback on the proof, I guess that it is correct.
Comment: Sorry, I haven't really looked and thought about it. I just wanted to give you this pointer to enable you to search for something that might suit your needs.
Comment: Ok, thanks. Then I'm still looking for the review of my proof.
Comment: Any point in $A'$ is either in the interior of $A'$ or on the boundary of $A'$ (true). At any point in the interior of $A'$ the gradient of $u$ exists (true). How can you deduce from that that at every point on the boundary of $A'$ the gradient of $u$ does not exist? You cannot (furthermore this happens to be false).
Comment: @Didier Piau: You right, this proof is wrong. Still, if the statement is correct?
Answer: For every $x$ in $\mathbb{R}^n$ and every closed set $B\subset\mathbb{R}^n$, let $$d(x,B)=\inf\{\|x-y\|;y\in B\}.$$ Then $d(x,B)=0$ if and only if $x$ belongs to $B$ and, for every $x$ and $y$ in $\mathbb{R}^n$, $$|d(x,B)-d(y,B)|\le\|x-y\|.$$
Choose $B\subset A$ and define the function $u$ on $A$ by $$u(x)=2^{-d(x,B)}.$$ Then $u$ is Lipschitz continuous and $A'=\{u=1\}=B$. Thus, the boundary of $A'$ may be the boundary of any closed set. In particular, it may have positive Lebesgue measure.
Comment: Thanks, just derived the same example.

Measure of the boundary


How to exit from a GUI application without frame's [x] button in Racket

Question: Is 'exit' the proper way to exit a gui application in Racket using a button or is some other method best for this?
(define myfr
    (new frame% [label "myframe"] [width 100] [height 100] ))
(new message% [label "my gui app"][parent myfr])
(define exitbutton
    (new button% [parent myfr] [label "Exit"]
                 [callback   (lambda (b e)   (exit)  )]))
(send myfr show #t)

I find that DrRacket gives "Interactions disabled" warning message on using the exit button but there is a clean exit when closing using  frame's [x] button.
I found "on-exit" method but not actual exit method on this page: https://docs.racket-lang.org/gui/frame_.html?q=frame%25 . Also nothing specific is mentioned on https://docs.racket-lang.org/reference/Exiting.html
Following statement, I believe, will only hide the frame and not close the application:
(send myfr show #f)

Comment: As Leif mentions "Interactions disabled" does indicate a clean exit. You stopped the program - which "includes the repl".
Answer: The exit is absolutely the way you want to end an application. The reason why the REPL in DrRacket stops when you do it, however, is because your program is no longer running, which will happen whenever you call exit, even if there is no gui in your program.
You are also correct that:
(send myfr show #f)

will hide the window but not actually quit the app.
So, one thing you could do is try to test if you are running in DrRacket or not. And if you are, rather than exiting the program, just close the window, and let DrRacket quit the application after the user clicks kill or starts a new program.
The problem with this is that in general, it is not possible to detect if you are running in a sandbox. There are some hacks you can do to test this out, but it's not very robust:
Determin if a racket program is in a sandbox
Another approach would be to create your own sandbox, and run your program in that sandbox (which is how DrRacket will run your code actually). Then, when your program exits, there will still be an available repl. Although be aware that the repl will be for the sandbox itself, rather than the repl it is in.
Here is the documentation for creating a sandbox.
Answer: Leif already explained it fully, but I just wanted to describe it differently to a more novice audience.
"gui application" you refer in your question may mean different things depending on where you're running that code piece.
If run from DrRacket REPL, the lambda (exit) will exit from the REPL (leading to "interactions disabled" message). If this code is a piece of a script run by racket executable (for example, from the command line), it will exit that script / shell (probably what you want).
I'm not sure if you want to try in the exit code understanding if you're in a sandbox or REPL and behave differently on exit button click per environment, but again Leif also pointed to an answer for it, too.
Another reason for writing this somehow unnecessary addition to Leif's answer is, I wanted to point to your term "gui application". My apologies if I'm sounding patronizing. Showing a gui frame or other graphical interfaces in your application is a functionality that you choose to use, and doesn't make the application something different. One difference from an Operating System's perspective would be a callback function associated with a top-level frame/window. And that callback function is nicely (most probably - although I didn't examine details of racket's code for it) registered and handled by the language in our case. So I'd like to say - showing a gui or not I would consider them the same, and it will exit when you call (exit). And normally (probably) the x button of a frame will hide it by default (as you're already aware, hence you added the exit lambda function). You application can unhide it during the application lifetime.


Python PVLIB error 'erbs() got an unexpected keyword argument 'min_cos_zenith''

Question: I run the following code to estimate DNI and DHI from GHI using the Erbs model available in Sandia's PVLIB.
The original code as here is:
pvlib.irradiance.erbs(ghi, zenith, datetime_or_doy, min_cos_zenith=0.065, max_zenith=87)
My code is:
df= pvlib.irradiance.erbs(df.ghi, sun.zenith, df.index, min_cos_zenith=0.065, max_zenith=85)
But my code returns erbs() got an unexpected keyword argument 'min_cos_zenith'. Any help is appreciated.
I use:
Python 3.7.3
PVLIB 0.6.1
Pandas 0.24.2
EDIT:
Maybe the solution is to upgrade to PVLIB v0.6.2. But, how to upgrade it. In my machine, none of the following works: pip install pvlib, pip install --upgrade pvlib, pip install pvliv ==0.6.2
Comment: for future reference: https://github.com/pvlib/pvlib-python/issues/689#issuecomment-485286373
Answer: Just call the function without the new (optional) arguments:
df= pvlib.irradiance.erbs(df.ghi, sun.zenith, df.index)
Comment: Thanks, but without new arguments, the resulted DHI become strange when plotted. https://stackoverflow.com/questions/53269553/estimate-diffuse-and-direct-component-from-global-irradiance
Comment: Well, if you got that far then the problem you asked about is solved.  If you have a question about your strange plot, then best thing would be to post another question about that--with an image of your strange plot and the code used to produce it.
Answer: AS of 2019-04-28 the pvlib version was v0.6.1 which does not yet have the min_cos_zenith optional argument for erbs(). As Will Holmgren commented, a workaround in the meantime would be to install the latest master from GitHub using pip vcs support:
$ pip install -e git+https://github.com/pvlib/pvlib-python.git@master#egg=pvlib


how to update my notification?

Question: i made an application that allow me to open conversation between friends, when someone send me a message i got a notification "John send you a new message" and when another person in the conversation send me a message a new notification is made ,,, my problem is that i don't want to make a new notification but i want to update the old notification to be for ex. like this " John and Alfred send you a new message"..
        var user = users.Where(x => x != CurrentUserId);

        foreach (var item in user)
        {
            var check = entities.Notifications.SingleOrDefault(i => (i.NotificationForId == id
                                      && i.NotificationForType == IdType && i.UserId == item));
            if (check == null)
            {
                Notification notify = new Notification()
                {
                    NotificationForId = id,
                    NotificationForType = IdType,
                    DateTime = DateTime.Now,
                    Message = GenerateMessage(),
                    UserId = item,
                    SenderID = CurrentUserId.ToString(),
                    SenderName = CurrentUserName
                };

                entities.Notifications.AddObject(notify);
            }
            else
            {

                check.Checked = false;
                check.DateTime = DateTime.Now;
            }

here it check if there are any notification for a user , if null then make a new notification else " Update the notification "
Answer: Your else block indicates the notification exists on the database, you can update the properties there. To save the changes made, both for insert and update call the method entities.SaveChanges()
Comment: how can i update the properties , i already made a method to save the changes that i call it from the controller.

how to update my notification?


Replace Values with Nan based on Date and matching value in a different data frame

Question: I have two data frames. One (customers) of which contains all customers with "ID" and "Date Sign up".
id                      date_joined
123 2019-09-12 17:05:27.916301+00:00
1234 2019-12-29 14:39:34.024478+00:00
1235 2020-03-09 12:47:52.284569+00:00
1236 2020-03-19 10:07:41.222140+00:00
1237 2020-03-19 06:34:25.896585+00:00

In a second data frame (turnover) I have the customer ID and year_month and turnover_sum. Important to point out: For every customer ID there are several rows in this dataframe as it tracks the monthyl turnoverover over a longe period.
ID  year_month       monthly_turnover
264  2019-Oct                 0
264  2019-Nov                 200
264  2019-Dec                 150
264  2020-Jan                 30
265  2020-Oct                 14

What I would like to do for every customer is to replace all values in the monthly_turnover column with Nan if the value in the year_month column is earlier than the "Date Sign up" of the respective user.
How to achieve this? I don't know where to begin.
Answer: You can do this in one (admittedly dense) line:
turnover.loc[turnover['year_month'] < customers['date'], 'monthly_turnover'] = np.nan

There's a lot packed into that single line, so let me unpack item by item:
turnover['year_month'] < customers['date']

Pandas knows about dates, so assuming your indices are matched pandas can automatically check if one date is after another. That clause will return a series of True and Falses.
We then take that series into the .loc function. which will match each row where the boolean is True, and then set 'monthly_turnover' to NaN.
You need to import numpy as np for np.nan to work.
This gave me the following output:

Let us know if you need more, and hope this helps!
Comment: Thanks for the input, unfortunately the code you provide doesn't work for me. I get the follow error prompt: ValueError: Can only compare identically-labeled Series objects
Answer: I found a solution to my problem. I have merged the two dataframes and then used np.where condition to compare the value of the year_month column and the date_joined column.
merged.pf["monthly_turnover"] = np.where(
merged.pf["year_month"] >= merged.pf["date_joined"],
merged.pf["monthly_turnover"], np.nan)

Thanks for the input anyways


Granting access based on an entity reference field using rules

Question: I have two content type one called audit and one called non-conformance, a non conformance is raised after an audit has been complete.
An audit contains an entity reference field called auditee which references a user, I would like to give edit access to the non conformance based on the the content of the auditee field in on the content type audit.
Thanks
Answer: This should do the trick for you. You only have to write the code for extract_uid_from_audit(); if you don't know how we can help as well.
<code>function mymodule_node_access($node, $op, $account) {
    if(!empty($node->type) && ($node->type == "non_conformance")
             && $op=='update') ){
        $uid = extract_uid_from_audit($node);
        if($uid = $account->uid)
             return NODE_ACCESS_ALLOW;
    }
}
</code>
Comment: So this has to be done with a custom module? It can't be done just using rules?
Comment: This is a non-standard access setting. You either have to find a module that will allow you to set this up or you program it yourself.
Comment: Using modules maybe you can fix it with Organic Groups. Audit being a group, non-conformance - group content, auditee - a role within a group. That way you could give specific access rights per group, per role.
Yes this is a lot of work but it should work better than node_access() hacks.
Answer: You can do this with a couple of modules + a view that uses an entity reference display.
If you add the "Node access user reference" module, you can create a view that lists users of a specific role (I guess they would have a role like auditees?). Details from project page:

Gives content access permissions to users for content that references the users with User reference or Entity reference.
This is great if you want your content authors to be able to choose additional authors, or choose who can view their content, as well as being able to display the list of authors or viewers within your content, or in views displays.

When you configure the auditee entity reference field, you can use that view to populate the field and you can set the field so that those auditee users have varying degrees of access. It's also a good idea to allow the original authors of the to continue having permissions of view, edit, delete.


Higher value of strides in conv1d

Question: I am using Conv1d for time-series data and I have create a model as follows,
<code>model=Sequential()
model.add(Conv1D(32,250,strides=9,padding='same',input_shape=(1500,9), kernel_regularizer=regularizers.l2(0.01)))
c1 = MaxPooling1D(2)
model.add(c1)
model.add(Dropout(0.5))
model.add(BatchNormalization())

model.add(Conv1D(64,250,strides=9, padding='same',kernel_regularizer=regularizers.l2(0.01)))
conv1 = MaxPooling1D(2)
model.add(conv1)
model.add(Dropout(0.5))
model.add(BatchNormalization())

model.add(Flatten())

model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(40, activation='softmax'))

opt = keras.optimizers.adam(lr = 0.001, decay = 1e-6)
model.compile(loss = 'categorical_crossentropy', optimizer = opt, metrics = ['accuracy'])
</code>
The model.summary() when using strides as 9 looks like
The model.summary() for the above model with stride as 1 looks as follows
I am confused as to how would the higher number of strides affect the performance of the model. Which would be better, strides = 1 or strides = 9? Can someone guide me with this?
Comment: Don't you want `causal` padding for most ts models?
Answer:
In most cases which I have seen, a larger number of strides skips the essential context of an image required for image classification. The higher number of strides moves the convolution windows far away from each other which may lower down validation accuracy. For smaller images, they are useless. But, for larger images they might prove useful.
Smaller strides like 1 are the best choice for Convolutional networks. They move the convolution window one by one to the next of pixels/values. This helps to retain the context of the image, and hence the validation accuracy remains high, if the images are smaller in image classification tasks.
Comment: I am using conv1d in time-series data. Its basically is a signal. So I am trying to use strides of size 10 or more to filter out the noise.

Higher value of strides in conv1d


finding number of unique outcomes of round-robin tournament with cycle

Question: A round robin tournament is a tournament where each player plays other player once and there is no draw. If a team wins against all other then it is called Total Winner. Similarly we define Total Looser as a team which looses all matches.
EDITED::

Let us define order by win/loss. If $A$ wins $B$ and $B$ wins $C$ then it is ordered as $\dots >A>B>C>\dots$
  Consider $n\ge 3 $ teams, $\frac{n(n-1)}{2}$ games are played  and $2^{\frac{n(n-1)}{2}}$ unique results might be possible. Out of those $2^{\frac{n(n-1)}{2}}$ possible results, how many unique outcomes are there such that we can order every player (only once) such that it forms a cycle. i.e.
  $$P_1 > P_2 > P_2 > \dots > P_n > P_1$$

Some of my thoughts::
There cannot be cycle if there is Total Winner or Total Looser. I found (by using Principle of Inclusion Exclusion) out there exists $$2^{\frac{n(n-1)}{2}}-(2n)2^{\frac{(n-1)(n-2)}{2}}+n(n-1)2^{\frac{(n-3)(n-2)}{2}}$$ possible results of the tournament where there is no Total Winner or no Total looser.
ADDED::  I wrote a short script for total matches outcomes and outcomes with no total winner or looser. But couldn't write more to detect cycles. For $n=3$ & $n=4$, all  matches where there is no total winner or total looser have some sort of cycles.
ADDED::
Here is updated code.
If three players played game, player $(1), (2)$, and $(3)$ the outcomes are

[(1, 2), (1, 3), (2, 3)]  $ \leftarrow (1)$ is total winner so there can't be cycle.
  [(1, 2), (1, 3), (3, 2)]
  [(1, 2), (3, 1), (2, 3)]
  [(1, 2), (3, 1), (3, 2)]
  [(2, 1), (1, 3), (2, 3)]
  [(2, 1), (1, 3), (3, 2)]
  [(2, 1), (3, 1), (2, 3)]
  [(2, 1), (3, 1), (3, 2)]

Here, $(1,2)$ means match was played by $(1)$ and $(2)$ and $(1)$ won the game.
The following forms a cycle (also it is the only outcome where there are no total winner and total looser)

[(1, 2), (3, 1), (2, 3)] $ \implies 1>2>3>1$
  [(2, 1), (1, 3), (3, 2)] $\implies 1>3>2>1$

Thanks you in advance.
Comment: Your question is now even less clear than it was originally. Exactly what are you trying to count? All $n$-player tournaments that have no $n$-cycle?
Comment: @BrianM.Scott I am trying to count the unique results of the tournament with cycles. Here is the [list](http://ideone.com/wgJvyb) of possible outcomes. $(a,b)$ means $a$ played agaist $(b)$ and $a$ won.
Comment: If by *cycle* you mean $n$-cycle, a cycle containing all of the players, then you're trying to count the $n$-player tournaments that have an $n$-cycle.
Comment: @BrianM.Scott yes that it is. sorry If I mislead you before.
Comment: Okay; that looks a bit messy, but I'll give it some thought.
Comment: @BrianM.Scott sure, anytime you find free, this used to homework question that i got little extra curious.
Answer: In graph theory, the default meaning of the word tournament is round-robin tournament. I.e., a tournament is an oriented complete graph; each pair of distinct vertices (nodes, players) is joined by a single directed edge in one direction; a directed edge $xy$ means that player x beats player y. In the context of tournaments (or more general directed graphs), the words path and cycle refer to directed paths and cycles. If $T$ is a tournament on $n$ vertices, a Hamiltonian cycle in $T$ is a cycle of length $n$, i.e., a cycle that contains all the vertices. Your question asks, of the $2^{\binom{n}{2}}=2^{n(n-1)/2}$ different tournaments on $n$ labeled vertices, how many have a Hamiltonian cycle?
Let's call two vertices $x$ and $y$ "equivalent" if there is a path from $x$ to $y$ and also a path from $y$ to $x$. It's easy to see that this is an equivalence relation; the equivalence classes are called the strong components of the tournament. The tournament is said to be strongly connected if all vertices are equivalent, i.e., if there is just one strong component. This notion is related to your question because of the following theorem. (The case $n=1$ is exceptional because, while a tournament on one node is strongly connected, it does not have a Hamiltonian cycle.)
Theorem 1. A tournament $T$ on $n$ vertices ($n\gt1$) has a Hamiltonian cycle if and only if it is strongly connected.
Proof. The "only if" is clear: if $T$ has a Hamiltonian cycle, then it is strongly connected. I will sketch a proof of the converse.
Suppose T is strongly connected; we have to show that $T$ has a Hamiltonian cycle. We start by showing that $T$ has a cycle, not necessarily of length $n$. Choose a directed edge $uv$. Since $T$ is strongly connected, there is a path from $v$ to $u$; this path, together with the edge $uv$, gives us a cycle in $T$.
Let $C$ be a cycle in $T$ of maximum length $m$; we have to show that $m=n$. Let $W$ (for "winners") be the set of all players that beat at least one player in $C$, i.e., $v\in W$ iff there is a directed edge from $v$ to some vertex in $C$. Similarly, let $L$ (for "losers") be the set of all players beaten by at least one player in $C$, i.e., $v\in L$ iff there is a directed edge to $v$ from some vertex in $C$. We consider several cases.
Case 1. $W=\emptyset=L$.
In this case, the cycle $C$ contains all vertices of $T$, and everything is fine. We shall see that each of the other cases leads to a contradiction.
Case 2. $W=\emptyset\ne L$.
Then $C$ has no "incoming" edges. If $u\in L$ and $v\in C$, then there is no path from $u$ to $v$, contradicting the assumption that $T$ is strongly connected.
Case 3. $W\ne\emptyset=L$.
If $u\in C$ and $v\in W$, then there is no path from $u$ to $v$, contradicting the assumption that $T$ is strongly connected.
Case 4. $W\cap L\ne\emptyset$.
If $v\in W\cap L$, then we can insert $v$ between two consecutive vertices of $C$ so as to obtain a cycle of length $m+1$, contradicting the assumed maximality of $m$.
Case 5. $W\ne\emptyset$, $L\ne\emptyset$, and $W\cap L=\emptyset$.
Since $T$ is strongly connected, there must be a directed edge $uv$ for some $u\in L$, $v\in W$. Then the vertices $u,v$ can be inserted in the cycle $C$ so as to obtain a cycle of length $m+2$, contradicting the assumed maximality of $m$. Q.E.D.
Thus, your question is equivalent to determining $a(n)$, the number of strongly connected labeled tournaments on $n$ vertices. This function is sequence number A054946 at the OEIS, the On-Line Encyclopedia of Integer Sequences. There you can find various information about this sequence, including references, tabulated values, and the following recurrence formula. Let $F(n)=2^{\binom{n}{2}}=2^{n(n-1)/2}$. Then (for $n\gt0$)$$F(n)=\sum_{s=1}^n\binom{n}{s}a(s)F(n-s).$$The recurrence is explained by the following structural property of tournaments:
Theorem 2. A nonempty tournament $T$ has a uniquely determined subtournament $S$ such that $S$ is nonempty and strongly connected, and each player in $S$ beats all players outside of $S$. Namely, $S$ consists of all players $u$ such that, for each $v\in T$, there is a path from $u$ to $v$.
Let's call the set $S$ in Theorem 2 the "top component" of $T$. (I don't know if it has an official name.) Now the meaning of the recurrence is clear: the term $\binom{n}{s}a(s)F(n-s)$ is just the number of tournaments on a set of $n$ labeled players whose top component has size $s$. Namely, there are $\binom{n}{s}$ ways to choose the elements of the top component $S$, then $a(s)$ ways to define a strongly connected tournament on the set $S$, and $F(n-s)$ ways to define a tournament on the set $T\setminus S$; of course, the edges between $S$ and $T\setminus S$ are all directed away from $S$.
Comment: Are you implying that if there are no total winners and no total losers, then there exists some cycle? It seems true for $n=3$ and $n=4$. I think this sequence can be generated by [this](http://www.wolframalpha.com/input/?i=Table[2^%28n%28n-1%29/2%29-2%20n%202^%28%28n-2%29%28n-1%29/2%29%2bn%28n-1%292^%28%28n-3%29%28n-2%29/2%29,{n,%203,%206}])
Comment: If by "cycle" you mean "cycle of length $n$", then no, I'm not implying that. Suppose $n=6$, call the teams A, B, C, D, E, F. Suppose that A beats B, B beats C, C beats A, D beats E, E beats F, F beats D, and suppose that A, B, C win all their games against D, E, F. There is no total winner, no total loser (each team scores either 4-1 or 1-4); there are two 3-cycles, but there is no 6-cycle.
Comment: I am looking for $n$-cycle in how many outcomes of all possible outcomes can I find $n$-cycle?
Comment: sorry ... my mistake earlier. (+1), would you add what is the idea behind this sequence? I don't have any experience with graph theory. just a little outline. this is just discrete math problem gone little bad. And i also thought I could solve it using combinatotics.
Comment: if you feel too long to type then, please give me some references, i'll have to read it anyway to understand it.
Comment: @hasExams The formula follows easily from setting up the recurrance relation.
Answer: HINT: For $k=1,\ldots,n$ let $w_k$ be the number of matches won by Team $k$. If there is no cycle, the tournament is transitive: if Team $j$ beats Team $k$ and Team $k$ beats Team $\ell$, then Team $j$ beats Team $\ell$. There must also be a Total Winner. Use these facts to prove by induction on $n$ that $$\{w_1,w_2,\ldots,w_n\}=\{0,1,\ldots,n-1\}\;:$$ one team wins all of its matches, another loses only to the Total Winner, another loses only to those top two teams, and so on down to a Total Loser. In other words, if there is no cycle, the teams can be completely ranked by their scores: for each possible score from $0$ wins through $n-1$ wins there is exactly one team with that score.
Once you have that, it's easy to count the tournaments with no cycle, and you already know the total number of tournaments, so you can easily get the number of tournaments that have at least one cycle.
Comment: hmm ... given that there no total winner or total looser, does it always contain at least one cycle? If there is total winner or total looser, that is dead end. But other case (with no total winner or looser), I am having difficulty to prove it. I think this assertion is wrong or my eyes are not working well.
Comment: @hasExams: Yes. Start with any team $T_1$; $T_1$ is not a total loser (note spelling!), so it beats some team $T_2$. Similarly, $T_2$ beats some $T_3$. There are only finitely many teams, so eventually you reach a $T_k$ that is the same as $T_j$ for some $j<k$. The teams $T_j,T_{j+1},\ldots,T_{k-1}$ then form a cycle. You can do the same sort of thing if there is no total winner.
Comment: I think i forgot to specify one thing ... the cycle has to visit vertex only once and it must visit all vertex. I am sorry if I didn't mention that earlier in my question.
Comment: Your question isn't really very clear. I couldn't tell whether you were asking for the number of tournaments that contain some cycle, or for the number that **are** cycles. Is it the latter that you want?


Postgresql WITH clause in crosstab queries

Question: I create a large query as a Common Table Expression - month_index using WITH clause. Is it possible to refer this Common Table Expression in a crosstab query's source sql?
When I did gets an error
relation "month_index" does not exist
WITH month_index AS
(
      SELECT ...
)
SELECT * FROM CROSSTAB(
   'SELECT rowid AS row_name,
   CONCAT(''m'',monthno) AS category,
   nic5dindex AS value
   FROM month_index',
   '*<categorysql>*')
AS ct(..)

I use Postgresql 9.3.
Comment: the outdated and unsupported version 8.3 did not have common table expressions. But I think that even with a supported version that does have CTEs the crosstab function won't be able to access the CTE. You will need to create a view that you can reference in the SELECT statement that you pass to the `crosstab()` function.
Answer: You want to have your CTE fully contained within the crosstab.
More like this... you can have a CTE in a crosstab, but not vice-versa.
    SELECT *
FROM CROSSTAB(
$$
    WITH month_index AS
    (
       SELECT rowid AS row_name,
       CONCAT(''m'',monthno) AS category,
       nic5dindex AS value
       FROM source_data_table
    )
    SELECT * FROM month_index
,
   '*<categorysql>*'
$$
   )
AS ct(..)


How does Backbone renew collection models when needed

Question: I have a this collection
var Product = Backbone.Model.extend({
           urlRoot:'/api/products',
           idAttribute:'id'
        });
    var Products = Backbone.Collection.extend({
        model:Product,
        url : '/api/products/'
    });

I fetch Models from server
var products = new Products();
products.fetch();

At some moment i change data on server and want 'products' to have the new data (renew the collection). I tried something like this
products.reset()
products.fetch()

But after this products in empty. Help me plz
Answer: It might be because you're checking the contents of products before the server has time to return with the data. Check if products is still empty on the success callback instead:
products.fetch({
    success: function() {
        console.log(products.toJSON()); //is it still empty here?
    }
});

Answer: It is possible that the server does not respond in time. The proper way to reset your data is
products.fetch({reset: true}).success(function(response){
  // update the view here if necessary
})


Melhoramento de Projeto em PHP

Question: Estou com uma dúvida sobre padrão de projeto em php.
Tenho uma classe DAO que tem um método de seleção, bom o que acontece é que tive que colocar nesse método alguns códigos HTML estilizado com bootstrap, isso que fiz esta correto?
class CategoriaDao {
public function selecionaTudo($sql) {
    $conexao = new PDOUtil();
    $consulta = $conexao->conectar()->query($sql);
    while ($sqlSeleciona = $consulta->fetch(PDO::FETCH_OBJ)){
        echo "" . $sqlSeleciona->descricao . "	";
        echo 'id_categoria_pagina.'" class="btn btn-default btn-sm"> Editar	';
        echo 'id_categoria_pagina.'" class="btn btn-default btn-sm"> Excluir	';
        echo "
";
    }
}

Comment: Olha.. o padrao é voce que faz por que o projeto é seu.. Porém para ficar um negócio bonito e com esse "padrao" que voce procura seria recomendado que nao houvesse HTML assim em classes internas e sim nas paginas em que a classe é manipulada..
Comment: obs: não estou com problemas relacionados a lógica mas sim referente a padrões de projeto.
Comment: Pesquise sobre MVC, a princípio.
Comment: Model view e controller é tranquilo em arquiteturas como JSF ou Spring o problema é que não estou utilizando nenhum framework, isso foi definido no inicio do projeto, o php não tem uma arquitetura mais fechada o que na minha humilde visão é mais ruim pois acaba aceitando gambiarras como essa.
Answer: O ideal é seperar o HTML da lógica PHP, dessa forma o código pode ser reaproveitado sem nenhum efeito colateral como Cannot modify header information outro ponto é sua classe deve ter uma unica responsabilidade e seus métodos devem realizar apenas uma tarefa, selecionaTudo faz duas coisas manipula o banco e formata uma saída de texto(que não é responsabilidade da classe CategoriaDao).
Se forem poucos registros exibidos e o select não tiver nenhum where pode simplificar o método dessa forma:
public function selecionaTudo($sql) {
    $conexao = new PDOUtil();
    return $conexao->conectar()->query($sql)->fetchAll(PDO::FETCH_ASSOC));
}

Sugestões
Ao invés de criar uma variável de conexão para cada método da sua classe, pode torna-la membro da classe e chame PDOUtil no construtor do DAO.
class CategoriaDao {
   private $conexao;

   public function __constructor($db){
     $this->conexao = $db;
   }

   public function selecionaTudo($sql) {
     return $this->conexao->query($sql)->fetchAll(PDO::FETCH_ASSOC);
   }
 }

A chamada ficaria assim:
$DAOCategoria = new CategoriaDao(PDOUtil::conectar());
$categoria = $DAOCategoria->selecionarTudo($sql);

Como não está usando nenhum framework, uma forma de seperar o html do php é usar um template engine como o smarty.
Answer: separe a lógica da view.
Entregue o objeto da consulta, como por exemplo um array dos itens, na visualização(HTML) faça um simples while. Assim você não é obrigado a ficar utilizando apenas Bootstrap e poderá aproveitar em outro projeto sua classe.
Uma dica é ter sua classe de Bootstrap, assim pegue o objeto com os itens e aplique sua chamada nesta classe, sendo ela específica para o Boostrap.
Answer: Você está criando um padrão ou quer seguir um padrão? Antes de pensar sobre o código temos que pensar sobre a arquitetura do projeto. Embora você diga que não tem problema de lógica esse método não faz "echo" na abertura da "
" gerando uma tabela potencialmente problemática para engines de browser que não compensem tags mal criadas (IE até 8, por exemplo). Ele também iria de encontro com o princípio da generalidade já que sua pesquisa vai precisar retornar de forma "garantida" um "id_categoria_pagina" e também teria problemas com o encapsulamento, já que a abertura da tabela e seu fechamento estariam em outras classes. Se estiverem na mesma entidade (algo como CategoriaView) poderia não ser um problema, mas ainda assim pensaria seriamente em deixar o que é "view" no View. ;)
Comment: obrigado pela crítica, apesar da mesma não responder a pergunta.

Melhoramento de Projeto em PHP


How can you determine if a file exists within the app bundle?

Question: Sorry, dumb question number 2 today.  Is it possible to determine if a file is contained within the App Bundle?  I can access files no problem, i.e.,
NSString *pathAndFileName = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"];

But can't figure out how to check if the file exists there in the first place.
Regards
Dave
Comment: Since [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"] will return NULL if the file does not exist, so I usually just check if (pathAndFileName != NULL) { //file exists }
Comment: You can do it without code, too. See my thread on another forum: http://forums.macrumors.com/threads/xcode-6-how-do-i-view-all-files-in-my-main-bundle.1904024/
Comment: @moonman239 The check I need to do, needs to be in code as I am preloading the app with cached thumbnail images so the initial run of the app is fast.  I have another thread that then downloads a new data file.  As the data is displayed the app needs to check if the image is in the bundle (the preloaded cached image).  If not then the image is retrieved from a server and saved to a disk cache.  Hope that makes sense.
Comment: @MagicBulletDave I still don't understand. Does the cached image come with the app, or is it downloaded? If it comes with the app, then the code should theoretically have no need to check if the picture's there - the app can just assume it is.
Comment: It's a general purpose algorithm.  The app comes with a data file in the form of a plist.  Each row of data has a thumbnail image.  On first load everything is in the bundle so can be displayed directly from it.  Regularly a new plist is downloaded (with some existing and some new data).  The existing data's images will be in the bundle, the new data's images will need to be downloaded and then cached to disk.  So the chain of events is: Look in bundle first, then disk cache, finally if still no image then try and download it from the server.  Make sense?
Answer: [[NSFileManager defaultManager] fileExistsAtPath:pathAndFileName];

Comment: According to [this answer](http://stackoverflow.com/a/7487235/1226722), however, even Apple is advising to actually __"attempt an operation (such as loading a file or creating a directory), check for errors, and handle any error gracefully than it is to try to figure out ahead of time whether the  operation will succeed"__.
Comment: Doh! thanks Rob, have been using that for files in the documents directory! It's getting late.  Thanks again.
Answer: This code worked for me...
NSString *pathAndFileName = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
if ([[NSFileManager defaultManager] fileExistsAtPath:pathAndFileName])
{
    NSLog(@"File exists in BUNDLE");
}
else
{
    NSLog(@"File not found");
}

Hopefully, it will help somebody...
Answer: pathForResource will return nil if the resource does not exist. Checking again with NSFileManager is redundant.
Obj-C:
 if (![[NSBundle mainBundle] pathForResource:@"FileName" ofType:@"plist"]) {
      NSLog(@"The path could not be created.");
      return;
 }

Swift 5:
 guard Bundle.main.path(forResource: "FileName", ofType: "plist") != nil else {
      print("The path could not be created.")
      return
 }

Answer: NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"filename"];
    if(![fileManager fileExistsAtPath:path])
    {
        // do something
    }

Comment: This is looking in the documents directory not the app bundle
Answer: Same as @Arkady, but with Swift 2.0:
First, call a method on mainBundle() to help create a path to the resource:
guard let path = NSBundle.mainBundle().pathForResource("MyFile", ofType: "txt") else {
    NSLog("The path could not be created.")
    return
}

Then, call a method on defaultManager() to check whether the file exists:
if NSFileManager.defaultManager().fileExistsAtPath(path) {
    NSLog("The file exists!")
} else {
    NSLog("Better luck next time...")
}


How can we select the best GARCH model by carrying out likelihood ratio test?

Question: I have carried out the likelihood ratios of different GARCH models.
GARCH(1,1) and GARCH(1,0)- Rejected null hypothesis so I chose GARCH(1,1) to do more sophistication.
GARCH(3,1) and GARCH(1,1)- Failed to reject the null hypothesis
GARCH(4,1) and GARCH(1,1)- Failed to reject the null hypothesis
GARCH(2,1) and GARCH(1,1)- Failed to reject the null hypothesis
GARCH(2,2) and GARCH(1,1)- Rejected null hypothesis so then I chose GARCH(2,2) to do more sophistication.
And further, every GARCH model failed to reject the null hypothesis against GARCH(2,2). So my best model should be GARCH(2,2) or GARCH(1,1)?
Answer: Your model selection strategy should be aligned with the intended use of the model. As far as I know, a sequence of likelihood ratio tests at the conventional 5% significance level is not a optimal strategy under any of the popular intended uses of the model. For a closely related explanation, see "Statistical tests for variable selection" by Rob J. Hyndman.
Among likelihood-based approaches, the prominent model selection strategies are minimization of AIC and BIC.

Minimization of AIC is optimal* for forecasting. The idea is that AIC estimates $-2n \ \times$ the expected likelihood on a new, unseen data point from the same data generating process (DGP) that generated your sample. ($n$ is the sample size, here the length of the time series.) The model with minimum AIC should therefore yield maximum likelihood. If likelihood is a relevant criterion for forecast evaluation (which it could be argued to be), go ahead with AIC minimization.
Similarly, BIC is optimal* for finding the true DGP, or its best approximation, among the candidate models. So if you care about identifying the true DGP or its best approximation, go ahead with BIC minimization.

Alternatively, you could rely on time series cross validation and select the model that yields the most accurate out-of-sample (test-sample) forecasts.
In a cross-sectional setting, AIC and BIC have been shown to be asymptotically equivalent to different types of cross validation; see e.g. this among several related threads on Cross Validated. I suppose something similar could be (has been?) shown in the time series case as well. Of course, the precise results depend on some assumptions, but they should give a flavor of what is going on in the broader case.
*Under certain assumptions which can be found in a textbook or papers introducing AIC and/or BIC, or some threads of Cross Validated. Claeskens & Hjort "Model Selection and Model Averaging" (2008; a book) and Claeskens "Statistical model choice" (2016; an article) are good starting points.
Comment: Model selection has been discussed extensively on Cross Validated. See [these](https://stats.stackexchange.com/questions/tagged/model-selection+garch) threads for GARCH model selection and [these](https://stats.stackexchange.com/questions/tagged/model-selection?tab=Votes) for model selecion in general.


Consider Van der Pol's equation

Question:
Consider Van der Pol's equation:

$$y''-0.2(1-y^2)y'+y=0, \quad y(0)= 0.1, \quad y'(0)=0.1$$
(i) Find the approximate solution for this problem using the Taylor series method.  Your expansion should include the first three non-zero terms and you should work to six decimal places accuracy. First find the approximate solutions for both  y (0.1)  and  y' (0.1)  using the first three non-zero terms of Taylor series expansion for each function and then use this information to calculate the approximate solution at  x = 0.2.
(ii)   Consider the following finite difference method for solving
<code>Is this method zero stable ?
</code>
.      Examine the stability of this method for non-zero (but small) h  if it is applied to the test
    equation :
$$   y ' = λ y$$
Comment: This is the third repetition of the same homework question. (http://math.stackexchange.com/questions/685280/consider-van-der-pols-equation) Editing and adding to your original question will keep it up in the queue, no need for reposts.
Comment: (ii) Consider the following finite difference method for solving

 Y'= f(x,y) , y (x0 )=yo   :

Y n+2 =4yn+1 -5yn = h (4 fn+1 +2 fn)
Comment: Dear amzoti, this part was missing
Comment: @ Amzoni : No we don't.. all it says is dat it's a non zero but small term
Answer: We are given the Van der Pol equation:
$$\tag 1 y''- 0.2(1-y^2)y'+ y = 0, \quad y(0)= 0.1, \quad y'(0)=0.1$$
$(i)$: The Taylor series about a points $x = a$, is given by:
$ \tag 2 f(a) + \dfrac{f'(a)}{1!} (x-a)  + \dfrac{f''(a)}{2!} (x-a)^2 + \dfrac{f^{(3)}(a)}{3!} (x-a)^3 + \ldots + \dfrac{f^{(n)}(a)}{n!} (x-a)^n$
For this problem, $a = 0$, hence:
$$ \tag 3 y(0) + \dfrac{y'(0)}{1!} (x)  + \dfrac{y''(0)}{2!} (x)^2 + \ldots +$$
We have:

$y(0) = 0.1$
$y'(0) = 0.1$
$y''(0) =  0.2(1-y(0)^2)y'(0)- y(0) = 0.2(1- (0.1)^2) 0.1 - 0.1 = -0.0802$

Using the first three nonzero terms of $(3)$ yields the Taylor series about $x = 0$ of:
$$\tag 4 y(x) = y(0) + \dfrac{y'(0)}{1!} (x)  + \dfrac{y''(0)}{2!} (x)^2 = 0.1 + 0.1x - \dfrac{0.0802x^2}{2!} = 0.1 + 0.1x - 0.0401x^2$$
The derivative of $(4)$ is:
$$\tag 5 y'(x) = 0.1 - 0.0802x$$
We can now find:

$y (0.1) = 0.1 + 0.1(0.1) - 0.0401(0.1)^2 = 0.109599$
$y'(0.1) = 0.1 - 0.0802(0.1) = 0.091980$
$y''(0.1) = 0.2(1-y(0.1)^2)y'(0.1)- y(0.1) = 0.2(1- (0.109599)^2) 0.091980 - 0.109599 = -0.091424$

Now the Taylor expansion about $x = 0.1$ is:
$$\tag 6 y(x) = y(0.1) + \dfrac{y'(0.1)}{1!} (x-0.1)  + \dfrac{y''(0.1)}{2!} (x-0.1)^2 = 0.109599 + 0.091980(x-0.1) - \dfrac{0.091424(x-0.1)^2}{2!}$$
At $x = 0.2$, the approximation $(6)$ yields:
$$y(0.2) = 0.109599 + 0.091980(0.2-0.1) - \dfrac{0.091424(0.2-0.1)^2}{2!} = 0.118340$$
$(ii)$: Please show your work.
Comment: Amzoti are you good at differential equation my dear.. if you can help me sort out a questiom on Euler's and Runge kutta?? Pleaaaaaaaase

Consider Van der Pol’s equation


Variance of AR(1) plus noise and its "equivalent" ARMA(1,1)

Question: Let us consider the following state-space model
$$
z_{t} = x_{t} + v_{t}\\
x_{t} = \phi x_{t-1} + w_{t}
$$
where $
\phi< 1$, the errors $v_{t}\sim \mathcal{N}(0,V^{2})$ and $w_{t}\sim \mathcal{N}(0,W^{2})$ are independent
The stationary variance of $x_{t}$ is given be $Var(x_{t}) = \frac{W^2}{1-\phi^2}$, therefore, the stationary variance of $z_{t}$ is given by
$$
Var(z_{t}) = V^{2} + \frac{W^2}{1-\phi^2}
$$
The state space model above is equivalent to ARMA(1,1) process
$$
z_{t} = \phi z_{t-1} + \theta \varepsilon_{t-1} + \varepsilon_{t},
$$
where $\theta = - \phi \frac{V}{\sqrt{V^{2} + W^{2}}}$ and $\varepsilon_{t}\sim \mathcal{N}(0,V^{2} + W^{2})$ are i.i.d. This is actually AR(1) plus noise, which is equivalent to ARMA(1,1). The prof can be found, for example, here http://www.stats.ox.ac.uk/~reinert/time/notesht10short.pdf
Next, let us consider "equivalent" ARMA(1,1) model. Its stationary variance is given by
$$
Var(z_{t}) = \frac{1+2\phi\theta +\theta^{2}}{1 - \phi^{2}}(V^2 + W^2),
$$
where $\theta = - \phi \frac{V}{\sqrt{V^{2} + W^{2}}}$, see, for example, https://math.unice.fr/~frapetti/CorsoP/chapitre_23_IMEA_1.pdf
I can not see that the variance of equivalent model is equal to the state-space model.
This question is related to
Estimation of ARMA from state-space generated data
where it was shown by simulations, that "equivalence" is not working and
Alternative construction of ARMA(1,1) process
where the equivalence is proved.
Comment: "Any time series textbook" is not all that helpful. A concrete reference could be more helpful.
Comment: Dear @Richard Hardy, I have added the reference.
Comment: Thank you very much.
Answer: There is a mistake in the ARMA(1,1) model. Look at
Alternative construction of ARMA(1,1) process
where the correct ARMA(1,1) was derived.


Unable to set max and min range in datepicker based on two dates

Question: I have three input types where which all are associated with date picker
JS FIDDLE DEMO

   From
   <input type="text" id="from" name="from"/>
   to
   <input type="text" id="to" name="to"/>

   Resultant
   <input type="text" id="resultant">

JavaScript
 $(function() {
       $("#from").datepicker({
          defaultDate: "+1w",
          changeMonth: true,
          numberOfMonths: 3,
          onSelect: function( selectedDate ) {
           this.focus();
          }
    });

        $("#to").datepicker({
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 3,
            onSelect: function( selectedDate ) {
            this.focus();

        }
    });

        $("#resultant").datepicker({
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 3,
            onSelect: function( selectedDate ) {
         $('#resultant').datepicker("option","minDate",$('#from').datepicker('getDate'));
         $('#resultant').datepicker("option","maxDate",$('#to').datepicker('getDate'));

        }
    });
});

Issue here is when I selected resultant date input box for first time, minRange and maxRange are not selected. Second time they are selected. The reason for this is min and max range are set after selecting for first time
How I can get rid of that?
UPDATE: After following one of the approach,I am getting this UI
But the year field is not seen. Any reason for that??
Comment: Can't you do like until user has not selected the from and to date make he selected input box as disable
Comment: Why dont you disabled the resultant till user set min and max dates
Comment: No we can't do that.Can't change project requirement.
Answer: Try to use below javascript code for range fix
  $("#resultant").datepicker({
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 3,
            beforeShow: changeRange,
            onSelect: function (selectedDate) {
                //$('#resultant').datepicker("option", "minDate", $('#from').datepicker('getDate'));
                //$('#resultant').datepicker("option", "maxDate", $('#to').datepicker('getDate'));
            }
        });

        function changeRange(input) {
            if ($('#from').val() != null && $('#to').val() != null) {
                var minDate = new Date($('#from').val());
                var maxDate = new Date($('#to').val());
                //minDate.setDate(minDate.getDate() + 1)

                return {
                    minDate: minDate,
                    maxDate: maxDate
                };
            }

            return {};
        }

Comment: Please see the updated question and can u tell me why year is not seen?? Before it was seen.
Comment: In your fiddle example, year getting display
Comment: Yupe, fiddle doesn't have problem. Just wanted to ask that do you have any idea that why year hides.
Answer: You should use "onSelect" for "from" and "to", not for "resultant".
Right now, boundary calculation takes place after picking date in "resultant".
$(function () {
    $("#from").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 3,
        onSelect: function (selectedDate) {
            $('#resultant').datepicker("option", "minDate", $('#from').datepicker('getDate'));
            this.focus();

        }
    });

    $("#to").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 3,
        onSelect: function (selectedDate) {
            $('#resultant').datepicker("option", "maxDate", $('#to').datepicker('getDate'));
            this.focus();

        }
    });

    $("#resultant").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 3,
        onSelect: function (selectedDate) {
            this.focus();
        }
    });
});

Answer: Another solution I found
  Resultant
  <input type="text" id="resultant" onclick="showDatePicker()">

JavaScript code
 function showDatePicker(){
   //check null safe too
     $('#resultant').datepicker("show");
     $('#resultant').datepicker("option","minDate",$('#from').datepicker('getDate'));
     $('#resultant').datepicker("option","maxDate",$('#to').datepicker('getDate'));

 }

Answer: That's because onSelect doesn't fire until you've selected something or put text into the input of the datepicker what you want to do is set the min and max date in the onClose of the from and to datepicker
working jsFiddle
$(function () {
     $("#from").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 3,
        onSelect: function (selectedDate) {
            this.focus();

        },
        onClose: function (selectedDate) {
            $('#resultant').datepicker("option", "minDate", selectedDate);
        }
    });

    $("#to").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 3,
        onSelect: function (selectedDate) {
            this.focus();

        },
        onClose: function (selectedDate) {
            $('#resultant').datepicker("option", "maxDate", selectedDate);
        }
    });

    $("#resultant").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 3
    });
});


Simple Html não retorna nada?

Question: Código:
<code>require_once("simple_html_dom/simple_html_dom.php");
html = new simple_html_dom();
$html->load_file('http://www.g1.globo.com/');
echo $html;
</code>
Erros:

Notice: file_get_contents(): send of 16 bytes failed with errno=10053
  Uma conex�o estabelecida foi anulada pelo software no computador host.
  in C:\xampp\htdocs\simple_html_dom\simple_html_dom.php on line 1081
Notice: file_get_contents(): send of 24 bytes failed with errno=10053
  Uma conex�o estabelecida foi anulada pelo software no computador host.
  in C:\xampp\htdocs\simple_html_dom\simple_html_dom.php on line 1081
Notice: file_get_contents(): send of 19 bytes failed with errno=10053
  Uma conex�o estabelecida foi anulada pelo software no computador host.
  in C:\xampp\htdocs\simple_html_dom\simple_html_dom.php on line 1081
Notice: file_get_contents(): send of 2 bytes failed with errno=10053
  Uma conex�o estabelecida foi anulada pelo software no computador host.
  in C:\xampp\htdocs\simple_html_dom\simple_html_dom.php on line 1081
Warning: file_get_contents(http://www.g1.globo.com/): failed to open
  stream: HTTP request failed! in
  C:\xampp\htdocs\simple_html_dom\simple_html_dom.php on line 1081
Fatal error: Call to a member function innertext() on null in
  C:\xampp\htdocs\simple_html_dom\simple_html_dom.php on line 1688

Como poderia solucionar esse problema?
Observação: g1.globo.com é apenas um exemplo.
Answer: Verifique se a opção allow_url_fopen e está ativo no seu servidor, esta configuração geralmente pode ser feita via php.ini.
Comment: Verifiquei e está sim, amigo.
"allow_url_fopen=On"
Comment: Esta classe **simple_html_dom** ela faz o que, você que a fez?

Simple Html não retorna nada?


Less-than, greater-than not working when different amount of digits

Question: I'm a python beginner in and got an assignment for school to make the simple number guessing "game" where you have to figure out a number by guessing and it either says higher or lower until you guess the correct number. It worked well until i added two player support and the ability to choose the interval that the random number will be in. Now the higher/lower result from the guess is the opposite if the amount of digits in the guess is different from the random unkown number. Lets say that the random number is 50, then guessing a number between 10-49 will give the result "guess higher", guessing a number between 99-51 will give the result "guess lower" like it's supposed to do. However if the guess is a different amount of digits like 0-9 it will say "guess lower" which is the opposite, same if i guess 100 or anything above it will say "guess higher".
The code:
<code>import random

while True:
  select = input("En eller två spelare? ")
  if select == '1':
    y = input("0 - ")
    num = str(random.randint(1, int(y)))
    while True:
      guess = input('Gissa mellan 0 - ' + y+": ")
      if guess == num:
        print('Rätt nummer: ' + guess)
        break
      elif guess == "haks":
        print(num)
      elif guess < num:
        print('Högre än ' + guess)
      elif guess > num:
        print('Lägre än ' + guess)

  if select == '2':
    spelare1 = input("Spelare 1: ")
    spelare2 = input("Spelare 2: ")
    y = input("0 - ")
    num = str(random.randint(1, int(y)))
    spelare = 0

    while True:
      if spelare%2 == 0:
        print(spelare1 + 's tur')
      if spelare%2 == 1:
        print(spelare2 +'s tur')
      guess = input('Gissa mellan 0 - ' + y+": ")
      if guess == num:
        print('Rätt nummer: ' + guess)
        if spelare%2 == 0:
          print(spelare1, "vann!")
          break
        if spelare%2 == 1:
          print(spelare2, "vann!")
          break
      elif guess == "haks":
        print(num)
      elif guess < num:
        print('Högre än ' + guess)
        spelare = spelare+1
      elif guess > num:
        print('Lägre än ' + guess)
        spelare = spelare+1
</code>
I can't find any logic in this and already spent to much time on trying to fix it. Any help is much appreciated and if there are any questions about the code I'll happily answer them.
Comment: Have you tried stepping through the code with a debugger?
Answer: The number of digits doesn't matter
There's a difference between inequality comparisons on strings (like you're doing) and on actual numbers.
When used on strings, it's comparing alphabetically, not numerically
You should convert all inputs to numbers, not compare them as strings (the default return type of input function). Similarly, don't cast your random digits to strings


Entity reference to user doesn't work without 'administer users' permission

Question: EDIT: I have most probably found out the reason thy it works (or doesn't work actually) this way. The problem is that I don't have legitimate access to the user node that is being edited. I only 'hacked' the menu system so it let me through but don't really have the real access. Unfortunately I have no idea how to resolve this problem...
I have reworked the system that handles permission to user edit pages so that it allowed editing certain users even without the 'administer users' permission.
The system itself works well, the only problems are entity reference fields on the user. To be more specific, there are two entity reference fields attached to user bundle -> one that references another user 'supervisor' and another that references certain content type.
The problem with these two fields is that the autocomplete feature (I use autocomplete widgets) in the widgets not only doesn't work but even throws a 'access denied' error (in AJAX response).
It works well when current user has the 'administer users' permission but I can't give every user this permission.
BTW: To make it perfectly clear, not even the field that references a normal content type (node) works so the problem can't be in access to user entities as the answer below suggests.
I have tried to look into the entity reference code but have not found anything that would explain this strange behaviour (there's literally zero calling of user_access that might lead to this kind of behaviour).
Comment: @DavidThomas Your answer would explain why the field that references users doesn't work but what about the other one that references simple nodes that are accessible to everyone and doesn't work either?
Comment: If the referenced entity isn't accessible by the current user, it will throw an error. To make it referenceable you'll need to either 1. Change access permissions. 2. Change entity reference code logic.
Answer: You could try creating an entityreference display in views which, with the 'access' options defined in views, handle user access however you please.
Here is an example view display to get you started:
https://gist.github.com/acao/7297342
Just configure the field settings to use this view instead of the default handler, and see if that gets you around your problem!
Comment: I can't do that because I have to use my own slightly altered entity reference selection handler (that does things you can't do in views).

(btw: it doesn't work even if I use the default handler, I tried that).

BTW2: Since there's no user_access code in the general entity reference handler I doubt it would help.
Answer: Ok, I've found where the problem was. There's an entity_access call in the entityreference module that caused the access denied error.
<code>function entityreference_autocomplete_callback_get_matches($type, $field, $instance, $entity_type, $entity_id = '', $string = '') {
          $matches = array();

          $entity = NULL;
          if ($entity_id !== 'NULL') {
            $entity = entity_load_single($entity_type, $entity_id);
            if (!$entity || !entity_access('view', $entity_type, $entity)) {
              return MENU_ACCESS_DENIED;
            }
          }
        ...
}
</code>
The entity access then called entity_metadata_user_access which implements traditional way of handling user access.
To solve this problem I just altered the entity_info, more specifically the access_callback function of user entity.


What is the "maven way" for this ant development workflow?

Question: How can maven be configured to support this type of workflow:
One Time Setup Invoke maven to do one time setup of a developers machine such as

Create a custom version of tomcat configured for this application
Create a local postgres database on the developers machine
load sample data into the database
run a junit test to configure other resources needed to run the application

Integration Tests Invoke maven to do run integration tests which should do the following

Create an integration test db
setup the db
Run command line integration tests against the db
Run a test version of tomcat with the application in it
Run command line junit tests that test the restful services exposed by the application

Release Build Invoke maven to do a release build of the system

do all the steps for an integration test
generate resources and configurations that are used on the server rather than production
deposit the end result in a git repo, commit, and push the changes to production

Test Build Invoke maven to do a test build of the system

do all the steps of a release build but configure the test release package with test server configuration

The main thing I am struggling with is that maven has a single build life-cycle with a well defined sequence of phases not sure if the workflow I want to build is a good fit for maven.
Can maven be configured for this type of workflow?  If yes what are the key features of maven that allow for the different configurations of the four main ways that I want to use maven?
Update What I mean by this workflow, is that I want to be able to do something like
mvn setup
mvn integration
mvn prod-release
mvn test-release

I know the above example look like ant, I am long time ant user and total noob with maven.
Comment: A programmer descends into madness and despair by using the wrong build tool. http://www.youtube.com/watch?v=mBmExt184vc#!
Answer: You could setup Maven to do all that...
You probably would use (shock horror) profiles to achieve some of this...
BUT you don't want to do that
You are following ANT style thinking... if you like that style of thinking then use ANT or Gradle and be happy.
If you want to follow the Maven way, then you will solve the problem differently.
Coming from the Maven way, here are my thoughts:

Why do you need one-time setup? I usually have a run profile that dynamically provisions the correct application server and starts it with the App deployed, tearing down everything afterwards when I hit ^C. Typically this involves starting up a database server or two... hence things I have developed like the cassandra-maven-plugin. That way when I am working on a different project (which could be in 10 minutes time) I don't have to worry about background database servers eating up all my laptop's ram.
Integration tests are actually trivial when you have the above working... in fact I created the Maven Failsafe Plugin to make it easy to have plugin execution tied to the appropriate phases for integration testing. The Maven convention is to have a profile called run-its for running integration tests.
Release builds being different from test builds... ugh! You should be building environment agnostic artifacts. Have them pick up their configuration from the environment they are deployed in. That removes the worry that something has changed between the "test" build and the "production" build. If you really need to bundle the config, then I usually would resort to a separate module for taking the agnostic artifact and rebundling with the required configuration. That way it is easy to prove that you have a reproducible transformation and that nothing has changed inbetween what went to QA vs what is going to Ops.
I always make the release builds include the integration testing.

So typically I have my projects such that
$ mvn -Prun

will fire up the application starting from zero. Hitting ^C will tear everything back down again, and mvn clean or in extreme situations if I have a more complex setup process and need some caching mvn post-clean (think really clean) will remove anything that the run profile put into play
To run the integration tests I typically do
$ mvn -Prun-its verify

To make a release I typically do
$ mvn release:prepare release:perform -B

That is (in my view) the ideal way of handling the above steps you need.
HTH.
BTW I have not had to use PostgreSQL specifically (typically my integration tests and run profile can get away with a pure java database such as derby or hsqldb and because the artifacts are environment agnostic it is easy to have the integration test/dev flyweight app server inject the correct JDBC url) so you may hit some issues with regard to PostgreSQL
Comment: Tomcat is using load time weaving with spring which means adding some jars to the tomcat\lib folder to make things work, thus the need for one time setup of the app server. A final release to the server includes a generated tomcat server that is configured for a specific server, such as a server.xml with specific ip address, logging paths ... etc, specific spring profiles turned on in the context.xml ... etc. The final out of the build is full installable system not just a war. App uses postgres views and stored procedures so can't use derby. Figuring out the maven way for newbies is hard.
Comment: The load-time weaving should be configurable via the appropriate `` entries added to `/project/build/plugins/plugin`.
Comment: I am working on a blog post that might help. The thing is to follow the maven way until you cannot and then as long as you know *why* you cannot follow the maven way, you should be ok. There is also nothing wrong with using a different tool for some/all of your build if your build (or parts if it) is far from the maven way. Maven works best when you produce environment agnostic artifacts. That doesn't mean db agnostic btw, more host name/DNS/IP address etc agnostic.


Eigenvalues of matrix and stability

Question: This is about general equilibrium:
Suppose that $x(t)$ represents outputs of all sectors and parts of the whole economy - represented as matrix. How outputs evolve to $x(t+1)$ is determined by the matrix $A$ - $A$ representes how previous outputs are used as inputs to produce new ouputs - so $A$ can be said as table of outputs produced from inputs.
$$x(t+1) = A \cdot x(t)$$
Assume that all sectors/parts of the economy satisfy the equal growth rate in ouput:
$$x(t+1) = (1+\alpha) \cdot x(t)$$
Also,
$$p = (1+\pi) \cdot p \cdot A$$
where $p$ is equilibrium price, and $\pi$ is the uniform rate of profit. (The equation is saying that the whole cost of input plus the profit is the total market price of outputs.)
The first and second equation show that
$$(A-(1+\alpha) \cdot I) \cdot x(t) = 0$$
where $I$ is identity matrix, and the third equation implies
$$p \cdot (A^{-1} - (1+\pi) \cdot I) = 0$$
The question is, then John Blatt and Steve Keen say,

for stability, biggest eigenvalue (root) of $A$ and $A^{-1}$ must be
  less than 1.

and I am not getting this. Why do $A$ and $A^{-1}$ need to be like this? Is this related to the stability of equilibrium? If so, can anyone explain this using linear algebra stuffs?
Comment: There's something strange here. If the biggest eigenvalue of $A$ is $c$, then the biggest eigenvalue of $A^{-1}$ is $1/c$, and the numbers $c$ and $1/c$ can't both be less than $1$. Maybe you can point us to the exact statement?
Comment: @GerryMyerson That's how Blatt and Keen say that general equilibrium approach is flawed. So it's strange, but it must be strange :)
Answer: I've expressed some misgivings in a comment, so I'll only take up a part of the question here. You have $$x(t+1)=Ax(t)$$ from which it follows for $n=0,1,2,\dots$ that $$x(t+n)=A^nx(t)$$ If the biggest eigenvalue of $A$ has (modulus) $c\gt1$, then (it can be proved that) the entries of $A^n$ grow like $c^n$, that is, exponentially --- they blow up. It follows that $x(t)$ blows up as $t\to\infty$ (small print: provided $x(0)$ has a component in the direction of the eigenvector of $A$ corresponding to that biggest eigenvector). That, presumably, is considered unstable.

Eigenvalues of matrix and stability


Calculate prediction values for linear curve estimation for next 5 intervals

Question: Below is my code with sample data as in screen shot ; in that i got response for only 21 to 33.
My requirement is to get response for 34, 35 , 36, 37
Sample Data

Below is R code
linearCurve<-lm(Month ~ Value, data = data)
CurveResponse<-cbind(predict(linearCurve,type="response"))
Comment: Where is your Month variable in the sample data?
Comment: First column is "Month" i.e. 21 to 33 and Second column is "Value" i.e. 6591.35 to 8575.81
Answer: You can do this in R using the predict function. For ease I used Month as predictor and Value as outcome. In the vector new are the predicted values as a function of Month. Please see also the plot that shows the projected values on the regression line.
data=data.frame(Month=c(21:33), Value=c(6591.69, 6579.62, 7133.84, 6955.89, 7573.27, 7556.87, 7751.17, 8001.76, 8399.5, 8560.36, 8517.53, 8602.57, 8575.81))

attach(data)

fit1<-lm(Value~Month, data=data)

new<-predict(fit1, data.frame(Month=(c(34,35,36))))

plot(Value~Month, data, col="red", xlim=c(20,36), ylim=c(6000,10000))
abline(fit1, col="orange", lwd=2)
abline(v=c(34,35,36), lty=6)
points(34, new[1], col="blue")
points(35, new[2], col="blue")
points(36, new[3], col="blue")

Comment: Hi thanks for the solution , it's work for me.
but i  need dynamic variable in that. Below code doesn't work for me , i don't know why ?
[code]
data = read.csv("c:/curve.csv", header=TRUE)
attach(data)
fit1<-lm(data[,2]~data[,1], data=data)
predict(fit1, data.frame(MONTH_=(c(34,35,36,37))))
[/code]